| 1 | package Plack::Middleware::DBIC::QueryLog; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 3 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 3 | ▶ | use Plack::Util; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 3 | ▶ | use 5.008008; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 3 | extends 'Plack::Middleware'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 3 | our $VERSION = '0.05'; | |||||||||||||||||||
| 10 | 5 | ▶ | sub PSGI_KEY { 'plack.middleware.dbic.querylog' } | ||||||||||||||||||
|
Subroutine
PSGI_KEY: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 0 | ▶ | sub get_querylog_from_env { | ||||||||||||||||||
|
Subroutine
get_querylog_from_env: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 13 | 0 | my ($self_or_class, $env) = @_; | |||||||||||||||||||
| 14 | 0 | $env->{+PSGI_KEY}; | |||||||||||||||||||
| 15 | } | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | has 'querylog_class' => ( | ||||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||
| 19 | 3 | ▶ | default => sub { 'DBIx::Class::QueryLog' }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 3 | ); | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | has 'querylog_args' => ( | ||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||
| 24 | 2 | ▶ | default => sub { +{} }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | 3 | ); | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 3 | ▶ | sub _create_querylog { | ||||||||||||||||||
|
Subroutine
_create_querylog: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 3 | Plack::Util::load_class($_[0]->querylog_class) | |||||||||||||||||||
| 29 | ->new($_[0]->querylog_args); | ||||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 2 | ▶ | sub find_or_create_querylog_in { | ||||||||||||||||||
|
Subroutine
find_or_create_querylog_in: called CC=2 SCAR=8.1
Pod
undocumented
| |||||||||||||||||||||
| 33 | 2 | my ($self, $env) = @_; | |||||||||||||||||||
| 34 | 2 | ▶ | $env->{+PSGI_KEY} ||= $self->_create_querylog; | ||||||||||||||||||
|
Condition33%1/3
$$env{PSGI_KEY()} ||= $self->_create_querylog
MC/DC0%0/2
$$env{PSGI_KEY()} ||= $self->_create_querylog
$$env{PSGI_KEY()}
$self->_create_querylog
Truth table33%1/3
A ||= B
A= $$env{PSGI_KEY()}B= $self->_create_querylog
| |||||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 2 | ▶ | sub call { | ||||||||||||||||||
|
Subroutine
call: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 38 | 2 | my ($self, $env) = @_; | |||||||||||||||||||
| 39 | 2 | $self->find_or_create_querylog_in($env); | |||||||||||||||||||
| 40 | 2 | $self->app->($env); | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 3 | 1; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | =head1 NAME | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | Plack::Middleware::DBIC::QueryLog - Expose a DBIC QueryLog Instance in Middleware | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | =head1 SYNOPSIS | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | use Plack::Builder; | ||||||||||||||||||||
| 52 | builder { | ||||||||||||||||||||
| 53 | enable 'DBIC::QueryLog', | ||||||||||||||||||||
| 54 | querylog_args => {passthrough => 1}; | ||||||||||||||||||||
| 55 | $app; | ||||||||||||||||||||
| 56 | }; | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | =head1 DESCRIPTION | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | L<Plack::Middleware::DBIC::QueryLog> does one thing, it places an object that | ||||||||||||||||||||
| 61 | is either an instance of L<DBIx::Class::QueryLog> OR a compatible object into | ||||||||||||||||||||
| 62 | the C<$env> under C<plack.middleware.dbic.querylog>. A new instance is created | ||||||||||||||||||||
| 63 | for each incoming request. | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | The querylog is intended to be used by L<DBIX::Class> to log and profile SQL | ||||||||||||||||||||
| 66 | queries, particularly during the context of a web request handled by your | ||||||||||||||||||||
| 67 | L<Plack> application. See the documentation for L<DBIx::Class::QueryLog> and | ||||||||||||||||||||
| 68 | in L<DBIx::Class::Storage/debugobj> for more information. | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | This middleware is intended to act as a bridge between L<DBIx::Class>, which | ||||||||||||||||||||
| 71 | can consume and populate the querylog, with a reporting tool such as seen in | ||||||||||||||||||||
| 72 | L<Plack::Middleware::Debug::DBIC::QueryLog>. This functionality was refactored | ||||||||||||||||||||
| 73 | out of L<Plack::Middleware::Debug::DBIC::QueryLog> to facilitate interoperation | ||||||||||||||||||||
| 74 | with other types of reporting tools. For example, you may want query logging | ||||||||||||||||||||
| 75 | but you don't need the Plack debug panels (maybe you are building an RPC or | ||||||||||||||||||||
| 76 | REST application server and want sql query logging, is a possible use case). | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | Unless you are building some custom logging tools, you probably just want to | ||||||||||||||||||||
| 79 | use the existing debug panel (L<Plack::Middleware::Debug::DBIC::QueryLog>) | ||||||||||||||||||||
| 80 | rather than building something custom around this middleware. | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | If you are using an existing web application development system such as L<Catalyst>, | ||||||||||||||||||||
| 83 | you can use L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog::AdoptPlack> to | ||||||||||||||||||||
| 84 | 'hook' the query log into your L<DBIx::Class> schema model. If you are using | ||||||||||||||||||||
| 85 | a different framework, or building your own, please consider releasing your | ||||||||||||||||||||
| 86 | code or sending me a document patch suitable for including in a workbook or FAQ. | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =head1 ARGUMENTS | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | This middleware accepts the following arguments. | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | =head2 querylog_class | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | This is the class which is used to build the C<querylog> unless one is already | ||||||||||||||||||||
| 95 | defined. It defaults to L<DBIx::Class::QueryLog>. You should probably leave | ||||||||||||||||||||
| 96 | this alone unless you need to subclass or augment L<DBIx::Class::QueryLog>. | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | If the class name you pass has not already been included (via C<use> or | ||||||||||||||||||||
| 99 | C<require>) we will automatically try to C<require> it. | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | =head2 querylog_args | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | Accepts a HashRef of data which will be passed to L</"querylog_class"> when | ||||||||||||||||||||
| 104 | building the C<querylog>. | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | =head1 SUBROUTINES | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | This middleware defines the following public subroutines | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =head2 PSGI_KEY | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | Returns the PSGI C<$env> key under which you'd expect to find an instance of | ||||||||||||||||||||
| 113 | L<DBIx::Class::QueryLog>. | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =head2 get_querylog_from_env | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | Given a L<Plack> C<$env>, returns a L<DBIx::Class::QueryLog>, if one exists. | ||||||||||||||||||||
| 118 | You should use this in your code that is trying to access the querylog. For | ||||||||||||||||||||
| 119 | example: | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | use Plack::Middleware::DBIC::QueryLog; | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | sub get_querylog_from_env { | ||||||||||||||||||||
| 124 | my ($self, $env) = @_; | ||||||||||||||||||||
| 125 | Plack::Middleware::DBIC::QueryLog->get_querylog_from_env($env); | ||||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | This returns undef if it does not exist. This is the officially supported | ||||||||||||||||||||
| 129 | interface for extracting a L<DBIx::Class::QueryLog> from a L<Plack> request. | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =head1 SEE ALSO | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | L<Plack>, L<Plack::Middleware>, L<Plack::Middleware::Debug::DBIC::QueryLog>, | ||||||||||||||||||||
| 134 | L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog::AdoptPlack> | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =head1 AUTHOR | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | John Napiorkowski, C<< <jjnapiork@cpan.org> >> | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | Copyright 2011, John Napiorkowski | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||
| 145 | it under the same terms as Perl itself. | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | =cut | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | |||||||||||||||||||||