File Coverage

blib/lib/Data/Printer/Filter/DBIx/Class.pm
Criterion Covered Total %
statement 27 28 96.4
branch 4 4 100.0
condition n/a
subroutine 5 5 100.0
pod n/a
total 36 37 97.3


line stmt bran cond sub pod time code
1 1     1   221467 use strict;
  1         3  
  1         24  
2 1     1   5 use warnings;
  1         2  
  1         48  
3              
4             package Data::Printer::Filter::DBIx::Class;
5             $Data::Printer::Filter::DBIx::Class::VERSION = '0.000001';
6 1     1   6 use Data::Printer::Filter;
  1         1  
  1         6  
7 1     1   108 use Term::ANSIColor;
  1         3  
  1         435  
8              
9             # DBIx::Class filters
10             filter '-class' => sub {
11             my ( $obj, $properties ) = @_;
12              
13             if ( $obj->isa( 'DBIx::Class::Row' ) ) {
14             my %row = $obj->get_columns;
15             return _add_prefix( $obj, $properties, \%row );
16             }
17              
18             if ( $obj->isa( 'DBIx::Class::ResultSet' ) ) {
19             my @rows;
20             while ( my $row = $obj->next ) {
21             push @rows, { $row->get_columns };
22             }
23             return _add_prefix( $obj, $properties, @rows );
24             }
25              
26             return;
27             };
28              
29             sub _add_prefix {
30 2     2   3 my $obj = shift;
31 2         3 my $properties = shift;
32 2         4 my @rows = @_;
33              
34 2         9 my $str = colored( ref( $obj ), $properties->{color}{class} );
35 2 100       40 $str .= ' (' . $obj->result_class . ')' if $obj->can( 'result_class' );
36              
37 2 100       24 if ( $obj->can( 'as_query' ) ) {
38 1         5 my $query_data = $obj->as_query;
39 1         6 my @query_data = @$$query_data;
40 1         6 indent;
41 1         5 my $sql = shift @query_data;
42             $str
43             .= ' {'
44             . newline
45             . colored( $sql, 'bright_yellow' )
46             . newline
47             . join( newline,
48 1         5 map { $_->[1] . ' (' . $_->[0]{sqlt_datatype} . ')' }
  0         0  
49             @query_data );
50 1         25 outdent;
51 1         7 $str .= newline . '}';
52              
53             }
54              
55             # Remove require once Data::Printer > 0.36 is released
56 2         21 require Data::Printer;
57              
58 2         10 return $str . q{ } . Data::Printer::np( @rows );
59             }
60             1;
61              
62             =pod
63              
64             =encoding UTF-8
65              
66             =head1 NAME
67              
68             Data::Printer::Filter::DBIx::Class - Apply special Data::Printer filters to DBIx::Class objects
69              
70             =head1 VERSION
71              
72             version 0.000001
73              
74             =head1 SYNOPSIS
75              
76             In your program:
77              
78             use Data::Printer filters => { -external => ['DBIx::Class'] };
79              
80             or, in your C<.dataprinter> file:
81              
82             { filters => { -external => ['DBIx::Class'] } };
83              
84             =head1 DESCRIPTION
85              
86             Huge chunks of this have been lifted directly from L
87             This filter differs in that it also adds the values from C to
88             the output. For a L object, the column values are return in
89             the data. For a L, all of the rows in the ResultSet
90             are returned, with the contents of C included. If you're
91             dealing with huge ResultSets, this may not be what you want. Caveat emptor.
92              
93             =head1 AUTHOR
94              
95             Olaf Alders
96              
97             =head1 COPYRIGHT AND LICENSE
98              
99             This software is Copyright (c) 2015 by MaxMind, Inc..
100              
101             This is free software, licensed under:
102              
103             The Artistic License 2.0 (GPL Compatible)
104              
105             =cut
106              
107             __END__