File Coverage

blib/lib/DBIx/Class/ResultSet/PrettyPrint.pm
Criterion Covered Total %
statement 23 23 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 30 30 100.0


line stmt bran cond sub pod time code
1             package DBIx::Class::ResultSet::PrettyPrint;
2              
3 2     2   306649 use 5.010;
  2         8  
4 2     2   11 use strict;
  2         3  
  2         47  
5 2     2   9 use warnings;
  2         3  
  2         122  
6              
7 2     2   1122 use Moo;
  2         16443  
  2         12  
8 2     2   6746 use Text::Table::Tiny qw( generate_table );
  2         24673  
  2         455  
9              
10              
11             =head1 NAME
12              
13             DBIx::Class::ResultSet::PrettyPrint - Pretty print DBIx::Class result sets.
14              
15             =head1 VERSION
16              
17             Version 0.01
18              
19             =cut
20              
21             our $VERSION = '0.01';
22              
23             =head1 SYNOPSIS
24              
25             use DBIx::Class::ResultSet::PrettyPrint;
26             use Schema; # load your DBIx::Class schema
27              
28             # load your database and fetch a result set
29             my $schema = Schema->connect( 'dbi:SQLite:books.db' );
30             my $books = $schema->resultset( 'Book' );
31              
32             # pretty print the result set
33             my $pp = DBIx::Class::ResultSet::PrettyPrint->new();
34             $pp->print_table( $books );
35              
36             +----+---------------------+---------------+------------+-----------+---------------+
37             | id | title | author | pub_date | num_pages | isbn |
38             +----+---------------------+---------------+------------+-----------+---------------+
39             | 2 | Perl by Example | Ellie Quigley | 1994-01-01 | 200 | 9780131228399 |
40             | 4 | Perl Best Practices | Damian Conway | 2005-07-01 | 517 | 9780596001735 |
41             +----+---------------------+---------------+------------+-----------+---------------+
42              
43             =head1 DESCRIPTION
44              
45             Ever wanted to quickly visualise what a C result set looks like
46             (for instance, in tests) without having to resort to reproducing the query
47             in SQL in a DBMS REPL? This is what this module does: it pretty prints
48             result sets wherever you are, be it in tests or within a debugging session.
49              
50             While searching for such a solution, I stumbled across L
51             StackOverflow|https://stackoverflow.com/a/4072923/10874800> and thought:
52             that would be nice as a module. And so here it is.
53              
54             =head1 SUBROUTINES/METHODS
55              
56             =head2 C
57              
58             Constructor; creates a new pretty printer object.
59              
60             =head2 C
61              
62             Print the "table" from the given result set.
63              
64             =cut
65              
66             sub print_table {
67 1     1 1 1169603 my ($self, $result_set) = @_;
68              
69 1         11 my @columns = $result_set->result_source->columns;
70              
71 1         39 my @rows = ( \@columns );
72 1         10 while ( my $row = $result_set->next ) {
73 5         12806 my @data = map { $row->get_column($_) } @columns;
  30         165  
74 5         50 push @rows, \@data;
75             }
76              
77 1         111 print generate_table( rows => \@rows, header_row => 1 ), "\n";
78              
79 1         1259 return; # Explicitly returning nothing meaningful
80             }
81              
82             =head1 ACKNOWLEDGEMENTS
83              
84             I borrowed heavily upon the test structure used in
85             L for the test database
86             setup and creation.
87              
88             =head1 AUTHOR
89              
90             Paul Cochrane, C<< >>
91              
92             =head1 BUGS
93              
94             Please report any bugs or feature requests via the project's GitHub
95             repository at
96             L.
97              
98             =head1 SUPPORT
99              
100             You can find documentation for this module with the perldoc command.
101              
102             perldoc DBIx::Class::ResultSet::PrettyPrint
103              
104             Bug reports and pull requests are welcome. Please submit these to the
105             L
106             repository|https://github.com/paultcochrane/DBIx-Class-ResultSet-PrettyPrint>.
107              
108             =head1 LICENSE AND COPYRIGHT
109              
110             This software is Copyright (c) 2024 by Paul Cochrane.
111              
112             This is free software; you can redistribute it and/or modify it under
113             the same terms as the Perl 5 programming language system itself.
114              
115             =cut
116              
117             1; # End of DBIx::Class::ResultSet::PrettyPrint