Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

sql debugging hooks

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


mintywalker at gmail

Jul 11, 2009, 11:45 AM

Post #1 of 4 (828 views)
Permalink
sql debugging hooks

Hi,

I'm trying to create some hooks to make it easy to see what sql
queries are being run by any particular catalyst request.

I have:

package My::Schema;
use base qw/DBIx::Class::Schema/;

and then Schema/Employee.pm module, etc:

package My::Schema::Employee;
use base qw/DBIx::Class/;
...

I also have:

package My::Model::DB;
use base qw/Catalyst::Model::DBIC::Schema/;
__PACKAGE__->config(schema_class => 'My::Schema');

as described in Catalyst::Model::DBIC::Schema, so that in my Catalyst
Controllers I can do:

my $model = $c->model('DB::Employee');

And that all works, I can run queries against it and the connection
details are magically loaded from my.conf.

Then, for debugging sql queries, I have:

package My::Model::Debugger;
use base qw<DBIx::Class::Storage::Statistics>;
sub query_start { ... }

Now currently I have this in a Controller module:

my $debugger = My::Model::Debugger->new();
my $model = $c->model('DB::Employee');
$model->result_source->storage->debugobj($debugger);
$model->result_source->storage->debug(1);
my $employee = $mode->find(123);
$model->result_source->storage->debug(0);

Which results in my query_start method being called with the sql being run and
the parameters for it. Which is what I was after.

But I want to inject my $debugger via the Model or schema, and
enable/disable it globablly via a configuration file flag.

I'm a bit lost as to where/how I can do that. Any pointers?

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


mintywalker at gmail

Jul 11, 2009, 11:47 AM

Post #2 of 4 (745 views)
Permalink
Re: sql debugging hooks [In reply to]

> But I want to inject my $debugger via the Model or schema, and
> enable/disable it globablly via a configuration file flag.

What I meant to say was I want to inject my custom debugobj handler
globally (at least in development), then simplify the code in the
Controllers to just:

$model->result_source->storage->debug(1);
my $employee = $mode->find(123);
$model->result_source->storage->debug(0);

When I want to debug that one particular query.

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


ash_cpan at firemirror

Jul 11, 2009, 12:06 PM

Post #3 of 4 (745 views)
Permalink
Re: Re: sql debugging hooks [In reply to]

On 11 Jul 2009, at 19:47, Minty wrote:

>> But I want to inject my $debugger via the Model or schema, and
>> enable/disable it globablly via a configuration file flag.
>
> What I meant to say was I want to inject my custom debugobj handler
> globally (at least in development), then simplify the code in the
> Controllers to just:
>
> $model->result_source->storage->debug(1);
> my $employee = $mode->find(123);
> $model->result_source->storage->debug(0);
>
> When I want to debug that one particular query.
>

See http://search.cpan.org/perldoc?Catalyst::Model::DBIC::Schema::QueryLog
and http://search.cpan.org/perldoc?Catalyst::TraitFor::Model::DBIC::Schema::QueryLog
.

That includes a TT snippet you can (conditionally) put in your
wrapper, or you could produce something similar for console output

-ash

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


bobtfish at bobtfish

Jul 11, 2009, 4:49 PM

Post #4 of 4 (735 views)
Permalink
Re: sql debugging hooks [In reply to]

On 11 Jul 2009, at 19:45, Minty wrote:
> But I want to inject my $debugger via the Model or schema, and
> enable/disable it globablly via a configuration file flag.
>
> I'm a bit lost as to where/how I can do that. Any pointers?

package MyApp::Model::DB;
use Moose;
use namespace::autoclean;

extends 'Catalyst::Model::DBIC::Schema';

__PACKAGE__->config(debug => 1);

after 'BUILD' => sub {
my ($self, $args) = @_;
if ($args->{debug}) {
my $debugger = My::Model::Debugger->new();
$self->result_source->storage->debugobj($debugger);
$self->result_source->storage->debug(1);
}
}

# Try something like that, untested...

Cheers
t0m


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.