ORM/Db/DBI.pm

filter: Statementstmt 73.4% 257 / 350 Branchbran 54.3% 87 / 160 Conditioncond 42.8% 18 / 42 MC/DCmcdc 19.2% 5 / 26 Subroutinesub 65.7% 23 / 35 Podpod 0.0% 0 / 24 totaltotal 61.2% 390 / 637 CC 111 SCAR 31.9
CC
111
Cov
66%
select
63.2
select_base
52.0
select_full
36.7
CRAP 24.4
« ORM/Filter/Case.pm ↑ Summary ORM/Base.pm »
1 #
2 # DESCRIPTION
3 # PerlORM - Object relational mapper (ORM) for Perl. PerlORM is Perl
4 # library that implements object-relational mapping. Its features are
5 # much similar to those of Java's Hibernate library, but interface is
6 # much different and easier to use.
7 #
8 # AUTHOR
9 # Alexey V. Akimov <akimov_alexey@sourceforge.net>
10 #
11 # COPYRIGHT
12 # Copyright (C) 2005-2006 Alexey V. Akimov
13 #
14 # This library is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU Lesser General Public
16 # License as published by the Free Software Foundation; either
17 # version 2.1 of the License, or (at your option) any later version.
18 #
19 # This library is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # Lesser General Public License for more details.
23 #
24 # You should have received a copy of the GNU Lesser General Public
25 # License along with this library; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #
28  
29 package ORM::Db::DBI;
30  
31 4 $VERSION = 0.83;
32  
33 4 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 4 ▶use base 'ORM::Db';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 4 ▶use ORM::Db::DBIResultSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 4 ▶use ORM::Db::DBIResultSetFull;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 ## use: $db = $class->new
39 ## (
40 ## host => string,
41 ## database => string,
42 ## options => string,
43 ## user => string,
44 ## password => string,
45 ##
46 ## delayed_connect => boolean,
47 ## connect_retries => integer,
48 ## retry_sleep => integer,
49 ## )
50 ##
51 ## 'retry_sleep' in seconds.
52 ##
53 sub new
54 4 ▶{
Subroutine new: called CC=7 SCAR=20.6
Pod undocumented
55 4 my $class = shift;
56 4 my $self = {};
57 4 my %arg = @_;
58 4 my $data_source;
59  
60 4 ▶ $self->{connect_retries} = defined $arg{connect_retries} ? int( $arg{connect_retries} ) : 3;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'connect_retries'} ? : 0 4 4
61 4 ▶ $self->{retry_sleep} = defined $arg{retry_sleep} ? int( $arg{retry_sleep} ) : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'retry_sleep'} ? : 0 4 4
62 4 $self->{delayed_connect} = $arg{delayed_connect};
63 4 $self->{database} = $arg{database};
64  
65 4 ▶ if( $arg{data_source} )
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{'data_source'}) { } 0 4 4
66 {
67 0 $data_source = $arg{data_source};
68 }
69 else
70 {
71 $data_source =
72 "DBI:$arg{driver}:$arg{database}"
73 . ($arg{host} ? ":$arg{host}" : '')
74 4 ▶ . ($arg{options} ? ";$arg{options}" : '');
Branch: 2 branches
BranchTrueFalseTotal
$arg{'host'} ? : 0 4 4
$arg{'options'} ? : 0 4 4
75 }
76  
77 4 $self->{db_arg} = [ $data_source, $arg{user}, $arg{password} ];
78 4 ▶ $self->{db} = DBI->connect( @{$self->{db_arg}} ) unless( $arg{delayed_connect} );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg{"delayed_connect"} 4 0 4
79  
80 4 return bless $self, $class;
81 }
82  
83 sub disconnect
84 0 ▶{
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod undocumented
85 0 my $self = shift;
86
87 0 $self->{db} = undef;
88 0 $self->{delayed_connect} = 1;
89 }
90  
91 0 ▶sub database { $_[0]->{database}; }
Subroutine database: not called CC=1 SCAR=6.9
Pod undocumented
92  
93 sub count
94 2 ▶{
Subroutine count: called CC=5 SCAR=16.6
Pod undocumented
95 2 my $self = shift;
96 2 my %arg = @_;
97 2 my $tjoin;
98 my $cond;
99  
100 2 $tjoin = ORM::Tjoin->new( class=>$arg{class}, all_tables=>1 );
101 2 ▶ $tjoin->merge( $arg{filter}->_tjoin ) if( $arg{filter} );
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"filter"} 2 0 2
102 2 $tjoin->assign_aliases;
103  
104 2 ▶ if( $arg{filter} )
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"filter"}) 2 0 2
105 {
106 2 $cond = $arg{filter}->_sql_str( tjoin=>$tjoin );
107 }
108  
109 my $res = $self->select
110 (
111 error => $arg{error},
112 2 ▶ query =>
Condition50%1/2
$cond && "WHERE $cond\n"
!ll
02
MC/DC0%0/1
$cond && "WHERE $cond\n"
$cond
Truth table50%1/2
A
A= $cond
Aresult
00
11
113 (
114 "SELECT count(DISTINCT "
115 . $self->qt( $tjoin->first_basic_table_alias ) . ".id) AS "
116 . $self->qi( 'count' ) . "\n"
117 . 'FROM ' . $tjoin->sql_table_list . "\n"
118 . ( $cond && "WHERE $cond\n" )
119 ),
120 );
121  
122 2 ▶ return $res ? $res->next_row->{count} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$res ? : 2 0 2
123 }
124  
125 sub select_base
126 3 ▶{
Subroutine select_base: called CC=26 SCAR=39.5
Pod undocumented
127 3 my $self = shift;
128 3 my %arg = @_;
129  
130 # Prepare $tjoin object
131 3 my $tjoin = ORM::Tjoin->new( class=>$arg{class}, all_tables=>1 );
132  
133 3 ▶ if( $arg{data} )
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"data"}) 1 2 3
134 {
135 1 for my $name ( keys %{$arg{data}} )
136 {
137 13 ▶ $tjoin->merge( $arg{data}{$name}->_tjoin ) if( defined $arg{data}{$name} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg{"data"}{$name} 13 0 13
138 }
139 1 for my $group_by ( @{$arg{group_by}} )
140 {
141 0 ▶ if( ref $group_by && UNIVERSAL::isa( $group_by, 'ORM::Metaprop' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $group_by and UNIVERSAL::isa($group_by, "ORM::Metaprop")) 0 0 0
Condition0%0/3
ref $group_by and UNIVERSAL::isa($group_by, "ORM::Metaprop")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $group_by and UNIVERSAL::isa($group_by, "ORM::Metaprop")
ref $group_by UNIVERSAL::isa($group_by, "ORM::Metaprop")
Truth table0%0/3
A and B
A= ref $group_byB= UNIVERSAL::isa($group_by, "ORM::Metaprop")
ABresult
0X0
100
111
142 {
143 0 $tjoin->merge( $group_by->_tjoin );
144 }
145 }
146 }
147  
148 3 ▶ $tjoin->merge( $arg{order}->_tjoin ) if( $arg{order} );
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"order"} 1 2 3
149 3 ▶ $tjoin->merge( $arg{filter}->_tjoin ) if( $arg{filter} );
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"filter"} 3 0 3
150 3 ▶ $tjoin->merge( $arg{post_filter}->_tjoin ) if( $arg{post_filter} );
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"post_filter"} 0 3 3
151 3 $tjoin->assign_aliases;
152  
153 # Prepare WHERE statement for SQL query
154 3 ▶ my $cond = $arg{filter} && $arg{filter}->_sql_str( tjoin=>$tjoin );
Condition33%1/3
$arg{'filter'} && $arg{'filter'}->_sql_str('tjoin', $tjoin)
!ll&&!rl&&r
003
MC/DC0%0/2
$arg{'filter'} && $arg{'filter'}->_sql_str('tjoin', $tjoin)
$arg{'filter'} $arg{'filter'}->_sql_str('tjoin', $tjoin)
Truth table33%1/3
A && B
A= $arg{'filter'}B= $arg{'filter'}->_sql_str('tjoin', $tjoin)
ABresult
0X0
100
111
155  
156 # Prepare HAVING statement for SQL query
157 3 ▶ my $having = $arg{post_filter} && $arg{post_filter}->_sql_str( tjoin=>$tjoin );
Condition33%1/3
$arg{'post_filter'} && $arg{'post_filter'}->_sql_str('tjoin', $tjoin)
!ll&&!rl&&r
300
MC/DC0%0/2
$arg{'post_filter'} && $arg{'post_filter'}->_sql_str('tjoin', $tjoin)
$arg{'post_filter'} $arg{'post_filter'}->_sql_str('tjoin', $tjoin)
Truth table33%1/3
A && B
A= $arg{'post_filter'}B= $arg{'post_filter'}->_sql_str('tjoin', $tjoin)
ABresult
0X0
100
111
158  
159 # Prepare GROUP BY statement for SQL query
160 3 my $group_by;
161 3 for my $grp ( @{$arg{group_by}} )
162 {
163 0 ▶ $group_by .= ', ' if( $group_by );
Branch: 1 branch
BranchTrueFalseTotal
if $group_by 0 0 0
164 0 ▶ if( UNIVERSAL::isa( $grp, 'ORM::Expr' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($grp, 'ORM::Expr')) { } 0 0 0
165 {
166 0 $group_by .= $grp->_sql_str( tjoin=>$tjoin );
167 }
168 else
169 {
170 0 $group_by .= $self->qi( $grp );
171 }
172 }
173  
174 # Prepare ORDER statement for SQL query
175 3 ▶ my $order = $arg{order} && $arg{order}->sql_order_by( tjoin=>$tjoin );
Condition66%2/3
$arg{'order'} && $arg{'order'}->sql_order_by('tjoin', $tjoin)
!ll&&!rl&&r
201
MC/DC50%1/2
$arg{'order'} && $arg{'order'}->sql_order_by('tjoin', $tjoin)
$arg{'order'} $arg{'order'}->sql_order_by('tjoin', $tjoin)
Truth table66%2/3
A && B
A= $arg{'order'}B= $arg{'order'}->sql_order_by('tjoin', $tjoin)
ABresult
0X0
100
111
176  
177 # Prepare SELECT statement for SQL query
178 3 my $select;
179 3 ▶ if( $arg{data} )
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{'data'}) { } 1 2 3
180 {
181 1 $select = '';
182 1 for my $alias ( keys %{$arg{data}} )
183 {
184 13 ▶ my $data = ref $arg{data}{$alias} ? $arg{data}{$alias} : ORM::Const->new( $arg{data}{$alias} );
Branch: 1 branch
BranchTrueFalseTotal
ref $arg{'data'}{$alias} ? : 13 0 13
185  
186 13 ▶ $select .= ",\n" if( $select );
Branch: 1 branch
BranchTrueFalseTotal
if $select 12 1 13
187 13 $select .= ' ' . $data->_sql_str( tjoin=>$tjoin ) . ' AS ' . $self->qi( $alias );
188 }
189 }
190 else
191 {
192 2 $select = ' DISTINCT ' . $tjoin->sql_select_basic_tables;
193 }
194  
195 # Prepare LIMIT statement for SQL query
196 3 my $limit = $self->_sql_limit( $arg{page}, $arg{pagesize} );
197  
198 # Prepare query string and fetch data
199 my $query =
200 "SELECT\n$select\n"
201 . 'FROM ' . $tjoin->sql_table_list . "\n"
202 . ( $cond ? "WHERE\n $cond\n" : '' )
203 . ( $group_by ? "GROUP BY $group_by\n" : '' )
204 . ( $having ? "HAVING\n $having\n" : '' )
205 . ( $order ? "ORDER BY $order\n" : '' )
206 . ( $limit ? "$limit\n" : '' )
207 3 ▶ . ( $self->{ta} ? $self->_ta_select."\n" : '' );
Branch: 6 branches
BranchTrueFalseTotal
$cond ? : 3 0 3
$group_by ? : 0 3 3
$having ? : 0 3 3
$order ? : 1 2 3
$limit ? : 2 1 3
$self->{'ta'} ? : 0 3 3
208  
209 $self->select
210 (
211 tables => $tjoin->select_basic_tables,
212 query => $query,
213 error => $arg{error},
214 3 );
215 }
216  
217 sub select_full
218 1 ▶{
Subroutine select_full: called CC=12 SCAR=36.0
Pod undocumented
219 1 my $self = shift;
220 1 my %arg = @_;
221 1 my $error = ORM::Error->new;
222 1 my $fullres;
223  
224 my $res = $self->select_base
225 (
226 class => $arg{class},
227 filter => $arg{filter},
228 order => $arg{order},
229 page => $arg{page},
230 pagesize => $arg{pagesize},
231 1 error => $error,
232 );
233  
234 1 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 1 0 1
235 {
236 1 my %class2id;
237 my %id2data;
238 1 my %residual_tables;
239 1 my $residual;
240 1 my $residual_data;
241 1 my $data;
242  
243 1 $fullres = ORM::Db::DBIResultSetFull->new;
244  
245 1 ▶ while( $data = $res->next_row )
Branch: 1 branch
BranchTrueFalseTotal
if ($data = $res->next_row) 1 1 2
246 {
247 1 my $obj = $arg{class}->_cache->get( $data->{id}, 0 );
248 1 ▶ if( $obj )
Branch: 1 branch
BranchTrueFalseTotal
if ($obj) { } 1 0 1
249 {
250 1 $fullres->add_row( $obj );
251 }
252 else
253 {
254 0 $fullres->add_row( $data );
255 0 ▶ if( $data->{class} ne $arg{class} )
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"class"} ne $arg{"class"}) 0 0 0
256 {
257 0 $class2id{ $data->{class} } .= $data->{id}.',';
258 0 $id2data{ $data->{id} } = $data;
259 }
260 }
261 }
262  
263 1 for my $inh_class ( keys %class2id )
264 {
265 0 $arg{class}->_load_ORM_class( $inh_class );
266 0 %residual_tables = ();
267 0 chop $class2id{ $inh_class };
268 0 for
269 (
270 0 ▶ my $i = scalar( @{$res->result_tables} );
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $inh_class->_db_tables_count) 0 0 0
271 $i < $inh_class->_db_tables_count;
272 $i++
273 )
274 {
275 0 $residual_tables{ $inh_class->_db_table( $i ) } = 1;
276 }
277 0 ▶ if( %residual_tables )
Branch: 1 branch
BranchTrueFalseTotal
if (%residual_tables) 0 0 0
278 {
279 $residual = $self->select_tables
280 (
281 0 id => $class2id{ $inh_class },
282 tables => \%residual_tables,
283 error => $error,
284 );
285 0 ▶ last if( $error->fatal );
Branch: 1 branch
BranchTrueFalseTotal
if $error->fatal 0 0 0
286 0 ▶ while( $residual_data = $residual->next_row )
Branch: 1 branch
BranchTrueFalseTotal
if ($residual_data = $residual->next_row) 0 0 0
287 {
288 0 $data = $id2data{ $residual_data->{id} };
289 0 for my $key ( keys %$residual_data )
290 {
291 0 $data->{$key} = $residual_data->{$key};
292 }
293 }
294 }
295 }
296 }
297  
298 1 $error->upto( $arg{error} );
299 1 ▶ return $error->fatal ? undef : $fullres;
Branch: 1 branch
BranchTrueFalseTotal
$error->fatal ? : 0 1 1
300 }
301  
302 sub select_tables
303 7 ▶{
Subroutine select_tables: called CC=6 SCAR=17.9
Pod undocumented
304 7 my $self = shift;
305 7 my %arg = @_;
306 7 my @tables = keys %{$arg{tables}};
307  
308 7 my $fields_to_select = '';
309 7 my $tables_str = '';
310 7 my $inner_join = '';
311  
312 7 ▶ for( my $i=0; $i<@tables; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @tables) 8 7 15
313 {
314 8 ▶ if( ref $arg{tables}{$tables[$i]} eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg{'tables'}{$tables[$i]} eq 'HASH') { } 1 7 8
315 {
316 1 for my $prop ( keys %{$arg{tables}{$tables[$i]}} )
317 {
318 1 $fields_to_select .= $self->qt($tables[$i]).'.'.$self->qf($prop).',';
319 }
320 }
321 else
322 {
323 7 $fields_to_select .= $self->qt( $tables[$i] ).'.*,';
324 }
325  
326 8 $tables_str .= $self->qt( $tables[$i] ).',';
327  
328 8 ▶ if( $i < $#tables )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#tables) 1 7 8
329 {
330 1 $inner_join .= $self->qt($tables[$i]).".id=".$self->qt($tables[$i+1]).".id AND ";
331 }
332 }
333 7 chop $fields_to_select;
334 7 chop $tables_str;
335  
336 my $query =
337 'SELECT ' . $fields_to_select
338 . ' FROM ' . $tables_str
339 . ' WHERE ' . $inner_join . $self->qt( $tables[0] ).'.id IN ('.$arg{id}.')'
340 7 ▶ . ( $self->{ta} ? ' '.$self->_ta_select : '' );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'ta'} ? : 2 5 7
341  
342 $self->select
343 (
344 7 tables => [ keys %{$arg{tables}} ],
345 query => $query,
346 error => $arg{error},
347 7 );
348 }
349  
350 1 ▶sub select_stat { shift->select_base( @_ ); }
Subroutine select_stat: called CC=1 SCAR=0.0
Pod undocumented
351  
352 sub insert_object
353 21 ▶{
Subroutine insert_object: called CC=10 SCAR=23.2
Pod undocumented
354 21 my $self = shift;
355 21 my %arg = @_;
356 21 my $obj = $arg{object};
357 21 my $obj_class = ref $obj;
358 21 my $id = $arg{id};
359 21 my $error = ORM::Error->new;
360 21 my $ta = $obj_class->new_transaction( error=>$error );
361  
362 # Insert new records into tables
363 21 my @table = $obj_class->_db_tables;
364 21 my %values;
365 my $i;
366  
367 21 ▶ for( $i=0; $i<@table && !$error->fatal; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @table and not $error->fatal) 22 21 43
Condition66%2/3
$i < @table and not $error->fatal
!ll&&!rl&&r
21022
MC/DC50%1/2
$i < @table and not $error->fatal
$i < @table not $error->fatal
Truth table66%2/3
A and not(B)
A= $i < @tableB= not $error->fatal
ABresult
0X0
100
111
368 {
369 22 %values = ();
370  
371 22 ▶ if( $i == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 21 1 22
372 {
373 21 ▶ $values{id} = $id if( $id );
Branch: 1 branch
BranchTrueFalseTotal
if $id 0 21 21
374 21 ▶ $values{class} = $obj_class if( !$obj_class->_is_sealed );
Branch: 1 branch
BranchTrueFalseTotal
unless $obj_class->_is_sealed 2 19 21
375 }
376 else
377 {
378 1 $values{id} = $id;
379 }
380  
381 22 for my $field ( $obj_class->_db_table_fields( $table[$i] ) )
382 {
383 160 $values{$field} = $obj->_property_id( $field );
384 }
385  
386 22 my $rows_affected = $self->insert
387 (
388 table => $table[$i],
389 values => \%values,
390 error => $error,
391 );
392  
393 22 ▶ if( $rows_affected == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($rows_affected == 1) { } 22 0 22
394 {
395 22 ▶ $id = $self->insertid if( $i==0 && ! $id );
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 and not $id 21 1 22
Condition66%2/3
$i == 0 and not $id
!ll&&!rl&&r
1021
MC/DC50%1/2
$i == 0 and not $id
$i == 0 not $id
Truth table66%2/3
A and not(B)
A= $i == 0B= not $id
ABresult
0X0
100
111
396 }
397 else
398 {
399 0 $error->add_fatal
400 (
401 "Insert into table '$table[$i]' failed, $rows_affected rows affected"
402 );
403 }
404 }
405  
406 21 $error->upto( $arg{error} );
407 21 return $id;
408 }
409  
410 sub update_object
411 5 ▶{
Subroutine update_object: called CC=4 SCAR=13.9
Pod undocumented
412 5 my $self = shift;
413 5 my %arg = @_;
414 5 my $obj = $arg{object};
415 5 my $obj_class = ref $obj;
416 5 my $error = ORM::Error->new;
417 5 my $ta = $obj_class->new_transaction( error=>$error );
418 5 my %table;
419  
420 5 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 5 0 5
421 {
422 5 for my $prop ( keys %{$arg{values}} )
423 {
424 7 $table{ $obj_class->_prop2table($prop) }{ $prop } = $arg{values}{$prop};
425 }
426  
427 5 for my $table ( keys %table )
428 {
429 $self->update_object_part
430 (
431 object => $obj,
432 6 values => $table{ $table },
433 error => $error,
434 );
435 }
436 }
437  
438 5 $error->upto( $arg{error} );
439 }
440  
441 sub update_object_part
442 6 ▶{
Subroutine update_object_part: called CC=13 SCAR=27.1
Pod undocumented
443 6 my $self = shift;
444 6 my %arg = @_;
445 6 my $obj = $arg{object};
446 6 my $obj_class = ref $obj;
447  
448 6 my $check_all_props = 0;
449 6 my $left_prop = (each %{$arg{values}})[0];
450 6 my $tjoin = ORM::Tjoin->new( class=>$obj_class, left_prop=>$left_prop, all_tables=>$arg{all_tables} );
451  
452 6 for my $prop ( keys %{$arg{values}} )
453 {
454 7 ▶ $check_all_props = 1 if( ! ref $arg{values}{$prop} );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg{"values"}{$prop} 6 1 7
455 7 ▶ if( UNIVERSAL::isa( $arg{values}{$prop}, 'ORM::Expr' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg{"values"}{$prop}, "ORM::Expr")) 1 6 7
456 {
457 1 $tjoin->merge( $arg{values}{$prop}->_tjoin );
458 }
459 }
460 6 $tjoin->assign_aliases;
461  
462 # Prepare WHERE statement
463 6 my $where;
464 6 my $filter = ORM::Expr->_and( $obj->M->id == $obj->id );
465 6 ▶ if( $check_all_props )
Branch: 1 branch
BranchTrueFalseTotal
if ($check_all_props) 5 1 6
466 {
467 5 for my $prop ( keys %{$arg{old_values}} )
468 {
469 $filter->add_expr
470 (
471 defined $arg{old_values}{$prop}
472 0 ▶ ? $obj->M->_prop( $prop ) == $arg{old_values}{$prop}
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'old_values'}{$prop} ? : 0 0 0
473 : $obj->M->_prop( $prop )->_is_undef
474 );
475 }
476 }
477 6 $where = $filter->_sql_str( tjoin=>$tjoin );
478  
479 # Prepare SET statement
480 6 my $set = '';
481 6 for my $prop ( keys %{$arg{values}} )
482 {
483 $set .=
484 $obj->M( $prop )->_sql_str( tjoin=>$tjoin )
485 . '='
486 . (
487 ( UNIVERSAL::isa( $arg{values}{$prop}, 'ORM::Expr' ) )
488 ? $arg{values}{$prop}
489 7 ▶ : ORM::Const->new( $arg{values}{$prop} )
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($arg{'values'}{$prop}, 'ORM::Expr') ? : 1 6 7
490 )->_sql_str( tjoin=>$tjoin )
491 . ',';
492 }
493 6 chop $set;
494  
495 my $rows_affected = $self->do
496 (
497 error => $arg{error},
498 6 query =>
499 (
500 "UPDATE " . $tjoin->sql_table_list . "\n"
501 . " SET $set\n"
502 . " WHERE $where"
503 ),
504 );
505  
506 6 ▶ if( $rows_affected == 0 )
Branch: 2 branches
BranchTrueFalseTotal
if ($rows_affected == 0) { } 0 6 6
elsif ($rows_affected > $tjoin->tables_count) { } 0 6 6
507 {
508 $arg{error} && $arg{error}->add_fatal
509 (
510 0 ▶ "Failed to update object with id#".$obj->id
Branch: 1 branch
BranchTrueFalseTotal
$arg{"error"} and $arg{"error"}->add_fatal('Failed to update object with id#' . $obj->id . " of class '${obj_class}', $rows_affected rows affected," . ' may be object was updated elsewhere.') 0 0 0
511 . " of class '$obj_class', $rows_affected rows affected,"
512 . " may be object was updated elsewhere."
513 );
514 }
515 elsif( $rows_affected > $tjoin->tables_count )
516 {
517 $arg{error} && $arg{error}->add_fatal
518 (
519 0 ▶ "Internal error occured!"
Branch: 1 branch
BranchTrueFalseTotal
$arg{"error"} and $arg{"error"}->add_fatal('Internal error occured!' . " More than expected number of rows was updated ($rows_affected)." . ' Please report to developer.') 0 0 0
520 . " More than expected number of rows was updated ($rows_affected)."
521 . " Please report to developer."
522 );
523 }
524 }
525  
526 sub delete_object
527 2 ▶{
Subroutine delete_object: called CC=6 SCAR=18.3
Pod undocumented
528 2 my $self = shift;
529 2 my %arg = @_;
530 2 my $obj = $arg{object};
531 2 my $obj_class = ref $obj;
532 2 my $error = ORM::Error->new;
533 2 my $ta = $obj_class->new_transaction( error=>$error );
534 2 my @table = $obj_class->_db_tables;
535 2 my $rows_affected;
536  
537 $self->check_object_referers
538 (
539 object => $obj,
540 error => $error,
541 check => $arg{emulate_foreign_keys},
542 2 );
543  
544 2 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 2 0 2
545 {
546 2 ▶ for( $i=$#table; $i>=0 && !$error->fatal; $i-- )
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0 and not $error->fatal) 3 2 5
Condition66%2/3
$i >= 0 and not $error->fatal
!ll&&!rl&&r
203
MC/DC50%1/2
$i >= 0 and not $error->fatal
$i >= 0 not $error->fatal
Truth table66%2/3
A and not(B)
A= $i >= 0B= not $error->fatal
ABresult
0X0
100
111
547 {
548 3 my $rows_affected = $self->delete_by_id
549 (
550 table => $table[$i],
551 id => $obj->id,
552 error => $error,
553 );
554 3 ▶ if( $rows_affected != 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($rows_affected != 1) 0 3 3
555 {
556 0 $error->add_fatal( "Failed to delete row with id#$id from '$table[$i]' during object delete" );
557 }
558 }
559  
560 # must check twise, new referers could be created during deletion
561 2 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 2 0 2
562 {
563 $self->check_object_referers
564 (
565 object => $obj,
566 error => $error,
567 check => $arg{emulate_foreign_keys},
568 2 );
569 }
570 }
571  
572 2 $error->upto( $arg{error} );
573 }
574  
575 sub optimize_tables
576 0 ▶{
Subroutine optimize_tables: not called CC=1 SCAR=6.9
Pod undocumented
577 0 my $self = shift;
578 0 my %arg = @_;
579  
580 $self->do
581 (
582 query => 'OPTIMIZE TABLE '.$arg{class}->_db_tables_str,
583 error => $arg{error},
584 0 );
585 }
586  
587 sub referencing_classes
588 9 ▶{
Subroutine referencing_classes: called CC=3 SCAR=11.0
Pod undocumented
589 9 my $self = shift;
590 9 my %arg = @_;
591 9 my $error = ORM::Error->new;
592 9 my $res;
593 my $data;
594 9 my @res;
595  
596 $res = $self->select
597 (
598 error => $error,
599 query =>
600 'SELECT class,prop FROM '.$self->qt('_ORM_refs').' WHERE ref_class='
601 . $self->qc( $arg{class} )
602 9 );
603  
604 9 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 9 0 9
605 {
606 9 ▶ while( $data = $res->next_row )
Branch: 1 branch
BranchTrueFalseTotal
if ($data = $res->next_row) 2 9 11
607 {
608 2 push @res, $data;
609 }
610 }
611  
612 9 $error->upto( $arg{error} );
613 9 return @res;
614 }
615  
616 sub begin_transaction
617 0 ▶{
Subroutine begin_transaction: not called CC=2 SCAR=17.9
Pod undocumented
618 0 my $self = shift;
619 0 my %arg = @_;
620 0 my $error = ORM::Error->new;
621  
622 0 $self->{ta} = 1;
623 0 $self->_db_handler->begin_work();
624 0 ▶ $error->add_fatal( $self->_db_handler->errstr ) if( $self->_db_handler->err );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_db_handler->err 0 0 0
625 0 ORM::DbLog->new( sql=>"BEGIN", error=>$error->text );
626  
627 0 $error->upto( $arg{error} );
628 }
629  
630 sub commit_transaction
631 0 ▶{
Subroutine commit_transaction: not called CC=2 SCAR=17.9
Pod undocumented
632 0 my $self = shift;
633 0 my %arg = @_;
634 0 my $error = ORM::Error->new;
635  
636 0 delete $self->{ta};
637 0 $self->_db_handler->commit();
638 0 ▶ $error->add_fatal( $self->_db_handler->errstr ) if( $self->_db_handler->err );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_db_handler->err 0 0 0
639 0 ORM::DbLog->new( sql=>"COMMIT", error=>$error->text );
640  
641 0 $error->upto( $arg{error} );
642 }
643  
644 sub rollback_transaction
645 0 ▶{
Subroutine rollback_transaction: not called CC=3 SCAR=24.8
Pod undocumented
646 0 my $self = shift;
647 0 my %arg = @_;
648 0 my $error = ORM::Error->new;
649  
650 0 delete $self->{ta};
651 0 ▶ unless( $self->{lost_connection} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"lost_connection"}) 0 0 0
652 {
653 0 $self->_db_handler->rollback();
654 0 ▶ $error->add_fatal( $self->_db_handler->errstr ) if( $self->_db_handler->err );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_db_handler->err 0 0 0
655 0 ORM::DbLog->new( sql=>"ROLLBACK", error=>$error->text );
656 }
657  
658 0 $error->upto( $arg{error} );
659 }
660  
661 ##
662 ## PROTECTED METHODS
663 ##
664  
665 ## use: $db->insert( table=>string, values=>hash, error=>ORM::Error )
666 ##
667 sub insert
668 22 ▶{
Subroutine insert: called CC=2 SCAR=6.9
Pod undocumented
669 22 my $self = shift;
670 22 my %arg = @_;
671 22 my $keys;
672 my $values;
673 22 my $table;
674  
675 22 $table = $self->qt( $arg{table} );
676 22 for my $key ( keys %{$arg{values}} )
677 {
678 163 $keys .= $self->qf( $key ) . ',';
679 163 $values .= $self->qc( $arg{values}{$key} ) . ',';
680 }
681 22 chop $keys;
682 22 chop $values;
683  
684 $self->do
685 (
686 query => "INSERT INTO $table ($keys) VALUES ($values)",
687 error => $arg{error},
688 22 );
689 }
690  
691 ## use: $db->delete_by_id
692 ## (
693 ## table => $string,
694 ## id => number,
695 ## error => ORM::Error,
696 ## )
697 ##
698 sub delete_by_id
699 3 ▶{
Subroutine delete_by_id: called CC=1 SCAR=0.0
Pod undocumented
700 3 my $self = shift;
701 3 my %arg = @_;
702  
703 $self->do
704 (
705 query => ( "DELETE FROM ".$self->qt($arg{table})." WHERE id=".$self->qc($arg{id}) ),
706 error => $arg{error},
707 3 );
708 }
709  
710 ## use: $db->do( query=>$string, error=>ORM::Error )
711 ##
712 sub do
713 61 ▶{
Subroutine do: called CC=1 SCAR=0.0
Pod undocumented
714 61 my $self = shift;
715 61 $self->select( return_rows_count=>1, @_ );
716 }
717  
718 ## use: $result = $db->select
719 ## (
720 ## tables => ARRAY,
721 ## query => string,
722 ## error => ORM::Error,
723 ##
724 ## return_rows_count => 1,
725 ## );
726 ##
727 sub select
728 102 ▶{
Subroutine select: called CC=18 SCAR=41.5
Pod undocumented
729 102 my $self = shift;
730 102 my %arg = @_;
731 102 my $error = ORM::Error->new;
732 102 my $h_error = ORM::Error->new;
733 102 my $retry = 1;
734 102 my $tries = $self->{connect_retries};
735 102 my $query = $arg{query};
736 102 my $rows_affected;
737 my $st;
738  
739 102 $self->{lost_connection} = 0;
740  
741 102 ▶ while( $retry )
Branch: 1 branch
BranchTrueFalseTotal
if ($retry) 102 102 204
742 {
743 102 $retry = 0;
744 102 ▶ if( ! $self->{db} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"db"}) 0 102 102
745 {
746 0 $self->_db_reconnect;
747 0 $retry = $tries--;
748 0 ▶ if( $retry )
Branch: 1 branch
BranchTrueFalseTotal
if ($retry) { } 0 0 0
749 {
750 0 ▶ if( $self->{delayed_connect} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'delayed_connect'}) { } 0 0 0
751 {
752 0 delete $self->{delayed_connect};
753 }
754 else
755 {
756 print STDERR
757 "No connection, connecting to SQL server '"
758 0 . $self->{db_arg}[0]
759 . "' ($tries tries left)\n";
760 }
761 0 next;
762 }
763 else
764 {
765 0 $error->add_fatal( "No db connection" );
766 0 $self->{lost_connection} = 1;
767 0 last;
768 }
769 }
770  
771 102 $st = $self->{db}->prepare( $query );
772 102 $h_error = ORM::Error->new;
773 $st && ( $DBI::VERSION >= 1.21 ) &&
774 (
775 $st->{HandleError} = sub
776 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
777 0 ▶ $h_error->add_fatal( "DBI Error Handler($_[1],'".($_[2]||'')."'): $_[0]" );
Condition0%0/2
$_[2] || ''
l!l
00
MC/DC0%0/1
$_[2] || ''
$_[2]
Truth table0%0/2
A
A= $_[2]
Aresult
11
00
778 0 return 1;
779 }
780 102 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$st and $DBI::VERSION >= "1.21" and $st->{"HandleError"} = sub { $h_error->add_fatal("DBI Error Handler($_[1],'" . ($_[2] || "") . "'): $_[0]"); return 1; } 102 0 102
Condition33%1/3
$st and $DBI::VERSION >= "1.21"
!ll&&!rl&&r
00102
MC/DC0%0/2
$st and $DBI::VERSION >= "1.21"
$st $DBI::VERSION >= "1.21"
Truth table33%1/3
A and B
A= $stB= $DBI::VERSION >= "1.21"
ABresult
0X0
100
111
781  
782 102 ▶ if( $st )
Branch: 1 branch
BranchTrueFalseTotal
if ($st) { } 102 0 102
783 {
784 102 $st->execute;
785 }
786 else
787 {
788 0 $error->add_fatal( "Failed to execute query, 'prepare' returned undef, query='$query'" );
789 }
790  
791 102 ▶ if( $st && $st->err && $self->_lost_connection( $st->err ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($st and $st->err and $self->_lost_connection($st->err)) 0 102 102
Condition33%2/6
$st and $st->err and $self->_lost_connection($st->err)
!ll&&!rl&&r
10200
$st and $st->err
!ll&&!rl&&r
01020
MC/DC0%0/3
$st and $st->err and $self->_lost_connection($st->err)
$st $st->err $self->_lost_connection($st->err)
Truth table25%1/4
A and B and C
A= $stB= $st->errC= $self->_lost_connection($st->err)
ABCresult
0XX0
10X0
1100
1111
792 {
793 0 $self->_db_reconnect;
794 0 $retry = $tries--;
795 print STDERR
796 "Lost connection, reconnecting to SQL server '"
797 0 . $self->{db_arg}[0]
798 . "' ($tries tries left)\n";
799 }
800 }
801  
802 # Catch up errors
803 102 ▶ if( $st && $st->err )
Branch: 2 branches
BranchTrueFalseTotal
if ($st and $st->err) { } 0 102 102
elsif ($h_error->any) { } 0 102 102
Condition33%1/3
$st and $st->err
!ll&&!rl&&r
01020
MC/DC0%0/2
$st and $st->err
$st $st->err
Truth table33%1/3
A and B
A= $stB= $st->err
ABresult
0X0
100
111
804 {
805 0 $error->add_fatal
806 (
807 'DBI Error '.$st->err.': ' . $st->errstr
808 . ', Query="' . $query . '"'
809 );
810 }
811 elsif( $h_error->any )
812 {
813 0 $error->add( error=>$h_error );
814 }
815  
816 ORM::DbLog->new
817 (
818 102 sql => $query,
819 error => $error->text,
820 );
821 102 $error->upto( $arg{error} );
822  
823 return $arg{return_rows_count}
824 ? ( $st && $st->rows != 4294967294 ? $st->rows : 0 )
825 : (
826 $error->fatal
827 ? undef
828 : ORM::Db::DBIResultSet->new( result=>$st, tables=>$arg{tables} )
829 102 ▶ );
Branch: 3 branches
BranchTrueFalseTotal
$arg{'return_rows_count'} ? : 61 41 102
$st && $st->rows != 4294967294 ? : 61 0 61
$error->fatal ? : 0 41 41
Condition33%1/3
$st and $st->rows != 4294967294
!ll&&!rl&&r
0061
MC/DC0%0/2
$st and $st->rows != 4294967294
$st $st->rows != 4294967294
Truth table33%1/3
A and B
A= $stB= $st->rows != 4294967294
ABresult
0X0
100
111
830 }
831  
832 sub ql
833 0 ▶{
Subroutine ql: not called CC=1 SCAR=6.9
Pod undocumented
834 0 my $class = shift;
835 0 my $str = shift;
836  
837 0 $str =~ s/_/\\_/g;
838 0 $str =~ s/%/\\%/g;
839  
840 0 return $class->qc( $str );
841 }
842  
843 42 ▶sub _db_handler { $_[0]->{db}; }
Subroutine _db_handler: called CC=1 SCAR=0.0
844  
845 sub _db_reconnect
846 0 ▶{
Subroutine _db_reconnect: not called CC=1 SCAR=6.9
847 0 my $self = shift;
848  
849 0 $self->{db} = undef;
850 0 sleep $self->{retry_sleep};
851 0 $self->{db} = DBI->connect( @{$self->{db_arg}} );
852 }
853  
854 0 ▶sub _ta_select { 'FOR UPDATE'; }
Subroutine _ta_select: not called CC=1 SCAR=6.9
855  
856 sub _sql_limit
857 3 ▶{
Subroutine _sql_limit: called CC=3 SCAR=11.0
858 3 my $self = shift;
859 3 ▶ my $page = (int shift)||1;
Condition100%2/2
int shift() || 1
l!l
21
MC/DC100%1/1
int shift() || 1
int shift()
Truth table100%2/2
A
A= int shift()
Aresult
11
00
860 3 my $pagesize = int shift;
861 3 my $sql;
862  
863 3 ▶ if( $pagesize )
Branch: 1 branch
BranchTrueFalseTotal
if ($pagesize) 2 1 3
864 {
865 2 $sql = "LIMIT ".(($page-1)*$pagesize).",$pagesize";
866 }
867  
868 3 return $sql;
869 }
870  
871 sub check_object_referers
872 4 ▶{
Subroutine check_object_referers: called CC=4 SCAR=14.2
Pod undocumented
873 4 my $self = shift;
874 4 my %arg = @_;
875 4 my $obj = $arg{object};
876 4 my $obj_class = ref $obj;
877
878 4 ▶ if( $arg{check} )
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"check"}) 4 0 4
879 {
880 4 for my $ref ( $obj_class->_rev_refs )
881 {
882 my $referers = $ref->[0]->count
883 (
884 filter => ( $ref->[0]->M->_prop($ref->[1])==$obj->id ),
885 error => $arg{error},
886 2 );
887 2 ▶ if( $referers )
Branch: 1 branch
BranchTrueFalseTotal
if ($referers) 0 2 2
888 {
889 $arg{error}->add_fatal
890 (
891 0 "Can't delete instance ID#" . $obj->id
892 . " of '$obj_class', because there're "
893 . "$referers instances of '$ref->[0]' refer to it."
894 );
895 }
896 }
897 }
898  
899 4 return undef;
900 }
901  
902 ##
903 ## ABSTRACT METHODS
904 ##
905  
906 sub insertid
907 0 ▶{
Subroutine insertid: not called CC=1 SCAR=6.9
Pod undocumented
908 0 die "You forget to override 'insertid' in '$_[0]'";
909 }
910  
911 sub _lost_connection
912 0 ▶{
Subroutine _lost_connection: not called CC=1 SCAR=6.9
913 0 die "You forget to override '_lost_connection' in '$_[0]'";
914 }
915  
916 ##
917 ## SQL FUNCTIONS
918 ##
919  
920 1 ▶sub _func_concat { shift; ORM::Filter::Cmp->new( '||', @_ ); }
Subroutine _func_concat: called CC=1 SCAR=0.0
« ORM/Filter/Case.pm ↑ Summary ORM/Base.pm »