die "--dsn is required for this example to run" if not defined get('dsn'); ({ # # Enable SET @@@var = mojo and SELECT @@@var # match => qr{^SET @@@([A-Za-z0-9_]*?)\s*=\s*('|"|)(.*)\2}sio, ok => sub { print localtime()." [$$] Setting $_[1] to $_[3]\n"; set($_[1], $_[3]); } }, { match => qr{(@@@[A-Za-z0-9_]*)}sio, rewrite => sub { $_[0] =~ s{@@@([A-Za-z0-9_]*)}{get('dbh')->quote(get($1))}segix; return $_[0]; }, continue => 1 }, { # # Execute SELECT REMOTE # match => qr{(SELECT REMOTE (.*?)(?:$|\)|ON ('|"|)(.*)\3))}io, before => sub { my ($foo, $bar, $select_query, $baz, $remote_dsn) = @_; my $local_dbh = get('dbh'); $remote_dsn = get('remote_dsn') if not defined $remote_dsn; my $remote_dbh = DBI->connect($remote_dsn, get('remote_dsn_user'), get('remote_dsn_password')); error($DBI::errstr, $DBI::errno) if not defined $remote_dbh; my $sth = $remote_dbh->prepare("SELECT $select_query"); $sth->execute(); error_dbi($remote_dbh) if $remote_dbh->err(); my @create_fields; foreach my $i (0..($sth->{NUM_OF_FIELDS}-1)) { my $create_field = "`$sth->{NAME}->[$i]` "; $create_field .= defined $sth->{mysql_type_name}->[$i] ? $sth->{mysql_type_name}->[$i] : "text"; if ( (defined $sth->{mysql_length}->[$i]) && ($sth->{mysql_type_name}->[$i] =~ m{char}sio) ) { $create_field .= "($sth->{mysql_length}->[$i])" } $create_field .= " not null " if ($sth->{NULLABLE}->[$i] == 0 || $sth->{NULLABLE}->[$i] eq ''); push @create_fields, $create_field; } $local_dbh->do(my $zz = "CREATE TEMPORARY TABLE remote_select (".join(', ',@create_fields).")"); while (my @array = $sth->fetchrow_array()) { my $row = join(', ', map {$local_dbh->quote($_)} @array); $local_dbh->do("INSERT INTO remote_select VALUES ($row)"); } }, rewrite => sub { my ($new_query, $old_part) = @_; $new_query =~ s{\Q$old_part}{SELECT * FROM remote_select}si; return $new_query; }, }, { match => qr{(.*)}, after => sub { get('dbh')->do("DROP TABLE IF EXISTS remote_select"); } } );