Easy.pm

filter: Statementstmt 88.6% 251 / 283 Branchbran 61.6% 69 / 112 Conditioncond 56.9% 33 / 58 MC/DCmcdc 27.2% 9 / 33 Subroutinesub 90.0% 27 / 30 Podpod 20.0% 1 / 5 totaltotal 74.8% 390 / 521 CC 80 SCAR 24.9
CC
80
Cov
78%
_prefix_manipulations
31.9
_dbh_error
17.0
_dbh_columns_info
11.2
CRAP 12.1
« Easy/SQL.pm ↑ Summary Easy/Helper.pm »
1 package DBI::Easy;
2  
3 6 ▶use Class::Easy::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use DBI 1.611;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 #use Hash::Util;
8  
9 6 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 $VERSION = '0.24';
11  
12 # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
13 # interface splitted to various sections:
14 # sql generation stuff prefixed with sql and located
15 # at DBI::Class::SQL
16 # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
17  
18 6 ▶use DBI::Easy::SQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
21 # real dbh operations contains methods fetch_* and no_fetch
22 # and placed in DBI::Class::DBH
23 # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
24  
25 6 ▶use DBI::Easy::DBH;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 6 ▶use DBI::Easy::DriverPatcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 6 ▶use DBI::Easy::Helper;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # bwahahahaha
32 6 our %GREP_COLUMN_INFO = qw(TYPE_NAME 1 mysql_values 1);
33  
34 6 our $wrapper = 1;
35  
36 6 our $H = 'DBI::Easy::Helper';
37  
38 77 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
39 77 my $class = shift;
40
41 77 my $params;
42 my $init_params;
43
44 77 ▶ $init_params = $class->_init (@_)
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("_init") 61 16 77
45 if $class->can ('_init');
46
47 $params = $init_params || {(@_ == 1 && ref $_[0] && ref $_[0] eq 'HASH')
48 77 ▶ ? %{$_[0]}
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] && ref $_[0] eq 'HASH' ? : 4 12 16
Condition75%6/8
$init_params || {@_ == 1 && ref $_[0] && ref $_[0] eq 'HASH' ? %{$_[0];} : @_}
l!l
6116
@_ == 1 and ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
1204
@_ == 1 and ref $_[0]
!ll&&!rl&&r
1204
MC/DC100%1/1
$init_params || {@_ == 1 && ref $_[0] && ref $_[0] eq 'HASH' ? %{$_[0];} : @_}
$init_params
MC/DC33%1/3
@_ == 1 and ref $_[0] and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] ref $_[0] eq "HASH"
Truth table100%2/2
A
A= $init_params
Aresult
11
00
Truth table50%2/4
A and B and C
A= @_ == 1B= ref $_[0]C= ref $_[0] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
49 : @_
50 };
51
52 77 bless $params, $class;
53 }
54  
55 33 ▶sub import {
Subroutine import: called CC=10 SCAR=23.2
56 33 my $class = shift;
57
58 33 ▶ unless (${"${class}::imported"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (${"${class}::imported";}) 23 10 33
59 23 make_accessor ($class, 'dbh', is => 'rw', global => 1);
60 42 ▶ make_accessor ($class, 'dbh_modify', is => 'rw', global => 1, default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
61 42 return shift->dbh;
62 23 });
63 }
64  
65 33 ▶ if (! ${"${class}::wrapper"} and $class ne __PACKAGE__ and ! ${"${class}::imported"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ${"${class}::wrapper";} and $class ne __PACKAGE__ and not ${"${class}::imported";}) 17 16 33
Condition83%5/6
not ${"${class}::wrapper";} and $class ne __PACKAGE__ and not ${"${class}::imported";}
!ll&&!rl&&r
12417
not ${"${class}::wrapper";} and $class ne __PACKAGE__
!ll&&!rl&&r
12021
MC/DC66%2/3
not ${"${class}::wrapper";} and $class ne __PACKAGE__ and not ${"${class}::imported";}
not ${"${class}::wrapper";} $class ne __PACKAGE__ not ${"${class}::imported";}
Truth table75%3/4
not(A) and B and not(C)
A= not ${"${class}::wrapper";}B= $class ne __PACKAGE__C= not ${"${class}::imported";}
ABCresult
0XX0
10X0
1100
1111
66
67 17 debug "importing $class";
68
69 17 my $t = timer ('init_class');
70 17 $class->_init_class;
71
72 17 $t->lap ('init_db');
73
74 # we call _init_db from package before real db
75 17 $class->_init_db;
76
77 17 $t->lap ("init_collection $class");
78
79 17 ▶ $class->_init_collection
Branch: 1 branch
BranchTrueFalseTotal
if $class->is_collection 6 11 17
80 if $class->is_collection;
81
82 17 $t->lap ("dbh check and accessors $class");
83
84 17 ▶ die "can't use database class '$class' without db connection: $DBI::errstr"
Branch: 1 branch
BranchTrueFalseTotal
if not $class->dbh or $class->dbh eq "0E0" 0 17 17
Condition33%1/3
not $class->dbh or $class->dbh eq "0E0"
l!l&&r!l&&!r
0017
MC/DC0%0/2
not $class->dbh or $class->dbh eq "0E0"
not $class->dbh $class->dbh eq "0E0"
Truth table33%1/3
not(A) or B
A= not $class->dbhB= $class->dbh eq "0E0"
ABresult
1X1
011
000
85 if ! $class->dbh or $class->dbh eq '0E0';
86  
87 17 ▶ die "can't retrieve table '".$class->table_name."' columns for '$class'"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_init_make_accessors 0 17 17
88 unless $class->_init_make_accessors;
89
90 17 $t->lap ("init_last $class");
91
92 17 $class->_init_last;
93
94 17 $t->end;
95
96 # my $driver = $class->dbh->get_info (17);
97 # warn "driver name from
98 # get_info ($DBI::Const::GetInfoType{SQL_DBMS_NAME}): $driver";
99 }
100
101 33 ${"${class}::imported"} = 1;
102
103 33 ▶ $class::SUPER->import (@_)
Branch: 1 branch
BranchTrueFalseTotal
if defined $class::SUPER 0 33 33
104 if (defined $class::SUPER);
105
106 }
107  
108 0 ▶sub _init_db {
Subroutine _init_db: not called CC=1 SCAR=6.9
109 0 my $self = shift;
110 0 $self->dbh (DBI->connect);
111 }
112  
113 17 ▶sub _init_class {
Subroutine _init_class: called CC=7 SCAR=19.9
114 17 my $self = shift;
115  
116 17 ▶ my $ref = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0170
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
117
118 17 my @pack_chunks = split /\:\:/, $ref;
119
120 17 my $is_collection = 0;
121
122 # fix for collections
123 17 ▶ if ($pack_chunks[-1] eq 'Collection') {
Branch: 2 branches
BranchTrueFalseTotal
if ($pack_chunks[-1] eq 'Collection') { } 6 11 17
elsif ($pack_chunks[-1] eq 'Record') { } 0 11 11
124 6 pop @pack_chunks;
125
126 6 make_accessor ($ref, 'record_package', is => 'rw', global => 1);
127
128 6 $is_collection = 1;
129
130 } elsif ($pack_chunks[-1] eq 'Record') {
131 0 pop @pack_chunks;
132 }
133
134 17 make_accessor ($ref, 'is_collection', default => $is_collection);
135
136 17 my $table_name = DBI::Easy::Helper::table_from_package ($pack_chunks[-1]);
137
138 # dies when this method called without object reference;
139 # expected behaviour
140
141 17 my $common_table_prefix = '';
142
143 17 ▶ $common_table_prefix = $ref->common_table_prefix
Branch: 1 branch
BranchTrueFalseTotal
if $ref->can("common_table_prefix") 2 15 17
144 if $ref->can ('common_table_prefix');
145
146 17 ▶ make_accessor (
Branch: 1 branch
BranchTrueFalseTotal
unless $ref->can("table_name") 17 0 17
147 $ref, 'table_name', is => 'rw', global => 1,
148 default => $common_table_prefix . $table_name
149 ) unless $ref->can ('table_name');
150
151 17 make_accessor ($ref, '_date_format', is => 'rw', global => 1);
152
153 17 ▶ make_accessor (
Branch: 1 branch
BranchTrueFalseTotal
unless $ref->can("column_prefix") 17 0 17
154 $ref, 'column_prefix', is => 'rw', global => 1,
155 default => $ref->table_name . "_"
156 ) unless $ref->can ('column_prefix');
157
158 17 make_accessor ($ref, 'fieldset', is => 'rw', default => '*');
159
160 17 make_accessor ($ref, 'prepare_method', is => 'rw', global => 1,
161 default => 'prepare_cached');
162 17 make_accessor ($ref, 'prepare_param', is => 'rw', global => 1,
163 default => 3);
164 17 make_accessor ($ref, 'undef_as_null', is => 'rw', global => 1,
165 default => 0);
166
167 }
168  
169 6 ▶sub _init_collection {
Subroutine _init_collection: called CC=5 SCAR=18.3
170 6 my $self = shift;
171
172 6 my $rec_pkg = $self->record_package;
173
174 6 ▶ unless ($rec_pkg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rec_pkg) 6 0 6
175 6 ▶ my $ref = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
176
177 6 my @pack_chunks = split /\:\:/, $ref;
178
179 6 pop @pack_chunks;
180
181 6 $rec_pkg = join '::', @pack_chunks;
182
183 # TODO: move to Class::Easy
184 6 ▶ unless (try_to_use ($rec_pkg)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (try_to_use($rec_pkg)) 0 6 6
185 0 ▶ die unless try_to_use ($rec_pkg . '::Record');
Branch: 1 branch
BranchTrueFalseTotal
unless try_to_use($rec_pkg . '::Record') 0 0 0
186 }
187
188 6 $self->record_package ($rec_pkg);
189 }
190
191 }
192  
193 17 ▶sub _detect_vendor {
Subroutine _detect_vendor: called CC=3 SCAR=11.0
194 17 my $class = shift;
195
196 17 my $dbh = $class->dbh;
197
198 17 my $vendor = lc ($dbh->get_info(17));
199
200 17 make_accessor ($class, 'dbh_vendor', default => $vendor);
201
202 17 my $vendor_pack = "DBI::Easy::Vendor::$vendor";
203 17 my $have_vendor_pack = try_to_use_quiet ($vendor_pack);
204
205 17 ▶ unless ($have_vendor_pack) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($have_vendor_pack) 17 0 17
206 17 $vendor_pack = "DBI::Easy::Vendor::Base";
207 17 ▶ die unless try_to_use_quiet ($vendor_pack);
Branch: 1 branch
BranchTrueFalseTotal
unless try_to_use_quiet($vendor_pack) 0 17 17
208 }
209
210 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
211
212 17 push @{"$class\::ISA"}, $vendor_pack;
213
214 6 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
215
216 17 $class->_init_vendor;
217
218 }
219  
220 # here we retrieve fields and create make_accessors
221 17 ▶sub _init_make_accessors {
Subroutine _init_make_accessors: called CC=10 SCAR=23.6
222 17 my $class = shift;
223
224 17 my $table_name = $class->table_name;
225 17 my $column_prefix = $class->column_prefix;
226
227 # detecting vendor
228 17 $class->_detect_vendor;
229
230 17 my $t = timer ('columns info wrapper');
231
232 17 my $columns = $class->_dbh_columns_info;
233
234 17 $t->end;
235
236 17 make_accessor ($class, 'columns', default => $columns);
237 17 make_accessor ($class, 'column_values', is => 'rw');
238
239 17 my $fields = {};
240
241 17 make_accessor ($class, 'fields', default => $fields);
242 17 make_accessor ($class, 'field_values', is => 'rw');
243
244 17 my $pri_key;
245 my $pri_key_column;
246
247 17 foreach my $col_name (keys %$columns) {
248 81 my $col_meta = $columns->{$col_name};
249 # here we translate rows
250 81 my $field_name = lc ($col_name); # oracle fix
251
252 81 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $column_prefix and $column_prefix ne "" and $col_name =~ /^$column_prefix(.*)/i) 32 49 81
Condition50%3/6
defined $column_prefix and $column_prefix ne "" and $col_name =~ /^$column_prefix(.*)/i
!ll&&!rl&&r
04932
defined $column_prefix and $column_prefix ne ""
!ll&&!rl&&r
0081
MC/DC33%1/3
defined $column_prefix and $column_prefix ne "" and $col_name =~ /^$column_prefix(.*)/i
defined $column_prefix $column_prefix ne "" $col_name =~ /^$column_prefix(.*)/i
Truth table50%2/4
A and B and C
A= defined $column_prefixB= $column_prefix ne ""C= $col_name =~ /^$column_prefix(.*)/i
ABCresult
0XX0
10X0
1100
1111
253 defined $column_prefix
254 and $column_prefix ne ''
255 and $col_name =~ /^$column_prefix(.*)/i
256 ) {
257 32 $field_name = lc($1);
258 }
259
260 # field meta referenced to column meta
261 # no we can use $field_meta->{col_name} and $col_meta->{field_name}
262 81 $fields->{$field_name} = $col_meta;
263
264 81 $col_meta->{field_name} = $field_name;
265
266 0 ▶ if ($col_meta->{type_name} eq 'ENUM' and $#{$col_meta->{mysql_values}} >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col_meta->{"type_name"} eq "ENUM" and $#{$$col_meta{"mysql_values"};} >= 0) 0 81 81
Condition33%1/3
$col_meta->{"type_name"} eq "ENUM" and $#{$$col_meta{"mysql_values"};} >= 0
!ll&&!rl&&r
8100
MC/DC0%0/2
$col_meta->{"type_name"} eq "ENUM" and $#{$$col_meta{"mysql_values"};} >= 0
$col_meta->{"type_name"} eq "ENUM" $#{$$col_meta{"mysql_values"};} >= 0
Truth table33%1/3
A and B
A= $col_meta->{"type_name"} eq "ENUM"B= $#{$$col_meta{"mysql_values"};} >= 0
ABresult
0X0
100
111
267 make_accessor ($class, "${field_name}_variants",
268 0 default => $col_meta->{mysql_values});
269 }
270
271 # attach decoder for complex datatypes, as example date, datetime, timestamp
272 81 $class->attach_decoder ($col_meta);
273
274 81 ▶ if (exists $col_meta->{X_IS_PK} and $col_meta->{X_IS_PK} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $col_meta->{"X_IS_PK"} and $col_meta->{"X_IS_PK"} == 1) 17 64 81
Condition66%2/3
exists $col_meta->{"X_IS_PK"} and $col_meta->{"X_IS_PK"} == 1
!ll&&!rl&&r
64017
MC/DC50%1/2
exists $col_meta->{"X_IS_PK"} and $col_meta->{"X_IS_PK"} == 1
exists $col_meta->{"X_IS_PK"} $col_meta->{"X_IS_PK"} == 1
Truth table66%2/3
A and B
A= exists $col_meta->{"X_IS_PK"}B= $col_meta->{"X_IS_PK"} == 1
ABresult
0X0
100
111
275
276 17 ▶ if ($pri_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pri_key) { } 0 17 17
277 0 warn "multiple pri keys: $fields->{$pri_key}->{column_name} and $field_name";
278 } else {
279 17 $pri_key = $field_name;
280 17 $pri_key_column = $col_name;
281 }
282
283 5 ▶ my $fetch_by_pk_sub = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
284 5 my $package = shift;
285 5 my $value = shift;
286
287 5 return $package->fetch ({$field_name => $value}, @_);
288 17 };
289
290 17 make_accessor ($class, "fetch_by_$field_name", default => $fetch_by_pk_sub);
291 17 make_accessor ($class, "fetch_by_pk", default => $fetch_by_pk_sub);
292 }
293
294 # access to the precise field value or column value without cool accessors
295
296 47 ▶ make_accessor ($class, $field_name, default => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
297 47 my $self = shift;
298
299 47 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 44 3 47
300 # bad style?
301 return
302 $self->{field_values}->{$field_name} || (
303 exists $self->columns->{$col_name}->{decoder}
304 ? $self->columns->{$col_name}->{decoder}->($self) # ($self->{column_values}->{$col_name});
305 44 ▶ : $self->{column_values}->{$col_name});
Branch: 1 branch
BranchTrueFalseTotal
exists $self->columns->{$col_name}{'decoder'} ? : 0 36 36
Condition100%3/3
$self->{'field_values'}{$field_name} || (exists $self->columns->{$col_name}{'decoder'} ? $self->columns->{$col_name}{'decoder'}->($self) : $self->{'column_values'}{$col_name})
l!l&&r!l&&!r
8342
MC/DC100%2/2
$self->{'field_values'}{$field_name} || (exists $self->columns->{$col_name}{'decoder'} ? $self->columns->{$col_name}{'decoder'}->($self) : $self->{'column_values'}{$col_name})
$self->{'field_values'}{$field_name} (exists $self->columns->{$col_name}{'decoder'} ? $self->columns->{$col_name}{'decoder'}->($self) : $self->{'column_values'}{$col_name})
Truth table100%3/3
A || B
A= $self->{'field_values'}{$field_name}B= (exists $self->columns->{$col_name}{'decoder'} ? $self->columns->{$col_name}{'decoder'}->($self) : $self->{'column_values'}{$col_name})
ABresult
1X1
011
000
306 }
307  
308 3 ▶ die "too many parameters" if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 3 3
309  
310 3 $self->assign_values ($field_name => $_[0]);
311
312 81 });
313
314 1 ▶ make_accessor ($class, "_fetched_${field_name}", default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
315 1 my $self = shift;
316
317 1 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 1 0 1
318 1 return $self->{column_values}->{$col_name};
319 }
320  
321 0 die "too many parameters";
322 81 });
323  
324 1 ▶ make_accessor ($class, "_raw_${field_name}", default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
325 1 my $self = shift;
326
327 1 ▶ die "you must supply one parameter" unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 1 1
328
329 1 $self->{field_values}->{$field_name} = $_[0];
330 81 });
331  
332 }
333
334 17 make_accessor ($class, '_pk_', default => $pri_key);
335 17 make_accessor ($class, '_pk_column_', default => $pri_key_column);
336  
337 17 return $class;
338 }
339  
340 3 ▶sub assign_values {
Subroutine assign_values: called CC=2 SCAR=6.9
Pod undocumented
341 3 my $self = shift;
342 3 my $to_assign = {@_};
343
344 3 foreach my $k (keys %$to_assign) {
345 3 $self->{field_values}->{$k} = $to_assign->{$k};
346 }
347 }
348  
349 81 ▶sub attach_decoder {
Subroutine attach_decoder: called CC=3 SCAR=14.8
Pod undocumented
350 81 my $class = shift;
351 81 my $col_meta = shift;
352
353 81 my $type = $col_meta->{type_name};
354
355 81 ▶ if (defined $type and $H->is_rich_type ($type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and $DBI::Easy::H->is_rich_type($type)) 0 81 81
Condition33%1/3
defined $type and $DBI::Easy::H->is_rich_type($type)
!ll&&!rl&&r
0810
MC/DC0%0/2
defined $type and $DBI::Easy::H->is_rich_type($type)
defined $type $DBI::Easy::H->is_rich_type($type)
Truth table33%1/3
A and B
A= defined $typeB= $DBI::Easy::H->is_rich_type($type)
ABresult
0X0
100
111
356
357 0 ▶ $col_meta->{decoder} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
358 0 my $self = shift;
359 0 my $value = $self->column_values->{$col_meta->{column_name}};
360 0 return $H->value_from_type ($type, $value, $self);
361 }
362 0 }
363 }
364  
365  
366 17 ▶sub _init_last {
Subroutine _init_last: called CC=1 SCAR=0.0
367  
368 }
369  
370 17 ▶sub _dbh_columns_info {
Subroutine _dbh_columns_info: called CC=11 SCAR=24.2
371 17 my $class = shift;
372
373 17 my $ts = timer ('inside columns info');
374  
375 17 my $dbh = $class->dbh;
376  
377 17 my $table_name = $class->table_name;
378
379 17 $ts->lap ('make accessor');
380
381 # preparations
382 17 make_accessor (
383 $class, 'table_quoted',
384 default => $class->quote_identifier ($table_name)
385 );
386
387 17 my $real_row_count = 0;
388
389 17 my $column_info = {};
390
391 17 $ts->lap ('eval column info');
392
393 17 eval {
394
395 17 my $t = timer ('column info call');
396
397 17 my $sth = $dbh->column_info(
398 undef, undef, $table_name, '%'
399 );
400
401 17 $t->lap ('execute');
402
403 $sth->execute
404 17 ▶ unless $sth->{Executed};
Branch: 1 branch
BranchTrueFalseTotal
unless $sth->{"Executed"} 17 0 17
405
406 17 $t->lap ('fetchrow hashref');
407
408 17 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 81 17 98
409 81 $real_row_count ++;
410
411 81 my $column_name = $row->{COLUMN_NAME};
412
413 $column_info->{$column_name} = {
414 (map {
415 81 lc($_) => $row->{$_}
416 } grep {
417 1458 exists $GREP_COLUMN_INFO{$_}
418 } keys %$row),
419
420 column_name => $column_name,
421 quoted_column_name => $dbh->quote_identifier ($column_name),
422 nullable => $row->{NULLABLE},
423 81 };
424
425 81 my $default_val = $row->{COLUMN_DEF};
426 81 ▶ if (defined $default_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default_val) 5 76 81
427 5 $default_val =~ s/^"(.*)"$/$1/;
428 5 $column_info->{$column_name}->{default} = $default_val;
429 }
430
431 }
432
433 17 $t->end;
434
435 17 $t->total;
436
437 17 ▶ if ($real_row_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($real_row_count == 0) 0 17 17
438 0 die "no rows for table '$table_name' fetched";
439 }
440 };
441
442 17 $ts->lap ('_dbh_error');
443
444 return
445 17 ▶ if $class->_dbh_error ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_dbh_error($@) 0 17 17
446
447 17 $real_row_count = 0;
448
449 17 $ts->lap ('primary_key_info');
450
451 17 eval {
452
453 17 my $t = timer ('primary key');
454
455 # fuckin oracle
456 17 my $schema = $class->vendor_schema;
457
458 17 my $sth = $dbh->primary_key_info(
459 undef, $schema, $table_name
460 );
461
462 17 $t->lap ('execute');
463
464 17 ▶ if ($sth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth) 17 0 17
465 $sth->execute
466 17 ▶ unless $sth->{Executed};
Branch: 1 branch
BranchTrueFalseTotal
unless $sth->{"Executed"} 17 0 17
467
468 17 $t->lap ('fetchrow');
469  
470 17 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 17 17 34
471 17 $real_row_count ++;
472 # here we translate rows
473 17 my $pri_key_name = $row->{COLUMN_NAME};
474
475 17 $column_info->{$row->{COLUMN_NAME}}->{X_IS_PK} = 1;
476 17 $column_info->{$row->{COLUMN_NAME}}->{nullable} = 0;
477
478 }
479 }
480
481 17 $t->end;
482
483 17 $t->total;
484
485 17 ▶ if ($real_row_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($real_row_count == 0) 0 17 17
486 0 warn "no primary keys for table '$table_name'";
487 }
488 };
489
490 return
491 17 ▶ if $class->_dbh_error ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_dbh_error($@) 0 17 17
492  
493 17 $ts->end;
494
495 #Hash::Util::lock_hash_recurse (%$column_info);
496
497 17 return $column_info;
498 }
499  
500 102 ▶sub _dbh_error {
Subroutine _dbh_error: called CC=5 SCAR=28.3
501 102 my $self = shift;
502 102 my $error = shift;
503 102 my $statement = shift;
504
505 102 ▶ return unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 102 0 102
506
507 0 my @caller = caller (1);
508 0 my @caller2 = caller (2);
509
510 0 ▶ if ($DBI::Easy::ERRHANDLER and ref $DBI::Easy::ERRHANDLER eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($DBI::Easy::ERRHANDLER and ref $DBI::Easy::ERRHANDLER eq 'CODE') { } 0 0 0
Condition0%0/3
$DBI::Easy::ERRHANDLER and ref $DBI::Easy::ERRHANDLER eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$DBI::Easy::ERRHANDLER and ref $DBI::Easy::ERRHANDLER eq "CODE"
$DBI::Easy::ERRHANDLER ref $DBI::Easy::ERRHANDLER eq "CODE"
Truth table0%0/3
A and B
A= $DBI::Easy::ERRHANDLERB= ref $DBI::Easy::ERRHANDLER eq "CODE"
ABresult
0X0
100
111
511 0 &$DBI::Easy::ERRHANDLER ($self, $error, $statement);
512 } else {
513 0 warn ("[db error at $caller[3] ($caller[2]) called at $caller2[3] ($caller2[2])] ",
514 $error
515 );
516 }
517
518 0 ▶ if ($self->{in_transaction}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_transaction"}) 0 0 0
519 0 eval {$self->rollback};
520 0 die $error;
521 }
522
523 0 return 1;
524 }
525  
526  
527 # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
528 # we always work with one table or view.
529 # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
530  
531 107 ▶sub _prefix_manipulations {
Subroutine _prefix_manipulations: called CC=22 SCAR=34.6
532 107 my $self = shift;
533 107 my $dir = shift;
534 107 my $values = shift;
535 107 ▶ my $in_place = shift || 0;
Condition50%1/2
shift() || 0
l!l
0107
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
536
537 107 my $entities;
538 my $ent_key;
539 107 my $convert;
540 107 ▶ if ($dir eq 'fields2cols') {
Branch: 2 branches
BranchTrueFalseTotal
if ($dir eq 'fields2cols') { } 107 0 107
elsif ($dir eq 'cols2fields') { } 0 0 0
541 107 $entities = $self->fields;
542 107 $ent_key = 'column_name';
543 107 $convert = 'value_to_type';
544 107 ▶ $values = $self->field_values
Branch: 1 branch
BranchTrueFalseTotal
unless $values 41 66 107
545 unless $values;
546 } elsif ($dir eq 'cols2fields') {
547 0 $entities = $self->cols;
548 0 $ent_key = 'field_name';
549 0 $convert = 'value_from_type';
550 0 ▶ $values = $self->column_values
Branch: 1 branch
BranchTrueFalseTotal
unless $values 0 0 0
551 unless $values;
552 } else {
553 0 die "you can't call _prefix_manipulations without direction";
554 }
555  
556 107 ▶ return $values if ! ref $values;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $values 28 79 107
557
558 79 my $place = $values;
559 79 ▶ unless ($in_place) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_place) 79 0 79
560 79 $place = {};
561 }
562
563 79 foreach (keys %$values) {
564
565 84 ▶ next unless exists $entities->{$_} or /^[_:-]\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $entities->{$_} or /^[_:-]\w+$/ 0 84 84
Condition66%2/3
exists $entities->{$_} or /^[_:-]\w+$/
l!l&&r!l&&!r
73110
MC/DC0%0/2
exists $entities->{$_} or /^[_:-]\w+$/
exists $entities->{$_} /^[_:-]\w+$/
Truth table66%2/3
A or B
A= exists $entities->{$_}B= /^[_:-]\w+$/
ABresult
1X1
011
000
566 #&& ($self->undef_as_null || defined $entities->{$_})
567  
568 84 ▶ if (/^:\w+$/) { # copy placeholders
Branch: 1 branch
BranchTrueFalseTotal
if (/^:\w+$/) 1 83 84
569 1 ▶ unless ($in_place) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_place) 1 0 1
570 1 $place->{$_} = $values->{$_};
571 }
572 1 next;
573 }
574 # next if $ent->{$ent_key} eq $_ and $in_place; #
575 83 my ($column_prefix, $k) = (/^(_?)(\w+)$/);
576 83 ▶ $column_prefix = ''
Branch: 1 branch
BranchTrueFalseTotal
unless defined $column_prefix 0 83 83
577 unless defined $column_prefix;
578
579 # debug $k, $_;
580
581 83 my $ent = $entities->{$k};
582 83 my $value = $values->{$_};
583
584 83 ▶ if ($in_place) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_place) 0 83 83
585 0 delete $values->{$_};
586 }
587
588 83 my $v = $value;
589
590 # we must convert only convertible values
591 # field => 'value'
592 # _field => {'>', 'value'}
593 83 ▶ if ($column_prefix eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($column_prefix eq '') { } 73 10 83
elsif ($column_prefix eq '_' and ref $value and ref $value eq 'HASH' and keys %$value == 1) { } 3 14 17
594 $v = $H->$convert (
595 73 $ent->{type_name}, $value, $self
596 );
597 ▶ } elsif (
Condition66%6/9
$column_prefix eq "_" and ref $value and ref $value eq "HASH" and keys %$value == 1
!ll&&!rl&&r
703
$column_prefix eq "_" and ref $value and ref $value eq "HASH"
!ll&&!rl&&r
703
$column_prefix eq "_" and ref $value
!ll&&!rl&&r
073
MC/DC25%1/4
$column_prefix eq "_" and ref $value and ref $value eq "HASH" and keys %$value == 1
$column_prefix eq "_" ref $value ref $value eq "HASH" keys %$value == 1
Truth table40%2/5
A and B and C and D
A= $column_prefix eq "_"B= ref $valueC= ref $value eq "HASH"D= keys %$value == 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
598 $column_prefix eq '_' and ref $value and ref $value eq 'HASH'
599 and keys %$value == 1
600 ) {
601 3 my $condition = (keys %$value)[0];
602 $v = $condition . $self->quote ($H->$convert (
603 3 $ent->{type_name}, $value->{$condition}, $self
604 ));
605 }
606
607 83 ▶ next unless exists $ent->{$ent_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ent->{$ent_key} 0 83 83
608
609 # warn "$prefix/$ent_key => $ent->{$ent_key}";
610 83 $place->{$column_prefix . $ent->{$ent_key}} = $v;
611 }
612
613 79 ▶ return $place
Branch: 1 branch
BranchTrueFalseTotal
unless $in_place 79 0 79
614 unless $in_place;
615
616 }
617  
618 107 ▶sub fields_to_columns {
Subroutine fields_to_columns: called CC=1 SCAR=0.0
Pod undocumented
619 107 my $self = shift;
620
621 107 $self->_prefix_manipulations ('fields2cols', shift, 0);
622 }
623  
624 0 ▶sub columns_to_fields {
Subroutine columns_to_fields: not called CC=1 SCAR=6.9
Pod undocumented
625 0 my $self = shift;
626
627 0 $self->_prefix_manipulations ('cols2fields', shift, 0);
628 }
629  
630 # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
631 # we always work with one table or view.
632 # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
633  
634  
635 # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
636 # simplified sql execute
637 # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
638  
639  
640  
641 6 1;
642  
643 =head1 NAME
644  
645 DBI::Easy - yet another perl ORM for SQL databases
646  
647 =head1 DESCRIPTION
648  
649 DBI::Easy is another ORM, aimed at making the life of the developer
650 using it a lot easier.
651  
652 =head1 INTRODUCTION
653  
654 The key notions of DBI::Easy are data records, collection of data records
655 and relations between them. A data record is a presentation of SQL result:
656 row or blessed hash, depending on how you look at it. Data records collection
657 is a set of records limited by certain criteria or without any limitations.
658 the differentiation between collections and records has to do with
659 different relations between them: one-to-one, one-to-many, many-to-many.
660  
661 For Example: Within a domain auction based on DBI::Easy, every user may
662 have a few bids, but each bid belongs to just one concrete user.
663  
664 It's also worth mentioning the relations between DBI::Easy and SQL. DBI::Easy
665 is currently using a small set of sql, limited to tables and views,
666 including four operations to work with data: insert, update, select, delete.
667 The relations between SQL objects are not formed automatically with the help
668 of constraints.
669  
670 Also it's important that DBI::Easy is not trying to hide SQL from you.
671 If you need it you can use it fully. However, it allows carrying out the vast
672 majority of simple operations with data without the participation of SQL.
673  
674 =head1 SYNOPSIS
675  
676 Let's start from the most simple things. To start the work you will need two
677 modules that will return database handler ($dbh) upon request.
678  
679 To avoid unpleasant consequences it's recommended to cache the returned
680 connection only after the fork, if there is a fork in your code.
681 for the case when CL environment variables for DBI_DSN and DBI_* are defined,
682 and they can be used to establish a connection that doesn't need to be cached,
683 you can do without these modules at all. The main task for 'Entity' is to
684 acquire DBI::Easy::Record[::Collection] or one of the child classes.
685  
686 package DBEntity;
687 use strict;
688 use DBI;
689 use DBI::Easy::Record;
690 use base qw(DBI::Easy::Record);
691 sub dbh { # optional. You don't have to write a procedure similar
692 # to this one since DBI->connect is requested
693 # when a ready $dbh hasn't been provided
694 return DBI->connect;
695 };
696 1;
697  
698 #-----------------------------------------
699  
700 package DBEntity::Collection;
701 use strict;
702 use DBI::Easy::Record::Collection;
703 use base qw(DBI::Easy::Record::Collection);
704 1;
705  
706 Now let's get down to something concrete. Let's assume we have a user and his
707 passport data (one-to-one relation) and some contact data (one-to-many)
708 NOTE: the many-to-many relations hasn't been realized yet.
709  
710  
711 package Entity::Passport;
712 use strict;
713 use DBEntity;
714 use base qw(DBEntity);
715 1;
716  
717 #-----------------------------------------
718  
719 package Entity::Contact;
720 use strict;
721 use DBEntity;
722 use base qw(DBEntity);
723 1;
724  
725 #-----------------------------------------
726  
727 package Entity::Contact::Collection;
728 use strict;
729 use DBEntity::Collection;
730 use base qw(DBEntity::Collection);
731 1;
732  
733 #-----------------------------------------
734  
735 package Entity::Account;
736 use strict;
737 use DBEntity;
738 use base qw(DBEntity);
739 use Entity::Passport;
740 use Entity::Contact::Collection;
741  
742 sub _init_last {
743 my $self = shift;
744 $self->is_related_to (
745 passport => 'Entity::Passport'
746 );
747 $self->is_related_to (
748 contacts => 'Entity::Contact::Collection'
749 );
750 }
751 1;
752  
753 #-----------------------------------------
754  
755 package Entity::Account::Collection;
756 use strict;
757 use DBEntity::Collection;
758 use base qw(DBEntity::Collection);
759 1;
760  
761 #-----------------------------------------
762  
763 Now let's create some SQL tables for our test application (using SQLite):
764  
765 create table account (
766 account_id serial not null primary key,
767 account_login varchar (50) not null
768 );
769 create table pasport (
770 passport_id serial not null primary key,
771 passport_serial varchar (50) not null,
772 account_id integer
773 );
774 create table contact (
775 contact_id serial not null primary key,
776 contact_proto varchar (10) not null,
777 contact_address varchar (200) not null,
778 account_id integer
779 );
780  
781 And now the funniest part: the script itself:
782  
783 #-----------------------------------------
784  
785 #!/usr/bin/perl
786  
787 use strict;
788 use Entity::Account;
789  
790 # here it doesn`t matter whether there is a user with such a login in
791 # the database, if needed we can create it.
792  
793 my $account = Entity::Account->fetch_or_create ({login => 'apla'});
794  
795 # here fetch_or_create is implicitly activated with the parameters
796 # {id => $account->id, serial => 'aabbcc'}
797  
798 $account->passport ({serial => 'aabbcc'});
799  
800 my $acc_contacts = $account->contacts;
801  
802 my $contact = $acc_contacts->new_record ({
803 proto => 'email', address => 'apla@localhost'
804 });
805 $contact->save;
806 $acc_contacts->count;
807  
808 1;
809  
810  
811  
812 =head1 AUTHOR
813  
814 Ivan Baktsheev, C<< <apla at the-singlers.us> >>
815  
816 =head1 BUGS
817  
818 Please report any bugs or feature requests to my email address,
819 or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBI-Easy>.
820 I will be notified, and then you'll automatically be notified
821 of progress on your bug as I make changes.
822  
823 =head1 SUPPORT
824  
825  
826  
827 =head1 ACKNOWLEDGEMENTS
828  
829  
830  
831 =head1 COPYRIGHT & LICENSE
832  
833 Copyright 2008-2009 Ivan Baktsheev
834  
835 This program is free software; you can redistribute it and/or modify it
836 under the same terms as Perl itself.
837  
838 =cut
839  
« Easy/SQL.pm ↑ Summary Easy/Helper.pm »