Data/Printer/Filter/DB.pm untested

filter: Statementstmt 0.0% 0 / 245 Branchbran 0.0% 0 / 122 Conditioncond 0.0% 0 / 67 MC/DCmcdc 0.0% 0 / 41 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 483 CC 22 SCAR 58.9
CC
22
Cov
0%
_show_column_details
462.0
_get_db_status
6.0
BEGIN
2.0
CRAP 360.1
1 package Data::Printer::Filter::DB;
2 use strict;
3 use warnings;
4 use Data::Printer::Filter;
5 use Data::Printer::Common;
6  
7 filter 'DBI::db', sub {
8 my ($dbh, $ddp) = @_;
9 my $name = $dbh->{Driver}{Name};
10  
11 my $string = "$name Database Handle "
12 . $ddp->maybe_colorize('(', 'brackets')
13 . _get_db_status($dbh->{Active}, $ddp)
14 . $ddp->maybe_colorize(')', 'brackets')
15 ;
16 return $string
17 if exists $ddp->extra_config->{filter_db}{connection_details}
18 && !$ddp->extra_config->{filter_db}{connection_details};
19  
20 $string .= ' ' . $ddp->maybe_colorize('{', 'brackets');
21 $ddp->indent;
22 my %dsn = split( /[;=]/, $dbh->{Name} );
23 foreach my $k (keys %dsn) {
24 $string .= $ddp->newline . $k . $ddp->maybe_colorize(':', 'separator')
25 . ' ' . $dsn{$k};
26 }
27 $string .= $ddp->newline . 'Auto Commit: ' . $dbh->{AutoCommit};
28  
29 my $kids = $dbh->{Kids};
30 $string .= $ddp->newline . 'Statement Handles: ' . $kids;
31 if ($kids > 0) {
32 $string .= ' (' . $dbh->{ActiveKids} . ' active)';
33 }
34  
35 if ( defined $dbh->err ) {
36 $string .= $ddp->newline . 'Error: ' . $dbh->errstr;
37 }
38 $string .= $ddp->newline . 'Last Statement: '
39 . $ddp->maybe_colorize(($dbh->{Statement} || '-'), 'string');
40  
41 $ddp->outdent;
42 $string .= $ddp->newline . $ddp->maybe_colorize('}', 'brackets');
43 return $string;
44 };
45  
46 filter 'DBI::st', sub {
47 my ($sth, $ddp) = @_;
48 my $str = $ddp->maybe_colorize(($sth->{Statement} || '-'), 'string');
49  
50 if ($sth->{NUM_OF_PARAMS} > 0) {
51 my $values = $sth->{ParamValues};
52 if ($values) {
53 $str .= ' ' . $ddp->maybe_colorize('(', 'brackets')
54 . join($ddp->maybe_colorize(',', 'separator') . ' ',
55 map {
56 my $v = $values->{$_};
57 $ddp->parse($v);
58 } 1 .. $sth->{NUM_OF_PARAMS}
59 )
60 . $ddp->maybe_colorize(')', 'brackets');
61 }
62 else {
63 $str .= ' ' . $ddp->maybe_colorize('(bindings unavailable)', 'undef');
64 }
65 }
66 return $str;
67 };
68  
69 # DBIx::Class filters
70 filter 'DBIx::Class::Schema' => sub {
71 my ($schema, $ddp) = @_;
72  
73 my $name = $ddp->maybe_colorize(ref($schema), 'class');
74 my $storage = $schema->storage;
75 my $config = {};
76 $config = $ddp->extra_config->{filter_db}{schema}
77 if exists $ddp->extra_config->{filter_db}
78 && exists $ddp->extra_config->{filter_db}{schema};
79  
80 my $expand = exists $config->{expand}
81 ? $config->{expand}
82 : $ddp->class->expand
83 ;
84 my $connected = _get_db_status($storage->connected, $ddp);
85 if (!$expand) {
86 return "$name " . $ddp->maybe_colorize('(', 'brackets')
87 . $storage->sqlt_type . " - $connected"
88 . $ddp->maybe_colorize(')', 'brackets')
89 ;
90 }
91  
92 $ddp->indent;
93 my $output = $name . ' ' . $ddp->maybe_colorize('{', 'brackets')
94 . $ddp->newline
95 . 'connection: ' . ($config->{show_handle}
96 ? $ddp->parse($storage->dbh)
97 : $storage->sqlt_type . " Database Handle ($connected)"
98 );
99 if ($storage->is_replicating) {
100 $output .= $ddp->newline . 'replication lag: ' . $storage->lag_behind_master;
101 }
102 my $load_sources = 'names';
103 if (exists $config->{loaded_sources}) {
104 my $type = $config->{loaded_sources};
105 if ($type && ($type eq 'names' || $type eq 'details' || $type eq 'none')) {
106 $load_sources = $type;
107 }
108 else {
109 Data::Printer::Common::_warn(
110 $ddp,
111 "filter_db.schema.loaded_sources must be names, details or none"
112 );
113 }
114 }
115 if ($load_sources ne 'none') {
116 my @sources = $schema->sources;
117 @sources = Data::Printer::Common::_nsort(@sources)
118 if $ddp->class->sort_methods && @sources;
119  
120 $output .= $ddp->newline . 'loaded sources:';
121 if ($load_sources eq 'names') {
122 $output .= ' ' . (@sources
123 ? join(', ', map($ddp->maybe_colorize($_, 'method'), @sources))
124 : '-'
125 );
126 }
127 else {
128 $ddp->indent;
129 foreach my $i (0 .. $#sources) {
130 my $source = $schema->source($sources[$i]);
131 $output .= $ddp->newline . $ddp->parse($source);
132 $output .= $ddp->maybe_colorize(',', 'separator') if $i < $#sources;
133 }
134 $ddp->outdent;
135 }
136 }
137 $ddp->outdent;
138 $output .= $ddp->newline . $ddp->maybe_colorize('}', 'brackets');
139 return $output;
140 };
141  
142 filter 'DBIx::Class::Row' => sub {
143 my ($row, $ddp) = @_;
144  
145 my $output = $row->result_source->source_name
146 . ' Row ' . $ddp->maybe_colorize('(', 'brackets')
147 . ($row->in_storage ? '' : 'NOT ') . 'in storage'
148 . $ddp->maybe_colorize(') {', 'brackets');
149  
150 $ddp->indent;
151 my %orig_columns = map { $_ => 1 } $row->columns;
152 my %data = $row->get_columns;
153 my %dirty = $row->get_dirty_columns;
154 # TODO: maybe also get_inflated_columns() ?
155 my @ordered = Data::Printer::Common::_nsort(keys %data);
156 my $longest = 0;
157 foreach my $col (@ordered) {
158 my $l = length $col;
159 $longest = $l if $l > $longest;
160 }
161 my $show_updated_label = !exists $ddp->extra_config->{filter_db}{show_updated_label}
162 || $ddp->extra_config->{filter_db}{show_updated_label};
163 my $show_extra_label = !exists $ddp->extra_config->{filter_db}{show_extra_label}
164 || $ddp->extra_config->{filter_db}{show_extra_label};
165  
166 foreach my $col (@ordered) {
167 my $padding = $longest - length($col);
168 my $content = $data{$col};
169 $output .= $ddp->newline . $col
170 . $ddp->maybe_colorize(':', 'separator')
171 . ' ' . (' ' x $padding)
172 . $ddp->parse(\$content, seen_override => 1)
173 ;
174  
175 if (exists $dirty{$col} && $show_updated_label) {
176 $output .= ' (updated)';
177 }
178 if (!exists $orig_columns{$col} && $show_extra_label) {
179 $output .= ' (extra)';
180 }
181 }
182 # TODO: methods: foo, bar <-- follows class.*, but can be overriden by filter_db.class.*
183 $ddp->outdent;
184 $output .= $ddp->newline . $ddp->maybe_colorize('}', 'brackets');
185 return $output;
186 };
187  
188 filter 'DBIx::Class::ResultSet' => sub {
189 my ($rs, $ddp) = @_;
190  
191 $ddp->indent;
192 my $output = $rs->result_source->source_name
193 . ' ResultSet ' . $ddp->maybe_colorize('{', 'brackets')
194 . $ddp->newline;
195  
196 # NOTE: we're totally breaking DBIC's encapsulation here. But since DDP
197 # is a tool to inspect the inner workings of objects, it's okay. Ish.
198 $output .= 'current search parameters: ';
199 my $attrs;
200 if ($rs->can('_resolved_attrs') && eval {
201 $attrs = { %{ $rs->_resolved_attrs } }; 1;
202 } && ref $attrs eq 'HASH'
203 ) {
204 if (exists $attrs->{where}) {
205 $output .= $ddp->parse($attrs->{where})
206 }
207 else {
208 $output .= '-';
209 }
210 }
211 else {
212 $output .= $ddp->maybe_colorize('(unable to lookup - patches welcome!)', 'unknown');
213 }
214 # TODO: show joins/prefetches/from
215 # TODO: look at get_cache() for results
216 if ($rs->can('as_query')) {
217 my $query_data = $rs->as_query;
218 my @query_data = @$$query_data;
219 my $sql = shift @query_data;
220 $output .= $ddp->newline . 'as query:';
221 $ddp->indent;
222 $output .= $ddp->newline
223 . $ddp->maybe_colorize( $sql, 'string' )
224 ;
225 if (@query_data) {
226 $output .= $ddp->newline . join( $ddp->newline, map {
227 my $bound = $_->[1];
228 if ($_->[0]{sqlt_datatype}) {
229 $bound .= ' ' . $ddp->maybe_colorize('(', 'brackets')
230 . $_->[0]{sqlt_datatype} . $ddp->maybe_colorize(')', 'brackets');
231 }
232 $bound
233 } @query_data
234 );
235 }
236 $ddp->outdent;
237 }
238 if (my $cached = $rs->get_cache) {
239 $output .= $ddp->newline . 'cached results:';
240 $ddp->indent;
241 $output .= $ddp->newline . $ddp->parse($cached);
242 $ddp->outdent;
243 }
244  
245 $ddp->outdent;
246 $output .= $ddp->newline . $ddp->maybe_colorize('}', 'brackets');
247 return $output;
248 };
249  
250 filter 'DBIx::Class::ResultSource' => sub {
251 my ($source, $ddp) = @_;
252 my $cols = $source->columns_info;
253  
254 my $output = $source->source_name . ' ResultSource';
255 if ($source->isa('DBIx::Class::ResultSource::View')) {
256 $output .= ' ' . $ddp->maybe_colorize('(', 'brackets')
257 . ($source->is_virtual ? 'Virtual ' : '')
258 . 'View' . $ddp->maybe_colorize(')', 'brackets')
259 ;
260 }
261  
262 my $show_source_table = !exists $ddp->extra_config->{filter_db}{show_source_table}
263 || $ddp->extra_config->{filter_db}{show_source_table};
264 my $column_info = 'details';
265 if (exists $ddp->extra_config->{filter_db}{column_info}) {
266 my $new = $ddp->extra_config->{filter_db}{column_info};
267 if ($new && ($new eq 'names' || $new eq 'details' || $new eq 'none')) {
268 $column_info = $new;
269 }
270 else {
271 Data::Printer::Common::_warn(
272 $ddp,
273 "filter_db.column_info must be names, details or none"
274 );
275 }
276 }
277 return $output if !$show_source_table && $column_info eq 'none';
278  
279 $ddp->indent;
280 $output .= ' ' . $ddp->maybe_colorize('{', 'brackets');
281 if ($show_source_table) {
282 $output .= $ddp->newline . 'table: ' . $ddp->parse(\$source->name);
283 }
284 if ($column_info ne 'none') {
285 my $columns = $source->columns_info;
286 $output .= $ddp->newline . 'columns:';
287 $output .= ' - ' unless %$columns;
288 my $separator = $ddp->maybe_colorize(',', 'separator') . ' ';
289 if ($column_info eq 'names') {
290 my %parsed_cols = map { $_ => 1 } keys %$columns;
291 my @primary = Data::Printer::Common::_nsort($source->primary_columns);
292 if (@primary) {
293 delete $parsed_cols{$_} foreach @primary;
294 $output .= ' ' . join($separator => map {
295 $ddp->maybe_colorize($_, 'method') . ' (primary)'
296 } @primary
297 );
298 $output .= ',' if keys %parsed_cols;
299 }
300 if (keys %parsed_cols) {
301 $output .= ' ' . join($separator => map {
302 $ddp->maybe_colorize($_, 'method')
303 } Data::Printer::Common::_nsort(keys %parsed_cols)
304 );
305 }
306 }
307 else { # details!
308 $output .= _show_column_details($source, $columns, $ddp);
309 }
310 my %uniques = $source->unique_constraints;
311 delete $uniques{primary};
312 if (keys %uniques) {
313 $output .= $ddp->newline . 'non-primary uniques:';
314 $ddp->indent;
315 foreach my $key (Data::Printer::Common::_nsort(keys %uniques)) {
316 $output .= $ddp->newline
317 . $ddp->maybe_colorize('(', 'brackets')
318 . join($separator, @{$uniques{$key}})
319 . $ddp->maybe_colorize(')', 'brackets') . " as '$key'"
320 ;
321 }
322 $ddp->outdent;
323 }
324  
325 # TODO: use $source->relationships and $source->relationship_info
326 # to list relationships between sources. (filter_db.show_relationships
327 # TODO: public methods implemented by the user
328 # TODO; "current result count" (touching the db)
329 # TODO: "first X eresults" (touching the db)
330 }
331 $ddp->outdent;
332 return $output . $ddp->newline . $ddp->maybe_colorize('}', 'brackets');
333 };
334  
335 sub _show_column_details {
336 my ($source, $columns, $ddp) = @_;
337 my $output = '';
338 my %parsed_columns;
339 foreach my $colname (keys %$columns) {
340 my $meta = $columns->{$colname};
341 my $parsed = ' ';
342 if (exists $meta->{data_type} && defined $meta->{data_type}) {
343 $parsed .= $meta->{data_type};
344 if (exists $meta->{size}) {
345 my @size = ref $meta->{size} eq 'ARRAY'
346 ? @{$meta->{size}} : ($meta->{size})
347 ;
348 if ($meta->{data_type} =~ /\((.+?)\)/) {
349 my @other_size = split ',' => $1;
350 my $different_sizes = @size != @other_size;
351 if (!$different_sizes) {
352 foreach my $i (0 .. $#size) {
353 if ($size[$i] != $other_size[$i]) {
354 $different_sizes = 1;
355 last;
356 }
357 }
358 }
359 if ($different_sizes) {
360 $parsed .= ' (meta size as ' . join(',' => @size) . ')';
361 }
362 }
363 else {
364 $parsed .= '(' . join(',' => @size) . ')';
365 }
366 }
367 }
368 else {
369 $parsed .= $ddp->maybe_colorize('(unknown data type)', 'unknown');
370 }
371 if (exists $meta->{is_nullable}) {
372 $parsed .= ((' not')x !$meta->{is_nullable}) . ' null';
373 }
374 if (exists $meta->{default_value} && defined $meta->{default_value}) {
375 my $default = $meta->{default_value};
376 if (ref $default) {
377 $default = $$default;
378 }
379 elsif (defined $meta->{is_numeric}) { # <-- not undef!
380 $default = $meta->{is_numeric} ? 0+$default : qq("$default");
381 }
382 elsif ($source->storage->is_datatype_numeric($meta->{data_type})) {
383 $default = 0+$default;
384 }
385 else {
386 $default = qq("$default");
387 }
388 $parsed .= " default $default";
389 }
390 if (exists $meta->{is_auto_increment} && $meta->{is_auto_increment}) {
391 $parsed .= ' auto_increment';
392 }
393 $parsed_columns{$colname} = $parsed;
394 }
395  
396 my @primary_keys = $source->primary_columns;
397 if (keys %parsed_columns || @primary_keys) {
398 my $separator = $ddp->maybe_colorize(',', 'separator');
399 $ddp->indent;
400 foreach my $colname (@primary_keys) {
401 my $value = exists $parsed_columns{$colname}
402 ? delete $parsed_columns{$colname} : '';
403 $output .= $ddp->newline . $colname
404 . (defined $value ? $value : '')
405 . ' (primary)'
406 . (keys %parsed_columns ? $separator : '')
407 ;
408 }
409 if (keys %parsed_columns) {
410 my @sorted_columns = Data::Printer::Common::_nsort(keys %parsed_columns);
411 foreach my $i (0 .. $#sorted_columns) {
412 my $colname = $sorted_columns[$i];
413 # TODO: v-align column names (like hash keys)
414 $output .= $ddp->newline . $colname
415 . $parsed_columns{$colname}
416 . ($i == $#sorted_columns ? '' : $separator)
417 ;
418 }
419 }
420 $ddp->outdent;
421 }
422 return $output;
423 }
424  
425  
426 sub _get_db_status {
427 my ($status, $ddp) = @_;
428 return $status
429 ? $ddp->maybe_colorize('connected', 'filter_db_connected', '#a0d332')
430 : $ddp->maybe_colorize('disconnected', 'filter_db_disconnected', '#b3422d')
431 ;
432 }
433  
434 1;
435 __END__