Skip to content

Instantly share code, notes, and snippets.

@jar-o
Last active January 9, 2017 05:03
Show Gist options
  • Save jar-o/25ba571709de15a83361 to your computer and use it in GitHub Desktop.
Save jar-o/25ba571709de15a83361 to your computer and use it in GitHub Desktop.
Want to see the trace leading up to the sql output? Here you go.
=pod
Put this is in the module in which you want to see SQL:
use DBIxOut;
my $dbout = DBIxOut->new();
$dbout->enable_from_rs($resultset);
# OR
my $schema = $self->schema;
$dbout->enable_from_schema($schema);
=cut
package DBIxOut;
use base qw<DBIx::Class::Storage::Statistics>;
use Data::Dumper qw<Dumper>;
use SQL::QueryBuilder::Pretty;
use Devel::StackTrace;
binmode STDOUT, ':encoding(UTF-8)';
sub query_start {
my ($self, $sql_query, @params) = @_;
print "-"x80, "\n";
my $trace = Devel::StackTrace->new();
# from bottom (least recent) of stack to top.
while ( my $frame = $trace->prev_frame() ) {
print join ' ', $frame->subroutine() . '()',
$frame->filename() . ':' . $frame->line() . "\n";
}
print "\n\n";
my $pretty = SQL::QueryBuilder::Pretty->new(
'-indent_ammount' => 4,
'-indent_char' => ' ',
'-new_line' => "\n",
);
print $pretty->print($sql_query), "\n";
print "/****** params *******\n\t";
print join "\n\t", @params;
print "\n*********************/\n";
print "-"x80, "\n\n";
}
sub enable_from_rs {
my ($self, $resultset) = @_;
$self->enable_from_schema($resultset->result_source->schema);
}
sub enable_from_schema {
my ($self, $schema) = @_;
$schema->storage->debug(1);
$schema->storage->debugobj($self);
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment