diff --git a/lib/WebGUI/SQL.pm b/lib/WebGUI/SQL.pm index b13d5a165..c3ed2cf2b 100644 --- a/lib/WebGUI/SQL.pm +++ b/lib/WebGUI/SQL.pm @@ -203,7 +203,6 @@ sub buildHashRef { unless ($options->{noOrder}) { tie %hash, "Tie::IxHash"; } - $self->session->log->query($sql, $params); my $dbh = $self->dbh; my $results = $dbh->selectall_arrayref($sql, {}, @$params); if ($dbh->err) { @@ -428,10 +427,15 @@ sub connect { my $pass = shift; my $params = shift; + require WebGUI::SQL::Trace; + open my $trace_handle, '>:via(WebGUI::SQL::Trace)', $session; my (undef, $driver) = DBI->parse_dsn($dsn); - my $dbh = DBI->connect($dsn,$user,$pass,{RaiseError => 0, AutoCommit => 1, + my $dbh = DBI->connect($dsn, $user, $pass, { + RaiseError => 0, + AutoCommit => 1, $driver eq 'mysql' ? (mysql_enable_utf8 => 1) : (), }); + $dbh->trace('2|SQL', $trace_handle); unless (defined $dbh) { $session->errorHandler->error("Couldn't connect to database: $dsn : $DBI::errstr"); diff --git a/lib/WebGUI/SQL/ResultSet.pm b/lib/WebGUI/SQL/ResultSet.pm index ad0d25c46..4d64ccd9b 100644 --- a/lib/WebGUI/SQL/ResultSet.pm +++ b/lib/WebGUI/SQL/ResultSet.pm @@ -132,9 +132,7 @@ sub execute { my $self = shift; my $placeholders = shift || []; my $sql = $self->{_sql}; - my $errorHandler = $self->db->session->errorHandler; - $errorHandler->query($sql,$placeholders); - $self->sth->execute(@{ $placeholders }) or $errorHandler->fatal("Couldn't execute prepared statement: $sql : With place holders: ".join(", ", @{$placeholders}).". Root cause: ". $self->errorMessage); + $self->sth->execute(@{ $placeholders }) or $self->session->errorHandler->fatal("Couldn't execute prepared statement: $sql : With place holders: ".join(", ", @{$placeholders}).". Root cause: ". $self->errorMessage); } diff --git a/lib/WebGUI/SQL/Trace.pm b/lib/WebGUI/SQL/Trace.pm new file mode 100644 index 000000000..efe382fdf --- /dev/null +++ b/lib/WebGUI/SQL/Trace.pm @@ -0,0 +1,55 @@ +package WebGUI::SQL::Trace; +use strict; +#use warnings; +use 5.008008; + +our $VERSION = '0.0.1'; + +sub PUSHED { + my ($class, $mode, $fh) = @_; + my $logger; + return bless \$logger, $class; +} + +sub OPEN { + my ($self, $session, $mode, $fh) = @_; + $$self = $session; + return 1; +} + +sub WRITE { + my ($self, $buf, $fh) = @_; + if ($buf =~ /\ABinding parameters: /) { + my $sql = $buf; + $sql =~ s/\ABinding parameters: //; + my $sub; + my $line; + for ( my $i = 0; caller($i); $i++) { + (my $package, undef, $line) = caller($i); + next + if $package eq 'WebGUI::SQL'; + next + if $package eq 'WebGUI::SQL::ResultSet'; + ($sub) = (caller($i + 1))[3]; + last; + } + $$self->log->debug("Query - $sub($line) : $sql"); + } + return length($buf); +} + +sub CLOSE { + my $self = shift; + return 0; +} + +1; + +__END__ + +=head1 NAME + +PerlIO::via::WebGUI - Log DBI output to WebGUI + +=cut +