| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Data::Printer::Filter::DBIx::Class; | ||
| 5 | $Data::Printer::Filter::DBIx::Class::VERSION = '0.000005'; | ||
| 6 | use Data::Printer::Filter; | ||
| 7 | use Scalar::Util qw(blessed); | ||
| 8 | use Term::ANSIColor; | ||
| 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 | my $row_limit | ||
| 22 | = defined $ENV{DDP_DBIC_ROW_LIMIT} ? $ENV{DDP_DBIC_ROW_LIMIT} : 5; | ||
| 23 | |||
| 24 | while ( defined( my $row = $obj->next ) ) { | ||
| 25 | |||
| 26 | # Could be inflating to a HashRef | ||
| 27 | push @rows, blessed( $row ) | ||
| 28 | && $row->can( 'get_columns' ) ? { $row->get_columns } : $row; | ||
| 29 | |||
| 30 | last if $row_limit && @rows == $row_limit; | ||
| 31 | } | ||
| 32 | my $msg = 'Showing all results'; | ||
| 33 | if ( $row_limit && @rows == $row_limit ) { | ||
| 34 | $msg = sprintf( 'Showing first %i out of %i results.', | ||
| 35 | scalar @rows, $obj->count ); | ||
| 36 | } | ||
| 37 | |||
| 38 | return _add_prefix( $obj, $properties, \@rows, $msg ); | ||
| 39 | } | ||
| 40 | |||
| 41 | return; | ||
| 42 | }; | ||
| 43 | |||
| 44 | sub _add_prefix { | ||
| 45 | my $obj = shift; | ||
| 46 | my $properties = shift; | ||
| 47 | my $rows = shift; | ||
| 48 | my $msg = shift; | ||
| 49 | |||
| 50 | my $str = colored( ref( $obj ), $properties->{color}{class} ); | ||
| 51 | $str .= ' (' . $obj->result_class . ')' if $obj->can( 'result_class' ); | ||
| 52 | |||
| 53 | if ( $obj->can( 'as_query' ) ) { | ||
| 54 | my $query_data = $obj->as_query; | ||
| 55 | my @query_data = @$$query_data; | ||
| 56 | indent; | ||
| 57 | my $sql = shift @query_data; | ||
| 58 | $str | ||
| 59 | .= ' {' | ||
| 60 | . newline | ||
| 61 | . colored( $sql, 'bright_yellow' ) | ||
| 62 | . newline | ||
| 63 | . join( newline, | ||
| 64 | map { $_->[1] . ' (' . $_->[0]{sqlt_datatype} . ')' } | ||
| 65 | @query_data ); | ||
| 66 | outdent; | ||
| 67 | $str .= newline . '}'; | ||
| 68 | |||
| 69 | } | ||
| 70 | |||
| 71 | $str .= ' (' . $msg . ')' if $msg; | ||
| 72 | return $str . q{ } . np( $rows ); | ||
| 73 | } | ||
| 74 | 1; | ||
| 75 | |||
| 76 | =pod | ||
| 77 | |||
| 78 | =encoding UTF-8 | ||
| 79 | |||
| 80 | =head1 NAME | ||
| 81 | |||
| 82 | Data::Printer::Filter::DBIx::Class - (DEPRECATED) Apply special Data::Printer filters to DBIx::Class objects | ||
| 83 | |||
| 84 | =head1 VERSION | ||
| 85 | |||
| 86 | version 0.000005 | ||
| 87 | |||
| 88 | =head1 SYNOPSIS | ||
| 89 | |||
| 90 | In your program: | ||
| 91 | |||
| 92 | use Data::Printer filters => { -external => ['DBIx::Class'] }; | ||
| 93 | |||
| 94 | or, in your C<.dataprinter> file: | ||
| 95 | |||
| 96 | { filters => { -external => ['DBIx::Class'] } }; | ||
| 97 | |||
| 98 | =head1 DESCRIPTION | ||
| 99 | |||
| 100 | Huge chunks of this have been lifted directly from L<Data::Printer::Filter::DB> | ||
| 101 | This filter differs in that it also adds the values from C<get_columns()> to | ||
| 102 | the output. For a L<DBIx::Class::Row> object, the column values are return in | ||
| 103 | the data. For a L<DBIx::Class::ResultSet>, by default the first 5 rows in the | ||
| 104 | ResultSet are returned, with the contents of C<get_columns()> included. You | ||
| 105 | can change this behaviour via C<$ENV{DDP_DBIC_ROW_LIMIT}>. | ||
| 106 | |||
| 107 | # Return up to 1,000 rows per ResultSet | ||
| 108 | $ENV{DDP_DBIC_ROW_LIMIT} = 1000; | ||
| 109 | |||
| 110 | # Return every row from every ResultSet | ||
| 111 | $ENV{DDP_DBIC_ROW_LIMIT} = 0; | ||
| 112 | |||
| 113 | =head1 DEPRECATED | ||
| 114 | |||
| 115 | This module breaks with the 1.x release of L<Data::Printer>, but that's ok. Use | ||
| 116 | L<Data::Printer::Filter::DB> instead. | ||
| 117 | |||
| 118 | =head1 AUTHOR | ||
| 119 | |||
| 120 | Olaf Alders <olaf@wundercounter.com> | ||
| 121 | |||
| 122 | =head1 COPYRIGHT AND LICENSE | ||
| 123 | |||
| 124 | This software is Copyright (c) 2016 by MaxMind, Inc. | ||
| 125 | |||
| 126 | This is free software, licensed under: | ||
| 127 | |||
| 128 | The Artistic License 2.0 (GPL Compatible) | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | __END__ |