line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Jorge::ObjectCollection; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
3268
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
1353
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Jorge::ObjectCollection - Base class for Object Collections |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Version 0.01 |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub new { |
20
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
21
|
0
|
|
|
|
|
|
return bless [], $class; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub get_next { |
25
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
my $value = shift @$self; |
28
|
0
|
0
|
|
|
|
|
return 0 unless $value; |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
my $obj = $self->create_object; |
31
|
0
|
0
|
|
|
|
|
if ( $obj->get_from_db($value) ) { return $obj } |
|
0
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
return 0; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _create_query { |
37
|
0
|
|
|
0
|
|
|
my $self = shift; |
38
|
0
|
|
|
|
|
|
my $params = shift; |
39
|
|
|
|
|
|
|
|
40
|
0
|
|
0
|
|
|
|
my $page = $params->{_page} || 0; |
41
|
0
|
|
0
|
|
|
|
my $pagelength = $params->{_entries_per_page} || 0; |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
my $obj = $self->create_object; |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
my @fields = @{ $obj->_fields->[0] }; |
|
0
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
my %fields = %{ $obj->_fields->[1] }; |
|
0
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
my $table_name = $obj->_fields->[2]; |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
my @pk = grep { $fields{$_}->{pk} } keys %fields; |
|
0
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
my $query = 'SELECT ' . $pk[0] . ' FROM ' . $table_name; |
52
|
0
|
|
|
|
|
|
my @query_params; |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
for my $key ( keys %$params ) { |
55
|
0
|
0
|
|
|
|
|
next if $key =~ /^_/; |
56
|
0
|
0
|
|
|
|
|
next unless grep { $_ eq $key } @fields; |
|
0
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
my ( $value, $oper ); |
59
|
0
|
0
|
|
|
|
|
if ( ref( $params->{$key} ) eq 'ARRAY' ) { |
60
|
0
|
|
|
|
|
|
$oper = $params->{$key}->[0]; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
#Porta. |
63
|
|
|
|
|
|
|
#Enable use a object as a parameter to search for |
64
|
0
|
0
|
0
|
|
|
|
if ( $fields{$key}->{class} |
65
|
|
|
|
|
|
|
&& ref( $params->{$key}->[1] ) eq ref( $fields{$key}->{class} ) |
66
|
|
|
|
|
|
|
) |
67
|
|
|
|
|
|
|
{ |
68
|
0
|
|
|
|
|
|
my $p = $params->{$key}->[1]->_pk; |
69
|
0
|
|
|
|
|
|
my $o = $params->{$key}->[1]; |
70
|
0
|
|
|
|
|
|
$value = $o->{ $$p[0] }; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
else { |
73
|
0
|
|
|
|
|
|
$value = $params->{$key}->[1]; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
#Porta |
77
|
|
|
|
|
|
|
#added OR support. |
78
|
|
|
|
|
|
|
#$params{'User'} = [ 'or',[ [$oper,$user1],[$oper,$user2],[$oper,$user3] ] ]; |
79
|
0
|
0
|
|
|
|
|
if ( lc($oper) eq 'or' ) { |
80
|
0
|
|
|
|
|
|
my $values = $value; |
81
|
0
|
|
|
|
|
|
my $or; |
82
|
|
|
|
|
|
|
my @p; |
83
|
0
|
|
|
|
|
|
foreach my $v ( @{$values} ) { |
|
0
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
$or .= " $key $$v[0] ? OR"; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# $user1, $user2, $user3 might be objects, so we check that |
87
|
|
|
|
|
|
|
# of course, the attribute has to be an object, too |
88
|
0
|
0
|
0
|
|
|
|
if ( $fields{$key}->{class} |
89
|
|
|
|
|
|
|
&& ref( $$v[1] ) eq ref( $fields{$key}->{class} ) ) |
90
|
|
|
|
|
|
|
{ |
91
|
0
|
|
|
|
|
|
my $p = $$v[1]->_pk; |
92
|
0
|
|
|
|
|
|
my $o = $$v[1]; |
93
|
0
|
|
|
|
|
|
push( @p, $o->{ $$p[0] } ); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
else { |
96
|
0
|
|
|
|
|
|
push( @p, $$v[1] ); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
} |
100
|
0
|
|
|
|
|
|
$or =~ s/(.*) OR/$1/; |
101
|
0
|
0
|
|
|
|
|
next unless $or; |
102
|
0
|
0
|
|
|
|
|
if (@query_params) { |
103
|
0
|
|
|
|
|
|
$query .= " AND ($or)"; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
else { |
106
|
0
|
|
|
|
|
|
$query .= " WHERE ($or)"; |
107
|
|
|
|
|
|
|
} |
108
|
0
|
|
|
|
|
|
push( @query_params, @p ); |
109
|
0
|
|
|
|
|
|
next; |
110
|
|
|
|
|
|
|
} |
111
|
0
|
0
|
|
|
|
|
if ( lc($oper) eq 'in' ) { |
112
|
0
|
|
|
|
|
|
my $str = '?' x $value; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
#IN expects an array in $value |
115
|
0
|
|
|
|
|
|
$str = join( ",", map { '?' } @$value ); |
|
0
|
|
|
|
|
|
|
116
|
0
|
0
|
|
|
|
|
if (@query_params) { |
117
|
0
|
|
|
|
|
|
$query .= " AND $key IN ($str)"; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
else { |
120
|
0
|
|
|
|
|
|
$query .= " WHERE $key IN ($str)"; |
121
|
|
|
|
|
|
|
} |
122
|
0
|
|
|
|
|
|
push( @query_params, @$value ); |
123
|
0
|
|
|
|
|
|
next; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
#Porta. |
127
|
|
|
|
|
|
|
#Added MySql Between. |
128
|
|
|
|
|
|
|
#NOTE: Allways provide min and max values |
129
|
|
|
|
|
|
|
#$params{Count} = ['between',$q->param('mv') || '1',$q->param('Mv') || '1000000']; |
130
|
|
|
|
|
|
|
#Start MySql BETWEEN Support. |
131
|
0
|
0
|
|
|
|
|
if ( lc($oper) eq 'between' ) { |
132
|
0
|
0
|
|
|
|
|
if (@query_params) { |
133
|
0
|
|
|
|
|
|
$query .= " AND $key BETWEEN ? AND ?"; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
else { |
136
|
0
|
|
|
|
|
|
$query .= " WHERE $key BETWEEN ? AND ?"; |
137
|
|
|
|
|
|
|
} |
138
|
0
|
|
|
|
|
|
push @query_params, @$value; |
139
|
0
|
|
|
|
|
|
next; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
#Joaquin |
143
|
|
|
|
|
|
|
#Added MySQL IS NULL support |
144
|
0
|
0
|
|
|
|
|
if ( lc($oper) eq 'is null' ) { |
145
|
0
|
0
|
|
|
|
|
if (@query_params) { |
146
|
0
|
|
|
|
|
|
$query .= " AND $key IS NULL"; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
else { |
149
|
0
|
|
|
|
|
|
$query .= " WHERE $key IS NULL"; |
150
|
|
|
|
|
|
|
} |
151
|
0
|
|
|
|
|
|
next; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
#End MySQL IS NULL support |
155
|
|
|
|
|
|
|
|
156
|
0
|
0
|
|
|
|
|
unless ( $oper =~ /^[<>=!]{1,2}||between||or||like$/ ) { |
157
|
0
|
|
|
|
|
|
$oper = '='; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
else { |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
#Porta. |
163
|
|
|
|
|
|
|
#Enable use a object as a parameter to search for |
164
|
0
|
0
|
|
|
|
|
if ( $fields{$key}->{class} ) { |
165
|
0
|
|
|
|
|
|
my $p = $params->{$key}->_pk; |
166
|
0
|
|
|
|
|
|
my $o = $params->{$key}; |
167
|
0
|
|
|
|
|
|
$value = $o->{ $$p[0] }; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
else { |
170
|
0
|
|
|
|
|
|
$value = $params->{$key}; |
171
|
|
|
|
|
|
|
} |
172
|
0
|
|
|
|
|
|
$oper = '='; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
0
|
0
|
|
|
|
|
if (@query_params) { |
176
|
0
|
|
|
|
|
|
$query .= " AND $key $oper ?"; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
else { |
179
|
0
|
|
|
|
|
|
$query .= " WHERE $key $oper ?"; |
180
|
|
|
|
|
|
|
} |
181
|
0
|
|
|
|
|
|
push @query_params, $value; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
0
|
0
|
|
|
|
|
if ( $params->{_order_by} ) { |
185
|
0
|
0
|
|
|
|
|
if ( ref( $params->{_order_by} ) eq 'ARRAY' ) { |
186
|
0
|
|
|
|
|
|
for my $param ( @{ $params->{_order_by} } ) { |
|
0
|
|
|
|
|
|
|
187
|
0
|
|
|
|
|
|
my $asc; |
188
|
0
|
0
|
|
|
|
|
if ( $param =~ /^\+/ ) { |
189
|
0
|
|
|
|
|
|
$asc = 1; |
190
|
0
|
|
|
|
|
|
substr( $param, 0, 1 ) = ''; |
191
|
|
|
|
|
|
|
} |
192
|
0
|
0
|
|
|
|
|
next unless grep { $_ eq $param } @fields; |
|
0
|
|
|
|
|
|
|
193
|
0
|
0
|
|
|
|
|
if ( $query !~ /ORDER BY/ ) { |
194
|
0
|
0
|
|
|
|
|
$query .= " ORDER BY $param" . ( $asc ? ' ASC' : ' DESC' ); |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
else { |
197
|
0
|
0
|
|
|
|
|
$query .= ", $param" . ( $asc ? ' ASC' : ' DESC' ); |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
else { |
202
|
0
|
|
|
|
|
|
my $param = $params->{_order_by}; |
203
|
0
|
|
|
|
|
|
my $asc; |
204
|
0
|
0
|
|
|
|
|
if ( $param =~ /^\+/ ) { |
205
|
0
|
|
|
|
|
|
$asc = 1; |
206
|
0
|
|
|
|
|
|
substr( $param, 0, 1 ) = ''; |
207
|
|
|
|
|
|
|
} |
208
|
0
|
0
|
|
|
|
|
if ( grep { $_ eq $param } @fields ) { |
|
0
|
|
|
|
|
|
|
209
|
0
|
0
|
|
|
|
|
$query .= ' ORDER BY ' . $param . ( $asc ? ' ASC' : ' DESC' ); |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
0
|
0
|
|
|
|
|
if ($pagelength) { |
215
|
0
|
|
|
|
|
|
$query .= " LIMIT $page,$pagelength"; |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
0
|
|
|
|
|
|
return ( $query, \@query_params ); |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
sub get_count { |
222
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
223
|
0
|
|
|
|
|
|
my $params = {@_}; |
224
|
|
|
|
|
|
|
|
225
|
0
|
|
|
|
|
|
my $obj = $self->create_object; |
226
|
|
|
|
|
|
|
|
227
|
0
|
|
|
|
|
|
my ( $query, $query_params ) = $self->_create_query($params); |
228
|
0
|
|
|
|
|
|
$query =~ s/^SELECT .+? FROM/SELECT COUNT(*) AS q FROM/; |
229
|
|
|
|
|
|
|
|
230
|
0
|
|
|
|
|
|
my $sth; |
231
|
0
|
|
|
|
|
|
$sth = $obj->_db->execute( $query, @$query_params ); |
232
|
0
|
|
|
|
|
|
my $row = $sth->fetchrow_hashref; |
233
|
|
|
|
|
|
|
|
234
|
0
|
|
|
|
|
|
return $row->{q}; |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
sub get_all { |
238
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
239
|
0
|
|
|
|
|
|
my $params = {@_}; |
240
|
|
|
|
|
|
|
|
241
|
0
|
|
|
|
|
|
my ( $query, $query_params ); |
242
|
0
|
0
|
|
|
|
|
if ( $params->{_query} ) { |
243
|
0
|
|
|
|
|
|
( $query, $query_params ) = |
244
|
|
|
|
|
|
|
( $params->{_query}, $params->{_query_params} ); |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
else { |
247
|
0
|
|
|
|
|
|
( $query, $query_params ) = $self->_create_query($params); |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
|
250
|
0
|
|
|
|
|
|
my $obj = $self->create_object; |
251
|
0
|
|
|
|
|
|
my $sth = $obj->_db->execute( $query, @$query_params ); |
252
|
0
|
|
|
|
|
|
while ( my $row = $sth->fetchrow_hashref ) { |
253
|
0
|
|
|
|
|
|
push @$self, $row->{ $obj->_pk->[0] }; |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
0
|
|
|
|
|
|
return scalar @$self; |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=head1 SYNOPSIS |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
It is not expected accessing to this package directly. So, move to main |
262
|
|
|
|
|
|
|
Jorge docs for reference. |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=head1 AUTHORS |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
Mondongo, C<< >> Did the important job and started |
268
|
|
|
|
|
|
|
this beauty. |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
Julian Porta, C<< >> took the code and tried |
271
|
|
|
|
|
|
|
to make it harder, better, faster, stronger. |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=head1 BUGS |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, |
276
|
|
|
|
|
|
|
or through the web interface at |
277
|
|
|
|
|
|
|
L. |
278
|
|
|
|
|
|
|
I will be notified, and then you'll |
279
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=head1 SUPPORT |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
perldoc Jorge |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
You can also look for information at: |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=over 4 |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=item * Github Project Page |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
L |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
L |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
L |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=item * CPAN Ratings |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
L |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=item * Search CPAN |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
L |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
=back |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
Mondongo C<< >> For starting this. |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
Copyright 2009 Julian Porta, all rights reserved. |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
325
|
|
|
|
|
|
|
under the same terms as Perl itself. |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
=cut |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
1; # End of Jorge::ObjectCollection |
331
|
|
|
|
|
|
|
|