ChangeHandler creates SQL statements for changing rows in a table.
ChangeHandler | ChangeHandler creates SQL statements for changing rows in a table. |
Variables | |
$DUPE_KEY | |
@ACTIONS | |
Functions | |
new | |
fetch_back | Set the fetch-back dbh. |
set_src | Set which side of left-right pair is the source. |
src | Return current source db.tbl (could be left or right table). |
dst | Return current destination db.tbl (could be left or right table). |
_take_action | Call the user-provied actions. |
change | Make an action SQL statment for the given parameters if not queueing. |
__queue | Queue an action for later execution. |
process_rows | Make changes to rows created/queued earlier. |
make_DELETE | Make a DELETE statement. |
make_UPDATE | Make an UPDATE statement. |
make_INSERT | Make an INSERT statement. |
make_REPLACE | Make a REPLACE statement. |
make_row | Make an INSERT or REPLACE statement. |
make_where_clause | Make a WHERE clause. |
get_changes | Get a summary of changes made. |
sort_cols | Sort a row’s columns based on their real order in the table. |
make_fetch_back_query | Make a SELECT statement to fetch-back values. |
_d |
sub new
%args | Arguments |
left_db | Left database (src by default) |
left_tbl | Left table (src by default) |
right_db | Right database (dst by default) |
right_tbl | Right table (dst by default) |
actions | arrayref of subroutines to call when handling a change. |
replace | Do UPDATE/INSERT as REPLACE. |
queue | Queue changes until process_rows() is called with a greater queue level. |
Quoter | Quoter object |
tbl_struct | Used to sort columns and detect binary columns |
hex_blob | HEX() BLOB columns (default yes) |
ChangeHandler object
sub fetch_back
Set the fetch-back dbh. If I’m supposed to fetch-back, that means I have to get the full row from the database. For example, someone might call me like so: $me->change(‘UPDATE’, { a => 1 }) But ‘a’ is only the primary key. I now need to select that row and make an UPDATE statement with all of its columns.
$dbh | dbh to use for fetching-back values |
sub set_src
Set which side of left-right pair is the source. For bidirectional syncing both tables are src and dst. Internally, we refer to the tables generically as the left and right. Either one can be src or dst, as set by this sub when called by the caller. Other subs don’t know to which table src or dst point. They just fetchback from src and change dst. If the optional $dbh arg is given, fetch_back() is set with it, too.
$src | Hashref with source host information |
$dbh | Set fetch_back() with this dbh if given |
sub _take_action
Call the user-provied actions. Actions are passed an action statement and an optional dbh. This sub is not called directly; it’s called by change() or process_rows().
sql | A SQL statement |
dbh | optional dbh passed to the action callback |
sub change
Make an action SQL statment for the given parameters if not queueing. This sub calls _take_action(), passing the action statement and optional dbh. If queueing, the parameters are saved and the same work is done in process_rows(). Queueing does not work with bidirectional syncs.
action | One of @ACTIONS |
row | Hashref of row data |
cols | Arrayref of column names |
dbh | Optional dbh passed to _take_action() |
sub __queue
Queue an action for later execution. This sub is called by change() process_rows() to defer action.
action | One of @ACTIONS |
row | Hashref of row data |
cols | Arrayref of column names |
dbh | Optional dbh passed to _take_action() |
sub process_rows
Make changes to rows created/queued earlier. If called with 1, will process rows that have been deferred from instant processing. If no arg, will process all rows.
$queue_level | Queue level caller is in |
$trace_msg | Optional string to append to each SQL statement for tracing them in binary logs. |
sub make_INSERT
Make an INSERT statement. This sub is stub for make_row() which does the real work.
$row | Hashref with row values |
$cols | Arrayref with column names |
An INSERT statement for the given row and columns
sub make_REPLACE
Make a REPLACE statement. This sub is a stub for make_row() which does the real work.
$row | Hashref with row values |
$cols | Arrayref with column names |
A REPLACE statement for the given row and columns
sub make_row
Make an INSERT or REPLACE statement. Values from $row are quoted with Quoter::quote_val().
$verb | ”INSERT” or “REPLACE” |
$row | Hashref with row values |
$cols | Arrayref with column names |
A SQL statement
sub make_where_clause
Make a WHERE clause. Values are quoted with Quoter::quote_val().
$row | Hashref with row values |
$cols | Arrayref with column names |
A WHERE clause without the word “WHERE”
sub sort_cols
Sort a row’s columns based on their real order in the table. This requires that the optional tbl_struct arg was passed to new(). If not, the rows are sorted alphabetically.
$row | Hashref with row values |
Array of column names
sub make_fetch_back_query
Make a SELECT statement to fetch-back values. This requires that the optional tbl_struct arg was passed to new().
$where | Optional WHERE clause without the word “WHERE” |
A SELECT statement
my $DUPE_KEY
our @ACTIONS
sub new
Set the fetch-back dbh.
sub fetch_back
Set which side of left-right pair is the source.
sub set_src
Return current source db.tbl (could be left or right table).
sub src
Return current destination db.tbl (could be left or right table).
sub dst
Call the user-provied actions.
sub _take_action
Make an action SQL statment for the given parameters if not queueing.
sub change
Queue an action for later execution.
sub __queue
Make changes to rows created/queued earlier.
sub process_rows
Make a DELETE statement.
sub make_DELETE
Make an UPDATE statement.
sub make_UPDATE
Make an INSERT statement.
sub make_INSERT
Make a REPLACE statement.
sub make_REPLACE
Make an INSERT or REPLACE statement.
sub make_row
Make a WHERE clause.
sub make_where_clause
Get a summary of changes made.
sub get_changes
Sort a row’s columns based on their real order in the table.
sub sort_cols
Make a SELECT statement to fetch-back values.
sub make_fetch_back_query
sub _d
Quote a value for use in a SQL statement.
sub quote_val