line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::MySQL5::Results; |
2
|
6
|
|
|
6
|
|
23
|
use Mojo::Base -base; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
35
|
|
3
|
|
|
|
|
|
|
|
4
|
6
|
|
|
6
|
|
3181
|
use Mojo::Collection; |
|
6
|
|
|
|
|
16447
|
|
|
6
|
|
|
|
|
197
|
|
5
|
6
|
|
|
6
|
|
29
|
use Mojo::Util 'tablify'; |
|
6
|
|
|
|
|
76
|
|
|
6
|
|
|
|
|
4509
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has _columns => sub { [] }; |
8
|
|
|
|
|
|
|
has _results => sub { [] }; |
9
|
|
|
|
|
|
|
has _result => 0; |
10
|
|
|
|
|
|
|
has _pos => 0; |
11
|
|
|
|
|
|
|
|
12
|
0
|
|
|
0
|
|
|
sub _current_columns { $_[0]->_columns->[$_[0]->_result] } |
13
|
|
|
|
|
|
|
|
14
|
0
|
|
0
|
0
|
|
|
sub _current_results { $_[0]->_results->[$_[0]->_result] // [] } |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub _hash { |
17
|
0
|
|
|
0
|
|
|
my ($self, $pos) = @_; |
18
|
|
|
|
|
|
|
return { |
19
|
0
|
|
|
|
|
|
map { $self->_current_columns->[$_]->{name} => $self->_current_results->[$pos]->[$_] } |
|
0
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
|
(0 .. scalar @{ $self->_current_columns } - 1) |
21
|
|
|
|
|
|
|
}; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub more_results { |
26
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
27
|
0
|
0
|
|
|
|
|
return undef unless $self->_pos >= $self->rows; |
28
|
0
|
|
|
|
|
|
$self->{_pos} = 0; |
29
|
0
|
|
|
|
|
|
$self->{_result} ++; |
30
|
0
|
|
|
|
|
|
return $self->_result < scalar @{ $self->_results }; |
|
0
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub array { |
34
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
35
|
0
|
0
|
|
|
|
|
return undef if $self->_pos >= $self->rows; |
36
|
0
|
|
|
|
|
|
my $array = $self->_current_results->[$self->_pos]; |
37
|
0
|
|
|
|
|
|
$self->{_pos}++; |
38
|
0
|
|
|
|
|
|
return $array; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub arrays { |
42
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
43
|
0
|
|
|
|
|
|
my $arrays = Mojo::Collection->new( |
44
|
0
|
|
|
|
|
|
map { $self->_current_results->[$_] } ($self->_pos .. $self->rows - 1) ); |
45
|
0
|
|
|
|
|
|
$self->{_pos} = $self->rows; |
46
|
0
|
|
|
|
|
|
return $arrays; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
0
|
1
|
|
sub columns { [ map { $_->{name} } @{ shift->_current_columns } ] } |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub hash { |
52
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
53
|
0
|
0
|
|
|
|
|
return undef if $self->_pos >= $self->rows; |
54
|
0
|
|
|
|
|
|
my $hash = $self->_hash($self->_pos); |
55
|
0
|
|
|
|
|
|
$self->{_pos}++; |
56
|
0
|
|
|
|
|
|
return $hash; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub hashes { |
60
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
61
|
0
|
|
|
|
|
|
my $hashes = Mojo::Collection->new( |
62
|
0
|
|
|
|
|
|
map { $self->_hash($_) } ($self->_pos .. $self->rows - 1) ); |
63
|
0
|
|
|
|
|
|
$self->{_pos} = $self->rows; |
64
|
0
|
|
|
|
|
|
return $hashes; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
0
|
1
|
|
sub rows { scalar @{ shift->_current_results } } |
|
0
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
0
|
1
|
|
sub affected_rows { shift->{affected_rows} } |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
0
|
1
|
|
sub warnings_count { shift->{warnings_count} } |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
0
|
1
|
|
sub last_insert_id { shift->{last_insert_id} } |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
0
|
1
|
|
sub err { shift->{error_code} } |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
0
|
1
|
|
sub errstr { shift->{error_message} } |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
0
|
0
|
1
|
|
sub state { shift->{sql_state} // '' } |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
0
|
1
|
|
sub text { tablify shift->arrays } |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
1; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=encoding utf8 |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 NAME |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Mojo::MySQL5::Results - Results |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 SYNOPSIS |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
use Mojo::MySQL5::Results; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
my $results = Mojo::MySQL5::Results->new(db => $db); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 DESCRIPTION |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
L is a container for results returned by call to |
100
|
|
|
|
|
|
|
$db->L. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 METHODS |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
L inherits all methods from L and implements |
105
|
|
|
|
|
|
|
the following ones. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head2 array |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
my $array = $results->array; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Fetch one row and return it as an array reference. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# Process one row at a time |
114
|
|
|
|
|
|
|
while (my $next = $results->array) { |
115
|
|
|
|
|
|
|
say $next->[3]; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 arrays |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
my $collection = $results->arrays; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Fetch all rows and return them as a L object containing |
123
|
|
|
|
|
|
|
array references. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# Process all rows at once |
126
|
|
|
|
|
|
|
say $results->arrays->reduce(sub { $a->[3] + $b->[3] }); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head2 columns |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
my $columns = $results->columns; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Return column names as an array reference. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 hash |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
my $hash = $results->hash; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Fetch one row and return it as a hash reference. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# Process one row at a time |
141
|
|
|
|
|
|
|
while (my $next = $results->hash) { |
142
|
|
|
|
|
|
|
say $next->{money}; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head2 hashes |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
my $collection = $results->hashes; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Fetch all rows and return them as a L object containing hash |
150
|
|
|
|
|
|
|
references. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
# Process all rows at once |
153
|
|
|
|
|
|
|
say $results->hashes->reduce(sub { $a->{money} + $b->{money} }); |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 rows |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
my $num = $results->rows; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Number of rows. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 text |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
my $text = $results->text; |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Fetch all rows and turn them into a table with L. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head2 more_results |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
do { |
170
|
|
|
|
|
|
|
my $columns = $results->columns; |
171
|
|
|
|
|
|
|
my $arrays = $results->arrays; |
172
|
|
|
|
|
|
|
} while ($results->more_results); |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Handle multiple results. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head2 affected_rows |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
my $affected = $results->affected_rows; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Number of affected rows by the query. |
181
|
|
|
|
|
|
|
The number reported is dependant from C option in L. |
182
|
|
|
|
|
|
|
For example |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
UPDATE $table SET id = 1 WHERE id = 1 |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
would return 1 if C is set, and 0 otherwise. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head2 last_insert_id |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
my $last_id = $results->last_insert_id; |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
That value of C column if executed query was C in a table with |
193
|
|
|
|
|
|
|
C column. |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head2 warnings_count |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
my $warnings = $results->warnings_count; |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
Number of warnings raised by the executed query. |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head2 err |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
my $err = $results->err; |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
Error code receieved. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head2 state |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
my $state = $results->state; |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Error state receieved. |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=head2 errstr |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
my $errstr = $results->errstr; |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
Error message receieved. |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=head1 SEE ALSO |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
L, L. |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=cut |