blib/lib/DBIx/QuickORM/DSL.pm

filter: Statementstmt 100% 219 / 219 Branchbran 94.6% 53 / 56 Conditioncond 88.5% 54 / 61 MC/DCmcdc 81.0% 30 / 37 Subroutinesub 100% 39 / 39 Podpod 100% 18 / 18 totaltotal 96.0% 413 / 430 CC 58 SCAR 18.9
CC
58
Cov
97%
import
14.0
_run
12.0
_remove
12.0
CRAP 6.6
« blib/lib/DBIx/QuickORM/WhereBuilder/Simple.pm ↑ Summary blib/lib/DBIx/QuickORM/SQLBuilder/SQLAbstract.pm »
1 package DBIx::QuickORM::DSL;
2 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 19 our $VERSION = '0.000031';
6  
7 19 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 ▶use Importer();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use Role::Tiny();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 ▶use Scalar::Util qw/refaddr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =pod
13  
14 =encoding UTF-8
15  
16 =head1 NAME
17  
18 DBIx::QuickORM::DSL - Blocks for defining an ORM as classes.
19  
20 =head1 DESCRIPTION
21  
22 Three blocks define an ORM as classes: C<orm> in the ORM class, C<table> in a
23 class that adjusts one table, and C<row> in a row class. They only collect
24 arguments for L<DBIx::QuickORM's C<new>|DBIx::QuickORM/"PUBLIC METHODS">. The
25 database stays the only schema, so a table block adjusts what the scan finds
26 and never declares columns. L<DBIx::QuickORM::ORM> turns what they collect
27 into those arguments.
28  
29 C<use DBIx::QuickORM::DSL @tags> imports blocks with their keywords, through
30 L<Importer>. Each tag holds one block and its keywords, and decides what the
31 package is:
32  
33 =over 4
34  
35 =item C<:orm>: the ORM class.
36  
37 =item C<:table>: a table class.
38  
39 =item C<:row>: a row class.
40  
41 =item C<:table> with C<:row>: a table class that is also that table's row class.
42  
43 =back
44  
45 A tag only makes its block available. A package registers when the block
46 runs: a C<table> block records the table spec, and a C<row> block makes a
47 combined package its table's row class. A package that imports C<:row> but
48 never runs a C<row> block gets the default row class.
49  
50 Importer's renaming works, so a keyword can take another name. A renamed
51 keyword is installed only under its new name:
52  
53 use DBIx::QuickORM::DSL ':table', type => {-as => 'col_type'};
54  
55 The import croaks for an empty list, a list that leaves out part of a tag,
56 C<:orm> together with C<:table> or C<:row>, or a package that already has a
57 sub of a name it would install.
58  
59 Keywords work only inside their block. Called anywhere else, they croak.
60  
61 Each block runs when the module's code runs, once per package. When it
62 finishes it removes itself and its keywords from the package, under whatever
63 names they were imported, so none is left behind as a method.
64  
65 C<no DBIx::QuickORM::DSL> (or C<no DBIx::QuickORM::DSL @tags>) removes the
66 names that still hold the DSL's own subs. It may come after the blocks: a
67 block compiled before it still calls its keywords.
68  
69 Perl compiles named subs before any block runs, wherever they are in the file.
70 A sub named like a keyword (C<type>, C<name>) and compiled before that C<no>,
71 or with no C<no>, replaces the keyword, so its block croaks when it runs. To
72 have such a sub, rename the keyword (C<< type => {-as => 'my_type'} >>), move
73 the sub to another package, or put C<no DBIx::QuickORM::DSL> before it.
74  
75 =head1 SYNOPSIS
76  
77 The ORM class, in F<lib/My/ORM.pm>:
78  
79 package My::ORM;
80 use DBIx::QuickORM::DSL ':orm';
81  
82 orm {
83 row_class 'My::ORM::Row';
84 override(omit => {posts => ['views']});
85 };
86  
87 1;
88  
89 A row class for every table, in F<lib/My/ORM/Row.pm>:
90  
91 package My::ORM::Row;
92 use DBIx::QuickORM::DSL ':row';
93  
94 row {
95 parent;
96 };
97  
98 sub label { my $self = shift; $self->table . ' ' . $self->field('id') }
99  
100 1;
101  
102 A table class that is also the table's row class, in F<lib/My/ORM/Users.pm>.
103 Modules directly under F<My/ORM/> are found and loaded:
104  
105 package My::ORM::Users;
106 use DBIx::QuickORM::DSL qw/:table :row/;
107  
108 table {
109 name 'users';
110 omit qw/prefs/;
111 type created => undef;
112 };
113  
114 row {
115 parent 'My::ORM::Row';
116 };
117  
118 sub display_name { $_[0]->field('name') }
119  
120 1;
121  
122 A table class that removes the DSL's names with C<no>, so a row method can
123 take a keyword's name, in F<lib/My/ORM/Posts.pm>:
124  
125 package My::ORM::Posts;
126 use DBIx::QuickORM::DSL qw/:table :row/;
127  
128 table {
129 name 'posts';
130 };
131  
132 row {
133 parent 'My::ORM::Row';
134 };
135  
136 no DBIx::QuickORM::DSL; # every tag, or name them: no DBIx::QuickORM::DSL ':table';
137  
138 sub name { $_[0]->field('title') } # a row method named like the keyword
139  
140 1;
141  
142 Using it:
143  
144 use My::ORM;
145  
146 my $orm = My::ORM->new(
147 credentials => {
148 dsn => $dsn,
149 user => $user,
150 pass => $pass,
151 },
152 );
153  
154 my $bob = $orm->insert(users => {name => 'Bob'});
155 my $name = $bob->display_name; # a method from My::ORM::Users
156  
157 my $post = $orm->insert(
158 posts => {
159 user_id => $bob->field('id'),
160 title => 'Hi',
161 },
162 );
163  
164 my $what = $post->label; # a method from My::ORM::Row
165 my $hi = $post->name; # a method from My::ORM::Posts
166  
167 =cut
168  
169 19 my %KEYWORDS = (
170 orm => [qw/row_class autotype append_autotype retain_cache tables override/],
171 table => [qw/name omit default_fields type fk row_class/],
172 row => [qw/with_role parent/],
173 );
174  
175 19 my %ORM_SPECS; # package => what its orm block collected
176 my %TABLE_SPECS; # package => what its table block collected
177 19 my %IMPORTED; # package => {block => 1} for every block it imported
178 19 my %RAN; # package => {block => 1} for every block that ran
179 19 my %GLOBS; # package => {keyword => {name => \*glob}} for every name the DSL installed
180 19 our $BUILDING; # {block, package, spec} while a block runs
181  
182 19 our %EXPORT_TAGS = map { ($_ => [$_, @{$KEYWORDS{$_}}]) } keys %KEYWORDS;
183 19 our @EXPORT_OK = do {
184 19 my %seen;
185 19 grep { !$seen{$_}++ } map { @$_ } values %EXPORT_TAGS;
186 };
187  
188 19 my %OURS; # keyword => our sub
189 my %NAME_OF; # refaddr of our sub => keyword
190  
191 =pod
192  
193 =head1 EXPORTS
194  
195 Nothing is exported by default. Each tag brings its block and the block's
196 keywords. A keyword croaks outside a block that takes it.
197  
198 =head2 :orm
199  
200 =over 4
201  
202 =item orm { ... }
203  
204 Defines the ORM class: makes the package a subclass of
205 L<DBIx::QuickORM::ORM>, whose C<new> uses what the block collected.
206  
207 =item row_class $class_or_undef
208  
209 =item autotype @types
210  
211 =item append_autotype @types
212  
213 =item retain_cache $bool
214  
215 The L<DBIx::QuickORM's C<new>|DBIx::QuickORM/"PUBLIC METHODS"> arguments of
216 the same names. C<autotype> with no types turns autotyping off.
217  
218 =item tables $namespace
219  
220 =item tables \@classes
221  
222 Where the table classes are, instead of the classes directly under the ORM
223 class's own namespace.
224  
225 =back
226  
227 =cut
228  
229 39 ▶sub orm(&) { _run(orm => scalar(caller), @_) }
Subroutine orm: called CC=1 SCAR=0.0
Pod documented
230  
231 21 ▶sub row_class { _building('row_class')->{spec}->{row_class} = $_[0] }
Subroutine row_class: called CC=1 SCAR=0.0
Pod documented
232 2 ▶sub autotype { _building('autotype')->{spec}->{autotype} = [@_] }
Subroutine autotype: called CC=1 SCAR=0.0
Pod documented
233 1 ▶sub append_autotype { _building('append_autotype')->{spec}->{append_autotype} = [@_] }
Subroutine append_autotype: called CC=1 SCAR=0.0
Pod documented
234 9 ▶sub retain_cache { _building('retain_cache')->{spec}->{retain_cache} = $_[0] }
Subroutine retain_cache: called CC=1 SCAR=0.0
Pod documented
235 19 ▶sub tables { _building('tables')->{spec}->{tables} = $_[0] }
Subroutine tables: called CC=1 SCAR=0.0
Pod documented
236  
237 =pod
238  
239 =over 4
240  
241 =item override(%args)
242  
243 Any other L<DBIx::QuickORM's C<new>|DBIx::QuickORM/"PUBLIC METHODS">
244 arguments. They beat the keywords above, and several calls add up.
245  
246 The connection arguments (C<dbh>, C<connect>, C<credentials>) croak.
247  
248 =back
249  
250 =cut
251  
252 22 ▶sub override {
Subroutine override: called CC=3 SCAR=11.0
Pod documented
253 22 my %args = @_;
254  
255 22 my @conn = sort grep { exists $args{$_} } qw/dbh connect credentials/;
256 22 ▶ croak "override() cannot take the connection argument(s): " . join(', ' => @conn) if @conn;
Branch: 1 branch
BranchTrueFalseTotal
if @conn 1 21 22
257  
258 21 my $spec = _building('override')->{spec};
259 21 ▶ $spec->{override} = {%{$spec->{override} // {}}, %args};
Condition100%2/2
$spec->{"override"} // {}
l!l
120
MC/DC100%1/1
$spec->{"override"} // {}
$spec->{"override"}
Truth table100%2/2
A
A= $spec->{"override"}
Aresult
11
00
260 }
261  
262 =pod
263  
264 =head2 :table
265  
266 =over 4
267  
268 =item table { ... }
269  
270 Adjusts one table. Croaks without C<name>, or with both C<omit> and
271 C<default_fields>.
272  
273 In a package whose C<row> block has run (before or after this block), the
274 table's row class is that package. Importing C<:row> alone does not make it
275 one.
276  
277 =item name $table
278  
279 The table, as the database names it. Required: package names never decide
280 anything.
281  
282 =item omit @fields
283  
284 =item default_fields @fields
285  
286 The table's entry in the L<DBIx::QuickORM's C<new>|DBIx::QuickORM/"PUBLIC METHODS">
287 argument of the same name. Several calls add up.
288  
289 =item row_class $class_or_undef
290  
291 The table's entry in C<row_classes>. In a package that also runs a C<row>
292 block it may only name that package. Otherwise that block croaks.
293  
294 =back
295  
296 =cut
297  
298 32 ▶sub table(&) { _run(table => scalar(caller), @_) }
Subroutine table: called CC=1 SCAR=0.0
Pod documented
299  
300 28 ▶sub name { _building('name')->{spec}->{name} = $_[0] }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
301 6 ▶sub omit { push @{_building('omit')->{spec}->{omit}} => @_ }
Subroutine omit: called CC=1 SCAR=0.0
Pod documented
302 3 ▶sub default_fields { push @{_building('default_fields')->{spec}->{default_fields}} => @_ }
Subroutine default_fields: called CC=1 SCAR=0.0
Pod documented
303  
304 =pod
305  
306 =over 4
307  
308 =item type $field => $type_or_undef
309  
310 =item type $field => {class => $type_or_undef, format => \&code, generate => $mode}
311  
312 The field's entry in C<types>: a type name (C<JSON>, or C<+My::Type> for a
313 full class name), undef to remove the field's type, or the hashref form with
314 field hints (see L<DBIx::QuickORM::Role::Type/Field hints>).
315  
316 =back
317  
318 =cut
319  
320 9 ▶sub type {
Subroutine type: called CC=1 SCAR=0.0
Pod documented
321 9 my ($field, $type) = @_;
322 9 _building('type')->{spec}->{types}->{$field} = $type;
323 }
324  
325 =pod
326  
327 =over 4
328  
329 =item fk $other_table => {$our_field => $their_field}
330  
331 A foreign key to add, as in C<fks>. Several may name one table.
332  
333 =back
334  
335 =cut
336  
337 5 ▶sub fk {
Subroutine fk: called CC=1 SCAR=0.0
Pod documented
338 5 my ($other, $link) = @_;
339 5 push @{_building('fk')->{spec}->{fks}->{$other}} => $link;
340 }
341  
342 =pod
343  
344 =head2 :row
345  
346 =over 4
347  
348 =item row { ... }
349  
350 Sets up the package as a row class. Its keywords act on the package at once.
351 In a package with a C<table> block, it makes the package that table's row
352 class.
353  
354 =back
355  
356 =cut
357  
358 21 ▶sub row(&) { _run(row => scalar(caller), @_) }
Subroutine row: called CC=1 SCAR=0.0
Pod documented
359  
360 =pod
361  
362 =over 4
363  
364 =item with_role
365  
366 =item with_role $role
367  
368 Applies the role, L<DBIx::QuickORM::Role::Row> unless given.
369  
370 =back
371  
372 =cut
373  
374 1 ▶sub with_role {
Subroutine with_role: called CC=2 SCAR=7.2
Pod documented
375 1 my ($role) = @_;
376 1 ▶ Role::Tiny->apply_roles_to_package(_building('with_role')->{package}, $role // 'DBIx::QuickORM::Role::Row');
Condition50%1/2
$role // "DBIx::QuickORM::Role::Row"
l!l
01
MC/DC0%0/1
$role // "DBIx::QuickORM::Role::Row"
$role
Truth table50%1/2
A
A= $role
Aresult
11
00
377 }
378  
379 =pod
380  
381 =over 4
382  
383 =item parent
384  
385 =item parent $class
386  
387 Loads the class and adds it to the package's C<@ISA>, as C<use parent> does:
388 L<DBIx::QuickORM::Row> unless given. A class already in C<@ISA> is not added
389 again. The class may be another row class built with this DSL.
390  
391 A class that already has a C<new> method is not loaded, so it may be defined
392 in the same file, with no F<.pm> of its own.
393  
394 =back
395  
396 =cut
397  
398 20 ▶sub parent {
Subroutine parent: called CC=4 SCAR=13.9
Pod documented
399 20 my ($class) = @_;
400 20 ▶ $class //= 'DBIx::QuickORM::Row';
Condition100%2/2
$class //= "DBIx::QuickORM::Row"
l!l
416
MC/DC100%1/1
$class //= "DBIx::QuickORM::Row"
$class
Truth table100%2/2
A
A= $class
Aresult
11
00
401  
402 20 my $package = _building('parent')->{package};
403 {
404 20 local $BUILDING; # the class may run its own blocks as it loads
405 20 require DBIx::QuickORM;
406 20 ▶ DBIx::QuickORM->load_class("parent names the class '$class', which", $class) unless $class->can('new');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("new") 3 17 20
407 }
408  
409 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
410 19 my $isa = \@{"$package\::ISA"};
411 19 ▶ push @$isa => $class unless grep { $_ eq $class } @$isa;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $class;} @$isa 18 1 19
412 }
413  
414 19 %OURS = map { $_ => \&{$_} } @EXPORT_OK;
415 19 %NAME_OF = map { (refaddr($OURS{$_}) => $_) } keys %OURS;
416  
417 =pod
418  
419 =head1 PUBLIC METHODS
420  
421 =over 4
422  
423 =item $spec = DBIx::QuickORM::DSL->orm_spec($package)
424  
425 =item $spec = DBIx::QuickORM::DSL->table_spec($package)
426  
427 What the package's C<orm> or C<table> block collected, or undef when it has
428 none. It is the registry's own hash, so do not change it.
429  
430 =back
431  
432 =cut
433  
434 30 ▶sub orm_spec { $ORM_SPECS{$_[1]} }
Subroutine orm_spec: called CC=1 SCAR=0.0
Pod documented
435 80 ▶sub table_spec { $TABLE_SPECS{$_[1]} }
Subroutine table_spec: called CC=1 SCAR=0.0
Pod documented
436  
437 102 ▶sub import {
Subroutine import: called CC=14 SCAR=26.4
438 102 my ($class, @args) = @_;
439 102 my @caller = caller;
440 102 my $package = $caller[0];
441  
442 102 ▶ croak "Name the tags to import: :orm, :table, or :row" unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 1 101 102
443  
444 101 my @symbols; # [name, keyword]
445 101 ▶ Importer->import_into($class, \@caller, @args, sub { push @symbols => [$_[0], $NAME_OF{refaddr($_[1])}] });
Subroutine __ANON__: called CC=1 SCAR=0.0
446  
447 # A keyword renamed explicitly is installed only under its new name, not also under the tag's.
448 100 my %renamed = map { ($_->[1] => 1) } grep { $_->[0] ne $_->[1] } @symbols;
449 100 ▶ @symbols = grep { $_->[0] ne $_->[1] || !$renamed{$_->[1]} } @symbols;
Condition100%3/3
$_->[0] ne $_->[1] || !$renamed{$_->[1]}
l!l&&r!l&&!r
146907
MC/DC100%2/2
$_->[0] ne $_->[1] || !$renamed{$_->[1]}
$_->[0] ne $_->[1] !$renamed{$_->[1]}
Truth table100%3/3
A || not(B)
A= $_->[0] ne $_->[1]B= !$renamed{$_->[1]}
ABresult
1X1
011
000
450  
451 100 my %got = map { ($_->[1] => 1) } @symbols;
452 100 my @blocks = grep { $got{$_} } sort keys %KEYWORDS;
453 100 my %allowed = map { ($_ => 1) } map { ($_, @{$KEYWORDS{$_}}) } @blocks;
454 100 ▶ my $whole = @blocks && !grep { !$allowed{$_} } keys %got;
Condition66%2/3
@blocks && !grep({not $allowed{$_};} keys %got)
!ll&&!rl&&r
0199
MC/DC50%1/2
@blocks && !grep({not $allowed{$_};} keys %got)
@blocks !grep({not $allowed{$_};} keys %got)
Truth table66%2/3
A && not(B)
A= @blocksB= !grep({not $allowed{$_};} keys %got)
ABresult
0X0
100
111
455 100 ▶ $whole &&= !grep { !$got{$_} } map { @{$KEYWORDS{$_}} } @blocks;
Condition100%3/3
$whole &&= !grep({not $got{$_};} map({@{$KEYWORDS{$_};};} @blocks))
!ll&&!rl&&r
1297
MC/DC100%2/2
$whole &&= !grep({not $got{$_};} map({@{$KEYWORDS{$_};};} @blocks))
$whole !grep({not $got{$_};} map({@{$KEYWORDS{$_};};} @blocks))
Truth table100%3/3
A &&= not(B)
A= $wholeB= !grep({not $got{$_};} map({@{$KEYWORDS{$_};};} @blocks))
ABresult
0X0
100
111
456 100 ▶ croak "Import the DSL by tag (:orm, :table, :row), each tag whole" unless $whole;
Branch: 1 branch
BranchTrueFalseTotal
unless $whole 3 97 100
457  
458 97 ▶ my $imported = {%{$IMPORTED{$package} // {}}, map { $_ => 1 } @blocks};
Condition100%2/2
$IMPORTED{$package} // {}
l!l
295
MC/DC100%1/1
$IMPORTED{$package} // {}
$IMPORTED{$package}
Truth table100%2/2
A
A= $IMPORTED{$package}
Aresult
11
00
459 97 ▶ croak "$package cannot be both an ORM class and a table or row class" if $imported->{orm} && ($imported->{table} || $imported->{row});
Branch: 1 branch
BranchTrueFalseTotal
if $imported->{"orm"} and $imported->{"table"} || $imported->{"row"} 2 95 97
Condition100%6/6
$imported->{"orm"} and $imported->{"table"} || $imported->{"row"}
!ll&&!rl&&r
52432
$imported->{"table"} or $imported->{"row"}
l!l&&r!l&&!r
1143
MC/DC100%3/3
$imported->{"orm"} and $imported->{"table"} || $imported->{"row"}
$imported->{"orm"} $imported->{"table"} $imported->{"row"}
Truth table100%4/4
A and B or C
A= $imported->{"orm"}B= $imported->{"table"}C= $imported->{"row"}
ABCresult
0XX0
1000
11X1
1011
460 95 $IMPORTED{$package} = $imported;
461  
462 95 for my $symbol (@symbols) {
463 670 my ($name, $keyword) = @$symbol;
464 670 my $code = _code($package, $name);
465 670 ▶ croak "$package already has a sub named '$name', which the DSL would replace" if $code && $code != $OURS{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
if $code and $code != $OURS{$keyword} 2 668 670
Condition100%3/3
$code and $code != $OURS{$keyword}
!ll&&!rl&&r
658102
MC/DC100%2/2
$code and $code != $OURS{$keyword}
$code $code != $OURS{$keyword}
Truth table100%3/3
A and B
A= $codeB= $code != $OURS{$keyword}
ABresult
0X0
100
111
466 }
467  
468 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
469 93 for my $symbol (@symbols) {
470 658 my ($name, $keyword) = @$symbol;
471 658 *{"$package\::$name"} = $OURS{$keyword};
472 658 $GLOBS{$package}->{$keyword}->{$name} = \*{"$package\::$name"};
473 }
474 }
475  
476 9 ▶sub unimport {
Subroutine unimport: called CC=4 SCAR=13.9
477 9 my ($class, @tags) = @_;
478 9 my $package = caller;
479  
480 9 ▶ my @bad = grep { !(m/^:(\w+)\z/ && $KEYWORDS{$1}) } @tags;
Condition66%2/3
/^:(\w+)\z/ && $KEYWORDS{$1}
!ll&&!rl&&r
102
MC/DC50%1/2
/^:(\w+)\z/ && $KEYWORDS{$1}
/^:(\w+)\z/ $KEYWORDS{$1}
Truth table66%2/3
A && B
A= /^:(\w+)\z/B= $KEYWORDS{$1}
ABresult
0X0
100
111
481 9 ▶ croak "Unknown tag(s): " . join(', ' => @bad) . "; give :orm, :table, or :row" if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 8 9
482  
483 8 ▶ my @blocks = @tags ? map { substr($_, 1) } @tags : sort keys %KEYWORDS;
Branch: 1 branch
BranchTrueFalseTotal
@tags ? : 2 6 8
484 8 _remove($package, map { ($_, @{$KEYWORDS{$_}}) } @blocks);
485 }
486  
487 =head1 PRIVATE METHODS
488  
489 These are functions, called directly within this module.
490  
491 =over 4
492  
493 =item $building = _building($keyword)
494  
495 The block being built. Croaks unless one is running that takes the keyword.
496  
497 =back
498  
499 =cut
500  
501 145 ▶sub _building {
Subroutine _building: called CC=3 SCAR=11.0
502 145 my ($keyword) = @_;
503  
504 145 ▶ return $BUILDING if $BUILDING && grep { $_ eq $keyword } @{$KEYWORDS{$BUILDING->{block}}};
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING and grep {$_ eq $keyword;} @{$KEYWORDS{$DBIx::QuickORM::DSL::BUILDING->{"block"}};} 142 3 145
Condition100%3/3
$DBIx::QuickORM::DSL::BUILDING and grep {$_ eq $keyword;} @{$KEYWORDS{$DBIx::QuickORM::DSL::BUILDING->{"block"}};}
!ll&&!rl&&r
21142
MC/DC100%2/2
$DBIx::QuickORM::DSL::BUILDING and grep {$_ eq $keyword;} @{$KEYWORDS{$DBIx::QuickORM::DSL::BUILDING->{"block"}};}
$DBIx::QuickORM::DSL::BUILDING grep {$_ eq $keyword;} @{$KEYWORDS{$DBIx::QuickORM::DSL::BUILDING->{"block"}};}
Truth table100%3/3
A and B
A= $DBIx::QuickORM::DSL::BUILDINGB= grep {$_ eq $keyword;} @{$KEYWORDS{$DBIx::QuickORM::DSL::BUILDING->{"block"}};}
ABresult
0X0
100
111
505  
506 4 my @blocks = map { "'$_'" } grep {
507 3 my $blk = $_;
508 9 grep { $_ eq $keyword } @{$KEYWORDS{$blk}}
509 } sort keys %KEYWORDS;
510 3 croak "'$keyword' can only be used inside " . join(' or ' => @blocks) . " block";
511 }
512  
513 =pod
514  
515 =over 4
516  
517 =item _run($block, $package, \&code)
518  
519 Runs a block: checks its keywords are still the DSL's, runs the code with the
520 block being built, records the spec, and removes the block and its keywords.
521 Croaks inside another block, when the block was not imported, and when it
522 already ran.
523  
524 =back
525  
526 =cut
527  
528 92 ▶sub _run {
Subroutine _run: called CC=12 SCAR=24.9
529 92 my ($block, $package, $code) = @_;
530  
531 92 ▶ croak "A '$block' block cannot run inside another block" if $BUILDING;
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING 0 92 92
532 92 ▶ croak "The '$block' block is not imported into $package" unless $IMPORTED{$package}->{$block};
Branch: 1 branch
BranchTrueFalseTotal
unless $IMPORTED{$package}{$block} 0 92 92
533 92 ▶ croak "The '$block' block in $package already ran; each block runs once per package" if $RAN{$package}->{$block};
Branch: 1 branch
BranchTrueFalseTotal
if $RAN{$package}{$block} 1 91 92
534  
535 # The block's code calls its keywords through the globs they were installed in, even after 'no'
536 # deleted the names. A same-named sub compiled before that 'no' replaced the sub in the glob.
537 91 for my $keyword (@{$KEYWORDS{$block}}) {
538 448 my $globs = $GLOBS{$package}->{$keyword};
539 448 for my $name (sort keys %$globs) {
540 448 my $code = *{$globs->{$name}}{CODE};
541 448 ▶ next if $code && $code == $OURS{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
if $code and $code == $OURS{$keyword} 444 4 448
Condition66%2/3
$code and $code == $OURS{$keyword}
!ll&&!rl&&r
04444
MC/DC50%1/2
$code and $code == $OURS{$keyword}
$code $code == $OURS{$keyword}
Truth table66%2/3
A and B
A= $codeB= $code == $OURS{$keyword}
ABresult
0X0
100
111
542 4 croak "The DSL keyword '$keyword' in $package, installed as '$name', was replaced or removed before its '$block' block ran; " . "rename the keyword (e.g. $keyword => {-as => 'my_$keyword'}), move any sub named '$name' to another package, " . "or put 'no DBIx::QuickORM::DSL' before that sub";
543 }
544 }
545  
546 87 my $spec = {};
547 {
548 87 local $BUILDING = {block => $block, package => $package, spec => $spec};
549 87 $code->();
550 }
551  
552 84 ▶ _finish_orm($package, $spec) if $block eq 'orm';
Branch: 1 branch
BranchTrueFalseTotal
if $block eq "orm" 38 46 84
553 84 ▶ _finish_table($package, $spec) if $block eq 'table';
Branch: 1 branch
BranchTrueFalseTotal
if $block eq "table" 27 57 84
554 81 ▶ _claim_table($package, $TABLE_SPECS{$package}) if $block eq 'row' && $TABLE_SPECS{$package};
Branch: 1 branch
BranchTrueFalseTotal
if $block eq "row" and $TABLE_SPECS{$package} 8 73 81
Condition100%3/3
$block eq "row" and $TABLE_SPECS{$package}
!ll&&!rl&&r
62118
MC/DC100%2/2
$block eq "row" and $TABLE_SPECS{$package}
$block eq "row" $TABLE_SPECS{$package}
Truth table100%3/3
A and B
A= $block eq "row"B= $TABLE_SPECS{$package}
ABresult
0X0
100
111
555  
556 79 $RAN{$package}->{$block} = 1;
557 79 _remove($package, $block, @{$KEYWORDS{$block}});
558  
559 79 return;
560 }
561  
562 =pod
563  
564 =over 4
565  
566 =item _finish_orm($package, \%spec)
567  
568 Records an C<orm> spec and makes the package a L<DBIx::QuickORM::ORM>.
569  
570 =back
571  
572 =cut
573  
574 38 ▶sub _finish_orm {
Subroutine _finish_orm: called CC=2 SCAR=7.0
575 38 my ($package, $spec) = @_;
576  
577 38 require DBIx::QuickORM::ORM;
578 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
579 38 ▶ push @{"$package\::ISA"} => 'DBIx::QuickORM::ORM' unless $package->isa('DBIx::QuickORM::ORM');
Branch: 1 branch
BranchTrueFalseTotal
unless $package->isa("DBIx::QuickORM::ORM") 38 0 38
580  
581 38 $ORM_SPECS{$package} = $spec;
582 }
583  
584 =pod
585  
586 =over 4
587  
588 =item _finish_table($package, \%spec)
589  
590 Checks and records a C<table> spec, naming the package as the row class when
591 its C<row> block already ran.
592  
593 =back
594  
595 =cut
596  
597 27 ▶sub _finish_table {
Subroutine _finish_table: called CC=6 SCAR=17.9
598 27 my ($package, $spec) = @_;
599  
600 27 ▶ croak "The 'table' block in $package needs a 'name'" unless defined($spec->{name}) && length($spec->{name});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec->{"name"} and length $spec->{"name"} 1 26 27
Condition66%2/3
defined $spec->{"name"} and length $spec->{"name"}
!ll&&!rl&&r
1026
MC/DC50%1/2
defined $spec->{"name"} and length $spec->{"name"}
defined $spec->{"name"} length $spec->{"name"}
Truth table66%2/3
A and B
A= defined $spec->{"name"}B= length $spec->{"name"}
ABresult
0X0
100
111
601 26 ▶ croak "The 'table' block in $package gives both 'omit' and 'default_fields'" if $spec->{omit} && $spec->{default_fields};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"omit"} and $spec->{"default_fields"} 1 25 26
Condition100%3/3
$spec->{"omit"} and $spec->{"default_fields"}
!ll&&!rl&&r
2231
MC/DC100%2/2
$spec->{"omit"} and $spec->{"default_fields"}
$spec->{"omit"} $spec->{"default_fields"}
Truth table100%3/3
A and B
A= $spec->{"omit"}B= $spec->{"default_fields"}
ABresult
0X0
100
111
602  
603 25 ▶ _claim_table($package, $spec) if $RAN{$package}->{row};
Branch: 1 branch
BranchTrueFalseTotal
if $RAN{$package}{"row"} 2 23 25
604  
605 24 $TABLE_SPECS{$package} = $spec;
606 }
607  
608 =pod
609  
610 =over 4
611  
612 =item _claim_table($package, \%spec)
613  
614 Names the package as the row class in its table spec, once both its C<table>
615 and C<row> blocks have run. Croaks when the
616 table block's C<row_class> names another class.
617  
618 =back
619  
620 =cut
621  
622 10 ▶sub _claim_table {
Subroutine _claim_table: called CC=5 SCAR=16.1
623 10 my ($package, $spec) = @_;
624  
625 10 my $named = $spec->{row_class};
626 10 ▶ croak "$package is the row class of the table '$spec->{name}', but its 'row_class' names " . ($named // 'undef') if exists($spec->{row_class}) && !(defined($named) && $named eq $package);
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{"row_class"} and not defined $named && $named eq $package 3 7 10
Condition100%8/8
exists $spec->{"row_class"} and not defined $named && $named eq $package
!ll&&!rl&&r
613
defined $named && $named eq $package
!ll&&!rl&&r
121
$named // "undef"
l!l
21
MC/DC100%3/3
exists $spec->{"row_class"} and not defined $named && $named eq $package
exists $spec->{"row_class"} defined $named $named eq $package
MC/DC100%1/1
$named // "undef"
$named
Truth table100%4/4
A and not(B && C)
A= exists $spec->{"row_class"}B= defined $namedC= $named eq $package
ABCresult
0XX0
1110
10X1
1101
Truth table100%2/2
A
A= $named
Aresult
11
00
627 7 $spec->{row_class} = $package;
628 }
629  
630 =pod
631  
632 =over 4
633  
634 =item $code = _code($package, $name)
635  
636 The sub named in the package, or undef.
637  
638 =back
639  
640 =cut
641  
642 670 ▶sub _code {
Subroutine _code: called CC=2 SCAR=6.9
643 670 my ($package, $name) = @_;
644  
645 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
646 670 ▶ return undef unless exists ${"$package\::"}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists do { "${package}::" }->{$name} 657 13 670
647 13 return *{"$package\::$name"}{CODE};
648 }
649  
650 =pod
651  
652 =over 4
653  
654 =item _remove($package, @keywords)
655  
656 Deletes the names the DSL installed for the keywords, when they are still in
657 the package and still hold the DSL's sub, keeping the glob's other slots. The
658 deleted globs keep the sub, so blocks already compiled still call it.
659  
660 =back
661  
662 =cut
663  
664 87 ▶sub _remove {
Subroutine _remove: called CC=12 SCAR=24.9
665 87 my ($package, @keywords) = @_;
666  
667 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
668 87 my $stash = \%{"$package\::"};
669  
670 87 for my $keyword (@keywords) {
671 597 ▶ my $globs = $GLOBS{$package}->{$keyword} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $globs = $GLOBS{$package}{$keyword} or next 549 48 597
672 549 for my $name (keys %$globs) {
673 549 ▶ next unless exists $stash->{$name} && \*{"$package\::$name"} == $globs->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{$name} and \*{"${package}::$name";} == $globs->{$name} 30 519 549
Condition100%3/3
exists $stash->{$name} and \*{"${package}::$name";} == $globs->{$name}
!ll&&!rl&&r
291519
MC/DC100%2/2
exists $stash->{$name} and \*{"${package}::$name";} == $globs->{$name}
exists $stash->{$name} \*{"${package}::$name";} == $globs->{$name}
Truth table100%3/3
A and B
A= exists $stash->{$name}B= \*{"${package}::$name";} == $globs->{$name}
ABresult
0X0
100
111
674 519 my $code = *{$globs->{$name}}{CODE};
675 519 ▶ next unless $code && $code == $OURS{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless $code and $code == $OURS{$keyword} 1 518 519
Condition66%2/3
$code and $code == $OURS{$keyword}
!ll&&!rl&&r
01518
MC/DC50%1/2
$code and $code == $OURS{$keyword}
$code $code == $OURS{$keyword}
Truth table66%2/3
A and B
A= $codeB= $code == $OURS{$keyword}
ABresult
0X0
100
111
676  
677 # Deleting the glob drops its other slots too, so put back the ones in use.
678 518 my $glob = delete $stash->{$name};
679 518 for my $slot (qw/SCALAR ARRAY HASH IO/) {
680 2072 ▶ my $ref = *{$glob}{$slot} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $ref = *{$glob;}{$slot} or next 519 1553 2072
681 519 ▶ next if $slot eq 'SCALAR' && !defined($$ref);
Branch: 1 branch
BranchTrueFalseTotal
if $slot eq "SCALAR" and not defined $$ref 518 1 519
Condition66%2/3
$slot eq "SCALAR" and not defined $$ref
!ll&&!rl&&r
10518
MC/DC50%1/2
$slot eq "SCALAR" and not defined $$ref
$slot eq "SCALAR" not defined $$ref
Truth table66%2/3
A and not(B)
A= $slot eq "SCALAR"B= not defined $$ref
ABresult
0X0
100
111
682 1 *{"$package\::$name"} = $ref;
683 }
684 }
685 }
686  
687 87 mro::method_changed_in($package);
688 }
689  
690 19 1;
691  
692 __END__
« blib/lib/DBIx/QuickORM/WhereBuilder/Simple.pm ↑ Summary blib/lib/DBIx/QuickORM/SQLBuilder/SQLAbstract.pm »