ChangeHandler

ChangeHandler creates SQL statements for changing rows in a table.

Summary
ChangeHandlerChangeHandler creates SQL statements for changing rows in a table.
Variables
$DUPE_KEY
@ACTIONS
Functions
new
fetch_backSet the fetch-back dbh.
set_srcSet which side of left-right pair is the source.
srcReturn current source db.tbl (could be left or right table).
dstReturn current destination db.tbl (could be left or right table).
_take_actionCall the user-provied actions.
changeMake an action SQL statment for the given parameters if not queueing.
__queueQueue an action for later execution.
process_rowsMake changes to rows created/queued earlier.
make_DELETEMake a DELETE statement.
make_UPDATEMake an UPDATE statement.
make_INSERTMake an INSERT statement.
make_REPLACEMake a REPLACE statement.
make_rowMake an INSERT or REPLACE statement.
make_where_clauseMake a WHERE clause.
get_changesGet a summary of changes made.
sort_colsSort a row’s columns based on their real order in the table.
make_fetch_back_queryMake a SELECT statement to fetch-back values.
_d

Variables

$DUPE_KEY

my $DUPE_KEY

@ACTIONS

our @ACTIONS

Functions

new

sub new

Parameters

%argsArguments

Required Arguments

left_dbLeft database (src by default)
left_tblLeft table (src by default)
right_dbRight database (dst by default)
right_tblRight table (dst by default)
actionsarrayref of subroutines to call when handling a change.
replaceDo UPDATE/INSERT as REPLACE.
queueQueue changes until process_rows() is called with a greater queue level.
QuoterQuoter object

Optional Arguments

tbl_structUsed to sort columns and detect binary columns
hex_blobHEX() BLOB columns (default yes)

Returns

ChangeHandler object

fetch_back

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.

Parameters

$dbhdbh to use for fetching-back values

set_src

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.

Parameters

$srcHashref with source host information
$dbhSet fetch_back() with this dbh if given

src

sub src

Return current source db.tbl (could be left or right table).

Returns

Source database-qualified table name

dst

sub dst

Return current destination db.tbl (could be left or right table).

Returns

Destination database-qualified table name

_take_action

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().

Parameters

sqlA SQL statement
dbhoptional dbh passed to the action callback

change

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.

Parameters

actionOne of @ACTIONS
rowHashref of row data
colsArrayref of column names
dbhOptional dbh passed to _take_action()

__queue

sub __queue

Queue an action for later execution.  This sub is called by change() process_rows() to defer action.

Parameters

actionOne of @ACTIONS
rowHashref of row data
colsArrayref of column names
dbhOptional dbh passed to _take_action()

process_rows

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.

Parameters

$queue_levelQueue level caller is in
$trace_msgOptional string to append to each SQL statement for tracing them in binary logs.

make_DELETE

sub make_DELETE

Make a DELETE statement.  DELETE never needs to be fetched back.

Parameters

$rowHashref with row values
$colsArrayref with column names

Returns

A DELETE statement for the given row and columns

make_UPDATE

sub make_UPDATE

Make an UPDATE statement.

Parameters

$rowHashref with row values
$colsArrayref with column names

Returns

An UPDATE statement for the given row and columns

make_INSERT

sub make_INSERT

Make an INSERT statement.  This sub is stub for make_row() which does the real work.

Parameters

$rowHashref with row values
$colsArrayref with column names

Returns

An INSERT statement for the given row and columns

make_REPLACE

sub make_REPLACE

Make a REPLACE statement.  This sub is a stub for make_row() which does the real work.

Parameters

$rowHashref with row values
$colsArrayref with column names

Returns

A REPLACE statement for the given row and columns

make_row

sub make_row

Make an INSERT or REPLACE statement.  Values from $row are quoted with Quoter::quote_val().

Parameters

$verb”INSERT” or “REPLACE”
$rowHashref with row values
$colsArrayref with column names

Returns

A SQL statement

make_where_clause

sub make_where_clause

Make a WHERE clause.  Values are quoted with Quoter::quote_val().

Parameters

$rowHashref with row values
$colsArrayref with column names

Returns

A WHERE clause without the word “WHERE”

get_changes

sub get_changes

Get a summary of changes made.

Returns

Hash of changes where the keys are actions like “DELETE” and the values are how many of the action were made

sort_cols

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.

Parameters

$rowHashref with row values

Returns

Array of column names

make_fetch_back_query

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().

Parameters

$whereOptional WHERE clause without the word “WHERE”

Returns

A SELECT statement

_d

sub _d
my $DUPE_KEY
our @ACTIONS
sub new
sub fetch_back
Set the fetch-back dbh.
sub set_src
Set which side of left-right pair is the source.
sub src
Return current source db.tbl (could be left or right table).
sub dst
Return current destination db.tbl (could be left or right table).
sub _take_action
Call the user-provied actions.
sub change
Make an action SQL statment for the given parameters if not queueing.
sub __queue
Queue an action for later execution.
sub process_rows
Make changes to rows created/queued earlier.
sub make_DELETE
Make a DELETE statement.
sub make_UPDATE
Make an UPDATE statement.
sub make_INSERT
Make an INSERT statement.
sub make_REPLACE
Make a REPLACE statement.
sub make_row
Make an INSERT or REPLACE statement.
sub make_where_clause
Make a WHERE clause.
sub get_changes
Get a summary of changes made.
sub sort_cols
Sort a row’s columns based on their real order in the table.
sub make_fetch_back_query
Make a SELECT statement to fetch-back values.
sub _d
Quoter handles value quoting, unquoting, escaping, etc.
sub quote_val
Quote a value for use in a SQL statement.
Close