blib/lib/DBIx/QuickORM/DSL.pm

filter: Statementstmt 99.7% 459 / 460 Branchbran 95.0% 192 / 202 Conditioncond 82.6% 190 / 230 MC/DCmcdc 69.9% 93 / 133 Subroutinesub 100% 79 / 79 Podpod 100% 46 / 46 totaltotal 92.0% 1059 / 1150 CC 191 SCAR 21.2
CC
191
Cov
94%
table_schema
22.3
import
18.0
_define_enum
15.4
CRAP 8.3
« blib/lib/DBIx/QuickORM/Process/BuildSchema.pm ↑ Summary blib/lib/DBIx/QuickORM/Handle.pm »
1 package DBIx::QuickORM::DSL;
2 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 36 our $VERSION = '0.000033';
6  
7 36 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 36 ▶use Importer();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 36 ▶use Role::Tiny();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 36 ▶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 A fourth, C<schema>, describes a table class's table for schema building
30 (L<DBIx::QuickORM::Manual::SchemaBuilding>): creating the database's tables
31 from code. It does not run when the class loads, and an ORM over a database
32 that exists never uses it.
33  
34 C<use DBIx::QuickORM::DSL @tags> imports blocks with their keywords, through
35 L<Importer>. Each tag holds one block and its keywords, and the blocks
36 imported decide what the package is:
37  
38 =over 4
39  
40 =item C<:orm>: the ORM class.
41  
42 =item C<:table>: a table class.
43  
44 =item C<:row>: a row class.
45  
46 =item C<:table> with C<:row>: a table class that is also that table's row class.
47  
48 =item C<:view>: a view class, as C<:table> is for tables (with C<:row> too).
49  
50 =item C<:schema>: with C<:table> or C<:view>, the table's or view's schema for schema building.
51  
52 =item C<:temp>: the schema keywords for C<< $orm->temp_table >> callbacks, in a C<use> of its own (see L</":temp">).
53  
54 =back
55  
56 Any keyword can also be imported by name, such as C<enum> alone in a module
57 of enum definitions:
58  
59 use DBIx::QuickORM::DSL 'enum';
60  
61 A tag only makes its block available. A package registers when the block
62 runs: a C<table> block records the table spec, and a C<row> block makes a
63 combined package its table's row class. A package that imports C<:row> but
64 never runs a C<row> block gets the default row class.
65  
66 Importer's renaming works, so a keyword can take another name. A renamed
67 keyword is installed only under its new name:
68  
69 use DBIx::QuickORM::DSL ':table', type => {-as => 'col_type'};
70  
71 The import croaks for an empty list, C<:orm> together with C<:table> or
72 C<:row>, C<:temp> with anything else, or a package that already has a sub of
73 a name it would install.
74  
75 Keywords work only inside their block. Called anywhere else, they croak.
76  
77 Each block runs when the module's code runs, once per package. When it
78 finishes it removes itself and its keywords from the package, under whatever
79 names they were imported, so none is left behind as a method.
80  
81 C<no DBIx::QuickORM::DSL> (or C<no DBIx::QuickORM::DSL @tags>) removes the
82 names that still hold the DSL's own subs. It may come after the blocks: a
83 block compiled before it still calls its keywords.
84  
85 Perl compiles named subs before any block runs, wherever they are in the file.
86 A sub named like a keyword (C<type>, C<name>) and compiled before that C<no>,
87 or with no C<no>, replaces the keyword, so its block croaks when it runs. To
88 have such a sub, rename the keyword (C<< type => {-as => 'my_type'} >>), move
89 the sub to another package, or put C<no DBIx::QuickORM::DSL> before it.
90  
91 =head1 SYNOPSIS
92  
93 The ORM class, in F<lib/My/ORM.pm>:
94  
95 package My::ORM;
96 use DBIx::QuickORM::DSL ':orm';
97  
98 orm {
99 row_class 'My::ORM::Row';
100 override(omit => {posts => ['views']});
101 };
102  
103 1;
104  
105 A row class for every table, in F<lib/My/ORM/Row.pm>:
106  
107 package My::ORM::Row;
108 use DBIx::QuickORM::DSL ':row';
109  
110 row {
111 parent;
112 };
113  
114 sub label { my $self = shift; $self->table . ' ' . $self->field('id') }
115  
116 1;
117  
118 A table class that is also the table's row class, in F<lib/My/ORM/Users.pm>.
119 Modules directly under F<My/ORM/> are found and loaded:
120  
121 package My::ORM::Users;
122 use DBIx::QuickORM::DSL qw/:table :row/;
123  
124 table {
125 name 'users';
126 omit qw/prefs/;
127 type created => undef;
128 };
129  
130 row {
131 parent 'My::ORM::Row';
132 };
133  
134 sub display_name { $_[0]->field('name') }
135  
136 1;
137  
138 A table class that removes the DSL's names with C<no>, so a row method can
139 take a keyword's name, in F<lib/My/ORM/Posts.pm>:
140  
141 package My::ORM::Posts;
142 use DBIx::QuickORM::DSL qw/:table :row/;
143  
144 table {
145 name 'posts';
146 };
147  
148 row {
149 parent 'My::ORM::Row';
150 };
151  
152 no DBIx::QuickORM::DSL; # every tag, or name them: no DBIx::QuickORM::DSL ':table';
153  
154 sub name { $_[0]->field('title') } # a row method named like the keyword
155  
156 1;
157  
158 Using it:
159  
160 use My::ORM;
161  
162 my $orm = My::ORM->new(
163 credentials => {
164 dsn => $dsn,
165 user => $user,
166 pass => $pass,
167 },
168 );
169  
170 my $bob = $orm->insert(users => {name => 'Bob'});
171 my $name = $bob->display_name; # a method from My::ORM::Users
172  
173 my $post = $orm->insert(
174 posts => {
175 user_id => $bob->field('id'),
176 title => 'Hi',
177 },
178 );
179  
180 my $what = $post->label; # a method from My::ORM::Row
181 my $hi = $post->name; # a method from My::ORM::Posts
182  
183 =head2 Schema building, if you want it
184  
185 C<:schema> is optional. It describes tables, views, and what goes around
186 them, only to generate the SQL that creates them (C<CREATE TABLE> and the
187 like) for a new database. It is never the ORM's source of truth: the ORM
188 still reads every table from the database, and an ORM over an existing
189 database ignores these definitions. See
190 L<DBIx::QuickORM::Manual::SchemaBuilding>.
191  
192 The ORM class may define enums for its tables, in F<lib/My/Shop.pm>:
193  
194 package My::Shop;
195 use DBIx::QuickORM::DSL qw/:orm enum/;
196  
197 enum status => [qw/open shipped/];
198  
199 orm { };
200  
201 1;
202  
203 A table class describes its table, in F<lib/My/Shop/Orders.pm>:
204  
205 package My::Shop::Orders;
206 use DBIx::QuickORM::DSL qw/:table :schema/;
207  
208 table { name 'orders' };
209  
210 schema {
211 column 'id' => sub { db_type 'integer'; primary_key; identity };
212 column 'status' => sub { enum 'status'; not_null; default 'open' };
213 column 'total' => {db_type => 'numeric(10,2)', check => \'total >= 0'};
214 };
215  
216 1;
217  
218 A view class, with C<:view> in place of C<:table>, can build its query from
219 a handle, in F<lib/My/Shop/OpenOrders.pm>:
220  
221 package My::Shop::OpenOrders;
222 use DBIx::QuickORM::DSL qw/:view :schema/;
223  
224 view { name 'open_orders'; pk 'id' };
225  
226 schema {
227 view_sql any => sub { $_[0]->handle('orders')->where({status => 'open'}) }, depends => ['orders'];
228 };
229  
230 1;
231  
232 Getting the SQL, or creating the schema on a new database before the usual
233 scan:
234  
235 my $sql = My::Shop->build_sql(dialect => 'postgresql', pretty => 1); # or qorm-generate-sql --pretty --dialect postgresql My::Shop
236 my $orm = My::Shop->new(credentials => {dsn => $dsn, user => $user, pass => $pass}, deploy => 1);
237  
238 =cut
239  
240 36 my @COLUMNS = qw/column db_type type not_null primary_key identity unique default check collate generated references indexed enum foreign_key add_index/;
241 36 my %KEYWORDS = (
242 orm => [qw/row_class autotype append_autotype auto_omit retain_cache tables override schema setting extension schema_sql/],
243 table => [qw/name omit default_fields type fk pk row_class elective/],
244 view => [qw/name omit default_fields type fk pk row_class elective/],
245 row => [qw/with_role parent/],
246 schema => [qw/schema_sql view_sql/, @COLUMNS],
247 );
248 36 my @TEMP = (@COLUMNS, qw/fill row_class/); # the :temp tag, which has no block
249  
250 36 my %ORM_SPECS; # package => what its orm block collected
251 my %TABLE_SPECS; # package => what its table block collected
252 36 my %IMPORTED; # package => {block => 1} for every block it imported
253 36 my %RAN; # package => {block => 1} for every block that ran
254 36 my %GLOBS; # package => {keyword => {name => \*glob}} for every name the DSL installed
255 36 my %ORM_SCHEMAS; # package => {code => $sub} from the schema block inside its orm block, and {spec => ...} once run
256 36 my %SCHEMA_SPECS; # package => {code => $sub} from its schema block, or {raw => [{sql => {...}, depends => [...]}, ...]} from schema_sql
257 36 my %ENUMS; # namespace => {name => {name, values, at, package}}
258 36 my @PENDING; # enum definitions whose namespace was not found yet
259 36 our $BUILDING; # {block, package, spec} while a block runs, {block, package, builder} for a schema block or temp_table callback, with {column} in a column
260  
261 36 our %EXPORT_TAGS = ((map { ($_ => [$_, @{$KEYWORDS{$_}}]) } keys %KEYWORDS), temp => [@TEMP]);
262 36 our @EXPORT_OK = do {
263 36 my %seen;
264 36 grep { !$seen{$_}++ } map { @$_ } values %EXPORT_TAGS;
265 };
266  
267 36 my %OURS; # keyword => our sub
268 my %NAME_OF; # refaddr of our sub => keyword
269  
270 =pod
271  
272 =head1 EXPORTS
273  
274 Nothing is exported by default. Each tag brings its block and the block's
275 keywords. A keyword croaks outside a block that takes it.
276  
277 =head2 :orm
278  
279 =over 4
280  
281 =item orm { ... }
282  
283 Defines the ORM class: makes the package a subclass of
284 L<DBIx::QuickORM::ORM>, whose C<new> uses what the block collected.
285  
286 =item row_class $class_or_undef
287  
288 =item autotype @types
289  
290 =item append_autotype @types
291  
292 =item retain_cache $bool
293  
294 The L<DBIx::QuickORM's C<new>|DBIx::QuickORM/"PUBLIC METHODS"> arguments of
295 the same names:
296  
297 orm {
298 row_class 'My::ORM::Row'; # every table's rows
299 autotype 'JSON'; # only JSON: users.created stays a string
300 retain_cache 1;
301 };
302  
303 C<autotype> with no types turns autotyping off:
304  
305 orm { autotype }; # no automatic types: only those given (a table class's type, or types)
306  
307 C<append_autotype> adds types after the C<autotype> list, or after the
308 default one:
309  
310 orm {
311 append_autotype '+My::Type::Email'; # UUID, JSON, DateTime, then this
312 };
313  
314 L<DBIx::QuickORM::Role::Type> shows a type such as C<My::Type::Email>.
315  
316 =item auto_omit types => \@types, db_types => \@db_types
317  
318 The C<auto_omit> argument of L<DBIx::QuickORM's C<new>|DBIx::QuickORM/"PUBLIC METHODS">,
319 given as a list:
320  
321 orm {
322 auto_omit
323 types => ['DateTime'], # users.created, by its ORM type
324 db_types => ['json']; # users.prefs, by its database type
325 };
326  
327 Both are left out of default selects, and fetched when a row reads them.
328  
329 A table class's bare C<omit;> selects every field again (see L</omit @fields>).
330  
331 =item tables $namespace
332  
333 =item tables \@classes
334  
335 Where the table classes are, instead of the classes directly under the ORM
336 class's own namespace:
337  
338 orm { tables 'My::ORM' }; # the classes directly under My::ORM
339  
340 Or a list of classes:
341  
342 orm { tables ['My::ORM::Users'] }; # this class only: posts keeps the defaults
343  
344 A listed class is loaded unless its C<table> block has run, so it may be
345 defined in the same file.
346  
347 =back
348  
349 =cut
350  
351 84 ▶sub orm(&) { _run(orm => scalar(caller), @_) }
Subroutine orm: called CC=1 SCAR=0.0
Pod documented
352  
353 18 ▶sub row_class {
Subroutine row_class: called CC=5 SCAR=16.2
Pod documented
354 18 ▶ if ($BUILDING && $BUILDING->{block} eq 'temp' && !$BUILDING->{column}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "temp" and not $DBIx::QuickORM::DSL::BUILDING->{"column"}) 1 17 18
Condition83%5/6
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "temp" and not $DBIx::QuickORM::DSL::BUILDING->{"column"}
!ll&&!rl&&r
1701
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "temp"
!ll&&!rl&&r
1161
MC/DC66%2/3
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "temp" and not $DBIx::QuickORM::DSL::BUILDING->{"column"}
$DBIx::QuickORM::DSL::BUILDING $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "temp" not $DBIx::QuickORM::DSL::BUILDING->{"column"}
Truth table75%3/4
A and B and not(C)
A= $DBIx::QuickORM::DSL::BUILDINGB= $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "temp"C= not $DBIx::QuickORM::DSL::BUILDING->{"column"}
ABCresult
0XX0
10X0
1100
1111
355 1 ▶ croak "'row_class' takes one class name or undef" unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 1 1
356 1 $BUILDING->{builder}->row_class(@_);
357 1 return;
358 }
359 17 _building('row_class')->{spec}->{row_class} = $_[0];
360 }
361 2 ▶sub autotype { _building('autotype')->{spec}->{autotype} = [@_] }
Subroutine autotype: called CC=1 SCAR=0.0
Pod documented
362 1 ▶sub append_autotype { _building('append_autotype')->{spec}->{append_autotype} = [@_] }
Subroutine append_autotype: called CC=1 SCAR=0.0
Pod documented
363 2 ▶sub auto_omit { _building('auto_omit')->{spec}->{auto_omit} = {@_} }
Subroutine auto_omit: called CC=1 SCAR=0.0
Pod documented
364 9 ▶sub retain_cache { _building('retain_cache')->{spec}->{retain_cache} = $_[0] }
Subroutine retain_cache: called CC=1 SCAR=0.0
Pod documented
365 60 ▶sub tables { _building('tables')->{spec}->{tables} = $_[0] }
Subroutine tables: called CC=1 SCAR=0.0
Pod documented
366  
367 =pod
368  
369 =over 4
370  
371 =item override(%args)
372  
373 Any other L<DBIx::QuickORM's C<new>|DBIx::QuickORM/"PUBLIC METHODS">
374 arguments. They beat the keywords above, and several calls add up:
375  
376 orm {
377 retain_cache 1;
378 override(retain_cache => 0); # beats the keyword
379 override(types => {users => {prefs => undef}}); # another argument: both calls apply
380 };
381  
382 The arguments of all calls are gathered into one set. A later call's
383 argument replaces an earlier call's argument of the same name whole: two
384 calls giving C<types> do not merge.
385  
386 The connection arguments (C<dbh>, C<connect>, C<credentials>) croak.
387  
388 =back
389  
390 =cut
391  
392 19 ▶sub override {
Subroutine override: called CC=3 SCAR=11.0
Pod documented
393 19 my %args = @_;
394  
395 19 my @conn = sort grep { exists $args{$_} } qw/dbh connect credentials/;
396 19 ▶ croak "override() cannot take the connection argument(s): " . join(', ' => @conn) if @conn;
Branch: 1 branch
BranchTrueFalseTotal
if @conn 1 18 19
397  
398 18 my $spec = _building('override')->{spec};
399 18 ▶ $spec->{override} = {%{$spec->{override} // {}}, %args};
Condition100%2/2
$spec->{"override"} // {}
l!l
117
MC/DC100%1/1
$spec->{"override"} // {}
$spec->{"override"}
Truth table100%2/2
A
A= $spec->{"override"}
Aresult
11
00
400 }
401  
402 =pod
403  
404 =head2 :orm, the schema block
405  
406 =over 4
407  
408 =item schema { my $builder = shift; ... }
409  
410 Inside an C<orm> block, what schema building creates before and after the
411 tables (see L<DBIx::QuickORM::Manual::SchemaBuilding>). Like a table's
412 C<schema> block, it runs only when schema building needs it, once. Its
413 keywords are the methods of L<DBIx::QuickORM::SchemaBuilder::ORM> of the same
414 name, and the block also gets the builder. In F<lib/My/App.pm>:
415  
416 package My::App;
417 use DBIx::QuickORM::DSL ':orm';
418  
419 orm {
420 schema {
421 my $builder = shift; # the same as the keywords: $builder->extension('citext')
422  
423 setting client_min_messages => 'warning';
424 extension 'citext';
425 schema_sql
426 postgresql => q{COMMENT ON TABLE users IS 'People who sign in'},
427 after => 1; # after the tables and views: users exists by then
428 };
429 };
430  
431 1;
432  
433 With its table class, in F<lib/My/App/Users.pm>:
434  
435 package My::App::Users;
436 use DBIx::QuickORM::DSL qw/:table :schema/;
437  
438 table { name 'users' };
439  
440 schema {
441 column 'id' => sub { db_type 'integer'; primary_key; identity };
442 column 'email' => sub { db_type 'citext'; unique }; # the extension's type
443 };
444  
445 1;
446  
447 =item setting $name => $value
448  
449 =item extension $name
450  
451 =item schema_sql dialect => $sql, ..., after => 1
452  
453 SQL of your own, keyed by dialect as below. A dialect without an entry skips
454 it. The same keyword as a table class's C<schema_sql>: inside this block it
455 means this.
456  
457 =back
458  
459 Enums are defined outside any block, with C<enum> (see L</enum>), and the
460 named enums the tables use come right after the extensions.
461  
462 =head2 Per-dialect SQL
463  
464 C<schema_sql> and C<view_sql> take C<< dialect => $sql >> pairs, keyed
465 C<postgresql>, C<mysql>, C<mariadb>, C<sqlite>, C<duckdb>, C<any_mysql>
466 (MySQL and MariaDB), or C<any> (every database). The exact dialect wins, then
467 C<any_mysql>, then C<any>. An undef value means "not on this database".
468 Options may sit among the pairs.
469  
470 =head2 :table
471  
472 =over 4
473  
474 =item table { ... }
475  
476 Adjusts one table. Croaks without C<name>, or with both C<omit> and
477 C<default_fields>.
478  
479 In a package whose C<row> block has run (before or after this block), the
480 table's row class is that package. Importing C<:row> alone does not make it
481 one.
482  
483 =item name $table
484  
485 The table, as the database names it. Required: package names never decide
486 anything.
487  
488 =item omit @fields
489  
490 =item default_fields @fields
491  
492 The table's entry in the L<DBIx::QuickORM's C<new>|DBIx::QuickORM/"PUBLIC METHODS">
493 argument of the same name. Several calls add up:
494  
495 table {
496 name 'posts';
497 default_fields qw/title/; # and the primary key: id, title
498 };
499  
500 A bare C<omit;> selects every field, in place of what C<auto_omit> would
501 leave out:
502  
503 table {
504 name 'users';
505 omit; # every field, prefs too
506 };
507  
508 =item row_class $class_or_undef
509  
510 The table's entry in C<row_classes>. Undef gives hashrefs:
511  
512 table {
513 name 'posts';
514 row_class undef; # posts come back as hashrefs
515 };
516  
517 In a package that also runs a C<row> block it may only name that package.
518 Otherwise that block croaks.
519  
520 =back
521  
522 =cut
523  
524 153 ▶sub table(&) { _run(table => scalar(caller), @_) }
Subroutine table: called CC=1 SCAR=0.0
Pod documented
525  
526 180 ▶sub name { _building('name')->{spec}->{name} = $_[0] }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
527 8 ▶sub omit { push @{_building('omit')->{spec}->{omit}} => @_ }
Subroutine omit: called CC=1 SCAR=0.0
Pod documented
528 3 ▶sub default_fields { push @{_building('default_fields')->{spec}->{default_fields}} => @_ }
Subroutine default_fields: called CC=1 SCAR=0.0
Pod documented
529  
530 =pod
531  
532 =over 4
533  
534 =item type $field => $type_or_undef
535  
536 =item type $field => {class => $type_or_undef, format => \&code, generate => $mode}
537  
538 The field's entry in C<types>: a type name (C<JSON>, or C<+My::Type> for a
539 full class name), undef to remove the field's type, or the hashref form with
540 field hints (see L<DBIx::QuickORM::Role::Type/Field hints>):
541  
542 table {
543 name 'tokens'; # token CHAR(36): a legacy table of upper case UUIDs
544 type token => {
545 class => 'UUID',
546 format => sub { uc $_[0] }, # gets the text written, returns what is sent: 0190A6E8-...
547 };
548 };
549  
550 =back
551  
552 =cut
553  
554 18 ▶sub type {
Subroutine type: called CC=6 SCAR=17.9
Pod documented
555 18 ▶ return _column_one(type => @_) if $BUILDING && $BUILDING->{column};
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"} 4 14 18
Condition100%3/3
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"}
!ll&&!rl&&r
1134
MC/DC100%2/2
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"}
$DBIx::QuickORM::DSL::BUILDING $DBIx::QuickORM::DSL::BUILDING->{"column"}
Truth table100%3/3
A and B
A= $DBIx::QuickORM::DSL::BUILDINGB= $DBIx::QuickORM::DSL::BUILDING->{"column"}
ABresult
0X0
100
111
556 14 my ($field, $type) = @_;
557 14 ▶ croak "'type' can only be used inside a 'table' or 'view' block, or a column" unless $BUILDING && ($BUILDING->{block} eq 'table' || $BUILDING->{block} eq 'view');
Branch: 1 branch
BranchTrueFalseTotal
unless $DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "table" || $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "view" 2 12 14
Condition83%5/6
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "table" || $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "view"
!ll&&!rl&&r
1112
$DBIx::QuickORM::DSL::BUILDING->{"block"} eq "table" or $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "view"
l!l&&r!l&&!r
1201
MC/DC66%2/3
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "table" || $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "view"
$DBIx::QuickORM::DSL::BUILDING $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "table" $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "view"
Truth table75%3/4
A and B or C
A= $DBIx::QuickORM::DSL::BUILDINGB= $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "table"C= $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "view"
ABCresult
0XX0
1000
11X1
1011
558 12 _building('type')->{spec}->{types}->{$field} = $type;
559 }
560  
561 =pod
562  
563 =over 4
564  
565 =item fk $other_table => {$our_field => $their_field}
566  
567 A foreign key to add, as in C<fks>. Several may name one table. A view has
568 no keys in the database, so its class (see L</":view">) declares them for
569 C<follow_fk> and joins:
570  
571 # CREATE VIEW popular_posts AS SELECT * FROM posts WHERE views > 100
572 view {
573 name 'popular_posts';
574 pk 'id';
575 fk users => {user_id => 'id'};
576 };
577  
578 =back
579  
580 =cut
581  
582 6 ▶sub fk {
Subroutine fk: called CC=1 SCAR=0.0
Pod documented
583 6 my ($other, $link) = @_;
584 6 push @{_building('fk')->{spec}->{fks}->{$other}} => $link;
585 }
586  
587 =pod
588  
589 =over 4
590  
591 =item pk @fields
592  
593 The entry in C<pks>: the primary key of a view, or of a table the database
594 reports none for. See C<pks> in L<DBIx::QuickORM/"PUBLIC METHODS">.
595  
596 table {
597 name 'post_tags'; # no primary key in the database
598 pk qw/post_id tag/;
599 };
600  
601 =back
602  
603 =cut
604  
605 26 ▶sub pk { _building('pk')->{spec}->{pks} = [@_] }
Subroutine pk: called CC=1 SCAR=0.0
Pod documented
606  
607 =pod
608  
609 =over 4
610  
611 =item elective
612  
613 The table may be missing from the database: C<new> leaves it out instead of
614 croaking, with what this class says about it. A handle on it then croaks,
615 saying it is elective and absent.
616  
617 table {
618 name 'audit';
619 elective; # new works without an audit table
620 };
621  
622 Needed for a C<schema_sql> or C<view_sql> entry of undef, which builds
623 without the table on that database (see C<schema_sql> under L</":schema">).
624  
625 =back
626  
627 =cut
628  
629 8 ▶sub elective { _building('elective')->{spec}->{elective} = 1; return }
Subroutine elective: called CC=1 SCAR=0.0
Pod documented
630  
631 =pod
632  
633 =head2 :view
634  
635 =over 4
636  
637 =item view { ... }
638  
639 Adjusts one view, as C<table> adjusts a table, with the same keywords (C<pk>
640 being the one a view most often needs). It says the class names a view:
641 C<new> croaks when the database object is a table. A C<:schema> view class
642 gives its query with C<view_sql>. C<:view> and C<:table> never share a
643 package. It combines with C<:row> and C<:schema> as C<:table> does.
644  
645 =back
646  
647 =cut
648  
649 31 ▶sub view(&) { _run(view => scalar(caller), @_) }
Subroutine view: called CC=1 SCAR=0.0
Pod documented
650  
651 =pod
652  
653 =head2 :row
654  
655 =over 4
656  
657 =item row { ... }
658  
659 Sets up the package as a row class. Its keywords act on the package at once.
660 In a package with a C<table> block, it makes the package that table's row
661 class.
662  
663 =back
664  
665 =cut
666  
667 21 ▶sub row(&) { _run(row => scalar(caller), @_) }
Subroutine row: called CC=1 SCAR=0.0
Pod documented
668  
669 =pod
670  
671 =over 4
672  
673 =item with_role
674  
675 =item with_role $role
676  
677 Applies the role, L<DBIx::QuickORM::Role::Row> unless given. A row class
678 that is no L<DBIx::QuickORM::Row> takes the row logic this way:
679  
680 package My::RawRow;
681 use Object::HashBase qw{<orm <table row_data};
682 use DBIx::QuickORM::DSL ':row';
683  
684 row {
685 with_role; # DBIx::QuickORM::Role::Row
686 with_role 'My::Role::Audit'; # and a Role::Tiny role of your own
687 };
688  
689 # The role gives field(), but a method of the class itself wins over a
690 # role's: this one returns values as the database returned them.
691 sub field { my $self = shift; $self->raw_field(@_) }
692  
693 =back
694  
695 =cut
696  
697 1 ▶sub with_role {
Subroutine with_role: called CC=2 SCAR=7.2
Pod documented
698 1 my ($role) = @_;
699 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
700 }
701  
702 =pod
703  
704 =over 4
705  
706 =item parent
707  
708 =item parent $class
709  
710 Loads the class and adds it to the package's C<@ISA>, as C<use parent> does:
711 L<DBIx::QuickORM::Row> unless given. A class already in C<@ISA> is not added
712 again. The class may be another row class built with this DSL.
713  
714 A class that already has a C<new> method is not loaded, so it may be defined
715 in the same file, with no F<.pm> of its own.
716  
717 =back
718  
719 =cut
720  
721 20 ▶sub parent {
Subroutine parent: called CC=4 SCAR=13.9
Pod documented
722 20 my ($class) = @_;
723 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
724  
725 20 my $package = _building('parent')->{package};
726 {
727 20 local $BUILDING; # the class may run its own blocks as it loads
728 20 require DBIx::QuickORM;
729 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
730 }
731  
732 36 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
733 19 my $isa = \@{"$package\::ISA"};
734 19 ▶ push @$isa => $class unless grep { $_ eq $class } @$isa;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $class;} @$isa 18 1 19
735 }
736  
737 =pod
738  
739 =head2 :schema
740  
741 =over 4
742  
743 =item schema { my $builder = shift; ... }
744  
745 The table's schema, for schema building, through the keywords below (or the
746 L<DBIx::QuickORM::SchemaBuilder::Table> builder the block gets). The block
747 does not run when the class loads: its code is kept, and runs only when
748 schema building needs it, once. The package needs a C<table> block naming
749 the table.
750  
751 The keywords and the builder's methods mix:
752  
753 schema {
754 my $builder = shift;
755 $builder->add_column('id', {db_type => 'integer', primary_key => 1});
756 column 'name' => {db_type => 'text'};
757 };
758  
759 =item schema_sql dialect => $sql, ..., depends => \@tables
760  
761 At a table class's top level, the table as the SQL that creates it, per
762 dialect (see L</"Per-dialect SQL">):
763  
764 package My::ORM::Audit;
765 use DBIx::QuickORM::DSL qw/:table :schema/;
766  
767 table { name 'audit'; elective };
768  
769 schema_sql
770 postgresql => 'CREATE TABLE audit (id bigserial PRIMARY KEY, user_id integer REFERENCES users (id))',
771 depends => ['users'];
772 schema_sql
773 any_mysql => 'CREATE TABLE audit (id bigint AUTO_INCREMENT PRIMARY KEY, user_id integer)';
774 schema_sql any => undef; # no audit table elsewhere
775  
776 A dialect without an entry croaks when the schema is built there, and an
777 undef entry builds without the table, so the table must be C<elective>.
778  
779 C<depends> names the tables it references, created first, for this call's
780 dialects: several calls may give dialects their own. A dialect in two calls
781 croaks, and so does C<schema_sql> with a C<schema> block.
782  
783 =item view_sql dialect => $query, ..., depends => \@names, columns => \%types
784  
785 Inside the C<schema> block of a view class (a C<view> block, see
786 L</":view">), the view's query, per dialect, with the same rules as
787 C<schema_sql>. A query is SQL, or a code reference that gets an offline ORM
788 and returns a handle:
789  
790 package My::Shop::OrderCents; # over the SYNOPSIS's orders table
791 use DBIx::QuickORM::DSL qw/:view :schema/;
792  
793 view { name 'order_cents'; pk 'id' };
794  
795 schema {
796 view_sql
797 postgresql => 'SELECT id, total * 100 AS cents FROM orders',
798 depends => ['orders'];
799 view_sql
800 any => sub {
801 my $orm = shift;
802 $orm->handle('orders')->fields({
803 id => 'id',
804 cents => \'total * 100',
805 });
806 },
807 depends => ['orders'],
808 columns => {cents => 'numeric'}; # cents is computed: no table field gives its type
809 };
810  
811 C<columns> gives the column type of each computed column a handle selects,
812 which no table field gives (see L<DBIx::QuickORM::SchemaBuilder::View>).
813 Views come after every table. The block describes no table, and croaks in a
814 C<table> class. What a scan cannot tell about a view, such as its C<pk>,
815 belongs in its C<view> block.
816  
817 =back
818  
819 =head3 Columns
820  
821 =over 4
822  
823 =item column $name => sub { ... }
824  
825 =item column $name => {%options}
826  
827 =item column $name => \$sql
828  
829 A column. The block uses the column keywords below. The hashref gives the
830 same as keys, a keyword that takes no arguments taking a true value, and
831 C<references> its arguments as an arrayref:
832  
833 column 'id' => sub { db_type 'integer'; primary_key; identity };
834 column 'email' => {db_type => 'citext', not_null => 1, unique => 1};
835 column 'org' => {db_type => 'integer', references => ['orgs', 'id', {on_delete => 'cascade'}]};
836 column 'note' => \'text COLLATE "C" NOT NULL';
837  
838 SQL is everything after the column's name, written as given.
839  
840 =item db_type $type
841  
842 =item db_type {postgresql => $type, any_mysql => $type, any => $type, ...}
843  
844 The column's type as the database spells it, or one per database:
845  
846 column 'title' => sub {
847 db_type {
848 any_mysql => 'varchar(200)', # MySQL and MariaDB
849 any => 'text', # PostgreSQL and the others
850 };
851 };
852  
853 =item type $type
854  
855 A type class (C<JSON>, C<UUID>, or C<+My::Type>) whose C<db_type_for> gives
856 the column type when there is no C<db_type>
857 (L<DBIx::QuickORM::Role::Type/db_type_for>):
858  
859 column 'prefs' => sub { type 'JSON' }; # jsonb on PostgreSQL 9.4 and later
860  
861 In a table's C<schema> block that is all it does: the field's type in the
862 ORM comes from the scan's autotyping, or from C<type> in the C<table> block.
863 In a C<temp_table> callback it also sets the field's type.
864  
865 =item not_null
866  
867 =item primary_key
868  
869 =item identity
870  
871 =item unique
872  
873 =item indexed
874  
875 =item indexed {postgresql => \%options, sqlite => undef, ...}
876  
877 The column is C<NOT NULL>, part of the primary key, numbered by the database,
878 unique, or indexed (per dialect with the hash):
879  
880 column 'id' => sub { db_type 'integer'; primary_key; identity };
881 column 'email' => sub { db_type 'text'; unique };
882 column 'name' => sub { db_type 'text'; not_null; indexed };
883 column 'title' => sub {
884 db_type 'text';
885 indexed {
886 postgresql => {using => 'hash'},
887 sqlite => undef, # no index on SQLite
888 };
889 };
890  
891 =item default $value | \$sql | sql(...)
892  
893 =item check \$sql | sql(...)
894  
895 =item generated \$sql | sql(...)
896  
897 =item collate $name | {postgresql => $name, ...}
898  
899 The column's default, check, generating expression, and collation. A plain
900 default is a value, and a reference to SQL or an C<sql()> marker
901 (L<DBIx::QuickORM::Value>) is an SQL expression:
902  
903 use DBIx::QuickORM::Value 'sql';
904  
905 column 'views' => sub { db_type 'integer'; not_null; default 0; check \'views >= 0' };
906 column 'created' => sub { db_type 'timestamp'; default \'now()' };
907 column 'slug' => sub { db_type 'text'; generated sql('lower(title)') };
908 column 'title' => sub {
909 db_type 'text';
910 collate {
911 postgresql => 'C',
912 any_mysql => 'utf8mb4_bin',
913 };
914 };
915  
916 =item references $table
917  
918 =item references $table, $column
919  
920 =item references $table, $column, {on_delete => $action, on_update => $action}
921  
922 =item references $table, {on_delete => $action}
923  
924 A foreign key to the other table's column, the column of the same name when
925 not given:
926  
927 table { name 'profiles' };
928  
929 schema {
930 # users.id, since the column is named id too
931 column 'id' => sub { db_type 'integer'; primary_key; references 'users', {on_delete => 'cascade'} };
932 column 'mentor_id' => sub { db_type 'integer'; references 'users', 'id', {on_delete => 'set null'} };
933 column 'pinned' => sub { db_type 'integer'; references 'posts', 'id' };
934 };
935  
936 =item enum $name
937  
938 =item enum $namespace, $name
939  
940 =item enum \@values
941  
942 The column holds one of an enum's values: one defined for the ORM class
943 being built, one defined for the namespace given, or a one-off enum for this
944 column alone. See L</enum> below.
945  
946 column 'status' => sub { enum 'status' }; # defined for the ORM class
947 column 'mood' => sub { enum 'My::ORM', 'mood' }; # defined for My::ORM
948 column 'priority' => sub { enum [qw/low high/] }; # this column's own
949  
950 =back
951  
952 The options are those of L<DBIx::QuickORM::SchemaBuilder::Table>'s
953 C<add_column>, which describes each.
954  
955 =head3 Table constraints
956  
957 Each takes what it applies to, then an optional hashref of options.
958  
959 =over 4
960  
961 =item primary_key $column | \@columns
962  
963 =item unique $column | \@columns => {name => $name}
964  
965 =item check \$sql => {name => $name}
966  
967 =item foreign_key $column | \@columns => {table => $table, columns => $theirs, name => $name, on_delete => $action, on_update => $action}
968  
969 =item add_index $column | \@columns => {name => $name, unique => $bool, using => $type, dialect => \%by_dialect}
970  
971 The table's primary key, a unique constraint, a check, a foreign key (to the
972 other table's columns of the same names without C<columns>), and an index:
973  
974 table { name 'post_roles' };
975  
976 schema {
977 column 'user_id' => {db_type => 'integer'};
978 column 'post_id' => {db_type => 'integer'};
979 column 'role' => {db_type => 'text'};
980  
981 primary_key ['user_id', 'post_id'];
982 unique ['post_id', 'role'] => {name => 'one_role_per_post'};
983 check \"role <> ''" => {name => 'role_given'};
984 foreign_key ['user_id', 'post_id'] => {
985 table => 'post_authors',
986 columns => ['author_id', 'post_id'],
987 on_delete => 'cascade',
988 };
989 add_index ['user_id', 'role'] => {
990 name => 'post_roles_user_role',
991 dialect => {sqlite => undef}, # no index on SQLite
992 };
993 };
994  
995 C<primary_key>, C<unique>, and C<check> mean the column's own inside a
996 column. The builder's C<add_index>, C<add_unique>, C<add_check>,
997 C<add_foreign_key>, and C<primary_key> describe the options.
998  
999 =back
1000  
1001 =head3 enum
1002  
1003 =over 4
1004  
1005 =item enum $name => \@values
1006  
1007 =item enum $namespace, $name => \@values
1008  
1009 Outside any block, defines an enum for an ORM class: the namespace given, or
1010 else the nearest package in the module's own name that is an ORM class
1011 (C<My::ORM::Users> finds C<My::ORM>), loaded when it is not yet. Columns of
1012 that ORM's tables use it by name. Defining one again with the same values is
1013 fine, with other values it croaks. With no ORM class found yet, the
1014 definition waits until an ORM builds, and croaks then if none was found.
1015  
1016 package My::ORM::Enums;
1017 use DBIx::QuickORM::DSL 'enum';
1018  
1019 enum mood => [qw/happy sad/];
1020 enum status => [qw/open closed/];
1021  
1022 1;
1023  
1024 With a namespace, it is for that ORM class, wherever the module is:
1025  
1026 enum 'My::Shop', size => [qw/small large/];
1027  
1028 =back
1029  
1030 =cut
1031  
1032 141 ▶sub schema(&) {
Subroutine schema: called CC=4 SCAR=13.9
Pod documented
1033 141 my ($code) = @_;
1034 141 my $package = caller;
1035 141 ▶ if ($BUILDING && $BUILDING->{block} eq 'orm') {
Branch: 1 branch
BranchTrueFalseTotal
if ($DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "orm") 18 123 141
Condition66%2/3
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "orm"
!ll&&!rl&&r
123018
MC/DC50%1/2
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "orm"
$DBIx::QuickORM::DSL::BUILDING $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "orm"
Truth table66%2/3
A and B
A= $DBIx::QuickORM::DSL::BUILDINGB= $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "orm"
ABresult
0X0
100
111
1036 18 ▶ croak "The orm block in $package gives 'schema' twice" if $ORM_SCHEMAS{$package};
Branch: 1 branch
BranchTrueFalseTotal
if $ORM_SCHEMAS{$package} 1 17 18
1037 17 $ORM_SCHEMAS{$package} = {code => $code};
1038 17 return;
1039 }
1040 123 _claim_schema(schema => $package);
1041 123 $SCHEMA_SPECS{$package} = {code => $code};
1042 123 return;
1043 }
1044  
1045 36 ▶sub schema_sql {
Subroutine schema_sql: called CC=12 SCAR=24.8
Pod documented
1046 36 my @args = @_;
1047 36 ▶ if ($BUILDING && $BUILDING->{block} eq 'orm_schema') {
Branch: 1 branch
BranchTrueFalseTotal
if ($DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "orm_schema") 3 33 36
Condition100%3/3
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "orm_schema"
!ll&&!rl&&r
3213
MC/DC100%2/2
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "orm_schema"
$DBIx::QuickORM::DSL::BUILDING $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "orm_schema"
Truth table100%3/3
A and B
A= $DBIx::QuickORM::DSL::BUILDINGB= $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "orm_schema"
ABresult
0X0
100
111
1048 3 $BUILDING->{builder}->schema_sql(@args);
1049 3 return;
1050 }
1051 33 my $package = caller;
1052 33 ▶ croak "'schema_sql' can only be used at the top level of a table class, or inside the 'schema' block of an 'orm' block" if $BUILDING;
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING 1 32 33
1053  
1054 32 require DBIx::QuickORM::Dialect;
1055 32 my ($sql, $opts) = DBIx::QuickORM::Dialect->dialect_pairs("'schema_sql'", \@args, options => ['depends']);
1056 29 ▶ croak "'schema_sql' takes 'depends' as an arrayref of table names" if exists($opts->{depends}) && ref($opts->{depends}) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"depends"} and ref $opts->{"depends"} ne "ARRAY" 1 28 29
Condition100%3/3
exists $opts->{"depends"} and ref $opts->{"depends"} ne "ARRAY"
!ll&&!rl&&r
2081
MC/DC100%2/2
exists $opts->{"depends"} and ref $opts->{"depends"} ne "ARRAY"
exists $opts->{"depends"} ref $opts->{"depends"} ne "ARRAY"
Truth table100%3/3
A and B
A= exists $opts->{"depends"}B= ref $opts->{"depends"} ne "ARRAY"
ABresult
0X0
100
111
1057  
1058 28 my $schema = $SCHEMA_SPECS{$package};
1059 28 ▶ croak "$package defines its table with a 'schema' block, so it takes no 'schema_sql'" if $schema && $schema->{code};
Branch: 1 branch
BranchTrueFalseTotal
if $schema and $schema->{"code"} 1 27 28
Condition100%3/3
$schema and $schema->{"code"}
!ll&&!rl&&r
1891
MC/DC100%2/2
$schema and $schema->{"code"}
$schema $schema->{"code"}
Truth table100%3/3
A and B
A= $schemaB= $schema->{"code"}
ABresult
0X0
100
111
1060 27 ▶ unless ($schema) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema) 18 9 27
1061 18 _claim_schema(schema_sql => $package);
1062 18 $schema = $SCHEMA_SPECS{$package} = {raw => []};
1063 }
1064 27 for my $key (sort keys %$sql) {
1065 29 ▶ croak "$package gives '$key' in two 'schema_sql' calls" if grep { exists $_->{sql}->{$key} } @{$schema->{raw}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {exists $_->{"sql"}{$key};} @{$$schema{"raw"};} 1 28 29
1066 }
1067 26 ▶ push @{$schema->{raw}} => {sql => $sql, depends => [@{$opts->{depends} // []}]};
Condition100%2/2
$opts->{"depends"} // []
l!l
818
MC/DC100%1/1
$opts->{"depends"} // []
$opts->{"depends"}
Truth table100%2/2
A
A= $opts->{"depends"}
Aresult
11
00
1068 26 return;
1069 }
1070  
1071 # The keywords of the schema block inside an orm block, which runs as a block of its own.
1072 4 ▶sub _orm_schema_building {
Subroutine _orm_schema_building: called CC=3 SCAR=11.0
1073 4 my ($keyword) = @_;
1074 4 ▶ return $BUILDING->{builder} if $BUILDING && $BUILDING->{block} eq 'orm_schema';
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "orm_schema" 3 1 4
Condition66%2/3
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "orm_schema"
!ll&&!rl&&r
013
MC/DC50%1/2
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "orm_schema"
$DBIx::QuickORM::DSL::BUILDING $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "orm_schema"
Truth table66%2/3
A and B
A= $DBIx::QuickORM::DSL::BUILDINGB= $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "orm_schema"
ABresult
0X0
100
111
1075 1 croak "'$keyword' can only be used inside the 'schema' block of an 'orm' block";
1076 }
1077  
1078 3 ▶sub setting { _orm_schema_building('setting')->setting(@_); return }
Subroutine setting: called CC=1 SCAR=0.0
Pod documented
1079 1 ▶sub extension { _orm_schema_building('extension')->extension(@_); return }
Subroutine extension: called CC=1 SCAR=0.0
Pod documented
1080  
1081 12 ▶sub view_sql {
Subroutine view_sql: called CC=2 SCAR=7.0
Pod documented
1082 12 my $building = _building('view_sql');
1083 12 require DBIx::QuickORM::SchemaBuilder::View;
1084 12 ▶ ($building->{view} //= DBIx::QuickORM::SchemaBuilder::View->new(name => $building->{builder}->name))->add_sql(@_);
Condition66%2/3
$building->{"view"} //= "DBIx::QuickORM::SchemaBuilder::View"->new("name", $building->{"builder"}->name)
l!l&&r!l&&!r
390
MC/DC0%0/2
$building->{"view"} //= "DBIx::QuickORM::SchemaBuilder::View"->new("name", $building->{"builder"}->name)
$building->{"view"} "DBIx::QuickORM::SchemaBuilder::View"->new("name", $building->{"builder"}->name)
Truth table66%2/3
A //= B
A= $building->{"view"}B= "DBIx::QuickORM::SchemaBuilder::View"->new("name", $building->{"builder"}->name)
ABresult
1X1
011
000
1085 10 return;
1086 }
1087  
1088 98 ▶sub column {
Subroutine column: called CC=6 SCAR=17.9
Pod documented
1089 98 my ($name, $how) = @_;
1090 98 my $building = _table_building('column');
1091 97 ▶ croak "'column' cannot be used inside another column" if $building->{column};
Branch: 1 branch
BranchTrueFalseTotal
if $building->{"column"} 1 96 97
1092 96 ▶ croak "'column' takes a name, then a block, a hashref of options, or SQL" unless @_ == 1 || @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 or @_ == 2 1 95 96
Condition100%3/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
1941
MC/DC100%2/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table100%3/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
1093 95 my $opts = $how;
1094 95 ▶ if (ref($how) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $how eq "CODE") 51 44 95
1095 51 $opts = {};
1096 51 local $building->{column} = {name => $name, opts => $opts};
1097 51 $how->();
1098 }
1099 84 ▶ $building->{builder}->add_column($name, defined($opts) ? ($opts) : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $opts ? : 83 1 84
1100 82 return;
1101 }
1102  
1103 29 ▶sub db_type { _column_one(db_type => @_) }
Subroutine db_type: called CC=1 SCAR=0.0
Pod documented
1104 12 ▶sub not_null { _column_flag(not_null => @_) }
Subroutine not_null: called CC=1 SCAR=0.0
Pod documented
1105 3 ▶sub identity { _column_flag(identity => @_) }
Subroutine identity: called CC=1 SCAR=0.0
Pod documented
1106 6 ▶sub default { _column_one(default => @_) }
Subroutine default: called CC=1 SCAR=0.0
Pod documented
1107 2 ▶sub generated { _column_one(generated => @_) }
Subroutine generated: called CC=1 SCAR=0.0
Pod documented
1108 2 ▶sub collate { _column_one(collate => @_) }
Subroutine collate: called CC=1 SCAR=0.0
Pod documented
1109  
1110 4 ▶sub indexed {
Subroutine indexed: called CC=3 SCAR=11.0
Pod documented
1111 4 ▶ croak "'indexed' takes nothing, or a hashref of dialect => index options" if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 1 3 4
1112 3 ▶ _set_column(indexed => @_ ? $_[0] : 1);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 2 3
1113 }
1114  
1115 7 ▶sub references {
Subroutine references: called CC=3 SCAR=11.1
Pod documented
1116 7 ▶ croak "'references' takes a table, then optionally its column and a hashref of 'on_delete' and 'on_update'" unless @_ >= 1 && @_ <= 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 1 and @_ <= 3 1 6 7
Condition66%2/3
@_ >= 1 and @_ <= 3
!ll&&!rl&&r
106
MC/DC50%1/2
@_ >= 1 and @_ <= 3
@_ >= 1 @_ <= 3
Truth table66%2/3
A and B
A= @_ >= 1B= @_ <= 3
ABresult
0X0
100
111
1117 6 _set_column(references => [@_]);
1118 }
1119  
1120 12 ▶sub primary_key {
Subroutine primary_key: called CC=3 SCAR=11.0
Pod documented
1121 12 ▶ return _column_flag(primary_key => @_) if $BUILDING && $BUILDING->{column};
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"} 8 4 12
Condition66%2/3
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"}
!ll&&!rl&&r
048
MC/DC50%1/2
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"}
$DBIx::QuickORM::DSL::BUILDING $DBIx::QuickORM::DSL::BUILDING->{"column"}
Truth table66%2/3
A and B
A= $DBIx::QuickORM::DSL::BUILDINGB= $DBIx::QuickORM::DSL::BUILDING->{"column"}
ABresult
0X0
100
111
1122 4 _table_building('primary_key')->{builder}->primary_key(@_);
1123 3 return;
1124 }
1125  
1126 5 ▶sub unique {
Subroutine unique: called CC=3 SCAR=12.5
Pod documented
1127 5 ▶ return _column_flag(unique => @_) if $BUILDING && $BUILDING->{column};
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"} 0 5 5
Condition33%1/3
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"}
!ll&&!rl&&r
050
MC/DC0%0/2
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"}
$DBIx::QuickORM::DSL::BUILDING $DBIx::QuickORM::DSL::BUILDING->{"column"}
Truth table33%1/3
A and B
A= $DBIx::QuickORM::DSL::BUILDINGB= $DBIx::QuickORM::DSL::BUILDING->{"column"}
ABresult
0X0
100
111
1128 5 _table_building('unique')->{builder}->add_unique(@_);
1129 4 return;
1130 }
1131  
1132 5 ▶sub check {
Subroutine check: called CC=3 SCAR=11.0
Pod documented
1133 5 ▶ return _column_one(check => @_) if $BUILDING && $BUILDING->{column};
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"} 2 3 5
Condition66%2/3
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"}
!ll&&!rl&&r
032
MC/DC50%1/2
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"}
$DBIx::QuickORM::DSL::BUILDING $DBIx::QuickORM::DSL::BUILDING->{"column"}
Truth table66%2/3
A and B
A= $DBIx::QuickORM::DSL::BUILDINGB= $DBIx::QuickORM::DSL::BUILDING->{"column"}
ABresult
0X0
100
111
1134 3 _table_building('check')->{builder}->add_check(@_);
1135 3 return;
1136 }
1137  
1138 5 ▶sub foreign_key {
Subroutine foreign_key: called CC=3 SCAR=11.0
Pod documented
1139 5 ▶ croak "Inside a column, give its foreign key with 'references'" if $BUILDING && $BUILDING->{column};
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"} 1 4 5
Condition66%2/3
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"}
!ll&&!rl&&r
041
MC/DC50%1/2
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"}
$DBIx::QuickORM::DSL::BUILDING $DBIx::QuickORM::DSL::BUILDING->{"column"}
Truth table66%2/3
A and B
A= $DBIx::QuickORM::DSL::BUILDINGB= $DBIx::QuickORM::DSL::BUILDING->{"column"}
ABresult
0X0
100
111
1140 4 _table_building('foreign_key')->{builder}->add_foreign_key(@_);
1141 3 return;
1142 }
1143  
1144 9 ▶sub add_index {
Subroutine add_index: called CC=3 SCAR=11.0
Pod documented
1145 9 ▶ croak "Inside a column, index it with 'indexed'" if $BUILDING && $BUILDING->{column};
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"} 1 8 9
Condition66%2/3
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"}
!ll&&!rl&&r
081
MC/DC50%1/2
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"}
$DBIx::QuickORM::DSL::BUILDING $DBIx::QuickORM::DSL::BUILDING->{"column"}
Truth table66%2/3
A and B
A= $DBIx::QuickORM::DSL::BUILDINGB= $DBIx::QuickORM::DSL::BUILDING->{"column"}
ABresult
0X0
100
111
1146 8 _table_building('add_index')->{builder}->add_index(@_);
1147 8 return;
1148 }
1149  
1150 54 ▶sub enum {
Subroutine enum: called CC=11 SCAR=24.1
Pod documented
1151 54 my @args = @_;
1152 54 ▶ if ($BUILDING && $BUILDING->{column}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"}) 18 36 54
Condition100%3/3
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"}
!ll&&!rl&&r
32418
MC/DC100%2/2
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"}
$DBIx::QuickORM::DSL::BUILDING $DBIx::QuickORM::DSL::BUILDING->{"column"}
Truth table100%3/3
A and B
A= $DBIx::QuickORM::DSL::BUILDINGB= $DBIx::QuickORM::DSL::BUILDING->{"column"}
ABresult
0X0
100
111
1153 18 ▶ croak "Define enums outside any block: enum NAME => \\\@values, then use one with enum NAME" if ref($args[-1]) eq 'ARRAY' && @args > 1;
Branch: 1 branch
BranchTrueFalseTotal
if ref $args[-1] eq "ARRAY" and @args > 1 1 17 18
Condition100%3/3
ref $args[-1] eq "ARRAY" and @args > 1
!ll&&!rl&&r
1251
MC/DC100%2/2
ref $args[-1] eq "ARRAY" and @args > 1
ref $args[-1] eq "ARRAY" @args > 1
Truth table100%3/3
A and B
A= ref $args[-1] eq "ARRAY"B= @args > 1
ABresult
0X0
100
111
1154 17 ▶ return _set_column(enum => $args[0]) if @args == 1 && defined($args[0]);
Branch: 1 branch
BranchTrueFalseTotal
if @args == 1 and defined $args[0] 14 3 17
Condition66%2/3
@args == 1 and defined $args[0]
!ll&&!rl&&r
3014
MC/DC50%1/2
@args == 1 and defined $args[0]
@args == 1 defined $args[0]
Truth table66%2/3
A and B
A= @args == 1B= defined $args[0]
ABresult
0X0
100
111
1155 3 ▶ return _set_column(enum => {namespace => $args[0], name => $args[1]}) if @args == 2 && !grep { !defined($_) || ref($_) } @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args == 2 and not grep({not defined $_ or ref $_;} @args) 2 1 3
Condition50%3/6
@args == 2 and not grep({not defined $_ or ref $_;} @args)
!ll&&!rl&&r
102
!defined($_) || ref $_
l!l&&r!l&&!r
004
MC/DC50%1/2
@args == 2 and not grep({not defined $_ or ref $_;} @args)
@args == 2 not grep({not defined $_ or ref $_;} @args)
MC/DC0%0/2
!defined($_) || ref $_
!defined($_) ref $_
Truth table66%2/3
A and not(B)
A= @args == 2B= not grep({not defined $_ or ref $_;} @args)
ABresult
0X0
100
111
Truth table33%1/3
not(A) || B
A= !defined($_)B= ref $_
ABresult
1X1
011
000
1156 1 croak "'enum' in a column takes an enum's name, a namespace and a name, or an arrayref of values";
1157 }
1158 36 ▶ croak "'enum' can only be used inside a column, or outside any block to define an enum" if $BUILDING;
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING 4 32 36
1159 32 _define_enum([caller], @args);
1160 24 return;
1161 }
1162  
1163 =pod
1164  
1165 =head2 :temp
1166  
1167 =over 4
1168  
1169 =item use DBIx::QuickORM::DSL ':temp'
1170  
1171 The column keywords, the table constraints, C<enum>, C<fill>, and
1172 C<row_class>, for the callback of C<< $orm->temp_table(sub { ... }) >>. They
1173 act on the builder being built, which the callback still gets:
1174  
1175 use DBIx::QuickORM::DSL ':temp';
1176  
1177 my $titles = $orm->temp_table(sub {
1178 column 'id' => sub { db_type 'integer'; primary_key; references 'posts', 'id' };
1179 column 'title' => {db_type => 'text', not_null => 1};
1180 add_index 'title';
1181 $orm->handle('posts')->fields(['id', 'title']);
1182 });
1183  
1184 no DBIx::QuickORM::DSL;
1185  
1186 When the callback returns a handle or C<\[$select_sql, @binds]>, the table is
1187 filled from it, as C<fill> does. Anything else it returns is ignored. With
1188 C<fill> too, both must be the same.
1189  
1190 The keywords stay in the package, since the callbacks call them whenever
1191 they run. Put C<no DBIx::QuickORM::DSL> after the last use, so they do not
1192 remain as methods: calls compiled before it still work.
1193  
1194 C<:temp> goes in a C<use> of its own. It first removes every DSL name in the
1195 package, so in a package with blocks too, import the block tags and write the
1196 blocks first, then C<use DBIx::QuickORM::DSL ':temp'>:
1197  
1198 package My::App::Reports;
1199 use DBIx::QuickORM::DSL qw/:table :schema/;
1200  
1201 table { name 'reports' };
1202 schema { column 'id' => sub { db_type 'integer'; primary_key } };
1203  
1204 use DBIx::QuickORM::DSL ':temp';
1205  
1206 sub ranked {
1207 my ($self, $orm) = @_;
1208 return $orm->temp_table(sub { column 'rank' => {db_type => 'integer'} });
1209 }
1210  
1211 no DBIx::QuickORM::DSL;
1212  
1213 =item fill $handle
1214  
1215 =item fill \[$select_sql, @binds]
1216  
1217 =item row_class $class_or_undef
1218  
1219 The builder's C<fill> and C<row_class>. A handle fills the columns by name,
1220 whatever their order:
1221  
1222 my $copy = $orm->temp_table(sub {
1223 column 'title' => {db_type => 'text'};
1224 column 'id' => {db_type => 'integer'};
1225 fill $orm->handle('posts')->fields(['id', 'title']);
1226 });
1227  
1228 A C<SELECT> fills them by position. The arrayref holds the SQL first, then
1229 its binds:
1230  
1231 my $popular = $orm->temp_table(sub {
1232 column 'post_id' => {db_type => 'integer'}; # gets the posts' id
1233 column 'title' => {db_type => 'text'};
1234 fill \['SELECT id, title FROM posts WHERE views > ?', 100];
1235 row_class undef; # rows come back as hashrefs
1236 });
1237  
1238 =back
1239  
1240 =cut
1241  
1242 5 ▶sub fill {
Subroutine fill: called CC=3 SCAR=11.0
Pod documented
1243 5 my $building = _table_building('fill');
1244 4 ▶ croak "'fill' can only be used in a temp_table() callback" unless $building->{block} eq 'temp' && !$building->{column};
Branch: 1 branch
BranchTrueFalseTotal
unless $building->{"block"} eq "temp" and not $building->{"column"} 1 3 4
Condition66%2/3
$building->{"block"} eq "temp" and not $building->{"column"}
!ll&&!rl&&r
103
MC/DC50%1/2
$building->{"block"} eq "temp" and not $building->{"column"}
$building->{"block"} eq "temp" not $building->{"column"}
Truth table66%2/3
A and not(B)
A= $building->{"block"} eq "temp"B= not $building->{"column"}
ABresult
0X0
100
111
1245 3 $building->{builder}->fill(@_);
1246 3 return;
1247 }
1248  
1249 36 %OURS = map { $_ => \&{$_} } @EXPORT_OK;
1250 36 %NAME_OF = map { (refaddr($OURS{$_}) => $_) } keys %OURS;
1251  
1252 =pod
1253  
1254 =head1 PUBLIC METHODS
1255  
1256 =over 4
1257  
1258 =item $spec = DBIx::QuickORM::DSL->orm_spec($package)
1259  
1260 =item $spec = DBIx::QuickORM::DSL->table_spec($package)
1261  
1262 What the package's C<orm> or C<table> block collected, or undef when it has
1263 none. It is the registry's own hash, so do not change it. These methods load
1264 nothing: load the class first.
1265  
1266 require My::ORM::Users;
1267  
1268 my $spec = DBIx::QuickORM::DSL->table_spec('My::ORM::Users');
1269 my $name = $spec->{name}; # 'users'
1270 my $omit = $spec->{omit}; # ['prefs']
1271  
1272 =back
1273  
1274 =cut
1275  
1276 98 ▶sub orm_spec { $ORM_SPECS{$_[1]} }
Subroutine orm_spec: called CC=1 SCAR=0.0
Pod documented
1277 611 ▶sub table_spec { $TABLE_SPECS{$_[1]} }
Subroutine table_spec: called CC=1 SCAR=0.0
Pod documented
1278  
1279 =pod
1280  
1281 =over 4
1282  
1283 =item \%spec = DBIx::QuickORM::DSL->orm_schema($package)
1284  
1285 What the C<schema> block inside the package's C<orm> block describes, as
1286 L<DBIx::QuickORM::SchemaBuilder::ORM>'s C<spec>, or undef without one. The
1287 block runs on the first call, and its spec is kept.
1288  
1289 my $spec = DBIx::QuickORM::DSL->orm_schema('My::App');
1290 my $exts = $spec->{extensions}; # ['citext']
1291  
1292 =item \%schema = DBIx::QuickORM::DSL->table_schema($package)
1293  
1294 The schema a table class defines, for schema building, or undef when it
1295 defines none. Its shape depends on how the class defines it:
1296  
1297 =over 4
1298  
1299 =item C<< {name => $table, spec => \%spec} >>
1300  
1301 A C<schema> block with columns:
1302 L<DBIx::QuickORM::SchemaBuilder::Table>'s C<spec>. The block runs on the
1303 first call, and its spec is kept.
1304  
1305 =item C<< {name => $view, view => {entries => [...]}} >>
1306  
1307 A view class's C<schema> block, which gives C<view_sql>:
1308 L<DBIx::QuickORM::SchemaBuilder::View>'s C<spec>.
1309  
1310 =item C<< {name => $table, raw => [{sql => \%by_dialect, depends => [...]}, ...]} >>
1311  
1312 C<schema_sql>, one entry per call.
1313  
1314 =back
1315  
1316 Croaks when the package has no C<table> block naming its table.
1317 Like C<table_spec>, it loads nothing.
1318  
1319 require My::Shop::Orders;
1320  
1321 my $schema = DBIx::QuickORM::DSL->table_schema('My::Shop::Orders');
1322 my $table = $schema->{name}; # 'orders'
1323 my @columns = map { $_->{name} } @{$schema->{spec}->{columns}}; # id, status, total
1324  
1325 require My::ORM::Audit;
1326 my $audit = DBIx::QuickORM::DSL->table_schema('My::ORM::Audit');
1327 my $pg = $audit->{raw}->[0]->{sql}->{postgresql}; # 'CREATE TABLE audit ...'
1328  
1329 =back
1330  
1331 =cut
1332  
1333 48 ▶sub orm_schema {
Subroutine orm_schema: called CC=4 SCAR=13.9
Pod documented
1334 48 my $class = shift;
1335 48 my ($package) = @_;
1336 48 ▶ my $schema = $ORM_SCHEMAS{$package} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $schema = $ORM_SCHEMAS{$package} or return undef 7 41 48
1337 7 ▶ $schema->{spec} //= do {
Condition100%3/3
$schema->{"spec"} //= do { require DBIx::QuickORM::SchemaBuilder::ORM; my $builder = "DBIx::QuickORM::SchemaBuilder::ORM"->new; croak("A 'schema' block cannot run inside another block") if $DBIx::QuickORM::DSL::BUILDING; local $DBIx::QuickORM::DSL::BUILDING = {"block", "orm_schema", "package", $package, "builder", $builder}; $schema->{"code"}->($builder); $builder->spec }
l!l&&r!l&&!r
421
MC/DC100%2/2
$schema->{"spec"} //= do { require DBIx::QuickORM::SchemaBuilder::ORM; my $builder = "DBIx::QuickORM::SchemaBuilder::ORM"->new; croak("A 'schema' block cannot run inside another block") if $DBIx::QuickORM::DSL::BUILDING; local $DBIx::QuickORM::DSL::BUILDING = {"block", "orm_schema", "package", $package, "builder", $builder}; $schema->{"code"}->($builder); $builder->spec }
$schema->{"spec"} do { require DBIx::QuickORM::SchemaBuilder::ORM; my $builder = "DBIx::QuickORM::SchemaBuilder::ORM"->new; croak("A 'schema' block cannot run inside another block") if $DBIx::QuickORM::DSL::BUILDING; local $DBIx::QuickORM::DSL::BUILDING = {"block", "orm_schema", "package", $package, "builder", $builder}; $schema->{"code"}->($builder); $builder->spec }
Truth table100%3/3
A //= B
A= $schema->{"spec"}B= do { require DBIx::QuickORM::SchemaBuilder::ORM; my $builder = "DBIx::QuickORM::SchemaBuilder::ORM"->new; croak("A 'schema' block cannot run inside another block") if $DBIx::QuickORM::DSL::BUILDING; local $DBIx::QuickORM::DSL::BUILDING = {"block", "orm_schema", "package", $package, "builder", $builder}; $schema->{"code"}->($builder); $builder->spec }
ABresult
1X1
011
000
1338 3 require DBIx::QuickORM::SchemaBuilder::ORM;
1339 3 my $builder = DBIx::QuickORM::SchemaBuilder::ORM->new;
1340 3 ▶ croak "A 'schema' block cannot run inside another block" if $BUILDING;
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING 0 3 3
1341 3 local $BUILDING = {block => 'orm_schema', package => $package, builder => $builder};
1342 3 $schema->{code}->($builder);
1343 2 $builder->spec;
1344 };
1345 6 return $schema->{spec};
1346 }
1347  
1348 171 ▶sub table_schema {
Subroutine table_schema: called CC=22 SCAR=31.0
Pod documented
1349 171 my $class = shift;
1350 171 my ($package) = @_;
1351 171 ▶ my $schema = $SCHEMA_SPECS{$package} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $schema = $SCHEMA_SPECS{$package} or return undef 160 11 171
1352 160 ▶ my $table = $TABLE_SPECS{$package} or croak "$package defines a schema but has no 'table' or 'view' block naming it";
Branch: 1 branch
BranchTrueFalseTotal
my $table = $TABLE_SPECS{$package} or croak("$package defines a schema but has no 'table' or 'view' block naming it") 159 1 160
1353 159 ▶ my $is_view = ($table->{kind} // 'table') eq 'view';
Condition50%1/2
$table->{"kind"} // "table"
l!l
1590
MC/DC0%0/1
$table->{"kind"} // "table"
$table->{"kind"}
Truth table50%1/2
A
A= $table->{"kind"}
Aresult
11
00
1354 159 ▶ croak "$package is a view class, so it gives its query with view_sql inside a 'schema' block, not with schema_sql" if $is_view && !$schema->{code};
Branch: 1 branch
BranchTrueFalseTotal
if $is_view and not $schema->{"code"} 1 158 159
Condition100%3/3
$is_view and not $schema->{"code"}
!ll&&!rl&&r
140181
MC/DC100%2/2
$is_view and not $schema->{"code"}
$is_view not $schema->{"code"}
Truth table100%3/3
A and not(B)
A= $is_viewB= not $schema->{"code"}
ABresult
0X0
100
111
1355 158 my $name = $table->{name};
1356 158 ▶ return {name => $name, raw => [map { +{sql => {%{$_->{sql}}}, depends => [@{$_->{depends}}]} } @{$schema->{raw}}]} unless $schema->{code};
Branch: 1 branch
BranchTrueFalseTotal
unless $schema->{"code"} 10 148 158
1357  
1358 148 ▶ $schema->{built} //= do {
Condition100%3/3
$schema->{"built"} //= do { require DBIx::QuickORM::SchemaBuilder::Table; my $builder = "DBIx::QuickORM::SchemaBuilder::Table"->new("name", $name); croak("A 'schema' block cannot run inside another block") if $DBIx::QuickORM::DSL::BUILDING; local $DBIx::QuickORM::DSL::BUILDING = {"block", "schema", "package", $package, "builder", $builder}; $schema->{"code"}->($builder); my $spec = $builder->spec; croak("The 'schema' block in $package calls fill(), which is for temporary tables only") if $builder->_fill; croak("The 'schema' block in $package calls row_class(), which is for temporary tables only: set the row class in its 'table' block") if exists $spec->{"row_class"}; croak("$package gives 'view_sql', so it names a view: use a 'view' block (import :view) in place of its 'table' block") if $DBIx::QuickORM::DSL::BUILDING->{"view"} and not $is_view; croak("$package is a view class, so its 'schema' block gives its query with 'view_sql'") if $is_view and not $DBIx::QuickORM::DSL::BUILDING->{"view"}; if (my $view = $DBIx::QuickORM::DSL::BUILDING->{"view"}) { my $empty = !(@{$spec->{"columns"};} || @{$spec->{"indexes"};} || @{$spec->{"checks"};} || @{$spec->{"uniques"};} || @{$spec->{"foreign_keys"};} || $spec->{"pk"}); croak("The 'schema' block in $package gives 'view_sql' and also describes a table; give one or the other") unless $empty; +{"view", $view->spec}; } else { +{"spec", $spec}; } }
l!l&&r!l&&!r
82651
MC/DC100%2/2
$schema->{"built"} //= do { require DBIx::QuickORM::SchemaBuilder::Table; my $builder = "DBIx::QuickORM::SchemaBuilder::Table"->new("name", $name); croak("A 'schema' block cannot run inside another block") if $DBIx::QuickORM::DSL::BUILDING; local $DBIx::QuickORM::DSL::BUILDING = {"block", "schema", "package", $package, "builder", $builder}; $schema->{"code"}->($builder); my $spec = $builder->spec; croak("The 'schema' block in $package calls fill(), which is for temporary tables only") if $builder->_fill; croak("The 'schema' block in $package calls row_class(), which is for temporary tables only: set the row class in its 'table' block") if exists $spec->{"row_class"}; croak("$package gives 'view_sql', so it names a view: use a 'view' block (import :view) in place of its 'table' block") if $DBIx::QuickORM::DSL::BUILDING->{"view"} and not $is_view; croak("$package is a view class, so its 'schema' block gives its query with 'view_sql'") if $is_view and not $DBIx::QuickORM::DSL::BUILDING->{"view"}; if (my $view = $DBIx::QuickORM::DSL::BUILDING->{"view"}) { my $empty = !(@{$spec->{"columns"};} || @{$spec->{"indexes"};} || @{$spec->{"checks"};} || @{$spec->{"uniques"};} || @{$spec->{"foreign_keys"};} || $spec->{"pk"}); croak("The 'schema' block in $package gives 'view_sql' and also describes a table; give one or the other") unless $empty; +{"view", $view->spec}; } else { +{"spec", $spec}; } }
$schema->{"built"} do { require DBIx::QuickORM::SchemaBuilder::Table; my $builder = "DBIx::QuickORM::SchemaBuilder::Table"->new("name", $name); croak("A 'schema' block cannot run inside another block") if $DBIx::QuickORM::DSL::BUILDING; local $DBIx::QuickORM::DSL::BUILDING = {"block", "schema", "package", $package, "builder", $builder}; $schema->{"code"}->($builder); my $spec = $builder->spec; croak("The 'schema' block in $package calls fill(), which is for temporary tables only") if $builder->_fill; croak("The 'schema' block in $package calls row_class(), which is for temporary tables only: set the row class in its 'table' block") if exists $spec->{"row_class"}; croak("$package gives 'view_sql', so it names a view: use a 'view' block (import :view) in place of its 'table' block") if $DBIx::QuickORM::DSL::BUILDING->{"view"} and not $is_view; croak("$package is a view class, so its 'schema' block gives its query with 'view_sql'") if $is_view and not $DBIx::QuickORM::DSL::BUILDING->{"view"}; if (my $view = $DBIx::QuickORM::DSL::BUILDING->{"view"}) { my $empty = !(@{$spec->{"columns"};} || @{$spec->{"indexes"};} || @{$spec->{"checks"};} || @{$spec->{"uniques"};} || @{$spec->{"foreign_keys"};} || $spec->{"pk"}); croak("The 'schema' block in $package gives 'view_sql' and also describes a table; give one or the other") unless $empty; +{"view", $view->spec}; } else { +{"spec", $spec}; } }
Truth table100%3/3
A //= B
A= $schema->{"built"}B= do { require DBIx::QuickORM::SchemaBuilder::Table; my $builder = "DBIx::QuickORM::SchemaBuilder::Table"->new("name", $name); croak("A 'schema' block cannot run inside another block") if $DBIx::QuickORM::DSL::BUILDING; local $DBIx::QuickORM::DSL::BUILDING = {"block", "schema", "package", $package, "builder", $builder}; $schema->{"code"}->($builder); my $spec = $builder->spec; croak("The 'schema' block in $package calls fill(), which is for temporary tables only") if $builder->_fill; croak("The 'schema' block in $package calls row_class(), which is for temporary tables only: set the row class in its 'table' block") if exists $spec->{"row_class"}; croak("$package gives 'view_sql', so it names a view: use a 'view' block (import :view) in place of its 'table' block") if $DBIx::QuickORM::DSL::BUILDING->{"view"} and not $is_view; croak("$package is a view class, so its 'schema' block gives its query with 'view_sql'") if $is_view and not $DBIx::QuickORM::DSL::BUILDING->{"view"}; if (my $view = $DBIx::QuickORM::DSL::BUILDING->{"view"}) { my $empty = !(@{$spec->{"columns"};} || @{$spec->{"indexes"};} || @{$spec->{"checks"};} || @{$spec->{"uniques"};} || @{$spec->{"foreign_keys"};} || $spec->{"pk"}); croak("The 'schema' block in $package gives 'view_sql' and also describes a table; give one or the other") unless $empty; +{"view", $view->spec}; } else { +{"spec", $spec}; } }
ABresult
1X1
011
000
1359 66 require DBIx::QuickORM::SchemaBuilder::Table;
1360 66 my $builder = DBIx::QuickORM::SchemaBuilder::Table->new(name => $name);
1361 66 ▶ croak "A 'schema' block cannot run inside another block" if $BUILDING;
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING 0 66 66
1362 66 local $BUILDING = {block => 'schema', package => $package, builder => $builder};
1363 66 $schema->{code}->($builder);
1364 36 my $spec = $builder->spec;
1365 35 ▶ croak "The 'schema' block in $package calls fill(), which is for temporary tables only" if $builder->_fill;
Branch: 1 branch
BranchTrueFalseTotal
if $builder->_fill 1 34 35
1366 34 ▶ croak "The 'schema' block in $package calls row_class(), which is for temporary tables only: set the row class in its 'table' block" if exists $spec->{row_class};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{"row_class"} 1 33 34
1367 33 ▶ croak "$package gives 'view_sql', so it names a view: use a 'view' block (import :view) in place of its 'table' block" if $BUILDING->{view} && !$is_view;
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING->{"view"} and not $is_view 1 32 33
Condition100%3/3
$DBIx::QuickORM::DSL::BUILDING->{"view"} and not $is_view
!ll&&!rl&&r
2661
MC/DC100%2/2
$DBIx::QuickORM::DSL::BUILDING->{"view"} and not $is_view
$DBIx::QuickORM::DSL::BUILDING->{"view"} not $is_view
Truth table100%3/3
A and not(B)
A= $DBIx::QuickORM::DSL::BUILDING->{"view"}B= not $is_view
ABresult
0X0
100
111
1368 32 ▶ croak "$package is a view class, so its 'schema' block gives its query with 'view_sql'" if $is_view && !$BUILDING->{view};
Branch: 1 branch
BranchTrueFalseTotal
if $is_view and not $DBIx::QuickORM::DSL::BUILDING->{"view"} 1 31 32
Condition100%3/3
$is_view and not $DBIx::QuickORM::DSL::BUILDING->{"view"}
!ll&&!rl&&r
2561
MC/DC100%2/2
$is_view and not $DBIx::QuickORM::DSL::BUILDING->{"view"}
$is_view not $DBIx::QuickORM::DSL::BUILDING->{"view"}
Truth table100%3/3
A and not(B)
A= $is_viewB= not $DBIx::QuickORM::DSL::BUILDING->{"view"}
ABresult
0X0
100
111
1369  
1370 31 ▶ if (my $view = $BUILDING->{view}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $view = $DBIx::QuickORM::DSL::BUILDING->{'view'}) { } 6 25 31
1371 6 ▶ my $empty = !@{$spec->{columns}} && !@{$spec->{indexes}} && !@{$spec->{checks}} && !@{$spec->{uniques}} && !@{$spec->{foreign_keys}} && !$spec->{pk};
Condition66%10/15
@{$spec->{'columns'};} || @{$spec->{'indexes'};} || @{$spec->{'checks'};} || @{$spec->{'uniques'};} || @{$spec->{'foreign_keys'};} || $spec->{'pk'}
l!l&&r!l&&!r
105
@{$spec->{"columns"};} or @{$spec->{"indexes"};} or @{$spec->{"checks"};} or @{$spec->{"uniques"};} or @{$spec->{"foreign_keys"};}
l!l&&r!l&&!r
105
@{$spec->{"columns"};} or @{$spec->{"indexes"};} or @{$spec->{"checks"};} or @{$spec->{"uniques"};}
l!l&&r!l&&!r
105
@{$spec->{"columns"};} or @{$spec->{"indexes"};} or @{$spec->{"checks"};}
l!l&&r!l&&!r
105
@{$spec->{"columns"};} or @{$spec->{"indexes"};}
l!l&&r!l&&!r
105
MC/DC16%1/6
@{$spec->{'columns'};} || @{$spec->{'indexes'};} || @{$spec->{'checks'};} || @{$spec->{'uniques'};} || @{$spec->{'foreign_keys'};} || $spec->{'pk'}
@{$spec->{"columns"};} @{$spec->{"indexes"};} @{$spec->{"checks"};} @{$spec->{"uniques"};} @{$spec->{"foreign_keys"};} $spec->{'pk'}
Truth table28%2/7
A or B or C or D or E || F
A= @{$spec->{"columns"};}B= @{$spec->{"indexes"};}C= @{$spec->{"checks"};}D= @{$spec->{"uniques"};}E= @{$spec->{"foreign_keys"};}F= $spec->{'pk'}
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
1372 6 ▶ croak "The 'schema' block in $package gives 'view_sql' and also describes a table; give one or the other" unless $empty;
Branch: 1 branch
BranchTrueFalseTotal
unless $empty 1 5 6
1373 5 +{view => $view->spec};
1374 }
1375 else {
1376 25 +{spec => $spec};
1377 }
1378 };
1379 112 return {name => $name, %{$schema->{built}}};
1380 }
1381  
1382 =pod
1383  
1384 =over 4
1385  
1386 =item ($name, \@values) = DBIx::QuickORM::DSL->enum_values($namespace, $enum, $what)
1387  
1388 What a column's C<enum> names: C<< (undef, \@values) >> for a one-off
1389 arrayref, otherwise the enum's name and values, from the namespace given in
1390 C<< {namespace => $class, name => $name} >>, or else C<$namespace> (the ORM
1391 class being built). Definitions still waiting for their namespace are
1392 placed first, croaking for any that has none. Croaks, starting with
1393 C<$what>, when no namespace is given or the enum is not defined.
1394  
1395 # ('status', ['open', 'shipped']): My::Shop's own
1396 my ($name, $values) = DBIx::QuickORM::DSL->enum_values('My::Shop', 'status', "column 'status'");
1397  
1398 # ('mood', ['happy', 'sad']): the namespace given wins
1399 my ($mood, $moods) = DBIx::QuickORM::DSL->enum_values(
1400 'My::Shop',
1401 {
1402 namespace => 'My::ORM',
1403 name => 'mood',
1404 },
1405 "column 'mood'",
1406 );
1407  
1408 # (undef, ['low', 'high']): a one-off
1409 my (undef, $levels) = DBIx::QuickORM::DSL->enum_values(undef, [qw/low high/], "column 'level'");
1410  
1411 =back
1412  
1413 =cut
1414  
1415 92 ▶sub enum_values {
Subroutine enum_values: called CC=7 SCAR=19.5
Pod documented
1416 92 my $class = shift;
1417 92 my ($namespace, $enum, $what) = @_;
1418 92 ▶ return (undef, [@$enum]) if ref($enum) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $enum eq "ARRAY" 35 57 92
1419  
1420 57 ▶ _place_pending() if @PENDING;
Branch: 1 branch
BranchTrueFalseTotal
if @PENDING 4 53 57
1421 55 ▶ my ($ns, $name) = ref($enum) eq 'HASH' ? @{$enum}{qw/namespace name/} : ($namespace, $enum);
Branch: 1 branch
BranchTrueFalseTotal
ref $enum eq 'HASH' ? : 3 52 55
1422 55 ▶ croak "$what uses the enum '$name', but this ORM is no ORM class, so it has no enums" unless defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ns 2 53 55
1423 53 ▶ my $def = ($ENUMS{$ns} // {})->{$name} or croak "$what uses the enum '$name', which nothing defines for $ns";
Branch: 1 branch
BranchTrueFalseTotal
my $def = +($ENUMS{$ns} // {})->{$name} or croak("$what uses the enum '${name}', which nothing defines for $ns") 49 4 53
Condition100%2/2
$ENUMS{$ns} // {}
l!l
512
MC/DC100%1/1
$ENUMS{$ns} // {}
$ENUMS{$ns}
Truth table100%2/2
A
A= $ENUMS{$ns}
Aresult
11
00
1424 49 return ($name, [@{$def->{values}}]);
1425 }
1426  
1427 353 ▶sub import {
Subroutine import: called CC=18 SCAR=28.9
1428 353 my ($class, @args) = @_;
1429 353 my @caller = caller;
1430 353 my $package = $caller[0];
1431  
1432 353 ▶ croak "Name the tags or keywords to import, such as :orm, :table, :view, :row, :schema, or :temp" unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 1 352 353
1433 352 ▶ if (grep { $_ eq ':temp' } @args) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":temp";} @args) 11 341 352
1434 11 ▶ croak "Import :temp in a 'use' of its own, after this package's blocks" if @args > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @args > 1 2 9 11
1435 9 _remove($package, sort keys %OURS);
1436 }
1437  
1438 350 my @symbols; # [name, keyword]
1439 350 ▶ Importer->import_into($class, \@caller, @args, sub { push @symbols => [$_[0], $NAME_OF{refaddr($_[1])}] });
Subroutine __ANON__: called CC=1 SCAR=0.0
1440  
1441 # A keyword renamed explicitly is installed only under its new name, not also under the tag's.
1442 348 my %renamed = map { ($_->[1] => 1) } grep { $_->[0] ne $_->[1] } @symbols;
1443 348 ▶ @symbols = grep { $_->[0] ne $_->[1] || !$renamed{$_->[1]} } @symbols;
Condition100%3/3
$_->[0] ne $_->[1] || !$renamed{$_->[1]}
l!l&&r!l&&!r
1461085
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
1444  
1445 348 my %got = map { ($_->[1] => 1) } @symbols;
1446 348 my @blocks = grep { $got{$_} } sort keys %KEYWORDS;
1447 # 'schema' is a block and also a keyword of 'orm', which shares 'schema_sql' with it too: a block named by
1448 # another block's keyword counts only with a keyword no other block has.
1449 @blocks = grep {
1450 348 my $b = $_;
1451 581 my %other = map { ($_ => 1) } map { @{$KEYWORDS{$_}} } grep { $_ ne $b } @blocks;
1452 581 ▶ !$other{$b} || grep { $got{$_} && !$other{$_} } @{$KEYWORDS{$b}};
Condition100%6/6
!$other{$b} || grep({$got{$_} and not $other{$_};} @{$KEYWORDS{$b};})
l!l&&r!l&&!r
4822178
$got{$_} && !$other{$_}
!ll&&!rl&&r
166110021
MC/DC100%2/2
!$other{$b} || grep({$got{$_} and not $other{$_};} @{$KEYWORDS{$b};})
!$other{$b} grep({$got{$_} and not $other{$_};} @{$KEYWORDS{$b};})
MC/DC100%2/2
$got{$_} && !$other{$_}
$got{$_} !$other{$_}
Truth table100%3/3
not(A) || B
A= !$other{$b}B= grep({$got{$_} and not $other{$_};} @{$KEYWORDS{$b};})
ABresult
1X1
011
000
Truth table100%3/3
A && not(B)
A= $got{$_}B= !$other{$_}
ABresult
0X0
100
111
1453 } @blocks;
1454  
1455 348 ▶ my $imported = {%{$IMPORTED{$package} // {}}, map { $_ => 1 } @blocks};
Condition100%2/2
$IMPORTED{$package} // {}
l!l
6342
MC/DC100%1/1
$IMPORTED{$package} // {}
$IMPORTED{$package}
Truth table100%2/2
A
A= $IMPORTED{$package}
Aresult
11
00
1456 348 ▶ croak "$package cannot be both an ORM class and a table, view, or row class" if $imported->{orm} && ($imported->{table} || $imported->{view} || $imported->{row});
Branch: 1 branch
BranchTrueFalseTotal
if $imported->{"orm"} and $imported->{"table"} || $imported->{"view"} || $imported->{"row"} 2 346 348
Condition88%8/9
$imported->{"orm"} and $imported->{"table"} || $imported->{"view"} || $imported->{"row"}
!ll&&!rl&&r
249972
$imported->{"table"} or $imported->{"view"} or $imported->{"row"}
l!l&&r!l&&!r
1197
$imported->{"table"} or $imported->{"view"}
l!l&&r!l&&!r
1098
MC/DC75%3/4
$imported->{"orm"} and $imported->{"table"} || $imported->{"view"} || $imported->{"row"}
$imported->{"orm"} $imported->{"table"} $imported->{"view"} $imported->{"row"}
Truth table80%4/5
A and B or C or D
A= $imported->{"orm"}B= $imported->{"table"}C= $imported->{"view"}D= $imported->{"row"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
1457 346 ▶ croak "$package cannot be both a table class and a view class; import :table or :view" if $imported->{table} && $imported->{view};
Branch: 1 branch
BranchTrueFalseTotal
if $imported->{"table"} and $imported->{"view"} 1 345 346
Condition100%3/3
$imported->{"table"} and $imported->{"view"}
!ll&&!rl&&r
1761691
MC/DC100%2/2
$imported->{"table"} and $imported->{"view"}
$imported->{"table"} $imported->{"view"}
Truth table100%3/3
A and B
A= $imported->{"table"}B= $imported->{"view"}
ABresult
0X0
100
111
1458 345 $IMPORTED{$package} = $imported;
1459  
1460 345 for my $symbol (@symbols) {
1461 6067 my ($name, $keyword) = @$symbol;
1462 6067 my $code = _code($package, $name);
1463 6067 ▶ 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 6065 6067
Condition100%3/3
$code and $code != $OURS{$keyword}
!ll&&!rl&&r
6053122
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
1464 }
1465  
1466 36 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1467 343 for my $symbol (@symbols) {
1468 6053 my ($name, $keyword) = @$symbol;
1469 6053 *{"$package\::$name"} = $OURS{$keyword};
1470 6053 $GLOBS{$package}->{$keyword}->{$name} = \*{"$package\::$name"};
1471 }
1472 }
1473  
1474 20 ▶sub unimport {
Subroutine unimport: called CC=6 SCAR=17.9
1475 20 my ($class, @tags) = @_;
1476 20 my $package = caller;
1477  
1478 20 ▶ my @bad = grep { !($EXPORT_TAGS{s/^://r} && m/^:/ || $OURS{$_}) } @tags;
Condition83%5/6
$DBIx::QuickORM::DSL::EXPORT_TAGS{s/^://r} && /^:/ || $OURS{$_}
l!l&&r!l&&!r
332
$DBIx::QuickORM::DSL::EXPORT_TAGS{s/^://r} and /^:/
!ll&&!rl&&r
503
MC/DC66%2/3
$DBIx::QuickORM::DSL::EXPORT_TAGS{s/^://r} && /^:/ || $OURS{$_}
$DBIx::QuickORM::DSL::EXPORT_TAGS{s/^://r} /^:/ $OURS{$_}
Truth table60%3/5
A and B || C
A= $DBIx::QuickORM::DSL::EXPORT_TAGS{s/^://r}B= /^:/C= $OURS{$_}
ABCresult
11X1
0X11
1011
0X00
1000
1479 20 ▶ croak "Unknown tag(s) or keyword(s): " . join(', ' => @bad) . ", give :orm, :table, :view, :row, :schema, :temp, or keywords" if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 19 20
1480  
1481 19 ▶ _remove($package, @tags ? (map { m/^:(\w+)\z/ ? @{$EXPORT_TAGS{$1}} : $_ } @tags) : sort keys %OURS);
Branch: 2 branches
BranchTrueFalseTotal
@tags ? : 5 14 19
/^:(\w+)\z/ ? : 3 3 6
1482 }
1483  
1484 =head1 PRIVATE METHODS
1485  
1486 These are functions, called directly within this module.
1487  
1488 =over 4
1489  
1490 =item $building = _building($keyword)
1491  
1492 The block being built. Croaks unless one is running that takes the keyword.
1493  
1494 =back
1495  
1496 =cut
1497  
1498 385 ▶sub _building {
Subroutine _building: called CC=3 SCAR=11.0
1499 385 my ($keyword) = @_;
1500  
1501 385 ▶ 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"}};} 382 3 385
Condition100%3/3
$DBIx::QuickORM::DSL::BUILDING and grep {$_ eq $keyword;} @{$KEYWORDS{$DBIx::QuickORM::DSL::BUILDING->{"block"}};}
!ll&&!rl&&r
21382
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
1502  
1503 7 my @blocks = map { "'$_'" } grep {
1504 3 my $blk = $_;
1505 15 grep { $_ eq $keyword } @{$KEYWORDS{$blk}}
1506 } sort keys %KEYWORDS;
1507 3 croak "'$keyword' can only be used inside " . join(' or ' => @blocks) . " block";
1508 }
1509  
1510 =pod
1511  
1512 =over 4
1513  
1514 =item $building = _table_building($keyword)
1515  
1516 The table being described: a C<schema> block's, or a C<temp_table> callback's
1517 under C<:temp>. Croaks outside both.
1518  
1519 =item $column = _column($keyword)
1520  
1521 The column being described, C<< {name, opts} >>. Croaks outside a column.
1522  
1523 =item _set_column($keyword, $value)
1524  
1525 =item _column_one($keyword, @args)
1526  
1527 =item _column_flag($keyword, @args)
1528  
1529 Set a column option, croaking when the column already has it. C<_column_one>
1530 takes exactly one argument, C<_column_flag> none (it sets 1).
1531  
1532 =item $value = DBIx::QuickORM::DSL->_temp_building($builder, \&code)
1533  
1534 Runs a C<temp_table> callback with its builder being built, so the C<:temp>
1535 keywords act on it, and returns what it returns, in scalar context.
1536  
1537 =back
1538  
1539 =cut
1540  
1541 127 ▶sub _table_building {
Subroutine _table_building: called CC=5 SCAR=16.6
1542 127 my ($keyword) = @_;
1543 127 ▶ return $BUILDING if $BUILDING && $BUILDING->{builder} && ($BUILDING->{block} eq 'schema' || $BUILDING->{block} eq 'temp');
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"builder"} and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "schema" || $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "temp" 125 2 127
Condition66%6/9
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"builder"} and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "schema" || $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "temp"
!ll&&!rl&&r
20125
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"builder"}
!ll&&!rl&&r
20125
$DBIx::QuickORM::DSL::BUILDING->{"block"} eq "schema" or $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "temp"
l!l&&r!l&&!r
109160
MC/DC25%1/4
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"builder"} and $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "schema" || $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "temp"
$DBIx::QuickORM::DSL::BUILDING $DBIx::QuickORM::DSL::BUILDING->{"builder"} $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "schema" $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "temp"
Truth table60%3/5
A and B and C or D
A= $DBIx::QuickORM::DSL::BUILDINGB= $DBIx::QuickORM::DSL::BUILDING->{"builder"}C= $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "schema"D= $DBIx::QuickORM::DSL::BUILDING->{"block"} eq "temp"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
1544 2 croak "'$keyword' can only be used inside a 'schema' block, or a temp_table() callback with :temp";
1545 }
1546  
1547 91 ▶sub _column {
Subroutine _column: called CC=3 SCAR=11.0
1548 91 my ($keyword) = @_;
1549 91 ▶ return $BUILDING->{column} if $BUILDING && $BUILDING->{column};
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"} 82 9 91
Condition100%3/3
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"}
!ll&&!rl&&r
1882
MC/DC100%2/2
$DBIx::QuickORM::DSL::BUILDING and $DBIx::QuickORM::DSL::BUILDING->{"column"}
$DBIx::QuickORM::DSL::BUILDING $DBIx::QuickORM::DSL::BUILDING->{"column"}
Truth table100%3/3
A and B
A= $DBIx::QuickORM::DSL::BUILDINGB= $DBIx::QuickORM::DSL::BUILDING->{"column"}
ABresult
0X0
100
111
1550 9 croak "'$keyword' can only be used inside a column: column 'name' => sub { $keyword ... }";
1551 }
1552  
1553 91 ▶sub _set_column {
Subroutine _set_column: called CC=2 SCAR=6.9
1554 91 my ($keyword, $value) = @_;
1555 91 my $column = _column($keyword);
1556 82 ▶ croak "column '$column->{name}' gives '$keyword' twice" if exists $column->{opts}->{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
if exists $column->{"opts"}{$keyword} 2 80 82
1557 80 $column->{opts}->{$keyword} = $value;
1558 80 return;
1559 }
1560  
1561 45 ▶sub _column_one {
Subroutine _column_one: called CC=2 SCAR=6.9
1562 45 my ($keyword, @args) = @_;
1563 45 ▶ croak "'$keyword' takes one argument" unless @args == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @args == 1 1 44 45
1564 44 _set_column($keyword => $args[0]);
1565 }
1566  
1567 23 ▶sub _column_flag {
Subroutine _column_flag: called CC=2 SCAR=6.9
1568 23 my ($keyword, @args) = @_;
1569 23 ▶ croak "'$keyword' takes no arguments inside a column" if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 1 22 23
1570 22 _set_column($keyword => 1);
1571 }
1572  
1573 43 ▶sub _temp_building {
Subroutine _temp_building: called CC=1 SCAR=0.0
1574 43 my $class = shift;
1575 43 my ($builder, $code) = @_;
1576 43 local $BUILDING = {block => 'temp', package => undef, builder => $builder};
1577 43 return scalar $code->($builder);
1578 }
1579  
1580 =pod
1581  
1582 =over 4
1583  
1584 =item _define_enum(\@caller, @args)
1585  
1586 Defines an enum from C<enum>'s arguments outside a block, in its namespace,
1587 or waiting for one.
1588  
1589 =item $namespace = _enum_namespace($package)
1590  
1591 The package, or the nearest package its name is inside of, that is an ORM
1592 class, each loaded first when it is not. Undef when there is none. A package
1593 with no module file is skipped, and one whose file fails to load rethrows
1594 its error.
1595  
1596 =item _add_enum($namespace, \%definition)
1597  
1598 Records a definition, croaking when the name has other values there.
1599  
1600 =item _place_pending()
1601  
1602 Records the waiting definitions whose namespace is found now, croaking for
1603 any that still has none.
1604  
1605 =back
1606  
1607 =cut
1608  
1609 32 ▶sub _define_enum {
Subroutine _define_enum: called CC=15 SCAR=27.3
1610 32 my ($caller, @args) = @_;
1611 32 ▶ my $ns = @args == 3 ? shift(@args) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@args == 3 ? : 4 28 32
1612 32 my ($name, $values) = @args;
1613 32 ▶ croak "'enum' defines with a name and an arrayref of values, as enum mood => [...] or enum 'My::ORM', mood => [...]" unless @args == 2 && defined($name) && !ref($name) && length($name) && ref($values) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless @args == 2 and defined $name and not ref $name and length $name and ref $values eq "ARRAY" 2 30 32
Condition75%9/12
@args == 2 and defined $name and not ref $name and length $name and ref $values eq "ARRAY"
!ll&&!rl&&r
1130
@args == 2 and defined $name and not ref $name and length $name
!ll&&!rl&&r
1031
@args == 2 and defined $name and not ref $name
!ll&&!rl&&r
1031
@args == 2 and defined $name
!ll&&!rl&&r
1031
MC/DC40%2/5
@args == 2 and defined $name and not ref $name and length $name and ref $values eq "ARRAY"
@args == 2 defined $name not ref $name length $name ref $values eq "ARRAY"
Truth table50%3/6
A and B and not(C) and D and E
A= @args == 2B= defined $nameC= not ref $nameD= length $nameE= ref $values eq "ARRAY"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
1614 30 ▶ croak "'enum' takes a package name as its namespace, such as 'My::ORM'" if defined($ns) && $ns !~ m/^\w+(?:::\w+)*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns and not $ns =~ /^\w+(?:::\w+)*\z/ 1 29 30
Condition100%3/3
defined $ns and not $ns =~ /^\w+(?:::\w+)*\z/
!ll&&!rl&&r
2631
MC/DC100%2/2
defined $ns and not $ns =~ /^\w+(?:::\w+)*\z/
defined $ns not $ns =~ /^\w+(?:::\w+)*\z/
Truth table100%3/3
A and not(B)
A= defined $nsB= not $ns =~ /^\w+(?:::\w+)*\z/
ABresult
0X0
100
111
1615 29 my %seen;
1616 29 ▶ croak "The enum '$name' needs distinct plain strings as values" if !@$values || grep { !defined($_) || ref($_) || $seen{$_}++ } @$values;
Branch: 1 branch
BranchTrueFalseTotal
if not @$values or grep {not defined $_ or ref $_ or $seen{$_}++;} @$values 2 27 29
Condition66%6/9
not @$values or grep {not defined $_ or ref $_ or $seen{$_}++;} @$values
l!l&&r!l&&!r
1127
!defined($_) || ref $_ || $seen{$_}++
l!l&&r!l&&!r
0152
not defined $_ or ref $_
l!l&&r!l&&!r
0053
MC/DC100%2/2
not @$values or grep {not defined $_ or ref $_ or $seen{$_}++;} @$values
not @$values grep {not defined $_ or ref $_ or $seen{$_}++;} @$values
MC/DC33%1/3
!defined($_) || ref $_ || $seen{$_}++
not defined $_ ref $_ $seen{$_}++
Truth table100%3/3
not(A) or B
A= not @$valuesB= grep {not defined $_ or ref $_ or $seen{$_}++;} @$values
ABresult
1X1
011
000
Truth table50%2/4
not(A) or B || C
A= not defined $_B= ref $_C= $seen{$_}++
ABCresult
1XX1
01X1
0011
0000
1617  
1618 27 my $def = {name => $name, values => [@$values], package => $caller->[0], at => "$caller->[1] line $caller->[2]"};
1619 27 ▶ $ns //= _enum_namespace($caller->[0]);
Condition100%3/3
$ns //= _enum_namespace($caller->[0])
l!l&&r!l&&!r
1224
MC/DC100%2/2
$ns //= _enum_namespace($caller->[0])
$ns _enum_namespace($caller->[0])
Truth table100%3/3
A //= B
A= $nsB= _enum_namespace($caller->[0])
ABresult
1X1
011
000
1620 26 ▶ if (defined $ns) { _add_enum($ns, $def) }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ns) { } 23 3 26
1621 3 else { push @PENDING => $def }
1622 }
1623  
1624 31 ▶sub _enum_namespace {
Subroutine _enum_namespace: called CC=8 SCAR=20.8
1625 31 my ($package) = @_;
1626 31 my @parts = split /::/ => $package;
1627 31 ▶ while (@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 52 5 57
1628 52 my $ns = join '::' => @parts;
1629 52 pop @parts;
1630 52 (my $file = "$ns.pm") =~ s{::}{/}g;
1631 52 ▶ unless (exists $INC{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $INC{$file}) 45 7 52
1632 45 local ($@, $BUILDING);
1633 # A package with no file is fine, a file that fails to load is not.
1634 0 ▶ eval { require $file; 1 } or $@ =~ m/^Can't locate \Q$file\E in \@INC/ or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $file; 1 } } or $@ =~ /^Can't locate \Q$file\E in \@INC/ or die $@ 44 1 45
Condition66%2/3
eval { do { require $file; 1 } } or $@ =~ /^Can't locate \Q$file\E in \@INC/
l!l&&r!l&&!r
0441
MC/DC50%1/2
eval { do { require $file; 1 } } or $@ =~ /^Can't locate \Q$file\E in \@INC/
eval { do { require $file; 1 } } $@ =~ /^Can't locate \Q$file\E in \@INC/
Truth table66%2/3
A or B
A= eval { do { require $file; 1 } }B= $@ =~ /^Can't locate \Q$file\E in \@INC/
ABresult
1X1
011
000
1635 }
1636 51 ▶ return $ns if ($IMPORTED{$ns} // {})->{orm} || $ns->isa('DBIx::QuickORM::ORM');
Branch: 1 branch
BranchTrueFalseTotal
if +($IMPORTED{$ns} // {})->{"orm"} or $ns->isa("DBIx::QuickORM::ORM") 25 26 51
Condition100%5/5
+($IMPORTED{$ns} // {})->{"orm"} or $ns->isa("DBIx::QuickORM::ORM")
l!l&&r!l&&!r
24126
$IMPORTED{$ns} // {}
l!l
3912
MC/DC100%2/2
+($IMPORTED{$ns} // {})->{"orm"} or $ns->isa("DBIx::QuickORM::ORM")
$IMPORTED{$ns} $ns->isa("DBIx::QuickORM::ORM")
Truth table100%3/3
A or B
A= $IMPORTED{$ns}B= $ns->isa("DBIx::QuickORM::ORM")
ABresult
1X1
011
000
1637 }
1638 5 return undef;
1639 }
1640  
1641 26 ▶sub _add_enum {
Subroutine _add_enum: called CC=4 SCAR=13.9
1642 26 my ($ns, $def) = @_;
1643 26 ▶ my $have = ($ENUMS{$ns} // {})->{$def->{name}};
Condition100%2/2
$ENUMS{$ns} // {}
l!l
818
MC/DC100%1/1
$ENUMS{$ns} // {}
$ENUMS{$ns}
Truth table100%2/2
A
A= $ENUMS{$ns}
Aresult
11
00
1644 26 ▶ if ($have) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have) 3 23 26
1645 3 ▶ return if join("\0" => @{$have->{values}}) eq join("\0" => @{$def->{values}});
Branch: 1 branch
BranchTrueFalseTotal
if join("\0", @{$have->{"values"};}) eq join("\0", @{$def->{"values"};}) 1 2 3
1646 2 croak "The enum '$def->{name}' of $ns, at $def->{at}, is already defined with other values (" . join(', ' => @{$have->{values}}) . ") at $have->{at}";
1647 }
1648 23 $ENUMS{$ns}->{$def->{name}} = $def;
1649 23 return;
1650 }
1651  
1652 4 ▶sub _place_pending {
Subroutine _place_pending: called CC=4 SCAR=13.9
1653 4 my @left;
1654 4 for my $def (@PENDING) {
1655 5 my $ns = _enum_namespace($def->{package});
1656 5 ▶ defined($ns) ? _add_enum($ns, $def) : push(@left => $def);
Branch: 1 branch
BranchTrueFalseTotal
defined $ns ? : 3 2 5
1657 }
1658 4 @PENDING = @left;
1659 4 ▶ return unless @left;
Branch: 1 branch
BranchTrueFalseTotal
unless @left 2 2 4
1660 2 my $def = $left[0];
1661 2 croak "The enum '$def->{name}' at $def->{at} belongs to no ORM class: no package in $def->{package}'s name is one, so give the namespace, as enum 'My::ORM', $def->{name} => [...]";
1662 }
1663  
1664 =pod
1665  
1666 =over 4
1667  
1668 =item _run($block, $package, \&code)
1669  
1670 Runs a block: checks its keywords are still the DSL's, runs the code with the
1671 block being built, records the spec, and removes the block and its keywords.
1672 Croaks inside another block, when the block was not imported, and when it
1673 already ran.
1674  
1675 =back
1676  
1677 =cut
1678  
1679 289 ▶sub _run {
Subroutine _run: called CC=13 SCAR=25.7
1680 289 my ($block, $package, $code) = @_;
1681  
1682 289 ▶ croak "A '$block' block cannot run inside another block" if $BUILDING;
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING 0 289 289
1683 289 ▶ croak "The '$block' block is not imported into $package" unless $IMPORTED{$package}->{$block};
Branch: 1 branch
BranchTrueFalseTotal
unless $IMPORTED{$package}{$block} 0 289 289
1684 289 ▶ 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 288 289
1685  
1686 # The block's code calls its keywords through the globs they were installed in, even after 'no'
1687 # deleted the names. A same-named sub compiled before that 'no' replaced the sub in the glob.
1688 288 for my $keyword (@{$KEYWORDS{$block}}) {
1689 2408 my $globs = $GLOBS{$package}->{$keyword};
1690 2408 for my $name (sort keys %$globs) {
1691 2408 my $code = *{$globs->{$name}}{CODE};
1692 2408 ▶ next if $code && $code == $OURS{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
if $code and $code == $OURS{$keyword} 2404 4 2408
Condition66%2/3
$code and $code == $OURS{$keyword}
!ll&&!rl&&r
042404
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
1693 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";
1694 }
1695 }
1696  
1697 284 my $spec = {};
1698 {
1699 284 local $BUILDING = {block => $block, package => $package, spec => $spec};
1700 284 $code->();
1701 }
1702  
1703 276 ▶ _finish_orm($package, $spec) if $block eq 'orm';
Branch: 1 branch
BranchTrueFalseTotal
if $block eq "orm" 80 196 276
1704 276 ▶ _finish_table($package, $spec, $block) if $block eq 'table' || $block eq 'view';
Branch: 1 branch
BranchTrueFalseTotal
if $block eq "table" or $block eq "view" 177 99 276
Condition100%3/3
$block eq "table" or $block eq "view"
l!l&&r!l&&!r
1463199
MC/DC100%2/2
$block eq "table" or $block eq "view"
$block eq "table" $block eq "view"
Truth table100%3/3
A or B
A= $block eq "table"B= $block eq "view"
ABresult
1X1
011
000
1705 273 ▶ _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 265 273
Condition100%3/3
$block eq "row" and $TABLE_SPECS{$package}
!ll&&!rl&&r
254118
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
1706  
1707 271 $RAN{$package}->{$block} = 1;
1708 271 _remove($package, $block, @{$KEYWORDS{$block}});
1709  
1710 271 return;
1711 }
1712  
1713 =pod
1714  
1715 =over 4
1716  
1717 =item _finish_orm($package, \%spec)
1718  
1719 Records an C<orm> spec and makes the package a L<DBIx::QuickORM::ORM>.
1720  
1721 =back
1722  
1723 =cut
1724  
1725 80 ▶sub _finish_orm {
Subroutine _finish_orm: called CC=2 SCAR=7.0
1726 80 my ($package, $spec) = @_;
1727  
1728 80 require DBIx::QuickORM::ORM;
1729 36 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1730 80 ▶ push @{"$package\::ISA"} => 'DBIx::QuickORM::ORM' unless $package->isa('DBIx::QuickORM::ORM');
Branch: 1 branch
BranchTrueFalseTotal
unless $package->isa("DBIx::QuickORM::ORM") 80 0 80
1731  
1732 80 $ORM_SPECS{$package} = $spec;
1733 }
1734  
1735 =pod
1736  
1737 =over 4
1738  
1739 =item _finish_table($package, \%spec, $block)
1740  
1741 Checks and records a C<table> or C<view> spec (C<kind> says which), naming
1742 the package as the row class when its C<row> block already ran.
1743  
1744 =back
1745  
1746 =cut
1747  
1748 177 ▶sub _finish_table {
Subroutine _finish_table: called CC=6 SCAR=17.9
1749 177 my ($package, $spec, $block) = @_;
1750  
1751 177 ▶ croak "The '$block' 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 176 177
Condition66%2/3
defined $spec->{"name"} and length $spec->{"name"}
!ll&&!rl&&r
10176
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
1752 176 ▶ croak "The '$block' 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 175 176
Condition100%3/3
$spec->{"omit"} and $spec->{"default_fields"}
!ll&&!rl&&r
17051
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
1753 175 $spec->{kind} = $block;
1754  
1755 175 ▶ _claim_table($package, $spec) if $RAN{$package}->{row};
Branch: 1 branch
BranchTrueFalseTotal
if $RAN{$package}{"row"} 2 173 175
1756  
1757 174 $TABLE_SPECS{$package} = $spec;
1758 }
1759  
1760 =pod
1761  
1762 =over 4
1763  
1764 =item _claim_table($package, \%spec)
1765  
1766 Names the package as the row class in its table spec, once both its C<table>
1767 and C<row> blocks have run. Croaks when the
1768 table block's C<row_class> names another class.
1769  
1770 =back
1771  
1772 =cut
1773  
1774 141 ▶sub _claim_schema {
Subroutine _claim_schema: called CC=4 SCAR=14.2
1775 141 my ($what, $package) = @_;
1776 141 ▶ croak "'$what' cannot be used inside a block" if $BUILDING;
Branch: 1 branch
BranchTrueFalseTotal
if $DBIx::QuickORM::DSL::BUILDING 0 141 141
1777 141 ▶ croak "The ':schema' tag is not imported into $package" unless $IMPORTED{$package}->{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless $IMPORTED{$package}{"schema"} 0 141 141
1778 141 ▶ croak "$package already defines its table's schema; give one 'schema' block, or 'schema_sql' calls" if $SCHEMA_SPECS{$package};
Branch: 1 branch
BranchTrueFalseTotal
if $SCHEMA_SPECS{$package} 0 141 141
1779 141 $RAN{$package}->{schema} = 1;
1780 # The block's code keeps calling the keywords it was compiled with, so they can go now.
1781 141 _remove($package, 'schema', @{$KEYWORDS{schema}});
1782 141 return;
1783 }
1784  
1785 10 ▶sub _claim_table {
Subroutine _claim_table: called CC=5 SCAR=16.1
1786 10 my ($package, $spec) = @_;
1787  
1788 10 my $named = $spec->{row_class};
1789 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
1790 7 $spec->{row_class} = $package;
1791 }
1792  
1793 =pod
1794  
1795 =over 4
1796  
1797 =item $code = _code($package, $name)
1798  
1799 The sub named in the package, or undef.
1800  
1801 =back
1802  
1803 =cut
1804  
1805 6067 ▶sub _code {
Subroutine _code: called CC=2 SCAR=6.9
1806 6067 my ($package, $name) = @_;
1807  
1808 36 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1809 6067 ▶ return undef unless exists ${"$package\::"}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists do { "${package}::" }->{$name} 6052 15 6067
1810 15 return *{"$package\::$name"}{CODE};
1811 }
1812  
1813 =pod
1814  
1815 =over 4
1816  
1817 =item _remove($package, @keywords)
1818  
1819 Deletes the names the DSL installed for the keywords, when they are still in
1820 the package and still hold the DSL's sub, keeping the glob's other slots. The
1821 deleted globs keep the sub, so blocks already compiled still call it.
1822  
1823 =back
1824  
1825 =cut
1826  
1827 440 ▶sub _remove {
Subroutine _remove: called CC=12 SCAR=24.9
1828 440 my ($package, @keywords) = @_;
1829  
1830 36 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1831 440 my $stash = \%{"$package\::"};
1832  
1833 440 for my $keyword (@keywords) {
1834 6232 ▶ my $globs = $GLOBS{$package}->{$keyword} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $globs = $GLOBS{$package}{$keyword} or next 5530 702 6232
1835 5530 for my $name (keys %$globs) {
1836 5530 ▶ next unless exists $stash->{$name} && \*{"$package\::$name"} == $globs->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{$name} and \*{"${package}::$name";} == $globs->{$name} 205 5325 5530
Condition100%3/3
exists $stash->{$name} and \*{"${package}::$name";} == $globs->{$name}
!ll&&!rl&&r
20415325
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
1837 5325 my $code = *{$globs->{$name}}{CODE};
1838 5325 ▶ next unless $code && $code == $OURS{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless $code and $code == $OURS{$keyword} 1 5324 5325
Condition66%2/3
$code and $code == $OURS{$keyword}
!ll&&!rl&&r
015324
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
1839  
1840 # Deleting the glob drops its other slots too, so put back the ones in use.
1841 5324 my $glob = delete $stash->{$name};
1842 5324 for my $slot (qw/SCALAR ARRAY HASH IO/) {
1843 21296 ▶ my $ref = *{$glob}{$slot} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $ref = *{$glob;}{$slot} or next 5325 15971 21296
1844 5325 ▶ next if $slot eq 'SCALAR' && !defined($$ref);
Branch: 1 branch
BranchTrueFalseTotal
if $slot eq "SCALAR" and not defined $$ref 5324 1 5325
Condition66%2/3
$slot eq "SCALAR" and not defined $$ref
!ll&&!rl&&r
105324
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
1845 1 *{"$package\::$name"} = $ref;
1846 }
1847 }
1848 }
1849  
1850 440 mro::method_changed_in($package);
1851 }
1852  
1853 36 1;
1854  
1855 __END__
« blib/lib/DBIx/QuickORM/Process/BuildSchema.pm ↑ Summary blib/lib/DBIx/QuickORM/Handle.pm »