[Jifty-commit] r5016 - in Jifty-DBI/branches/tisql: lib/Jifty/DBI

Jifty commits jifty-commit at lists.jifty.org
Tue Feb 5 21:15:57 EST 2008


Author: ruz
Date: Tue Feb  5 21:15:57 2008
New Revision: 5016

Modified:
   Jifty-DBI/branches/tisql/   (props changed)
   Jifty-DBI/branches/tisql/lib/Jifty/DBI/Handle.pm

Log:
 r4567 at cubic-pc (orig r4566):  sartak | 2007-11-29 20:00:27 +0300
  r45752 at onn:  sartak | 2007-11-29 11:59:58 -0500
  Let users run arbitrary code during SQL statement logging
 


Modified: Jifty-DBI/branches/tisql/lib/Jifty/DBI/Handle.pm
==============================================================================
--- Jifty-DBI/branches/tisql/lib/Jifty/DBI/Handle.pm	(original)
+++ Jifty-DBI/branches/tisql/lib/Jifty/DBI/Handle.pm	Tue Feb  5 21:15:57 2008
@@ -255,7 +255,31 @@
     return ( $self->{'_dologsql'} );
 }
 
-=head2 _log_sql_statement STATEMENT DURATION
+=head2 log_sql_hook NAME [, CODE]
+
+Used in instrumenting the SQL logging. You can use this to, for example, get a
+stack trace for each query (so you can find out where the query is being made).
+The name is required so that multiple hooks can be installed without stepping
+on eachother's toes.
+
+The coderef is run in scalar context and (currently) receives no arguments.
+
+If you don't pass CODE in, then the coderef currently assigned under
+NAME is returned.
+
+=cut
+
+sub log_sql_hook {
+    my $self = shift;
+    my $name = shift;
+
+    if (@_) {
+        $self->{'_logsqlhooks'}{$name} = shift;
+    }
+    return ( $self->{'_logsqlhooks'}{$name} );
+}
+
+=head2 _log_sql_statement STATEMENT DURATION BINDINGS
 
 add an SQL statement to our query log
 
@@ -266,8 +290,14 @@
     my $statement = shift;
     my $duration  = shift;
     my @bind      = @_;
+
+    my $results = {};
+    while (my ($name, $code) = each %{ $self->{'_logsqlhooks'} || {} }) {
+        $results->{$name} = $code->();
+    }
+
     push @{ $self->{'StatementLog'} },
-        ( [ Time::HiRes::time(), $statement, [@bind], $duration ] );
+        ( [ Time::HiRes::time(), $statement, [@bind], $duration, $results ] );
 
 }
 
@@ -284,9 +314,12 @@
 
 =head2 sql_statement_log
 
-Returns the current SQL statement log as an array of arrays. Each entry is a list of 
+Returns the current SQL statement log as an array of arrays. Each entry is a list of:
+
+(Time, Statement, [Bindings], Duration, {HookResults})
 
-(Time, Statement, [Bindings], Duration)
+Bindings is an arrayref of the values of any placeholders. HookResults is a
+hashref keyed by hook name.
 
 =cut
 


More information about the Jifty-commit mailing list