File Coverage

blib/lib/Data/Printer/Filter/DBIx/Class.pm
Criterion Covered Total %
statement 30 31 96.7
branch 4 4 100.0
condition n/a
subroutine 6 6 100.0
pod n/a
total 40 41 97.5


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