blib/lib/DBIx/QuickORM/DSL.pm

filter: Statementstmt 100% 220 / 220 Branchbran 94.6% 53 / 56 Conditioncond 88.5% 54 / 61 MC/DCmcdc 81.0% 30 / 37 Subroutinesub 100% 40 / 40 Podpod 100% 19 / 19 totaltotal 96.0% 416 / 433 CC 58 SCAR 18.8
CC
58
Cov
97%
import
14.0
_run
12.0
_remove
12.0
CRAP 6.6
« blib/lib/DBIx/QuickORM/Txn.pm ↑ Summary blib/lib/DBIx/QuickORM/SQLBuilder/SQLAbstract.pm »
1 package DBIx::QuickORM::DSL;
2 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 20 our $VERSION = '0.000032';
6  
7 20 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use Importer();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use Role::Tiny();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶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 20 my %KEYWORDS = (
170 orm => [qw/row_class autotype append_autotype auto_omit retain_cache tables override/],
171 table => [qw/name omit default_fields type fk row_class/],
172 row => [qw/with_role parent/],
173 );
174  
175 20 my %ORM_SPECS; # package => what its orm block collected
176 my %TABLE_SPECS; # package => what its table block collected
177 20 my %IMPORTED; # package => {block => 1} for every block it imported
178 20 my %RAN; # package => {block => 1} for every block that ran
179 20 my %GLOBS; # package => {keyword => {name => \*glob}} for every name the DSL installed
180 20 our $BUILDING; # {block, package, spec} while a block runs
181  
182 20 our %EXPORT_TAGS = map { ($_ => [$_, @{$KEYWORDS{$_}}]) } keys %KEYWORDS;
183 20 our @EXPORT_OK = do {
184 20 my %seen;
185 20 grep { !$seen{$_}++ } map { @$_ } values %EXPORT_TAGS;
186 };
187  
188 20 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 auto_omit types => \@types, db_types => \@db_types
219  
220 The C<auto_omit> argument of L<DBIx::QuickORM's C<new>|DBIx::QuickORM/"PUBLIC METHODS">,
221 given as a list. A table class's bare C<omit;> selects every field again.
222  
223 =item tables $namespace
224  
225 =item tables \@classes
226  
227 Where the table classes are, instead of the classes directly under the ORM
228 class's own namespace.
229  
230 =back
231  
232 =cut
233  
234 41 ▶sub orm(&) { _run(orm => scalar(caller), @_) }
Subroutine orm: called CC=1 SCAR=0.0
Pod documented
235  
236 21 ▶sub row_class { _building('row_class')->{spec}->{row_class} = $_[0] }
Subroutine row_class: called CC=1 SCAR=0.0
Pod documented
237 2 ▶sub autotype { _building('autotype')->{spec}->{autotype} = [@_] }
Subroutine autotype: called CC=1 SCAR=0.0
Pod documented
238 1 ▶sub append_autotype { _building('append_autotype')->{spec}->{append_autotype} = [@_] }
Subroutine append_autotype: called CC=1 SCAR=0.0
Pod documented
239 2 ▶sub auto_omit { _building('auto_omit')->{spec}->{auto_omit} = {@_} }
Subroutine auto_omit: called CC=1 SCAR=0.0
Pod documented
240 9 ▶sub retain_cache { _building('retain_cache')->{spec}->{retain_cache} = $_[0] }
Subroutine retain_cache: called CC=1 SCAR=0.0
Pod documented
241 21 ▶sub tables { _building('tables')->{spec}->{tables} = $_[0] }
Subroutine tables: called CC=1 SCAR=0.0
Pod documented
242  
243 =pod
244  
245 =over 4
246  
247 =item override(%args)
248  
249 Any other L<DBIx::QuickORM's C<new>|DBIx::QuickORM/"PUBLIC METHODS">
250 arguments. They beat the keywords above, and several calls add up.
251  
252 The connection arguments (C<dbh>, C<connect>, C<credentials>) croak.
253  
254 =back
255  
256 =cut
257  
258 22 ▶sub override {
Subroutine override: called CC=3 SCAR=11.0
Pod documented
259 22 my %args = @_;
260  
261 22 my @conn = sort grep { exists $args{$_} } qw/dbh connect credentials/;
262 22 ▶ croak "override() cannot take the connection argument(s): " . join(', ' => @conn) if @conn;
Branch: 1 branch
BranchTrueFalseTotal
if @conn 1 21 22
263  
264 21 my $spec = _building('override')->{spec};
265 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
266 }
267  
268 =pod
269  
270 =head2 :table
271  
272 =over 4
273  
274 =item table { ... }
275  
276 Adjusts one table. Croaks without C<name>, or with both C<omit> and
277 C<default_fields>.
278  
279 In a package whose C<row> block has run (before or after this block), the
280 table's row class is that package. Importing C<:row> alone does not make it
281 one.
282  
283 =item name $table
284  
285 The table, as the database names it. Required: package names never decide
286 anything.
287  
288 =item omit @fields
289  
290 =item default_fields @fields
291  
292 The table's entry in the L<DBIx::QuickORM's C<new>|DBIx::QuickORM/"PUBLIC METHODS">
293 argument of the same name. Several calls add up. A bare C<omit;> selects every
294 field, in place of what C<auto_omit> would leave out.
295  
296 =item row_class $class_or_undef
297  
298 The table's entry in C<row_classes>. In a package that also runs a C<row>
299 block it may only name that package. Otherwise that block croaks.
300  
301 =back
302  
303 =cut
304  
305 34 ▶sub table(&) { _run(table => scalar(caller), @_) }
Subroutine table: called CC=1 SCAR=0.0
Pod documented
306  
307 30 ▶sub name { _building('name')->{spec}->{name} = $_[0] }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
308 7 ▶sub omit { push @{_building('omit')->{spec}->{omit}} => @_ }
Subroutine omit: called CC=1 SCAR=0.0
Pod documented
309 3 ▶sub default_fields { push @{_building('default_fields')->{spec}->{default_fields}} => @_ }
Subroutine default_fields: called CC=1 SCAR=0.0
Pod documented
310  
311 =pod
312  
313 =over 4
314  
315 =item type $field => $type_or_undef
316  
317 =item type $field => {class => $type_or_undef, format => \&code, generate => $mode}
318  
319 The field's entry in C<types>: a type name (C<JSON>, or C<+My::Type> for a
320 full class name), undef to remove the field's type, or the hashref form with
321 field hints (see L<DBIx::QuickORM::Role::Type/Field hints>).
322  
323 =back
324  
325 =cut
326  
327 11 ▶sub type {
Subroutine type: called CC=1 SCAR=0.0
Pod documented
328 11 my ($field, $type) = @_;
329 11 _building('type')->{spec}->{types}->{$field} = $type;
330 }
331  
332 =pod
333  
334 =over 4
335  
336 =item fk $other_table => {$our_field => $their_field}
337  
338 A foreign key to add, as in C<fks>. Several may name one table.
339  
340 =back
341  
342 =cut
343  
344 5 ▶sub fk {
Subroutine fk: called CC=1 SCAR=0.0
Pod documented
345 5 my ($other, $link) = @_;
346 5 push @{_building('fk')->{spec}->{fks}->{$other}} => $link;
347 }
348  
349 =pod
350  
351 =head2 :row
352  
353 =over 4
354  
355 =item row { ... }
356  
357 Sets up the package as a row class. Its keywords act on the package at once.
358 In a package with a C<table> block, it makes the package that table's row
359 class.
360  
361 =back
362  
363 =cut
364  
365 21 ▶sub row(&) { _run(row => scalar(caller), @_) }
Subroutine row: called CC=1 SCAR=0.0
Pod documented
366  
367 =pod
368  
369 =over 4
370  
371 =item with_role
372  
373 =item with_role $role
374  
375 Applies the role, L<DBIx::QuickORM::Role::Row> unless given.
376  
377 =back
378  
379 =cut
380  
381 1 ▶sub with_role {
Subroutine with_role: called CC=2 SCAR=7.2
Pod documented
382 1 my ($role) = @_;
383 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
384 }
385  
386 =pod
387  
388 =over 4
389  
390 =item parent
391  
392 =item parent $class
393  
394 Loads the class and adds it to the package's C<@ISA>, as C<use parent> does:
395 L<DBIx::QuickORM::Row> unless given. A class already in C<@ISA> is not added
396 again. The class may be another row class built with this DSL.
397  
398 A class that already has a C<new> method is not loaded, so it may be defined
399 in the same file, with no F<.pm> of its own.
400  
401 =back
402  
403 =cut
404  
405 20 ▶sub parent {
Subroutine parent: called CC=4 SCAR=13.9
Pod documented
406 20 my ($class) = @_;
407 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
408  
409 20 my $package = _building('parent')->{package};
410 {
411 20 local $BUILDING; # the class may run its own blocks as it loads
412 20 require DBIx::QuickORM;
413 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
414 }
415  
416 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
417 19 my $isa = \@{"$package\::ISA"};
418 19 ▶ push @$isa => $class unless grep { $_ eq $class } @$isa;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $class;} @$isa 18 1 19
419 }
420  
421 20 %OURS = map { $_ => \&{$_} } @EXPORT_OK;
422 20 %NAME_OF = map { (refaddr($OURS{$_}) => $_) } keys %OURS;
423  
424 =pod
425  
426 =head1 PUBLIC METHODS
427  
428 =over 4
429  
430 =item $spec = DBIx::QuickORM::DSL->orm_spec($package)
431  
432 =item $spec = DBIx::QuickORM::DSL->table_spec($package)
433  
434 What the package's C<orm> or C<table> block collected, or undef when it has
435 none. It is the registry's own hash, so do not change it.
436  
437 =back
438  
439 =cut
440  
441 32 ▶sub orm_spec { $ORM_SPECS{$_[1]} }
Subroutine orm_spec: called CC=1 SCAR=0.0
Pod documented
442 84 ▶sub table_spec { $TABLE_SPECS{$_[1]} }
Subroutine table_spec: called CC=1 SCAR=0.0
Pod documented
443  
444 106 ▶sub import {
Subroutine import: called CC=14 SCAR=26.4
445 106 my ($class, @args) = @_;
446 106 my @caller = caller;
447 106 my $package = $caller[0];
448  
449 106 ▶ croak "Name the tags to import: :orm, :table, or :row" unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 1 105 106
450  
451 105 my @symbols; # [name, keyword]
452 105 ▶ Importer->import_into($class, \@caller, @args, sub { push @symbols => [$_[0], $NAME_OF{refaddr($_[1])}] });
Subroutine __ANON__: called CC=1 SCAR=0.0
453  
454 # A keyword renamed explicitly is installed only under its new name, not also under the tag's.
455 104 my %renamed = map { ($_->[1] => 1) } grep { $_->[0] ne $_->[1] } @symbols;
456 104 ▶ @symbols = grep { $_->[0] ne $_->[1] || !$renamed{$_->[1]} } @symbols;
Condition100%3/3
$_->[0] ne $_->[1] || !$renamed{$_->[1]}
l!l&&r!l&&!r
147657
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
457  
458 104 my %got = map { ($_->[1] => 1) } @symbols;
459 104 my @blocks = grep { $got{$_} } sort keys %KEYWORDS;
460 104 my %allowed = map { ($_ => 1) } map { ($_, @{$KEYWORDS{$_}}) } @blocks;
461 104 ▶ my $whole = @blocks && !grep { !$allowed{$_} } keys %got;
Condition66%2/3
@blocks && !grep({not $allowed{$_};} keys %got)
!ll&&!rl&&r
01103
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
462 104 ▶ $whole &&= !grep { !$got{$_} } map { @{$KEYWORDS{$_}} } @blocks;
Condition100%3/3
$whole &&= !grep({not $got{$_};} map({@{$KEYWORDS{$_};};} @blocks))
!ll&&!rl&&r
12101
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
463 104 ▶ croak "Import the DSL by tag (:orm, :table, :row), each tag whole" unless $whole;
Branch: 1 branch
BranchTrueFalseTotal
unless $whole 3 101 104
464  
465 101 ▶ my $imported = {%{$IMPORTED{$package} // {}}, map { $_ => 1 } @blocks};
Condition100%2/2
$IMPORTED{$package} // {}
l!l
299
MC/DC100%1/1
$IMPORTED{$package} // {}
$IMPORTED{$package}
Truth table100%2/2
A
A= $IMPORTED{$package}
Aresult
11
00
466 101 ▶ 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 99 101
Condition100%6/6
$imported->{"orm"} and $imported->{"table"} || $imported->{"row"}
!ll&&!rl&&r
54452
$imported->{"table"} or $imported->{"row"}
l!l&&r!l&&!r
1145
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
467 99 $IMPORTED{$package} = $imported;
468  
469 99 for my $symbol (@symbols) {
470 743 my ($name, $keyword) = @$symbol;
471 743 my $code = _code($package, $name);
472 743 ▶ 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 741 743
Condition100%3/3
$code and $code != $OURS{$keyword}
!ll&&!rl&&r
731102
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
473 }
474  
475 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
476 97 for my $symbol (@symbols) {
477 731 my ($name, $keyword) = @$symbol;
478 731 *{"$package\::$name"} = $OURS{$keyword};
479 731 $GLOBS{$package}->{$keyword}->{$name} = \*{"$package\::$name"};
480 }
481 }
482  
483 9 ▶sub unimport {
Subroutine unimport: called CC=4 SCAR=13.9
484 9 my ($class, @tags) = @_;
485 9 my $package = caller;
486  
487 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
488 9 ▶ croak "Unknown tag(s): " . join(', ' => @bad) . "; give :orm, :table, or :row" if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 8 9
489  
490 8 ▶ my @blocks = @tags ? map { substr($_, 1) } @tags : sort keys %KEYWORDS;
Branch: 1 branch
BranchTrueFalseTotal
@tags ? : 2 6 8
491 8 _remove($package, map { ($_, @{$KEYWORDS{$_}}) } @blocks);
492 }
493  
494 =head1 PRIVATE METHODS
495  
496 These are functions, called directly within this module.
497  
498 =over 4
499  
500 =item $building = _building($keyword)
501  
502 The block being built. Croaks unless one is running that takes the keyword.
503  
504 =back
505  
506 =cut
507  
508 154 ▶sub _building {
Subroutine _building: called CC=3 SCAR=11.0
509 154 my ($keyword) = @_;
510  
511 154 ▶ 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"}};} 151 3 154
Condition100%3/3
$DBIx::QuickORM::DSL::BUILDING and grep {$_ eq $keyword;} @{$KEYWORDS{$DBIx::QuickORM::DSL::BUILDING->{"block"}};}
!ll&&!rl&&r
21151
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
512  
513 4 my @blocks = map { "'$_'" } grep {
514 3 my $blk = $_;
515 9 grep { $_ eq $keyword } @{$KEYWORDS{$blk}}
516 } sort keys %KEYWORDS;
517 3 croak "'$keyword' can only be used inside " . join(' or ' => @blocks) . " block";
518 }
519  
520 =pod
521  
522 =over 4
523  
524 =item _run($block, $package, \&code)
525  
526 Runs a block: checks its keywords are still the DSL's, runs the code with the
527 block being built, records the spec, and removes the block and its keywords.
528 Croaks inside another block, when the block was not imported, and when it
529 already ran.
530  
531 =back
532  
533 =cut
534  
535 96 ▶sub _run {
Subroutine _run: called CC=12 SCAR=24.9
536 96 my ($block, $package, $code) = @_;
537  
538 96 ▶ croak "A '$block' block cannot run inside another block" if $BUILDING;
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING 0 96 96
539 96 ▶ croak "The '$block' block is not imported into $package" unless $IMPORTED{$package}->{$block};
Branch: 1 branch
BranchTrueFalseTotal
unless $IMPORTED{$package}{$block} 0 96 96
540 96 ▶ 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 95 96
541  
542 # The block's code calls its keywords through the globs they were installed in, even after 'no'
543 # deleted the names. A same-named sub compiled before that 'no' replaced the sub in the glob.
544 95 for my $keyword (@{$KEYWORDS{$block}}) {
545 513 my $globs = $GLOBS{$package}->{$keyword};
546 513 for my $name (sort keys %$globs) {
547 513 my $code = *{$globs->{$name}}{CODE};
548 513 ▶ next if $code && $code == $OURS{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
if $code and $code == $OURS{$keyword} 509 4 513
Condition66%2/3
$code and $code == $OURS{$keyword}
!ll&&!rl&&r
04509
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
549 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";
550 }
551 }
552  
553 91 my $spec = {};
554 {
555 91 local $BUILDING = {block => $block, package => $package, spec => $spec};
556 91 $code->();
557 }
558  
559 88 ▶ _finish_orm($package, $spec) if $block eq 'orm';
Branch: 1 branch
BranchTrueFalseTotal
if $block eq "orm" 40 48 88
560 88 ▶ _finish_table($package, $spec) if $block eq 'table';
Branch: 1 branch
BranchTrueFalseTotal
if $block eq "table" 29 59 88
561 85 ▶ _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 77 85
Condition100%3/3
$block eq "row" and $TABLE_SPECS{$package}
!ll&&!rl&&r
66118
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
562  
563 83 $RAN{$package}->{$block} = 1;
564 83 _remove($package, $block, @{$KEYWORDS{$block}});
565  
566 83 return;
567 }
568  
569 =pod
570  
571 =over 4
572  
573 =item _finish_orm($package, \%spec)
574  
575 Records an C<orm> spec and makes the package a L<DBIx::QuickORM::ORM>.
576  
577 =back
578  
579 =cut
580  
581 40 ▶sub _finish_orm {
Subroutine _finish_orm: called CC=2 SCAR=7.0
582 40 my ($package, $spec) = @_;
583  
584 40 require DBIx::QuickORM::ORM;
585 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
586 40 ▶ push @{"$package\::ISA"} => 'DBIx::QuickORM::ORM' unless $package->isa('DBIx::QuickORM::ORM');
Branch: 1 branch
BranchTrueFalseTotal
unless $package->isa("DBIx::QuickORM::ORM") 40 0 40
587  
588 40 $ORM_SPECS{$package} = $spec;
589 }
590  
591 =pod
592  
593 =over 4
594  
595 =item _finish_table($package, \%spec)
596  
597 Checks and records a C<table> spec, naming the package as the row class when
598 its C<row> block already ran.
599  
600 =back
601  
602 =cut
603  
604 29 ▶sub _finish_table {
Subroutine _finish_table: called CC=6 SCAR=17.9
605 29 my ($package, $spec) = @_;
606  
607 29 ▶ 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 28 29
Condition66%2/3
defined $spec->{"name"} and length $spec->{"name"}
!ll&&!rl&&r
1028
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
608 28 ▶ 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 27 28
Condition100%3/3
$spec->{"omit"} and $spec->{"default_fields"}
!ll&&!rl&&r
2341
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
609  
610 27 ▶ _claim_table($package, $spec) if $RAN{$package}->{row};
Branch: 1 branch
BranchTrueFalseTotal
if $RAN{$package}{"row"} 2 25 27
611  
612 26 $TABLE_SPECS{$package} = $spec;
613 }
614  
615 =pod
616  
617 =over 4
618  
619 =item _claim_table($package, \%spec)
620  
621 Names the package as the row class in its table spec, once both its C<table>
622 and C<row> blocks have run. Croaks when the
623 table block's C<row_class> names another class.
624  
625 =back
626  
627 =cut
628  
629 10 ▶sub _claim_table {
Subroutine _claim_table: called CC=5 SCAR=16.1
630 10 my ($package, $spec) = @_;
631  
632 10 my $named = $spec->{row_class};
633 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
634 7 $spec->{row_class} = $package;
635 }
636  
637 =pod
638  
639 =over 4
640  
641 =item $code = _code($package, $name)
642  
643 The sub named in the package, or undef.
644  
645 =back
646  
647 =cut
648  
649 743 ▶sub _code {
Subroutine _code: called CC=2 SCAR=6.9
650 743 my ($package, $name) = @_;
651  
652 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
653 743 ▶ return undef unless exists ${"$package\::"}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists do { "${package}::" }->{$name} 730 13 743
654 13 return *{"$package\::$name"}{CODE};
655 }
656  
657 =pod
658  
659 =over 4
660  
661 =item _remove($package, @keywords)
662  
663 Deletes the names the DSL installed for the keywords, when they are still in
664 the package and still hold the DSL's sub, keeping the glob's other slots. The
665 deleted globs keep the sub, so blocks already compiled still call it.
666  
667 =back
668  
669 =cut
670  
671 91 ▶sub _remove {
Subroutine _remove: called CC=12 SCAR=24.9
672 91 my ($package, @keywords) = @_;
673  
674 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
675 91 my $stash = \%{"$package\::"};
676  
677 91 for my $keyword (@keywords) {
678 671 ▶ my $globs = $GLOBS{$package}->{$keyword} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $globs = $GLOBS{$package}{$keyword} or next 617 54 671
679 617 for my $name (keys %$globs) {
680 617 ▶ next unless exists $stash->{$name} && \*{"$package\::$name"} == $globs->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{$name} and \*{"${package}::$name";} == $globs->{$name} 30 587 617
Condition100%3/3
exists $stash->{$name} and \*{"${package}::$name";} == $globs->{$name}
!ll&&!rl&&r
291587
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
681 587 my $code = *{$globs->{$name}}{CODE};
682 587 ▶ next unless $code && $code == $OURS{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless $code and $code == $OURS{$keyword} 1 586 587
Condition66%2/3
$code and $code == $OURS{$keyword}
!ll&&!rl&&r
01586
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
683  
684 # Deleting the glob drops its other slots too, so put back the ones in use.
685 586 my $glob = delete $stash->{$name};
686 586 for my $slot (qw/SCALAR ARRAY HASH IO/) {
687 2344 ▶ my $ref = *{$glob}{$slot} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $ref = *{$glob;}{$slot} or next 587 1757 2344
688 587 ▶ next if $slot eq 'SCALAR' && !defined($$ref);
Branch: 1 branch
BranchTrueFalseTotal
if $slot eq "SCALAR" and not defined $$ref 586 1 587
Condition66%2/3
$slot eq "SCALAR" and not defined $$ref
!ll&&!rl&&r
10586
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
689 1 *{"$package\::$name"} = $ref;
690 }
691 }
692 }
693  
694 91 mro::method_changed_in($package);
695 }
696  
697 20 1;
698  
699 __END__
« blib/lib/DBIx/QuickORM/Txn.pm ↑ Summary blib/lib/DBIx/QuickORM/SQLBuilder/SQLAbstract.pm »