File Coverage

blib/lib/Acrux/DBI/Res.pm
Criterion Covered Total %
statement 18 64 28.1
branch 0 18 0.0
condition 0 4 0.0
subroutine 6 27 22.2
pod 20 20 100.0
total 44 133 33.0


line stmt bran cond sub pod time code
1             package Acrux::DBI::Res;
2 5     5   40 use strict;
  5         13  
  5         211  
3 5     5   29 use utf8;
  5         11  
  5         34  
4              
5             =encoding utf8
6              
7             =head1 NAME
8              
9             Acrux::DBI::Res - Results of your database queries
10              
11             =head1 SYNOPSIS
12              
13             use Acrux::DBI::Res;
14              
15             my $res = Acrux::DBI::Res->new(sth => $sth);
16              
17             $res->collection->map(sub { $_->{foo} })->shuffle->join("\n")->say;
18              
19             =head1 DESCRIPTION
20              
21             Class to works with results of your database queries
22              
23             =head2 new
24              
25             my $res = Acrux::DBI::Res->new( sth => $sth, dbi => $dbi );
26              
27             Construct a new Acrux::DBI::Res object
28              
29             =head1 ATTRIBUTES
30              
31             This method implements the following attributes
32              
33             =head2 dbi
34              
35             my $dbi = $res->dbi;
36             $res = $res->dbi(Acrux::DBI->new);
37              
38             L object these results belong to.
39              
40             =head2 sth
41              
42             my $sth = $res->sth;
43             $res = $res->sth($sth);
44              
45             L statement handle results are fetched from
46              
47             =head1 METHODS
48              
49             This class implements the following methods
50              
51             =head2 affected_rows
52              
53             my $affected = $res->affected_rows;
54              
55             Number of affected rows by the query. For example
56              
57             UPDATE testtable SET id = 1 WHERE id = 1
58              
59             would return 1
60              
61             =head2 array
62              
63             my $array = $res->array;
64              
65             Fetch one row from L and return it as an array reference
66              
67             # [
68             # 'foo', 'bar', 'baz'
69             # ]
70              
71             See also L
72              
73             =head2 arrays
74              
75             my $arrays = $res->arrays;
76              
77             Fetch all rows from L and return them as an array of arrays
78              
79             # [
80             # [ 'foo', 'bar', 'baz' ],
81             # [ 'qux', 'quux' ],
82             # ]
83              
84             See also L
85              
86             =head2 collection
87              
88             my $collection = $res->collection;
89              
90             Fetch all rows from L and return them as a L object containing hash references
91              
92             # Process all rows at once
93             say $res->collection->reduce(sub { $a + $b->{money} }, 0);
94              
95             =head2 collection_list
96              
97             my $collection_list = $res->collection_list;
98              
99             Fetch all rows from L and return them as a L object containing array references
100              
101             # Process all rows at once
102             say $res->collection_list->reduce(sub { $a + $b->[3] }, 0);
103              
104             =head2 columns
105              
106             my $columns = $res->columns;
107              
108             Return column names as an array reference
109              
110             # Names of all columns
111             say for @{$res->columns};
112              
113             =head2 err
114              
115             my $err = $res->err;
116              
117             Error code received
118              
119             =head2 errstr
120              
121             my $errstr = $res->errstr;
122              
123             Error message received
124              
125             =head2 finish
126              
127             $res->finish;
128              
129             Indicate that you are finished with L and will not be fetching all the remaining rows
130              
131             =head2 hash
132              
133             my $hash = $res->hash;
134              
135             Fetch one row from L and return it as a hash reference
136              
137             # {
138             # 'foo' => 1,
139             # 'bar' => 'one',
140             # }
141              
142             See also L
143              
144             =head2 hashed_by
145              
146             my $hash = $res->hashed_by( $key_field );
147             my $hash = $res->hashed_by( 'id' );
148              
149             This method returns a reference to a hash containing a key for each distinct
150             value of the C<$key_field> column that was fetched.
151             For each key the corresponding value is a reference to a hash containing
152             all the selected columns and their values, as returned by C
153              
154             For example:
155              
156             my $hash = $res->hashed_by( 'id' );
157              
158             # {
159             # 1 => {
160             # 'id' => 1,
161             # 'name' => 'foo'
162             # },
163             # 2 => {
164             # 'id' => 2,
165             # 'name' => 'bar'
166             # }
167             # }
168              
169             See L for details
170              
171             See also L
172              
173             =head2 hashes
174              
175             my $hashes = $res->hashes;
176              
177             Fetch all rows from L and return them as an array containing hash references
178              
179             # [
180             # {
181             # 'id' => 1,
182             # 'name' => 'foo'
183             # },
184             # {
185             # 'id' => 2,
186             # 'name' => 'bar'
187             # }
188             # ]
189              
190             =head2 last_insert_id
191              
192             my $last_id = $res->last_insert_id;
193              
194             That value of C column if executed query was C in a table with
195             C column
196              
197             =head2 more_results
198              
199             do {
200             my $columns = $res->columns;
201             my $arrays = $res->arrays;
202             } while ($res->more_results);
203              
204             Handle multiple results
205              
206             =head2 rows
207              
208             my $num = $res->rows;
209              
210             Number of rows
211              
212             =head2 state
213              
214             my $state = $res->state;
215              
216             Error state received
217              
218             =head2 text
219              
220             my $text = $res->text;
221              
222             Fetch all rows from L and turn them into a table with L.
223              
224             =head1 HISTORY
225              
226             See C file
227              
228             =head1 TO DO
229              
230             See C file
231              
232             =head1 SEE ALSO
233              
234             L, L, L, L, L
235              
236             =head1 AUTHOR
237              
238             Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE
239              
240             =head1 COPYRIGHT
241              
242             Copyright (C) 1998-2026 D&D Corporation
243              
244             =head1 LICENSE
245              
246             This program is distributed under the terms of the Artistic License Version 2.0
247              
248             See the C file or L for details
249              
250             =cut
251              
252 5     5   494 use Carp qw/croak/;
  5         12  
  5         352  
253 5     5   2859 use Mojo::Collection;
  5         32824  
  5         298  
254 5     5   6000 use Mojo::JSON qw(from_json);
  5         165499  
  5         634  
255 5     5   65 use Mojo::Util qw(tablify);
  5         36  
  5         4668  
256              
257             sub new {
258 0     0 1   my $class = shift;
259 0 0         my $args = scalar(@_) ? scalar(@_) > 1 ? {@_} : {%{$_[0]}} : {};
  0 0          
260 0           my $sth = $args->{sth};
261 0 0         croak 'Invalid STH' unless ref($sth);
262             my $self = bless {
263             sth => $sth,
264             dbi => undef,
265             driver => '',
266 0   0       affected_rows => $args->{affected_rows} || 0,
267             }, $class;
268 0           $self->dbi($args->{dbi});
269 0           return $self;
270             }
271              
272             sub sth {
273 0     0 1   my $self = shift;
274 0 0         if (scalar(@_) >= 1) {
275 0           $self->{sth} = shift;
276 0           return $self;
277             }
278 0           return $self->{sth};
279             }
280             sub dbi {
281 0     0 1   my $self = shift;
282 0 0         if (scalar(@_) >= 1) {
283 0           my $dbi = $self->{dbi} = shift;
284 0 0 0       $self->{driver} = $dbi ? ($dbi->dbh->{Driver}{Name} || '') : '';
285 0           return $self;
286             }
287 0           return $self->{dbi};
288             }
289 0     0 1   sub state { shift->sth->state }
290 0     0 1   sub err { shift->sth->err }
291 0     0 1   sub errstr { shift->sth->errstr }
292 0     0 1   sub finish { shift->sth->finish }
293              
294             # Main Accessors
295 0     0 1   sub array { return shift->sth->fetchrow_arrayref() } # See CTK::DBI::record
296 0     0 1   sub arrays { return shift->sth->fetchall_arrayref() } # See CTK::DBI::table
297 0     0 1   sub collection_list { return Mojo::Collection->new(shift->sth->fetchall_arrayref()) }
298 0     0 1   sub columns { return shift->sth->{NAME} }
299 0     0 1   sub hash { return shift->sth->fetchrow_hashref() } # See CTK::DBI::recordh
300 0     0 1   sub hashes { return shift->sth->fetchall_arrayref({}) }
301 0     0 1   sub collection { return Mojo::Collection->new(@{(shift->sth->fetchall_arrayref({}))}) }
  0            
302 0     0 1   sub rows { shift->sth->rows }
303 0     0 1   sub text { tablify shift->arrays }
304 0     0 1   sub affected_rows { shift->{affected_rows} }
305 0     0 1   sub more_results { shift->sth->more_results }
306             sub last_insert_id {
307 0     0 1   my $self = shift;
308 0 0         return $self->sth->last_insert_id() if $self->sth->can('last_insert_id');
309 0           my $liid = sprintf('%s_insertid', $self->{driver});
310 0           return $self->sth->{$liid};
311             }
312             sub hashed_by { # See CTK::DBI::tableh
313 0     0 1   my $self = shift;
314 0           my $key_field = shift; # See keys (http://search.cpan.org/~timb/DBI-1.607/DBI.pm#fetchall_hashref)
315 0 0         return unless defined($key_field);
316 0           return $self->sth->fetchall_hashref($key_field)
317             }
318              
319             sub DESTROY {
320 0     0     my $self = shift;
321 0 0         return unless my $sth = $self->{sth};
322 0           $sth->finish;
323             }
324              
325             1;
326              
327             __END__