blib/lib/Catalyst/Plugin/DBIC/ConsoleQueryLog.pm

filter: Statementstmt 29.4% 20 / 68 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 23.0% 3 / 13 Podpod 100% 2 / 2 totaltotal 20.0% 25 / 125 CC 18 SCAR 25.2
CC
18
Cov
20%
__ANON__
30.0
__ANON__
12.0
__ANON__
12.0
CRAP 12.4
↑ Summary
1 package Catalyst::Plugin::DBIC::ConsoleQueryLog;
2  
3 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Catalyst::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Text::SimpleTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.002';
8  
9 1 my $_model_name = '';
10 0 ▶my $model_name = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
11 0 my $c = shift;
12 0 ▶ return $_model_name ||= do {
Condition0%0/3
$_model_name ||= do { if (my $config = $c->config->{"Plugin::DBIC::ConsoleQueryLog"}) { $config->{"model_name"}; } else { undef; } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$_model_name ||= do { if (my $config = $c->config->{"Plugin::DBIC::ConsoleQueryLog"}) { $config->{"model_name"}; } else { undef; } }
$_model_name do { if (my $config = $c->config->{"Plugin::DBIC::ConsoleQueryLog"}) { $config->{"model_name"}; } else { undef; } }
Truth table0%0/3
A ||= B
A= $_model_nameB= do { if (my $config = $c->config->{"Plugin::DBIC::ConsoleQueryLog"}) { $config->{"model_name"}; } else { undef; } }
ABresult
1X1
011
000
13 0 ▶ if(my $config = $c->config->{'Plugin::DBIC::ConsoleQueryLog'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $config = $c->config->{'Plugin::DBIC::ConsoleQueryLog'}) { } 0 0 0
14 0 $config->{'model_name'};
15 } else {
16 0 undef;
17 }
18 };
19 1 };
20  
21 0 ▶my $model = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
22 0 my $c = shift;
23 0 my $model = $c->model($c->$model_name);
24 0 ▶ if($model) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model) { } 0 0 0
25 0 return $model;
26 } else {
27 0 ▶ $c->log->info("You specified a model '$model_name' but I can't find it.") if $model_name;
Branch: 1 branch
BranchTrueFalseTotal
if $model_name 0 0 0
28 0 return;
29 }
30 1 };
31  
32 0 ▶my $querylog = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
33 0 my $c = shift;
34 0 ▶ my $model = $c->$model || return;
Condition0%0/2
$c->$model || (return)
l!l
00
MC/DC0%0/1
$c->$model || (return)
$c->$model
Truth table0%0/2
A
A= $c->$model
Aresult
11
00
35 0 ▶ if($model->can('querylog')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model->can('querylog')) { } 0 0 0
36 0 return $model->querylog;
37 } else {
38 0 $c->log->info("You requested querylog for model $model but there's no querylog_analyzer");
39 0 return;
40 }
41 1 };
42  
43 0 ▶my $time_elapsed = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
44 0 my $c = shift;
45 0 ▶ return ($c->$querylog || return)->time_elapsed;
Condition0%0/2
$c->$querylog || (return)
l!l
00
MC/DC0%0/1
$c->$querylog || (return)
$c->$querylog
Truth table0%0/2
A
A= $c->$querylog
Aresult
11
00
46 1 };
47  
48 0 ▶my $query_count = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
49 0 my $c = shift;
50 0 ▶ return ($c->$querylog || return)->count;
Condition0%0/2
$c->$querylog || (return)
l!l
00
MC/DC0%0/1
$c->$querylog || (return)
$c->$querylog
Truth table0%0/2
A
A= $c->$querylog
Aresult
11
00
51 1 };
52  
53 0 ▶my $querylog_analyzer = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
54 0 my $c = shift;
55 0 ▶ my $model = $c->$model || return;
Condition0%0/2
$c->$model || (return)
l!l
00
MC/DC0%0/1
$c->$model || (return)
$c->$model
Truth table0%0/2
A
A= $c->$model
Aresult
11
00
56 0 ▶ if($model->can('querylog_analyzer')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model->can('querylog_analyzer')) { } 0 0 0
57 0 return $model->querylog_analyzer;
58 } else {
59 0 $c->log->info("You requested querylog_analyzer for model $model but there's no querylog_analyzer");
60 0 return;
61 }
62 1 };
63  
64 0 ▶my $sorted_queries = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
65 0 my $c = shift;
66 0 ▶ my @sorted_queries = @{($c->$querylog_analyzer||return)
Condition0%0/4
($c->$querylog_analyzer || (return))->get_sorted_queries || []
l!l
00
$c->$querylog_analyzer || (return)
l!l
00
MC/DC0%0/1
($c->$querylog_analyzer || (return))->get_sorted_queries || []
($c->$querylog_analyzer || (return))->get_sorted_queries
MC/DC0%0/1
$c->$querylog_analyzer || (return)
$c->$querylog_analyzer
Truth table0%0/2
A
A= ($c->$querylog_analyzer || (return))->get_sorted_queries
Aresult
11
00
Truth table0%0/2
A
A= $c->$querylog_analyzer
Aresult
11
00
67 ->get_sorted_queries ||[]};
68 0 return @sorted_queries;
69 1 };
70  
71 0 ▶after 'finalize', sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
72 0 ▶ return unless (my $c = shift)->debug;
Branch: 1 branch
BranchTrueFalseTotal
unless (my $c = shift())->debug 0 0 0
73 0 my $t = $c->querylog_table;
74 0 my @sorted_queries = $c->$sorted_queries;
75 0 foreach my $q (@sorted_queries) {
76 0 $c->add_querylog_table_row($t, $q);
77 }
78 0 my $count = $c->$query_count;
79 0 my $time = sprintf('%0.6fs', $c->$time_elapsed);
80 0 ▶ my $q_display = $count > 1 ? 'queries':'query';
Branch: 1 branch
BranchTrueFalseTotal
$count > 1 ? : 0 0 0
81 0 ▶ $c->log->info( "SQL Profile Data ($count $q_display / $time elapsed time):\n" . $t->draw . "\n" ) if @sorted_queries;
Branch: 1 branch
BranchTrueFalseTotal
if @sorted_queries 0 0 0
82 1 };
83  
84 0 ▶sub querylog_table {
Subroutine querylog_table: not called CC=1 SCAR=6.9
Pod documented
85 0 my $column_width = Catalyst::Utils::term_width() - 6 - 16;
86 0 my $t = Text::SimpleTable->new( [ $column_width, 'SQL' ], [ 9, 'Time' ] );
87 0 return $t;
88 };
89  
90 0 ▶sub add_querylog_table_row {
Subroutine add_querylog_table_row: not called CC=2 SCAR=17.9
Pod documented
91 0 my ($c, $t, $q) = @_;
92 0 ▶ my $q_sql = $q->sql . ' : ' . join(', ', @{$q->params||[]});
Condition0%0/2
$q->params || []
l!l
00
MC/DC0%0/1
$q->params || []
$q->params
Truth table0%0/2
A
A= $q->params
Aresult
11
00
93 0 my $q_total = sprintf('%0.6fs', $q->time_elapsed);
94 0 $t->row($q_sql, $q_total);
95 };
96  
97 =head1 NAME
98
99 Catalyst::Plugin::DBIC::ConsoleQueryLog - Log DBIC queries and timings to the console
100  
101 =head1 SYNOPSIS
102
103 Use the plugin in your application class:
104
105 package MyApp;
106  
107 use Catalyst qw/DBIC::ConsoleQueryLog/;
108  
109 __PACKAGE__->config(
110 'Plugin::DBIC::ConsoleQueryLog' => { model_name => 'Schema' },
111 'Model::Schema' => { # This is a model of Catalyst::Model::DBIC::Schema
112 schema_class => 'MyApp::Schema',
113 traits => ['QueryLog'], # Please note you NEED to use this trait.
114 },
115 );
116  
117 __PACKAGE__->setup;
118
119 =head1 DESCRIPTION
120
121 This is a very basic console logger for L<DBIx::Class::QueryLog::Analyzer>, which you
122 might be using in L<Catalyst> via L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog>
123 in order to get tracing and performance information on you SQL calls. You must be
124 using L<Catalyst::Model::DBIC::Schema> and have added the L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog>
125 trait in configuration as in the L</SYNOPSIS> example.
126  
127 I wrote this because I got tired of adding it manually over and over to my basic
128 application framework. However its a very basic logger that doesn't support some of
129 the more powerful bits such as creating buckets to classify you logging, etc. It will
130 get you started but eventually you'll need to roll your own as you needs become more complex.
131  
132 Console logging will only occur when the application is run in debug mode. I recommend
133 only adding this plugin in development since it's needless overhead in production.
134  
135 =head1 METHOD
136  
137 This plugin exposes the following public methods.
138  
139 =head2 querylog_table
140  
141 Returns an instance of L<Text::SimpleTable> that is setup to display the rows of querylog
142 information. You can override this if you trying to customize the querylog information.
143  
144 =head2 add_querylog_table_row
145  
146 Receives an instance of L<Text::SimpleTable>, and L<DBIx::Class::QueryLog::Analyzer> so
147 that you can customize drawing a row of data. Override if you are adding custom display
148 information.
149  
150 =head1 AUTHOR
151
152 John Napiorkowski L<email:jjnapiork@cpan.org>
153
154 =head1 SEE ALSO
155
156 L<DBIx::Class>, L<DBIx::Class::QueryLog>, L<DBIx::Class::QueryLog::Analyzer>, L<Catalyst>,
157 L<Catalyst::Model::DBIC::Schema>, L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog>,
158
159 =head1 COPYRIGHT & LICENSE
160
161 Copyright 2017, John Napiorkowski L<email:jjnapiork@cpan.org>
162
163 This library is free software; you can redistribute it and/or modify it under
164 the same terms as Perl itself.
165
166 =cut
167  
168 1 1;
↑ Summary