| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | DBIx::ShowCaller - adds a Perl caller info to the SQL command | ||
| 5 | |||
| 6 | =cut | ||
| 7 | |||
| 8 | use DBI; | ||
| 9 | |||
| 10 | package DBIx::ShowCaller; | ||
| 11 | use vars qw! @ISA $AUTOLOAD $VERSION !; | ||
| 12 | @ISA = qw! DBI !; | ||
| 13 | |||
| 14 | $VERSION = '0.80'; | ||
| 15 | |||
| 16 | sub connect { | ||
| 17 | my $class = shift; | ||
| 18 | my ($dsn, $user, $password, $attrib) = @_; | ||
| 19 | $attrib->{'RootClass'} = 'DBIx::ShowCaller'; | ||
| 20 | $class->SUPER::connect($dsn, $user, $password, $attrib); | ||
| 21 | } | ||
| 22 | DBI::init_rootclass('DBIx::ShowCaller'); | ||
| 23 | |||
| 24 | package DBIx::ShowCaller::db; | ||
| 25 | use vars qw! @ISA $AUTOLOAD !; | ||
| 26 | @ISA = qw! DBI::db !; | ||
| 27 | |||
| 28 | sub prepare { | ||
| 29 | my ($self, $stmt) = ( shift, shift ); | ||
| 30 | |||
| 31 | my $comment = '/* no stack info found */ '; | ||
| 32 | for (my $i = 0; ; $i++) { | ||
| 33 | my ($package, $filename, $line) = caller $i; | ||
| 34 | last unless defined $package; | ||
| 35 | |||
| 36 | next if $package =~ /^(DB(Ix?\b|D::))/; | ||
| 37 | $filename =~ s!\*/!*_/!g; | ||
| 38 | $comment = "/* $filename at line $line */ "; | ||
| 39 | last; | ||
| 40 | } | ||
| 41 | |||
| 42 | $self->SUPER::prepare($comment . $stmt, @_); | ||
| 43 | } | ||
| 44 | |||
| 45 | package DBIx::ShowCaller::st; | ||
| 46 | use strict; | ||
| 47 | use vars qw! @ISA $AUTOLOAD !; | ||
| 48 | @ISA = qw! DBI::st !; | ||
| 49 | |||
| 50 | 1; | ||
| 51 | |||
| 52 | =head1 SYNOPSIS | ||
| 53 | |||
| 54 | use DBIx::ShowCaller; | ||
| 55 | my $dbh = DBIx::ShowCaller->connect('dbi:Oracle:prod', | ||
| 56 | 'test', 'test', { 'RaiseError' => 1 }); | ||
| 57 | |||
| 58 | # and follow as with normal DBI | ||
| 59 | $dbh->do('insert into jezek values (?)', {}, 45); | ||
| 60 | # will call (and log in V$SQL) | ||
| 61 | /* script.pl at line 7 */ | ||
| 62 | insert into jezek values (:p1) | ||
| 63 | |||
| 64 | =head1 DESCRIPTION | ||
| 65 | |||
| 66 | This module can be used instead of the DBI module. For each SQL command | ||
| 67 | that is prepared (both using $dbh->prepare and via do, selectall_* and | ||
| 68 | the like) it prepends a /* */ style comment containing information about | ||
| 69 | file and line that called that prepare/selectall_*/other method. Thus | ||
| 70 | it makes it easier to see where a particular SQL command came from. | ||
| 71 | |||
| 72 | Only database servers that log the whole SQL command (like Oracle or | ||
| 73 | MySQL) can make reasonable use of this. Also, if you call the same SQL | ||
| 74 | from different places of your code, the comment will be different and | ||
| 75 | the SQL server won't be able to reuse parsed info. Consider the speed | ||
| 76 | tradeoff here. | ||
| 77 | |||
| 78 | =head1 VERSION | ||
| 79 | |||
| 80 | 0.80 | ||
| 81 | |||
| 82 | =head1 AUTHOR | ||
| 83 | |||
| 84 | (c) 2000 Jan Pazdziora, adelton@fi.muni.cz, | ||
| 85 | http://www.fi.muni.cz/~adelton/ | ||
| 86 | at Faculty of Informatics, Masaryk University in Brno, Czech Republic | ||
| 87 | |||
| 88 | All rights reserved. This package is free software; you can | ||
| 89 | redistribute it and/or modify it under the same terms as Perl itself. | ||
| 90 | |||
| 91 | =head1 SEE ALSO | ||
| 92 | |||
| 93 | DBI(1) | ||
| 94 | |||
| 95 | =cut | ||
| 96 |