Declarative/Schema.pm

filter: Statementstmt 61.2% 248 / 405 Branchbran 29.3% 34 / 116 Conditioncond 20.9% 22 / 105 MC/DCmcdc 1.9% 1 / 52 Subroutinesub 40.4% 19 / 47 Podpod 66.6% 2 / 3 totaltotal 44.7% 326 / 728 CC 99 SCAR 41.8
CC
99
Cov
49%
__check_constraints
182.0
join
117.0
__ANON__
90.0
CRAP 65.3
« Declarative/Row.pm ↑ Summary Declarative.pm »
1 package DBIx::ORM::Declarative::Schema;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 @ISA = qw(DBIx::ORM::Declarative);
8  
9 # Check a create action against constraints, return a data structure suitable
10 # for use in constructing the required INSERT statement
11 sub __check_constraints
12 0 ▶{
Subroutine __check_constraints: not called CC=13 SCAR=52.0
13 0 my ($self, $tab_obj, %params) = @_;
14 0 ▶ carp "This method requires an object" and return unless ref $tab_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tab_obj 0 0 0
Condition0%0/5
ref $tab_obj || carp('This method requires an object') && (return)
l!l&&r!l&&!r
000
carp("This method requires an object") and return
!ll
00
MC/DC0%0/2
ref $tab_obj || carp('This method requires an object') && (return)
ref $tab_obj carp("This method requires an object")
Truth table0%0/3
A || B
A= ref $tab_objB= carp("This method requires an object")
ABresult
1X1
011
000
15 0 my %rv = ();
16  
17 # Create a map of constraints by name
18 0 my %cons = map { @{$_}{qw(name constraint)} } $tab_obj->_columns;
19  
20 # Primary keys
21 0 my @pk = $tab_obj->_primary_key;
22  
23 0 my @keys = ();
24 0 my @vals = ();
25 0 my @binds = ();
26 0 my $npk = 0;
27 0 ▶ if(@pk)
Branch: 1 branch
BranchTrueFalseTotal
if (@pk) 0 0 0
28 {
29 0 for my $k (@pk)
30 {
31 0 my $v = delete $params{$k};
32 0 ▶ if(defined $v)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) { } 0 0 0
33 {
34 0 ▶ if(not $self->apply_method($cons{$k}, 0, $v))
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->apply_method($cons{$k}, 0, $v)) 0 0 0
35 {
36 0 carp "column $k constraint failed";
37 0 return;
38 }
39 0 push @binds, $v;
40 0 push @vals, '?';
41 0 push @keys, $k;
42 }
43 else
44 {
45 0 $npk = 1;
46 0 my $fnp = $tab_obj->_for_null_primary;
47 0 ▶ if($fnp)
Branch: 1 branch
BranchTrueFalseTotal
if ($fnp) 0 0 0
48 {
49 0 push @vals, $fnp;
50 0 push @keys, $k;
51 }
52 }
53 }
54 }
55
56 # Non-primary keys
57 0 for my $k (map { $_->{name} } $tab_obj->_columns)
58 {
59 0 ▶ next if grep { $_ eq $k } @pk;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $k;} @pk 0 0 0
60 0 my $v = $params{$k};
61 0 ▶ if(not $self->apply_method($cons{$k}, 0, $v))
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->apply_method($cons{$k}, 0, $v)) 0 0 0
62 {
63 0 carp "column $k constraint failed";
64 0 return;
65 }
66  
67 # We only need to save the key if it was presented in the parameters
68 0 ▶ if(exists $params{$k})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{$k}) 0 0 0
69 {
70 0 push @keys, $k;
71 0 ▶ if(defined $v)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) { } 0 0 0
72 {
73 0 push @binds, $v;
74 0 push @vals, '?';
75 }
76 else
77 {
78 0 push @vals, 'NULL';
79 }
80 }
81 }
82  
83 # Generate the columns list
84 0 my %n2s = $tab_obj->_column_map;
85 0 my $kstr = '' . join(',', map { $n2s{$_} } @keys);
86  
87 # The values string
88 0 my $vstr = '' . join(',', @vals);
89  
90 # We return a 1 first, because it's conceivable that everything else
91 # is empty
92 0 return (1, $kstr, $vstr, $npk, @binds);
93 }
94  
95 # Make a SQL-safe alias from a table's name or alias
96 sub __make_sql_safe
97 2 ▶{
Subroutine __make_sql_safe: called CC=2 SCAR=7.4
98 2 my ($self, $str) = @_;
99 2 $str =~ s/\W/_/g;
100 2 ▶ if($str =~ /^[^a-zA-Z_]/)
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^[^a-zA-Z_]/) 0 2 2
101 {
102 0 $str = "_$str";
103 }
104 2 $str;
105 }
106  
107 # Create an alias for a table
108 sub alias
109 0 ▶{
Subroutine alias: not called CC=5 SCAR=32.5
Pod undocumented
110 0 my ($self, @args) = @_;
111 0 ▶ if(@args<2)
Branch: 1 branch
BranchTrueFalseTotal
if (@args < 2) 0 0 0
112 {
113 0 ▶ if(@args==1)
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1) 0 0 0
114 {
115 0 return $self->table(@args);
116 }
117 0 my $alias;
118 0 eval { $alias = $self->_alias; };
119 0 return $alias;
120 }
121  
122 # Create/install a new alias
123 0 my ($alias, $table) = @args;
124 0 ▶ my $schema_class = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
125 0 my $alias_class = $schema_class . "::$alias";
126 0 my $table_class = $schema_class . "::$table";
127  
128 # Set it up
129 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
130 0 ▶ if(not @{$alias_class . '::ISA'})
Branch: 1 branch
BranchTrueFalseTotal
if (not @{$alias_class . '::ISA';}) 0 0 0
131 {
132 0 @{$alias_class . '::ISA'} = ($table_class);
133 0 ▶ *{$alias_class . '::_class'} = sub { $alias_class; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 0 ▶ *{$alias_class . '::_table'} = sub { $alias; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 0 ▶ *{$alias_class . '::_alias'} = sub { $table; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
136 0 my $cons = *{$alias_class} = sub
137 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
138 0 my ($self) = @_;
139 0 my $rv = $self->new;
140 0 ▶ bless $rv, $alias_class unless $rv->isa($alias_class);
Branch: 1 branch
BranchTrueFalseTotal
unless $rv->isa($alias_class) 0 0 0
141 0 return $rv;
142 0 } ;
143  
144 # Make sure row objects promote to alias objects, NOT table objects
145 0 my $row_class = $alias_class . '::Rows';
146 0 @{$row_class . '::ISA'} = ($self->ROW_CLASS, $alias_class);
147 0 ▶ *{$alias_class . '::_row_class'} = sub { $row_class; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
148  
149 # Install into the table methods hash
150 0 $self->table_method($alias, $cons);
151 }
152 }
153  
154 # Get the current table name, or switch to a new table, or create a new
155 # table class
156 sub table
157 5 ▶{
Subroutine table: called CC=32 SCAR=40.5
Pod documented
158 5 my ($self, @args) = @_;
159 5 ▶ if(@args<2)
Branch: 1 branch
BranchTrueFalseTotal
if (@args < 2) 2 3 5
160 {
161 2 ▶ if(@args==1)
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1) 2 0 2
162 {
163 2 my $table = shift @args;
164 2 ▶ return $self unless $table;
Branch: 1 branch
BranchTrueFalseTotal
unless $table 0 2 2
165 2 my $meth = $self->table_method($table);
166 2 ▶ return unless $meth;
Branch: 1 branch
BranchTrueFalseTotal
unless $meth 0 2 2
167 2 return $meth->($self);
168 }
169 0 my $table;
170 0 eval { $table = $self->_table; };
171 0 return $table;
172 }
173  
174 # Get the table's name
175 3 my %args = @args;
176 3 my $table = delete $args{table};
177 3 ▶ carp "missing table argument" and return unless $table;
Branch: 1 branch
BranchTrueFalseTotal
unless $table 0 3 3
Condition25%1/4
$table || carp('missing table argument') && (return)
l!l
30
carp("missing table argument") and return
!ll
00
MC/DC0%0/2
$table || carp('missing table argument') && (return)
$table carp("missing table argument")
Truth table33%1/3
A || B
A= $tableB= carp("missing table argument")
ABresult
1X1
011
000
178 3 ▶ my $name = delete $args{alias} || $table;
Condition33%1/3
delete $args{'alias'} || $table
l!l&&r!l&&!r
030
MC/DC0%0/2
delete $args{'alias'} || $table
delete $args{'alias'} $table
Truth table33%1/3
A || B
A= delete $args{'alias'}B= $table
ABresult
1X1
011
000
179  
180 # Column definitions
181 3 my $primary = delete $args{primary};
182 3 my $unique = delete $args{unique};
183 3 my $columns = delete $args{columns};
184 3 ▶ carp "missing column definitions" and return unless
Branch: 1 branch
BranchTrueFalseTotal
unless $primary or $unique or $columns 0 3 3
Condition50%5/10
$primary || $unique || $columns || carp('missing column definitions') && (return)
l!l
30
$primary or $unique or $columns
l!l&&r!l&&!r
210
$primary or $unique
l!l&&r!l&&!r
201
carp("missing column definitions") and return
!ll
00
MC/DC0%0/4
$primary || $unique || $columns || carp('missing column definitions') && (return)
$primary $unique $columns carp("missing column definitions")
Truth table40%2/5
A or B or C || D
A= $primaryB= $uniqueC= $columnsD= carp("missing column definitions")
ABCDresult
1XXX1
01XX1
001X1
00011
00000
185 $primary or $unique or $columns;
186  
187 # Other miscellany
188 3 my $onpnull = delete $args{for_null_primary};
189 3 my $selonpnull = delete $args{select_null_primary};
190 3 my $join = delete $args{join_clause};
191 3 my $group_by = delete $args{group_by};
192  
193 # class and schema names
194 3 my $super = $self->_schema_class;
195 3 my $table_class = $super . "::$name";
196 3 my $row_class = $table_class . "::Rows";
197 3 my $schema = $self->_schema;
198
199 # Set up the class heirarchy
200 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
201 3 @{$table_class . '::ISA'} = ($super, $self->TABLE_CLASS);
202 3 @{$row_class . '::ISA'} = ($self->ROW_CLASS, $table_class);
203  
204 # Information methods
205 3 ▶ *{$table_class . '::_class'} = sub { $table_class; };
Subroutine __ANON__: called CC=1 SCAR=0.0
206 0 ▶ *{$table_class . '::_row_class'} = sub { $row_class; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
207 3 ▶ *{$table_class . '::_table'} = sub { $name; };
Subroutine __ANON__: called CC=1 SCAR=0.0
208 3 ▶ *{$table_class . '::_sql_name'} = sub { $table; };
Subroutine __ANON__: called CC=1 SCAR=0.0
209 0 ▶ *{$table_class . '::_for_null_primary'} = sub { $onpnull; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
210 0 ▶ *{$table_class . '::_select_null_primary'} = sub { $selonpnull; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
211 0 ▶ *{$table_class . '::_join_clause'} = sub { $join; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
212  
213 # handle GROUP BY
214 3 ▶ if($group_by)
Branch: 1 branch
BranchTrueFalseTotal
if ($group_by) { } 0 3 3
215 {
216 0 my @p = @$group_by;
217 0 ▶ *{$table_class . '::_group_by' } = sub { @p; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
218 }
219 else
220 {
221 3 ▶ *{$table_class . '::_group_by' } = sub { };
Subroutine __ANON__: called CC=1 SCAR=0.0
222 }
223  
224 # The table object constructor
225 my $cons = sub
226 7 ▶ {
Subroutine __ANON__: called CC=2 SCAR=7.0
227 7 my ($self) = @_;
228 7 my $rv = $self->new;
229 7 ▶ bless $rv, $table_class unless $rv->isa($table_class);
Branch: 1 branch
BranchTrueFalseTotal
unless $rv->isa($table_class) 7 0 7
230 7 return $rv;
231 3 } ;
232  
233 3 *{$table_class} = $cons;
234  
235 3 $self->table_method($name, $cons);
236  
237 # Handle column information
238 3 my %seen_keys;
239 my @newcolumns;
240 3 my @p;
241  
242 # The primary keys
243 3 ▶ @p = @$primary if $primary;
Branch: 1 branch
BranchTrueFalseTotal
if $primary 2 1 3
244 3 ▶ *{$table_class . '::_primary_key'} = sub { @p; };
Subroutine __ANON__: called CC=1 SCAR=0.0
245  
246 # Just in case the primary keys aren't formally defined elsewhere...
247 $seen_keys{$_} =
248 3 { sql_name => $_, name => $_, constraint => 'isstring' } foreach @p;
249 3 my %pk = map {($_,1);} @p;
250 3 @newcolumns = @p;
251  
252 # Process unique keys
253 3 my @uniqs;
254 3 ▶ push @uniqs, [@p] if @p;
Branch: 1 branch
BranchTrueFalseTotal
if @p 2 1 3
255 # This is not strictly needed, since the loop will autovivify
256 # $unique to contain an empty array ref if it's undefined at this
257 # point. The loop provides the lvalue context to make this work.
258 3 ▶ $unique ||= [ ];
Condition100%2/2
$unique ||= []
l!l
12
MC/DC100%1/1
$unique ||= []
$unique
Truth table100%2/2
A
A= $unique
Aresult
11
00
259 3 for my $un (@$unique)
260 {
261 # Check to see if they've duplicated the primary key
262 1 my %kv = map {($_,1)} @$un;
263 1 delete @kv{@p};
264 1 ▶ next if not %kv and scalar(@p) == scalar(@$un);
Branch: 1 branch
BranchTrueFalseTotal
if not %kv and scalar @p == scalar @$un 0 1 1
Condition33%1/3
not %kv and scalar @p == scalar @$un
!ll&&!rl&&r
100
MC/DC0%0/2
not %kv and scalar @p == scalar @$un
not %kv scalar @p == scalar @$un
Truth table33%1/3
not(A) and B
A= not %kvB= scalar @p == scalar @$un
ABresult
0X0
100
111
265  
266 # Create a copy so they can't change things out from under us
267 1 push @uniqs, [ @$un ];
268  
269 # Add the keys to the %seen_keys hash
270 1 for my $k (@$un)
271 {
272 2 ▶ next if $seen_keys{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $seen_keys{$k} 0 2 2
273 2 $seen_keys{$k} = { sql_name => $k, name => $k,
274 constraint => 'isnullablestring' };
275 2 push @newcolumns, $k;
276 }
277 }
278  
279 # Get the unique keys data
280 # For stability, this _should_ be a Readonly variable
281 # Unfortunately, Readonly is really slow on older Perls
282 0 ▶ *{$table_class . '::_unique_keys' } = sub { @uniqs; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
283  
284 # The rest of the column definitions
285 3 my @coldefs;
286 my %colmap;
287 3 for my $col (@$columns)
288 {
289 # Copy the column definition, hack-n-slash at will...
290 11 my %cdef = %$col;
291  
292 # Column names
293 11 my $sql_name = delete $cdef{name};
294 11 ▶ my $name = delete $cdef{alias} || $sql_name;
Condition33%1/3
delete $cdef{'alias'} || $sql_name
l!l&&r!l&&!r
0110
MC/DC0%0/2
delete $cdef{'alias'} || $sql_name
delete $cdef{'alias'} $sql_name
Truth table33%1/3
A || B
A= delete $cdef{'alias'}B= $sql_name
ABresult
1X1
011
000
295 11 $colmap{$name} = $sql_name;
296 11 delete $seen_keys{$sql_name};
297  
298 # Handle constraints and type matching
299 11 my $constraint = delete $cdef{constraint};
300 11 my $match = delete $cdef{matches};
301 11 my $type = delete $cdef{type};
302 11 ▶ if (not $constraint)
Branch: 1 branch
BranchTrueFalseTotal
unless ($constraint) 11 0 11
303 {
304 # The default constraint is "match EVERYTHING"
305 11 $constraint = 'isnullablestring';
306  
307 # If we have a regular expression, use it
308 11 ▶ if($match)
Branch: 2 branches
BranchTrueFalseTotal
if ($match) { } 0 11 11
elsif ($type) { } 0 11 11
309 {
310 $constraint = sub
311 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
312 0 my ($self, $value) = @_;
313 0 $value =~ /$match/;
314 0 };
315 }
316  
317 # Or if we have a type, use that
318 elsif($type)
319 {
320 # We check for every type except nullablestring,
321 # because we already set that as the default
322 0 ▶ if($type eq 'number') { $constraint = 'isnumber'; }
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'number') { } 0 0 0
elsif ($type eq 'string') { } 0 0 0
elsif ($type eq 'nullablenumber') { } 0 0 0
323 0 elsif($type eq 'string') { $constraint = 'isstring'; }
324 elsif($type eq 'nullablenumber')
325 {
326 0 $constraint = 'isnullablenumber';
327 }
328 }
329 }
330  
331 # Save the column definition
332 11 push @coldefs,
333 {
334 sql_name => $sql_name,
335 name => $name,
336 constraint => $constraint,
337 column_name => $sql_name,
338 };
339  
340 # Create the column method
341 11 *{$row_class . "::$name"} = $self->__create_column_accessor(
342 11 $sql_name, $pk{$sql_name});
343 }
344  
345 # Add columns for missing primary/unique key components
346 3 for my $col (@newcolumns)
347 {
348 4 my $def = delete $seen_keys{$col};
349 4 ▶ next unless $def;
Branch: 1 branch
BranchTrueFalseTotal
unless $def 4 0 4
350 0 push @coldefs, $def;
351 0 $colmap{$col} = $col;
352 0 *{$row_class . "::$col"} = $self->__create_column_accessor(
353 0 $col, $pk{$col});
354 }
355  
356 # Save the column and mapping information
357 3 ▶ *{$table_class . '::_columns' } = sub { @coldefs; } ;
Subroutine __ANON__: called CC=1 SCAR=0.0
358 0 ▶ *{$table_class . '::_column_map' } = sub { %colmap; } ;
Subroutine __ANON__: not called CC=1 SCAR=0.4
359 3 my @sql_cols = sort values %colmap;
360 0 ▶ *{$table_class . '::_column_sql_names' } = sub { @sql_cols; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
361  
362 3 return &{$table_class}($self);
363 }
364  
365 # Create a new join, or return the name of this join object
366 sub join
367 1 ▶{
Subroutine join: called CC=35 SCAR=47.6
Pod documented
368 1 my ($self, @args) = @_;
369 1 ▶ if(@args<2)
Branch: 1 branch
BranchTrueFalseTotal
if (@args < 2) 0 1 1
370 {
371 # Turn this into a join object, if requested
372 0 ▶ if(@args==1)
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1) 0 0 0
373 {
374 0 my $join = shift @args;
375 0 ▶ return $self unless $join;
Branch: 1 branch
BranchTrueFalseTotal
unless $join 0 0 0
376 0 my $meth = $self->join_method($join);
377 0 ▶ return unless $meth;
Branch: 1 branch
BranchTrueFalseTotal
unless $meth 0 0 0
378 0 return $meth->($self);
379 }
380  
381 0 my $join;
382 0 eval { $join = $self->_join; };
383 0 return $join;
384 }
385  
386 # If we get to here, we're adding a new join declaration.
387 1 my %args = @args;
388 1 my $name = delete $args{name};
389 1 ▶ carp "duplicate table/join declaration" and return if $self->can($name);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($name) 0 1 1
Condition25%1/4
$self->can($name) && (carp('duplicate table/join declaration') && (return))
!ll
10
carp("duplicate table/join declaration") and return
!ll
00
MC/DC0%0/2
$self->can($name) && (carp('duplicate table/join declaration') && (return))
$self->can($name) carp("duplicate table/join declaration")
Truth table33%1/3
A && B
A= $self->can($name)B= carp("duplicate table/join declaration")
ABresult
0X0
100
111
390  
391 # Class family names
392 1 my $super = $self->_schema_class;
393 1 my $join_class = $super . "::$name";
394 1 my $row_class = $join_class . '::Rows';
395 1 my $schema = $self->_schema;
396  
397 1 my $ptab = delete $args{primary};
398 1 ▶ carp "missing join name" and return unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 1 1
Condition25%1/4
$name || carp('missing join name') && (return)
l!l
10
carp("missing join name") and return
!ll
00
MC/DC0%0/2
$name || carp('missing join name') && (return)
$name carp("missing join name")
Truth table33%1/3
A || B
A= $nameB= carp("missing join name")
ABresult
1X1
011
000
399  
400 1 my $tables = delete $args{tables};
401 1 ▶ carp "missing table(s) to join" and return unless $ptab and $tables;
Branch: 1 branch
BranchTrueFalseTotal
unless $ptab and $tables 0 1 1
Condition28%2/7
$ptab && $tables || carp('missing table(s) to join') && (return)
l!l
10
$ptab and $tables
!ll&&!rl&&r
001
carp("missing table(s) to join") and return
!ll
00
MC/DC0%0/3
$ptab && $tables || carp('missing table(s) to join') && (return)
$ptab $tables carp("missing table(s) to join")
Truth table20%1/5
A and B || C
A= $ptabB= $tablesC= carp("missing table(s) to join")
ABCresult
11X1
0X11
1011
0X00
1000
402  
403 # Look for the table class(es) we need
404 1 my @req_tabs = ($ptab, map { $_->{table} } @$tables);
405  
406 carp "missing required tables" and return
407 1 ▶ if grep { not $self->can($_); } @req_tabs;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $self->can($_);} @req_tabs 0 1 1
Condition25%1/4
grep({not $self->can($_);} @req_tabs) && (carp('missing required tables') && (return))
!ll
10
carp("missing required tables") and return
!ll
00
MC/DC0%0/2
grep({not $self->can($_);} @req_tabs) && (carp('missing required tables') && (return))
grep({not $self->can($_);} @req_tabs) carp("missing required tables")
Truth table33%1/3
A && B
A= grep({not $self->can($_);} @req_tabs)B= carp("missing required tables")
ABresult
0X0
100
111
408  
409 # Create a primary table object
410 1 my $ptab_obj = $self->table($ptab);
411 1 ▶ carp "No such table '$ptab'" and return unless $ptab_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $ptab_obj 0 1 1
Condition25%1/4
$ptab_obj || carp("No such table '${ptab}'") && (return)
l!l
10
carp("No such table '${ptab}'") and return
!ll
00
MC/DC0%0/2
$ptab_obj || carp("No such table '${ptab}'") && (return)
$ptab_obj carp("No such table '${ptab}'")
Truth table33%1/3
A || B
A= $ptab_objB= carp("No such table '${ptab}'")
ABresult
1X1
011
000
412  
413 # Info to create the join
414 1 my $ptab_name = $ptab_obj->_table;
415 1 my $ptab_alias = $self->__make_sql_safe($ptab_name);
416  
417 # Primary table's columns
418 1 my @ptab_cols = map { $_->{name} } $ptab_obj->_columns;
419  
420 # Will be turned into _sql_name
421 1 my @join_table_info = ($ptab_obj->_sql_name . " $ptab_alias");
422  
423 # Will be turned into _columns
424 my @column_info =
425 map
426 {
427 1 (
428 {
429 sql_name => "$ptab_alias." . $_->{sql_name},
430 name => $_->{name},
431 constraint => $_->{constraint},
432 table => $ptab_name,
433 table_alias => $ptab_alias,
434 column_name => $_->{sql_name},
435 },
436 {
437 sql_name => "$ptab_alias." . $_->{sql_name},
438 name => $ptab_name . '_' . $_->{name},
439 constraint => $_->{constraint},
440 table => $ptab_name,
441 table_alias => $ptab_alias,
442 column_name => $_->{sql_name},
443 },
444 )
445 5 }
446 $ptab_obj->_columns;
447  
448 # Will be turned into _column_map
449 my %column_map =
450 1 map { @{$_}{qw(name sql_name)} } @column_info;
451  
452 # The "where" clause info
453 1 my @wherefrags;
454  
455 # The "group by" clause info
456 0 my @group_by = map { $column_map{$_} } $ptab_obj->_group_by;
457  
458 # Primary table's primary keys
459 1 my %pkeys = map {($_ => 1, $ptab_name . "_$_" => 1)}
460 $ptab_obj->_primary_key;
461  
462 1 my @tables_seen;
463  
464 # Need to clone table info so it doesn't get changed out from under us
465 my @tab_info;
466 1 for my $tab (@$tables)
467 {
468 1 my $tab_name = $tab->{table};
469 1 my $tab_obj = $self->table($tab_name);
470  
471 # No sense doing all the work if the table doesn't exist...
472 1 ▶ carp "No such table '$tab_name'" and return unless $tab_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $tab_obj 0 1 1
Condition25%1/4
$tab_obj || carp("No such table '${tab_name}'") && (return)
l!l
10
carp("No such table '${tab_name}'") and return
!ll
00
MC/DC0%0/2
$tab_obj || carp("No such table '${tab_name}'") && (return)
$tab_obj carp("No such table '${tab_name}'")
Truth table33%1/3
A || B
A= $tab_objB= carp("No such table '${tab_name}'")
ABresult
1X1
011
000
473 1 my $info_ref = { table => $tab_name };
474  
475 # Support secondary table joins
476 1 my @use_cols = @ptab_cols;
477 1 my $usetab_name = $ptab_name;
478 1 my $usetab_alias = $ptab_alias;
479 1 my $secondary = $tab->{on_secondary};
480 1 ▶ if($secondary)
Branch: 1 branch
BranchTrueFalseTotal
if ($secondary) 0 1 1
481 {
482 carp "Secondary table '$secondary' unknown" and return
483 0 ▶ unless grep { $secondary eq $_ } @tables_seen;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$secondary eq $_;} @tables_seen 0 0 0
Condition0%0/5
grep({$secondary eq $_;} @tables_seen) || carp("Secondary table '${secondary}' unknown") && (return)
l!l&&r!l&&!r
000
carp("Secondary table '${secondary}' unknown") and return
!ll
00
MC/DC0%0/2
grep({$secondary eq $_;} @tables_seen) || carp("Secondary table '${secondary}' unknown") && (return)
grep({$secondary eq $_;} @tables_seen) carp("Secondary table '${secondary}' unknown")
Truth table0%0/3
A || B
A= grep({$secondary eq $_;} @tables_seen)B= carp("Secondary table '${secondary}' unknown")
ABresult
1X1
011
000
484 0 my $secondary_obj = $self->table($secondary);
485 0 ▶ carp "No such table '$secondary'" and return unless $secondary_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $secondary_obj 0 0 0
Condition0%0/5
$secondary_obj || carp("No such table '${secondary}'") && (return)
l!l&&r!l&&!r
000
carp("No such table '${secondary}'") and return
!ll
00
MC/DC0%0/2
$secondary_obj || carp("No such table '${secondary}'") && (return)
$secondary_obj carp("No such table '${secondary}'")
Truth table0%0/3
A || B
A= $secondary_objB= carp("No such table '${secondary}'")
ABresult
1X1
011
000
486 0 $info_ref->{on_secondary} = $secondary;
487 0 @use_cols = map { $_->{name} } $secondary_obj->_columns;
488 0 $usetab_name = $secondary;
489 0 $usetab_alias = $self->__make_sql_safe($usetab_name);
490 }
491  
492 1 push @tables_seen, $tab_name;
493  
494 1 my $tab_alias = $self->__make_sql_safe($tab_obj->_table);
495  
496 1 my %join_info = %{$tab->{columns}};
497 1 my @tab_cols = $tab_obj->_columns;
498 1 for my $k (keys %join_info)
499 {
500 carp "No such key '$k' on primary table '$usetab_name'" and return
501 1 ▶ unless grep { $k eq $_ } @use_cols;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$k eq $_;} @use_cols 0 1 1
Condition25%1/4
grep({$k eq $_;} @use_cols) || carp("No such key '${k}' on primary table '${usetab_name}'") && (return)
l!l
10
carp("No such key '${k}' on primary table '${usetab_name}'") and return
!ll
00
MC/DC0%0/2
grep({$k eq $_;} @use_cols) || carp("No such key '${k}' on primary table '${usetab_name}'") && (return)
grep({$k eq $_;} @use_cols) carp("No such key '${k}' on primary table '${usetab_name}'")
Truth table33%1/3
A || B
A= grep({$k eq $_;} @use_cols)B= carp("No such key '${k}' on primary table '${usetab_name}'")
ABresult
1X1
011
000
502 carp "No such key '$k' on secondary table '$tab_name'" and return
503 1 ▶ unless grep { $join_info{$k} eq $_->{name} } @tab_cols;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$join_info{$k} eq $_->{"name"};} @tab_cols 0 1 1
Condition25%1/4
grep({$join_info{$k} eq $_->{'name'};} @tab_cols) || carp("No such key '${k}' on secondary table '${tab_name}'") && (return)
l!l
10
carp("No such key '${k}' on secondary table '${tab_name}'") and return
!ll
00
MC/DC0%0/2
grep({$join_info{$k} eq $_->{'name'};} @tab_cols) || carp("No such key '${k}' on secondary table '${tab_name}'") && (return)
grep({$join_info{$k} eq $_->{'name'};} @tab_cols) carp("No such key '${k}' on secondary table '${tab_name}'")
Truth table33%1/3
A || B
A= grep({$join_info{$k} eq $_->{'name'};} @tab_cols)B= carp("No such key '${k}' on secondary table '${tab_name}'")
ABresult
1X1
011
000
504  
505 1 $info_ref->{columns}{$k} = $join_info{$k};
506  
507 # We set the join keys as primary so they don't get changed on us
508 1 $pkeys{$k} = $pkeys{$join_info{$k}} = 1;
509  
510 # Save the "where" clause info
511 1 push @wherefrags, "$usetab_alias.$k = $tab_alias.$join_info{$k}";
512 }
513 # Save the copy
514 1 push @tab_info, $info_ref;
515  
516 # Save table join information
517 1 push @join_table_info, $tab_obj->_sql_name . " $tab_alias";
518  
519 # Save group by information
520 0 my %tab_group_by = map { ($_,1) } $tab_obj->_group_by;
521  
522 # Housekeeping information
523 1 for my $col ($tab_obj->_columns)
524 {
525 5 my $column_ref = "$tab_alias." . $col->{sql_name};
526 # Save the column information
527 push @column_info,
528 {
529 sql_name => $column_ref,
530 name => $col->{name},
531 constraint => $col->{constraint},
532 table => $tab_name,
533 table_alias => $tab_alias,
534 column_name => $_->{sql_name},
535 },
536 {
537 sql_name => $column_ref,
538 name => $tab_name . '_' .$col->{name},
539 constraint => $col->{constraint},
540 table => $tab_name,
541 table_alias => $tab_alias,
542 column_name => $_->{sql_name},
543 5 };
544  
545 # Save column mapping info
546 5 ▶ $column_map{$col->{name}} ||= $column_ref;
Condition33%1/3
$column_map{$col->{"name"}} ||= $column_ref
l!l&&r!l&&!r
050
MC/DC0%0/2
$column_map{$col->{"name"}} ||= $column_ref
$column_map{$col->{"name"}} $column_ref
Truth table33%1/3
A ||= B
A= $column_map{$col->{"name"}}B= $column_ref
ABresult
1X1
011
000
547 5 ▶ $column_map{$tab_name . '_' . $col->{name}} ||= $column_ref;
Condition33%1/3
$column_map{$tab_name . '_' . $col->{"name"}} ||= $column_ref
l!l&&r!l&&!r
050
MC/DC0%0/2
$column_map{$tab_name . '_' . $col->{"name"}} ||= $column_ref
$column_map{$tab_name . '_' . $col->{"name"}} $column_ref
Truth table33%1/3
A ||= B
A= $column_map{$tab_name . '_' . $col->{"name"}}B= $column_ref
ABresult
1X1
011
000
548  
549 5 ▶ push @group_by, $column_ref if $tab_group_by{$col->{name}};
Branch: 1 branch
BranchTrueFalseTotal
if $tab_group_by{$col->{"name"}} 0 5 5
550 }
551  
552 # Keep track of housekeeping information
553 $pkeys{$tab_name . "_$_"} = $pkeys{$_} = 1
554 1 foreach $tab_obj->_primary_key;
555 }
556  
557 # We're constructing refs, so turn off strictness
558 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
559
560 # Class heirarchy
561 1 @{$join_class . '::ISA'} = ($super, $self->JOIN_CLASS);
562 1 @{$row_class . '::ISA'} = ($self->JROW_CLASS, $join_class);
563  
564 # Information methods
565 1 ▶ *{$join_class . '::_class'} = sub { $join_class; };
Subroutine __ANON__: called CC=1 SCAR=0.0
566 0 ▶ *{$join_class . '::_row_class'} = sub { $row_class; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
567 0 ▶ *{$join_class . '::_join'} = sub { $name; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
568 0 ▶ *{$join_class . '::_primary'} = sub { $ptab; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
569  
570 # The stuff to make searching for joins work...
571 1 my $join_tabs = join(', ', @join_table_info);
572 0 ▶ *{$join_class . '::_sql_name'} = sub { $join_tabs; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
573  
574 1 my $where_prefix = join(' AND ', @wherefrags);
575 0 ▶ *{$join_class . '::_where_prefix'} = sub { $where_prefix; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
576 0 ▶ *{$join_class . '::_columns'} = sub { @column_info; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
577 0 ▶ *{$join_class . '::_column_map'} = sub { %column_map; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
578 0 ▶ *{$join_class . '::_group_by'} = sub { @group_by; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
579
580 # We need to get the list of columns...
581 1 my %h = reverse %column_map;
582 1 my @sql_cols = sort keys %h;
583 0 ▶ *{$join_class . '::_column_sql_names' } = sub { @sql_cols; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
584  
585 my $cons = sub
586 3 ▶ {
Subroutine __ANON__: called CC=2 SCAR=7.0
587 3 my ($self) = @_;
588 3 my $rv = $self->new;
589 3 ▶ bless $rv, $join_class unless $rv->isa($join_class);
Branch: 1 branch
BranchTrueFalseTotal
unless $rv->isa($join_class) 3 0 3
590 3 return $rv;
591 1 } ;
592  
593 1 *{$join_class} = $cons;
594  
595 1 $self->join_method($name, $cons);
596
597 0 ▶ *{$join_class . '::_join_info'} = sub { @tab_info; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
598  
599 # Create the accessors
600 20 *{$row_class . "::$_" } =
601 $self->__create_column_accessor($column_map{$_}, $pkeys{$_})
602 1 foreach keys %column_map;
603  
604 # Return success
605 1 return &{$join_class}($self);
606 }
607  
608 # Create a method to access column data
609 sub __create_column_accessor
610 31 ▶{
Subroutine __create_column_accessor: called CC=2 SCAR=15.8
611 31 my ($self, $name, $pk_flag) = @_;
612 31 ▶ if($pk_flag)
Branch: 1 branch
BranchTrueFalseTotal
if ($pk_flag) 8 23 31
613 {
614 # This is a primary key, so it's read-only
615 return sub
616 0 ▶ {
Subroutine __ANON__: not called CC=5 SCAR=34.0
617 0 my $self = shift;
618 0 ▶ carp "$name is not a class method" and return unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
Condition0%0/5
ref $self || carp("$name is not a class method") && (return)
l!l&&r!l&&!r
000
carp("$name is not a class method") and return
!ll
00
MC/DC0%0/2
ref $self || carp("$name is not a class method") && (return)
ref $self carp("$name is not a class method")
Truth table0%0/3
A || B
A= ref $selfB= carp("$name is not a class method")
ABresult
1X1
011
000
619 0 ▶ carp "$name is part of the primary key" and return $self if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
Condition0%0/5
@_ && (carp("$name is part of the primary key") && (return $self))
!ll&&!rl&&r
000
carp("$name is part of the primary key") and return $self
!ll
00
MC/DC0%0/2
@_ && (carp("$name is part of the primary key") && (return $self))
@_ carp("$name is part of the primary key")
Truth table0%0/3
A && B
A= @_B= carp("$name is part of the primary key")
ABresult
0X0
100
111
620 0 return $self->{__data}{$name};
621 8 };
622 }
623  
624 # If we get to here, it's not a primary key, so we can beat on it
625 return sub
626 0 ▶ {
Subroutine __ANON__: not called CC=9 SCAR=45.0
627 0 my $self = shift;
628 0 ▶ carp "$name is not a class method" and return unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
Condition0%0/5
ref $self || carp("$name is not a class method") && (return)
l!l&&r!l&&!r
000
carp("$name is not a class method") and return
!ll
00
MC/DC0%0/2
ref $self || carp("$name is not a class method") && (return)
ref $self carp("$name is not a class method")
Truth table0%0/3
A || B
A= ref $selfB= carp("$name is not a class method")
ABresult
1X1
011
000
629 0 my $val = $self->{__data}{$name};
630 0 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
631 {
632 0 my $nval = $_[0];
633 # Changing undef to undef is not a change...
634 0 ▶ return $self if not defined $val and not defined $nval;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val or defined $nval 0 0 0
Condition0%0/3
defined $val or defined $nval
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $val or defined $nval
defined $val defined $nval
Truth table0%0/3
A or B
A= defined $valB= defined $nval
ABresult
1X1
011
000
635 0 ▶ if(not defined $val or $nval ne $val)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $val or $nval ne $val) 0 0 0
Condition0%0/3
not defined $val or $nval ne $val
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $val or $nval ne $val
not defined $val $nval ne $val
Truth table0%0/3
not(A) or B
A= not defined $valB= $nval ne $val
ABresult
1X1
011
000
636 {
637 0 delete $self->{__data}{$name};
638 0 ▶ $self->{__data}{$name} = $nval if defined $nval;
Branch: 1 branch
BranchTrueFalseTotal
if defined $nval 0 0 0
639 0 $self->{__dirty} = 1;
640 }
641 0 return $self;
642 }
643 0 return $val;
644 23 };
645 }
646  
647 1 1;
« Declarative/Row.pm ↑ Summary Declarative.pm »