Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Can DBI modify queries on the fly? (solution)

by kyle (Abbot)
on Feb 18, 2009 at 19:16 UTC ( #744853=note: print w/ replies, xml ) Need Help??


in reply to Can DBI modify queries on the fly?

This is the solution I eventually came up with uses callbacks on methods that make a request to the database.

The important clues came from these two notes.

Thanks to everyone for their patient replies!

use strict; use warnings; use DBI; my $dbh = DBI->connect( 'DBI:mysql:database=xxx', 'xxx', 'xxx', { RaiseError => 1 } ); my $callback = sub { return if $_[1] =~ m{ \n-- \s* \S+ .* \n \z }xms; my $drh = shift @_; my $new_req = shift @_; my $method_name = $_; $new_req =~ s{ ([^\n]) \z }{$1\n}xms; my $frame_num = 1; while ( my @frame_info = caller( $frame_num++ ) ) { $new_req .= "-- $frame_info[3] ($frame_info[1], line $frame_in +fo[2])\n"; } undef $_; return $drh->can( $method_name )->( $drh, $new_req, @_ ); }; my @methods = qw( do prepare prepare_cached selectrow_array selectrow_arrayref selectrow_hashref selectall_arrayref selectall_hashref selectcol_arrayref ); foreach my $method ( @methods ) { $dbh->{Callbacks}{$method} = $callback; } junker(); sub junker { for ( 1 .. 1_000_000 ) { my @junk = $dbh->selectrow_array( 'SELECT 1' ); } } __END__ mysql> show processlist; +----+------+-----------+-------+---------+------+-----------+-------- +------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info + | +----+------+-----------+-------+---------+------+-----------+-------- +------------------------------------------------+ | 36 | root | localhost | xxxxx | Query | 0 | NULL | show pr +ocesslist | | 51 | root | localhost | xxxxx | Query | 0 | executing | SELECT +1 -- main::junker (/home/kyle/junk.pl, line 45) | +----+------+-----------+-------+---------+------+-----------+-------- +------------------------------------------------+ 2 rows in set (0.00 sec)


Comment on Re: Can DBI modify queries on the fly? (solution)
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://744853]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2015-07-04 23:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls