[Catalyst] Re: Catalyst::Plugin::DBIC::QueryLog

Fayland Lam fayland at gmail.com
Tue Sep 4 13:02:14 GMT 2007


Jonathan Rockway wrote:
> Fayland Lam wrote:
>> see attachment. check if it's OK?
>>
>> now it's not per request any more. hmm, after several requests,
> mst wrote:
>> That's trivial with Catalyst::Component::InstancePerContext.
> =

> He intended for you to use that. =

> =

> It implements the per-request magic for you with some clever use of
> ACCEPT_CONTEXT and the stash.  Basically, it calls your builder method
> if you don't already have an instance in $c->stash.  Since that gets
> cleared every request, you get your per-request.
> =

> So, try again :)  You're almost there :)


Thank God. it works now. :)
plz have a check, then I'll commit it to Catalyst trunk and release as a =

CPAN module.

Thanks for your help.


> =

> Regards,
> Jonathan Rockway
> =

> =

> =

> ------------------------------------------------------------------------
> =

> _______________________________________________
> List: Catalyst at lists.rawmode.org
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.or=
g/
> Dev site: http://dev.catalyst.perl.org/


-- =

Fayland Lam // http://www.fayland.org/
-------------- next part --------------
package Catalyst::Model::DBIC::Schema::QueryLog;

use warnings;
use strict;
use vars qw/$VERSION/;
$VERSION =3D '0.01';

use base 'Catalyst::Model::DBIC::Schema';
__PACKAGE__->mk_accessors('querylog');

use DBIx::Class::QueryLog;
use DBIx::Class::QueryLog::Analyzer;
use Moose;
with 'Catalyst::Component::InstancePerContext';

sub querylog_analyzer {
    my $self =3D shift;
    =

    my $ann =3D new DBIx::Class::QueryLog::Analyzer( { querylog =3D> $self-=
>querylog } );
    return $ann;
}

sub build_per_context_instance {
    my ($self, $c) =3D @_;
    =

    my $schema =3D $self->schema;
    =

    my $querylog =3D new DBIx::Class::QueryLog();
    $self->querylog($querylog);
    =

    $schema->storage->debugobj( $querylog );
    $schema->storage->debug(1);
    =

    return $self;
}

1; # End of Catalyst::Model::DBIC::Schema::QueryLog
__END__

=3Dhead1 NAME

Catalyst::Model::DBIC::Schema::QueryLog - DBIx::Class::QueryLog Model Class

=3Dhead1 SYNOPSIS

  package MyApp::Model::FilmDB;
  use base qw/Catalyst::Model::DBIC::Schema::QueryLog/;

  __PACKAGE__->config(
      schema_class =3D> 'MyApp::Schema::FilmDB',
      connect_info =3D> [
                        "DBI:...",
                        "username",
                        "password",
                        {AutoCommit =3D> 1}
                      ]
  );

=3Dhead1 DESCRIPTION

Generally, you should check the document of L<Catalyst::Model::DBIC::Schema=
>. this module use base of it, and only provide extra two methods below.

=3Dhead1 METHODS

=3Dover 4

=3Ditem querylog

an instance of DBIx::Class::QueryLog.

=3Ditem querylog_analyzer

an instance of DBIx::Class::QueryLog::Analyzer.

=3Dback

=3Dhead1 EXAMPLE CODE

  <div class=3D"featurebox">
    <h3>Query Log Report</h3>
    [% SET total =3D c.model('DBIC').querylog.time_elapsed | format('%0.6f'=
) %]
    <div>Total SQL Time: [% total | format('%0.6f') %] seconds</div>
    [% SET qcount =3D c.model('DBIC').querylog.count %]
    <div>Total Queries: [% qcount %]</div>
    [% IF qcount %]
    <div>Avg Statement Time: [% (c.model('DBIC').querylog.time_elapsed / qc=
ount) | format('%0.6f') %] seconds.</div>
    <div>
     <table class=3D"table1">
      <thead>
       <tr>
        <th colspan=3D"3">5 Slowest Queries</th>
       </tr>
      </thead>
      <tbody>
       <tr>
        <th>Time</th>
        <th>%</th>
        <th>SQL</th>
       </tr>
       [% SET i =3D 0 %]
       [% FOREACH q =3D c.model('DBIC').querylog_analyzer.get_sorted_querie=
s %]
       <tr class=3D"[% IF loop.count % 2 %]odd[% END %]">
        <th class=3D"sub">[% q.time_elapsed | format('%0.6f') %]
        <td>[% ((q.time_elapsed / total ) * 100 ) | format('%i') %]%</td>
        <td>[% q.sql %]</td>
       </th></tr>
       [% IF i =3D=3D 5 %]
        [% LAST %]
       [% END %]
       [% SET i =3D i + 1 %]
       [% END %]
      </tbody>
     </table>
    </div>
    [% END %]
  </div>

=3Dhead1 SEE ALSO

L<Catalyst::Model::DBIC::Schema>

L<DBIx::Class::QueryLog>

=3Dhead1 AUTHOR

Fayland Lam, C<< <fayland at gmail.com> >>

=3Dhead1 COPYRIGHT & LICENSE

Copyright 2007 Fayland Lam, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=3Dcut


More information about the Catalyst mailing list