blib/lib/DBIx/QuickORM/SchemaBuilder/Table.pm

filter: Statementstmt 97.1% 239 / 246 Branchbran 97.7% 131 / 134 Conditioncond 74.4% 96 / 129 MC/DCmcdc 55.2% 37 / 67 Subroutinesub 96.3% 26 / 27 Podpod 83.3% 10 / 12 totaltotal 87.6% 539 / 615 CC 224 SCAR 25.5
CC
224
Cov
92%
add_column
28.0
add_column
28.0
_check_enum
16.7
CRAP 12.8
« blib/lib/DBIx/QuickORM/AggregateRow.pm ↑ Summary blib/lib/DBIx/QuickORM/Source/Join.pm »
1 package DBIx::QuickORM::SchemaBuilder::Table;
2 260 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 260 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 260 our $VERSION = '0.000033';
6  
7 260 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 260 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 260 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
11 <orm
12 <name
13 +columns
14 +indexes
15 +pk
16 +row_class
17 +built
18 +fill
19 +checks
20 +uniques
21 +foreign_keys
22 260 };
23  
24 =pod
25  
26 =encoding UTF-8
27  
28 =head1 NAME
29  
30 DBIx::QuickORM::SchemaBuilder::Table - Describe a table: its columns, indexes, and key.
31  
32 =head1 DESCRIPTION
33  
34 The description of a table, as plain data the dialect turns into SQL. This
35 base holds what every table has. A subclass creates the table:
36 L<DBIx::QuickORM::SchemaBuilder::Table::Temp> as a temporary table, which is
37 what C<< $orm->temp_table >> returns or passes to its block.
38  
39 Each method takes what it adds, then a hashref of options named as the
40 L<DBIx::QuickORM::DSL> keywords are. C<add_column>, C<add_index>,
41 C<add_unique>, C<add_check>, C<add_foreign_key>, C<primary_key>,
42 C<row_class>, and C<fill> return the builder, so calls chain, and croak once
43 the table is built.
44  
45 =head1 SYNOPSIS
46  
47 my $t = $orm->temp_table(sub {
48 my $t = shift;
49 $t->add_column('id', {
50 type => 'UUID',
51 primary_key => 1,
52 });
53 $t->add_column('name', {
54 db_type => 'varchar(255)',
55 unique => 1,
56 });
57 $t->add_column('rank', {db_type => 'integer'});
58 $t->add_index('rank');
59 });
60  
61 A table class's C<schema> block gets the same builder (see
62 L<DBIx::QuickORM::DSL/":schema">), and the examples below use it there.
63  
64 =head1 ATTRIBUTES
65  
66 =over 4
67  
68 =item $name = $builder->name
69  
70 The table's name. Required.
71  
72 =item $orm = $builder->orm
73  
74 The ORM, which a subclass that creates the table needs.
75  
76 =back
77  
78 =cut
79  
80 260 my %COLUMN_OPTS = map { $_ => 1 } qw/db_type type not_null primary_key identity unique default check collate generated references indexed enum/;
81 260 my %FK_OPTS = map { $_ => 1 } qw/table columns name on_delete on_update/;
82 260 my %INDEX_OPTS = map { $_ => 1 } qw/name unique using dialect/;
83 260 my %ENTRY_OPTS = map { $_ => 1 } qw/name unique using/;
84 260 my %DB_TYPE = map { $_ => 1 } qw/postgresql mysql mariadb sqlite duckdb any_mysql any/; # DBIx::QuickORM::Dialect's dialect_keys
85  
86 191 ▶sub init {
Subroutine init: called CC=3 SCAR=14.9
Pod undocumented
87 0 ▶ my $self = shift;
Subroutine init: not called CC=3 SCAR=14.9
Pod undocumented
88 0 ▶ croak "A table builder needs a 'name'" unless defined($self->{+NAME}) && length($self->{+NAME});
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->{"name"} and length $self->{"name"} 1 190 191
unless defined $self->{"name"} and length $self->{"name"} 0 0 0
Condition33%2/6
defined $self->{"name"} and length $self->{"name"}
!ll&&!rl&&r
10190
defined $self->{"name"} and length $self->{"name"}
!ll&&!rl&&r
000
MC/DC50%1/2
defined $self->{"name"} and length $self->{"name"}
defined $self->{"name"} length $self->{"name"}
MC/DC0%0/2
defined $self->{"name"} and length $self->{"name"}
defined $self->{"name"} length $self->{"name"}
Truth table66%2/3
A and B
A= defined $self->{"name"}B= length $self->{"name"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $self->{"name"}B= length $self->{"name"}
ABresult
0X0
100
111
89 0 $self->{+COLUMNS} = [];
90 0 $self->{+INDEXES} = [];
91 0 $self->{+CHECKS} = [];
92 0 $self->{+UNIQUES} = [];
93 0 $self->{+FOREIGN_KEYS} = [];
94 }
95  
96 =head1 PUBLIC METHODS
97  
98 =over 4
99  
100 =item $builder->add_column($name, \%options)
101  
102 =item $builder->add_column($name, \$sql)
103  
104 =item $builder->add_column($name, sql($sql, @binds))
105  
106 =item $builder->add_field(...)
107  
108 Adds a column, from options or from SQL.
109  
110 # the users table's schema block
111 schema {
112 my $t = shift;
113 $t->add_column('id', {
114 db_type => 'integer',
115 identity => 1,
116 primary_key => 1,
117 });
118 $t->add_column('name', \'text NOT NULL');
119 $t->add_column('email', {
120 db_type => 'text',
121 unique => 1,
122 });
123 $t->add_column('prefs', {type => 'JSON'});
124 $t->add_column('referred_by', {
125 db_type => 'integer',
126 references => ['users', 'id'],
127 });
128 $t->add_column('created', {
129 db_type => 'timestamp',
130 not_null => 1,
131 default => \'now()',
132 });
133 };
134  
135 On PostgreSQL 10 and later that builds (earlier versions have no identity
136 columns, so building croaks there):
137  
138 =for highlighter language=sql
139  
140 CREATE TABLE "users" (
141 "id" integer GENERATED BY DEFAULT AS IDENTITY,
142  
143 "name" text NOT NULL,
144 "email" text UNIQUE,
145 "prefs" jsonb,
146 "referred_by" integer,
147 "created" timestamp DEFAULT now() NOT NULL,
148  
149 PRIMARY KEY ("id"),
150 FOREIGN KEY ("referred_by") REFERENCES "users" ("id")
151 );
152  
153 =for highlighter language=perl
154  
155 An C<sql()> marker's binds are written in as literals:
156  
157 use DBIx::QuickORM::Value qw/sql/;
158  
159 $t->add_column('status', sql('text NOT NULL DEFAULT ?', 'new')); # "status" text NOT NULL DEFAULT 'new'
160  
161 C<add_field> is the same method: a temporary table's column is also its
162 field, so either name works.
163  
164 Given SQL, it is everything after the column's name, written as given
165 (C<< \'text COLLATE "C" NOT NULL' >>), with an C<sql()> marker's binds
166 (L<DBIx::QuickORM::Value>) written in as literals. The offline map takes its
167 type from the first word, and knows nothing else of it.
168  
169 The options are the column keywords of L<DBIx::QuickORM::DSL>. One that
170 takes no arguments takes a true value here, and C<references> takes its
171 arguments as an arrayref.
172  
173 =over 4
174  
175 =item db_type => $type
176  
177 =item db_type => {postgresql => $type, any_mysql => $type, sqlite => $type, any => $type, ...}
178  
179 The column type as the database spells it. The hash gives one per database,
180 keyed C<postgresql>, C<mysql>, C<mariadb>, C<sqlite>, C<duckdb>,
181 C<any_mysql> (MySQL and MariaDB), or C<any> (every database), the exact
182 dialect first, then C<any_mysql>, then C<any>. C<mysql> is MySQL's alone.
183 Without one for the database in use, the C<type> gives the column type
184 (L<DBIx::QuickORM::Role::Type/db_type_for>), or the build croaks.
185  
186 =item type => $type
187  
188 The field's type class in the ORM, as in C<new>'s C<types> (C<JSON>, or
189 C<+My::Type>).
190  
191 =item primary_key => 1
192  
193 The column is part of the primary key, in the order columns are added. Or
194 call the C<primary_key> method instead, but not both.
195  
196 =item identity => 1
197  
198 The database numbers the column. See L<DBIx::QuickORM::Manual::TempTables>
199 for each database's rules.
200  
201 =item unique => 1
202  
203 The column is unique, enforced by the database.
204  
205 =item indexed => 1
206  
207 =item indexed => {postgresql => \%options, sqlite => undef, any_mysql => \'CREATE INDEX ...'}
208  
209 An index on the column, named for the table and column
210 (C<posts_created_idx>). The hash varies it per dialect as C<add_index>'s
211 C<dialect> does, a dialect without an entry getting the plain index. A
212 unique column already has one, so C<indexed> with C<unique> croaks.
213  
214 =item default => $value | \$sql | sql(...)
215  
216 The column's default. A plain value is quoted as a value. A reference to SQL
217 or an C<sql()> marker is an SQL expression, such as C<\'CURRENT_TIMESTAMP'>.
218  
219 =item generated => \$sql | sql(...)
220  
221 The SQL expression that computes the column, such as C<\'price * qty'>.
222 DuckDB computes it on read, the others store it.
223  
224 =item references => $table
225  
226 =item references => [$table, $column, {on_delete => $action, on_update => $action}]
227  
228 A foreign key to that table's column, the column of the same name when not
229 given. The actions are what the database does to this row when the row it
230 points at is deleted or its key updated: C<cascade>, C<set null>,
231 C<set default>, C<restrict>, or C<no action>. DuckDB has none.
232  
233 For a temporary table it is a link in the map only: the database is not
234 told, so it enforces nothing, and actions croak. C<follow_fk> and joins use
235 it either way.
236  
237 =item not_null => 1
238  
239 The column is C<NOT NULL>.
240  
241 =item enum => $name
242  
243 =item enum => \@values
244  
245 The column holds one of the enum's values, in place of C<db_type> and
246 C<type>. A name is an enum defined with the DSL's C<enum> for the ORM class
247 (L<DBIx::QuickORM::Manual::SchemaBuilding/ENUMS>). Values make a
248 one-off enum for this column alone. The DSL's C<enum 'My::ORM', 'mood'>
249 gives C<< {namespace => 'My::ORM', name => 'mood'} >>.
250  
251 =item check => \$sql | sql(...)
252  
253 A C<CHECK> on the column, the SQL expression it must satisfy.
254  
255 =item collate => $name | {postgresql => $name, mysql => $name, ...}
256  
257 The column's collation, resolved like C<db_type>, with the same keys.
258 Collation names differ on every database
259 (L<DBIx::QuickORM::Manual::SchemaBuilding> lists them).
260  
261 =back
262  
263 =back
264  
265 =cut
266  
267 2 ▶sub add_field { my $self = shift; $self->add_column(@_) }
Subroutine add_field: called CC=1 SCAR=0.0
Pod documented
268  
269 221 ▶sub add_column {
Subroutine add_column: called CC=28 SCAR=33.3
Pod documented
270 221 my $self = shift;
271 221 $self->_check_open('add_column');
272 219 my ($name, $how) = @_;
273  
274 219 ▶ croak "add_column() takes the column's name first: add_column('note', \\'text')" if ref($name);
Branch: 1 branch
BranchTrueFalseTotal
if ref $name 2 217 219
275 217 ▶ croak "add_column() needs a column name" unless defined($name) && length($name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 1 216 217
Condition66%2/3
defined $name and length $name
!ll&&!rl&&r
01216
MC/DC50%1/2
defined $name and length $name
defined $name length $name
Truth table66%2/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
276 216 ▶ croak "add_column('$name') takes one hashref of options, or SQL" if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 2 214 216
277 214 ▶ croak "The column '$name' is already added" if grep { $_->{name} eq $name } @{$self->{+COLUMNS}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->{"name"} eq $name;} @{$$self{"columns"};} 2 212 214
278  
279 212 ▶ if (ref($how) eq 'SCALAR' || $self->_is_sql($how)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $how eq "SCALAR" or $self->_is_sql($how)) 6 206 212
Condition100%3/3
ref $how eq "SCALAR" or $self->_is_sql($how)
l!l&&r!l&&!r
51206
MC/DC100%2/2
ref $how eq "SCALAR" or $self->_is_sql($how)
ref $how eq "SCALAR" $self->_is_sql($how)
Truth table100%3/3
A or B
A= ref $how eq "SCALAR"B= $self->_is_sql($how)
ABresult
1X1
011
000
280 6 push @{$self->{+COLUMNS}} => {name => $name, sql => $how};
281 6 return $self;
282 }
283 206 ▶ croak "add_column('$name') takes a hashref of options, \\\$sql, or an sql() marker" if defined($how) && ref($how) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if defined $how and ref $how ne "HASH" 1 205 206
Condition100%3/3
defined $how and ref $how ne "HASH"
!ll&&!rl&&r
32021
MC/DC100%2/2
defined $how and ref $how ne "HASH"
defined $how ref $how ne "HASH"
Truth table100%3/3
A and B
A= defined $howB= ref $how ne "HASH"
ABresult
0X0
100
111
284  
285 205 ▶ my %opts = %{$how // {}};
Condition100%2/2
$how // {}
l!l
2023
MC/DC100%1/1
$how // {}
$how
Truth table100%2/2
A
A= $how
Aresult
11
00
286 205 my @bad = sort grep { !$COLUMN_OPTS{$_} } keys %opts;
287 205 ▶ croak "column '$name' got unknown option(s): " . join(', ' => @bad) if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 5 200 205
288  
289 200 for my $opt (qw/db_type collate/) {
290 398 my $value = $opts{$opt};
291 398 ▶ next unless ref($value);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 386 12 398
292 12 ▶ croak "column '$name': '$opt' must be a string or a hashref of database => value" unless ref($value) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 1 11 12
293 11 my @keys = sort grep { !$DB_TYPE{$_} } keys %$value;
294 11 ▶ croak "column '$name': '$opt' names unknown database(s): " . join(', ' => @keys) . ", known: " . join(', ' => sort keys %DB_TYPE) if @keys;
Branch: 1 branch
BranchTrueFalseTotal
if @keys 2 9 11
295 }
296 197 ▶ $opts{enum} = $self->_check_enum($name, $opts{enum}) if exists $opts{enum};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"enum"} 31 166 197
297 193 ▶ croak "column '$name': 'enum' gives the column's type, so it takes no 'db_type' or 'type'" if exists($opts{enum}) && (exists($opts{db_type}) || exists($opts{type}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"enum"} and exists $opts{"db_type"} || exists $opts{"type"} 3 190 193
Condition100%6/6
exists $opts{"enum"} and exists $opts{"db_type"} || exists $opts{"type"}
!ll&&!rl&&r
166243
exists $opts{"db_type"} or exists $opts{"type"}
l!l&&r!l&&!r
2124
MC/DC100%3/3
exists $opts{"enum"} and exists $opts{"db_type"} || exists $opts{"type"}
exists $opts{"enum"} exists $opts{"db_type"} exists $opts{"type"}
Truth table100%4/4
A and B or C
A= exists $opts{"enum"}B= exists $opts{"db_type"}C= exists $opts{"type"}
ABCresult
0XX0
1000
11X1
1011
298 190 ▶ if (my $index = $opts{indexed}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $index = $opts{"indexed"}) 14 176 190
299 14 ▶ croak "column '$name': 'unique' already gives the column an index, so it takes no 'indexed'" if $opts{unique};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"unique"} 1 13 14
300 13 ▶ $self->_check_index_dialects("column '$name': 'indexed'", $index) if ref $index;
Branch: 1 branch
BranchTrueFalseTotal
if ref $index 8 5 13
301 }
302 185 for my $opt (qw/check generated/) {
303 369 ▶ croak "column '$name': '$opt' must be \\\$sql or an sql() marker" if exists($opts{$opt}) && !(ref($opts{$opt}) eq 'SCALAR' || $self->_is_sql($opts{$opt}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{$opt} and not ref $opts{$opt} eq "SCALAR" || $self->_is_sql($opts{$opt}) 2 367 369
Condition83%5/6
exists $opts{$opt} and not ref $opts{$opt} eq "SCALAR" || $self->_is_sql($opts{$opt})
!ll&&!rl&&r
36252
ref $opts{$opt} eq 'SCALAR' || $self->_is_sql($opts{$opt})
l!l&&r!l&&!r
502
MC/DC66%2/3
exists $opts{$opt} and not ref $opts{$opt} eq "SCALAR" || $self->_is_sql($opts{$opt})
exists $opts{$opt} ref $opts{$opt} eq 'SCALAR' $self->_is_sql($opts{$opt})
Truth table75%3/4
A and not(B || C)
A= exists $opts{$opt}B= ref $opts{$opt} eq 'SCALAR'C= $self->_is_sql($opts{$opt})
ABCresult
0XX0
11X0
1010
1001
304 }
305 183 ▶ $opts{references} = $self->_check_references($name, $opts{references}) if exists $opts{references};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"references"} 20 163 183
306  
307 180 push @{$self->{+COLUMNS}} => {%opts, name => $name};
308 180 return $self;
309 }
310  
311 =pod
312  
313 =over 4
314  
315 =item $builder->add_index($column)
316  
317 =item $builder->add_index($column, \%options)
318  
319 =item $builder->add_index(\@columns, \%options)
320  
321 Adds an index over one column or several.
322  
323 # the posts table's schema block, after its columns
324 $t->add_index('user_id');
325 $t->add_index(['user_id', 'title'], {
326 name => 'posts_user_title',
327 unique => 1,
328 });
329 $t->add_index('title', {
330 dialect => {
331 postgresql => {using => \'gin (to_tsvector(\'english\', title))'},
332 sqlite => undef,
333 },
334 });
335  
336 On PostgreSQL those build these, after the table:
337  
338 =for highlighter language=sql
339  
340 CREATE INDEX "posts_idx_1" ON "posts" ("user_id");
341 CREATE UNIQUE INDEX "posts_user_title" ON "posts" ("user_id", "title");
342 CREATE INDEX "posts_idx_2" ON "posts" USING gin (to_tsvector('english', title));
343  
344 =for highlighter language=perl
345  
346 SQLite gets the first two, and no index on C<title>.
347  
348 The options:
349  
350 =over 4
351  
352 =item name => $name
353  
354 Its name, by default the table's name with C<_idx_N>.
355  
356 =item unique => 1
357  
358 A unique index.
359  
360 =item using => $type
361  
362 =item using => \$sql
363  
364 The index type (C<btree>, C<hash>, C<gin>, and so on) on PostgreSQL and
365 MySQL, which croaks at build on SQLite and DuckDB, which have none. A
366 reference to SQL is everything after C<USING>: the method, the columns, and
367 anything after them (C<< \'gin (body gin_trgm_ops)' >>,
368 C<< \'btree (user_id) WHERE deleted IS NULL' >>). It is not checked: the
369 database reports a mistake, and schema building names the statement.
370  
371 =item dialect => \%by_dialect
372  
373 The index per dialect, keyed as other per-database values: an options hash
374 (C<name>, C<unique>, C<using>) merged over the others, undef for no index on
375 that dialect, or a reference to the whole C<CREATE INDEX> statement, written
376 as given right after the table (not for temporary tables). A dialect without
377 an entry gets the index as the other options give it.
378  
379 =back
380  
381 =item $builder->add_check(\$sql)
382  
383 =item $builder->add_check(\$sql, {name => $name})
384  
385 =item $builder->add_check(sql($sql, @binds), {name => $name})
386  
387 A C<CHECK> on the table.
388  
389 # the posts table's schema block, after its columns
390 $t->add_check(\'views >= 0'); # CHECK (views >= 0)
391 $t->add_check(sql('title <> ?', ''), {name => 'title_given'}); # CONSTRAINT "title_given" CHECK (title <> '')
392 $t->add_unique(['user_id', 'title']); # UNIQUE ("user_id", "title")
393  
394 An C<sql()> marker's binds are written in as literals.
395  
396 =item $builder->add_unique($column)
397  
398 =item $builder->add_unique(\@columns, {name => $name})
399  
400 A C<UNIQUE> constraint over the columns. The example under C<add_check>
401 shows one.
402  
403 =item $builder->add_foreign_key($column, {table => $table})
404  
405 =item $builder->add_foreign_key(\@columns, {table => $table, columns => \@theirs, name => $name, on_delete => $action, on_update => $action})
406  
407 A foreign key over several columns, as C<references> is over one.
408  
409 # the grants table's schema block, after its columns
410 $t->add_foreign_key(['user_id', 'org_id'], {
411 table => 'memberships',
412 on_delete => 'cascade',
413 });
414 $t->add_foreign_key('granted_by', {
415 table => 'users',
416 columns => ['id'],
417 name => 'grants_granted_by',
418 });
419  
420 On PostgreSQL:
421  
422 =for highlighter language=sql
423  
424 CREATE TABLE "grants" (
425 "user_id" integer,
426 "org_id" integer,
427 "granted_by" integer,
428  
429 FOREIGN KEY ("user_id", "org_id") REFERENCES "memberships" ("user_id", "org_id") ON DELETE CASCADE,
430 CONSTRAINT "grants_granted_by" FOREIGN KEY ("granted_by") REFERENCES "users" ("id")
431 );
432  
433 =for highlighter language=perl
434  
435 Without C<columns>, the other table's columns have the same names.
436  
437 =item $builder->primary_key($column)
438  
439 =item $builder->primary_key(\@columns)
440  
441 Sets the primary key, in place of C<< primary_key => 1 >> on columns.
442  
443 =item $builder->row_class($class_or_undef)
444  
445 The table's row class, as in C<row_classes>. Undef gives hashrefs.
446  
447 Temporary tables only: a C<schema> block calling it croaks. A table class sets its
448 row class in its C<table> block.
449  
450 =item $builder->fill($handle)
451  
452 =item $builder->fill(\[$select_sql, @binds])
453  
454 Fills a temporary table from the query's rows once it is created
455 (C<INSERT INTO ... SELECT>).
456  
457 # from a handle: columns match by name
458 my $names = $orm->temp_table(sub {
459 my $t = shift;
460 $t->add_column('id', {
461 db_type => 'integer',
462 primary_key => 1,
463 });
464 $t->add_column('name', {db_type => 'text'});
465 $t->fill($orm->handle('users')->fields(['name'])); # the key comes too: id and name
466 });
467  
468 # from a SELECT: columns match by position
469 my $popular = $orm->temp_table(sub {
470 my $t = shift;
471 $t->add_column('post_id', {db_type => 'integer'});
472 $t->add_column('title', {db_type => 'text'});
473 $t->fill(\['SELECT id, title FROM posts WHERE views > ?', 10]);
474 });
475  
476 From a handle, each field the handle selects must be a column of the table
477 (a table handle given C<fields> as an arrayref adds its primary key). A join, or a
478 handle over SQL without C<fields>, croaks: give a C<SELECT> instead, whose
479 columns match by position, in the order columns were added. A handle with a
480 background mode croaks at build.
481  
482 Temporary tables only: a C<schema> block calling it croaks.
483  
484 =back
485  
486 =cut
487  
488 22 ▶sub add_index {
Subroutine add_index: called CC=4 SCAR=13.9
Pod documented
489 22 my $self = shift;
490 22 $self->_check_open('add_index');
491 22 my ($columns, $opts) = $self->_target(add_index => @_);
492 20 my @bad = sort grep { !$INDEX_OPTS{$_} } keys %$opts;
493 20 ▶ croak "add_index() got unknown option(s): " . join(', ' => @bad) . ", it takes 'name', 'unique', 'using', and 'dialect'" if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 19 20
494 19 ▶ $self->_check_using('add_index()', $opts->{using}) if exists $opts->{using};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"using"} 3 16 19
495 18 ▶ $self->_check_index_dialects('add_index()', $opts->{dialect}) if exists $opts->{dialect};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"dialect"} 4 14 18
496 17 push @{$self->{+INDEXES}} => {%$opts, columns => $columns};
497 17 return $self;
498 }
499  
500 11 ▶sub primary_key {
Subroutine primary_key: called CC=2 SCAR=6.9
Pod documented
501 11 my $self = shift;
502 11 $self->_check_open('primary_key');
503 10 ▶ croak "primary_key() takes one column name or an arrayref of them" unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 3 7 10
504 7 my ($columns) = $self->_target(primary_key => @_);
505 7 $self->{+PK} = $columns;
506 7 return $self;
507 }
508  
509 5 ▶sub row_class {
Subroutine row_class: called CC=2 SCAR=6.9
Pod documented
510 5 my $self = shift;
511 5 $self->_check_open('row_class');
512 5 ▶ croak "row_class() takes one class name or undef" unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 1 4 5
513 4 $self->{+ROW_CLASS} = $_[0];
514 4 return $self;
515 }
516  
517 9 ▶sub add_check {
Subroutine add_check: called CC=5 SCAR=16.1
Pod documented
518 9 my $self = shift;
519 9 $self->_check_open('add_check');
520 9 my ($sql, $opts) = @_;
521 9 ▶ croak "add_check() takes \\\$sql or an sql() marker, then an optional hashref of options" unless (ref($sql) eq 'SCALAR' || $self->_is_sql($sql)) && @_ <= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sql eq "SCALAR" || $self->_is_sql($sql) and @_ <= 2 2 7 9
Condition100%6/6
ref $sql eq "SCALAR" || $self->_is_sql($sql) and @_ <= 2
!ll&&!rl&&r
117
ref $sql eq "SCALAR" or $self->_is_sql($sql)
l!l&&r!l&&!r
711
MC/DC100%3/3
ref $sql eq "SCALAR" || $self->_is_sql($sql) and @_ <= 2
ref $sql eq "SCALAR" $self->_is_sql($sql) @_ <= 2
Truth table80%4/5
A or B and C
A= ref $sql eq "SCALAR"B= $self->_is_sql($sql)C= @_ <= 2
ABCresult
00X0
1X00
0100
1X11
0111
522 7 $opts = $self->_options(add_check => $opts);
523 6 my @bad = sort grep { $_ ne 'name' } keys %$opts;
524 6 ▶ croak "add_check() got unknown option(s): " . join(', ' => @bad) . ", it takes 'name'" if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 5 6
525 5 push @{$self->{+CHECKS}} => {%$opts, sql => $sql};
526 5 return $self;
527 }
528  
529 14 ▶sub add_unique {
Subroutine add_unique: called CC=2 SCAR=6.9
Pod documented
530 14 my $self = shift;
531 14 $self->_check_open('add_unique');
532 14 my ($columns, $opts) = $self->_target(add_unique => @_);
533 8 my @bad = sort grep { $_ ne 'name' } keys %$opts;
534 8 ▶ croak "add_unique() got unknown option(s): " . join(', ' => @bad) . ", it takes 'name'" if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 7 8
535 7 push @{$self->{+UNIQUES}} => {%$opts, columns => $columns};
536 7 return $self;
537 }
538  
539 14 ▶sub add_foreign_key {
Subroutine add_foreign_key: called CC=8 SCAR=20.8
Pod documented
540 14 my $self = shift;
541 14 $self->_check_open('add_foreign_key');
542 14 my ($columns, $opts) = $self->_target(add_foreign_key => @_);
543 13 my @bad = sort grep { !$FK_OPTS{$_} } keys %$opts;
544 13 ▶ croak "add_foreign_key() got unknown option(s): " . join(', ' => @bad) . ", it takes 'table', 'columns', 'name', 'on_delete', and 'on_update'" if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 12 13
545 12 my %fk = %$opts;
546 12 my $table = delete $fk{table};
547 12 ▶ croak "add_foreign_key() needs the other table: {table => \$name}" unless defined($table) && !ref($table) && length($table);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table and not ref $table and length $table 2 10 12
Condition66%4/6
defined $table and not ref $table and length $table
!ll&&!rl&&r
2010
defined $table and not ref $table
!ll&&!rl&&r
2010
MC/DC33%1/3
defined $table and not ref $table and length $table
defined $table not ref $table length $table
Truth table50%2/4
A and not(B) and C
A= defined $tableB= not ref $tableC= length $table
ABCresult
0XX0
10X0
1100
1111
548 10 ▶ my $theirs = exists($fk{columns}) ? (ref($fk{columns}) eq 'ARRAY' ? [@{$fk{columns}}] : [$fk{columns}]) : [@$columns];
Branch: 2 branches
BranchTrueFalseTotal
exists $fk{'columns'} ? : 7 3 10
ref $fk{'columns'} eq 'ARRAY' ? : 3 4 7
549 10 delete $fk{columns};
550 10 ▶ croak "add_foreign_key() needs as many of the other table's columns as its own" unless @$theirs == @$columns;
Branch: 1 branch
BranchTrueFalseTotal
unless @$theirs == @$columns 1 9 10
551 9 push @{$self->{+FOREIGN_KEYS}} => {%fk, columns => $columns, table => $table, their_columns => $theirs};
552 9 return $self;
553 }
554  
555 14 ▶sub fill {
Subroutine fill: called CC=4 SCAR=13.9
Pod documented
556 14 my $self = shift;
557 14 $self->_check_open('fill');
558 14 ▶ croak "fill() takes one handle or \\[\$select_sql, \@binds]" unless @_ == 1 && ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 and ref $_[0] 1 13 14
Condition66%2/3
@_ == 1 and ref $_[0]
!ll&&!rl&&r
0113
MC/DC50%1/2
@_ == 1 and ref $_[0]
@_ == 1 ref $_[0]
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0]
ABresult
0X0
100
111
559 13 ▶ croak "fill() was called already" if $self->{+FILL};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fill"} 1 12 13
560 12 $self->{+FILL} = $_[0];
561 12 return $self;
562 }
563  
564 =pod
565  
566 =over 4
567  
568 =item \%spec = $builder->spec
569  
570 The description as plain data, which the dialect turns into SQL.
571  
572 my $t = DBIx::QuickORM::SchemaBuilder::Table->new(name => 'tags');
573 $t->add_column('name', {db_type => 'text'});
574 $t->primary_key('name');
575  
576 my $spec = $t->spec;
577 # {
578 # columns => [{name => 'name', db_type => 'text'}],
579 # indexes => [],
580 # pk => ['name'],
581 # checks => [],
582 # uniques => [],
583 # foreign_keys => [],
584 # }
585  
586 It holds C<columns>, C<indexes>, C<pk> (undef for
587 none), C<checks>, C<uniques>, C<foreign_keys> (from C<add_foreign_key>,
588 each with C<columns>, C<table>, and C<their_columns>), and C<row_class> only
589 when C<row_class()> was called (its value may be undef). Each column's
590 C<references> is C<< {table => $table, column => $column} >> with its
591 actions, and its C<db_type> is as given.
592  
593 =back
594  
595 =cut
596  
597 97 ▶sub spec {
Subroutine spec: called CC=11 SCAR=24.0
Pod documented
598 97 my $self = shift;
599 97 my @columns = @{$self->{+COLUMNS}};
600 97 my @marked = map { $_->{name} } grep { $_->{primary_key} } @columns;
601 97 ▶ croak "The primary key is given both by primary_key() and by 'primary_key' on columns" if @marked && $self->{+PK};
Branch: 1 branch
BranchTrueFalseTotal
if @marked and $self->{"pk"} 1 96 97
Condition100%3/3
@marked and $self->{"pk"}
!ll&&!rl&&r
49471
MC/DC100%2/2
@marked and $self->{"pk"}
@marked $self->{"pk"}
Truth table100%3/3
A and B
A= @markedB= $self->{"pk"}
ABresult
0X0
100
111
602 96 ▶ my $pk = $self->{+PK} // (@marked ? \@marked : undef);
Branch: 1 branch
BranchTrueFalseTotal
@marked ? : 47 43 90
Condition100%2/2
$self->{"pk"} // (@marked ? \@marked : undef)
l!l
690
MC/DC100%1/1
$self->{"pk"} // (@marked ? \@marked : undef)
$self->{"pk"}
Truth table100%2/2
A
A= $self->{"pk"}
Aresult
11
00
603 96 my %named = map { ($_->{name} => 1) } @columns;
604 96 ▶ for my $c (@{$pk // []}, map { @{$_->{columns}} } @{$self->{+INDEXES}}, @{$self->{+UNIQUES}}, @{$self->{+FOREIGN_KEYS}}) {
Condition100%2/2
$pk // []
l!l
5343
MC/DC100%1/1
$pk // []
$pk
Truth table100%2/2
A
A= $pk
Aresult
11
00
605 98 ▶ croak "The table names the column '$c', which was not added" unless $named{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless $named{$c} 5 93 98
606 }
607 return {
608 172 ▶ columns => [map { +{%$_, (ref($_->{references}) ? (references => {%{$_->{references}}}) : ())} } @columns],
Branch: 1 branch
BranchTrueFalseTotal
ref $_->{'references'} ? : 17 155 172
609 15 ▶ indexes => [map { +{%$_, columns => [@{$_->{columns}}]} } @{$self->{+INDEXES}}],
Branch: 1 branch
BranchTrueFalseTotal
$pk ? : 52 39 91
610 pk => $pk ? [@$pk] : undef,
611 5 checks => [map { +{%$_} } @{$self->{+CHECKS}}],
612 6 uniques => [map { +{%$_, columns => [@{$_->{columns}}]} } @{$self->{+UNIQUES}}],
613 8 ▶ foreign_keys => [map { +{%$_, columns => [@{$_->{columns}}], their_columns => [@{$_->{their_columns}}]} } @{$self->{+FOREIGN_KEYS}}],
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'row_class'} ? : 4 87 91
614 91 (exists($self->{+ROW_CLASS}) ? (row_class => $self->{+ROW_CLASS}) : ()),
615 };
616 }
617  
618 =head1 PRIVATE METHODS
619  
620 =over 4
621  
622 =item $builder->_check_open($method)
623  
624 Croaks once the builder is built.
625  
626 =item $bool = $builder->_is_sql($value)
627  
628 True for an C<sql()> marker.
629  
630 =item (\@columns, \%options) = $builder->_target($method, $column_or_columns, \%options)
631  
632 A table-level method's arguments: one column or an arrayref of them, then an
633 optional hashref of options.
634  
635 =item \%options = $builder->_options($method, $options)
636  
637 The optional hashref of options, or an empty one.
638  
639 =item $enum = $builder->_check_enum($column, $enum)
640  
641 A column's C<enum>, checked: a name, an arrayref of distinct plain strings,
642 or C<< {namespace => $class, name => $name} >>. The values are copied.
643  
644 =item \%references = $builder->_check_references($column, $references)
645  
646 A column's C<references> as C<< {table, column, on_delete, on_update} >>,
647 the column defaulting to the same name.
648  
649 =item $builder->_check_using($what, $using)
650  
651 =item $builder->_check_index_dialects($what, \%by_dialect)
652  
653 An index's C<using>, and its per-dialect entries, checked.
654  
655 =item $what = $builder->_fill
656  
657 What C<fill> was given, or undef.
658  
659 =item $builder->_finish(\&create)
660  
661 For a subclass's C<build>: croaks when the builder was built already, or has
662 no columns, then runs C<create> and marks the builder built. Returns what
663 C<create> returns.
664  
665 =back
666  
667 =cut
668  
669 57 ▶sub _target {
Subroutine _target: called CC=10 SCAR=23.3
670 57 my $self = shift;
671 57 my ($method, $target, $opts, @extra) = @_;
672 57 ▶ croak "$method() takes one column or an arrayref of columns, then an optional hashref of options" if @extra;
Branch: 1 branch
BranchTrueFalseTotal
if @extra 4 53 57
673 53 ▶ my @columns = ref($target) eq 'ARRAY' ? @$target : (defined($target) && !ref($target) && length($target)) ? ($target) : croak "$method() needs a column name or an arrayref of them";
Branch: 2 branches
BranchTrueFalseTotal
ref $target eq 'ARRAY' ? : 17 36 53
defined $target && !ref($target) && length $target ? : 35 1 36
Condition66%4/6
defined $target and not ref $target and length $target
!ll&&!rl&&r
1035
defined $target and not ref $target
!ll&&!rl&&r
1035
MC/DC33%1/3
defined $target and not ref $target and length $target
defined $target not ref $target length $target
Truth table50%2/4
A and not(B) and C
A= defined $targetB= not ref $targetC= length $target
ABCresult
0XX0
10X0
1100
1111
674 52 ▶ croak "$method() needs at least one column" unless @columns;
Branch: 1 branch
BranchTrueFalseTotal
unless @columns 1 51 52
675 51 ▶ croak "$method() takes column names as plain strings" if grep { !defined($_) || ref($_) || !length($_) } @columns;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_ or ref $_ or not length $_;} @columns 1 50 51
Condition66%4/6
!defined($_) || ref $_ || !length($_)
l!l&&r!l&&!r
1062
not defined $_ or ref $_
l!l&&r!l&&!r
0162
MC/DC33%1/3
!defined($_) || ref $_ || !length($_)
not defined $_ ref $_ !length($_)
Truth table50%2/4
not(A) or B || not(C)
A= not defined $_B= ref $_C= !length($_)
ABCresult
1XX1
01X1
0011
0000
676 50 return (\@columns, $self->_options($method, $opts));
677 }
678  
679 57 ▶sub _options {
Subroutine _options: called CC=3 SCAR=11.0
680 57 my $self = shift;
681 57 my ($method, $opts) = @_;
682 57 ▶ return {} unless defined $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 25 32 57
683 32 ▶ croak "$method() takes its options as a hashref" unless ref($opts) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts eq "HASH" 3 29 32
684 29 return {%$opts};
685 }
686  
687 31 ▶sub _check_enum {
Subroutine _check_enum: called CC=15 SCAR=28.2
688 31 my $self = shift;
689 31 my ($column, $enum) = @_;
690 31 ▶ if (ref($enum) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $enum eq "ARRAY") 13 18 31
691 13 my %seen;
692 13 ▶ croak "column '$column': 'enum' values must be distinct plain strings" if !@$enum || grep { !defined($_) || ref($_) || $seen{$_}++ } @$enum;
Branch: 1 branch
BranchTrueFalseTotal
if not @$enum or grep {not defined $_ or ref $_ or $seen{$_}++;} @$enum 2 11 13
Condition66%6/9
not @$enum or grep {not defined $_ or ref $_ or $seen{$_}++;} @$enum
l!l&&r!l&&!r
1111
!defined($_) || ref $_ || $seen{$_}++
l!l&&r!l&&!r
0122
not defined $_ or ref $_
l!l&&r!l&&!r
0023
MC/DC100%2/2
not @$enum or grep {not defined $_ or ref $_ or $seen{$_}++;} @$enum
not @$enum grep {not defined $_ or ref $_ or $seen{$_}++;} @$enum
MC/DC33%1/3
!defined($_) || ref $_ || $seen{$_}++
not defined $_ ref $_ $seen{$_}++
Truth table100%3/3
not(A) or B
A= not @$enumB= grep {not defined $_ or ref $_ or $seen{$_}++;} @$enum
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
693 11 return [@$enum];
694 }
695 18 ▶ if (ref($enum) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $enum eq "HASH") 4 14 18
696 4 my ($ns, $name) = @{$enum}{qw/namespace name/};
697 4 ▶ croak "column '$column': 'enum' as a hashref is {namespace => \$class, name => \$name}" unless keys(%$enum) == 2 && defined($ns) && defined($name) && !ref($ns) && !ref($name);
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$enum == 2 and defined $ns and defined $name and not ref $ns and not ref $name 1 3 4
Condition66%8/12
keys %$enum == 2 and defined $ns and defined $name and not ref $ns and not ref $name
!ll&&!rl&&r
103
keys %$enum == 2 and defined $ns and defined $name and not ref $ns
!ll&&!rl&&r
103
keys %$enum == 2 and defined $ns and defined $name
!ll&&!rl&&r
103
keys %$enum == 2 and defined $ns
!ll&&!rl&&r
103
MC/DC20%1/5
keys %$enum == 2 and defined $ns and defined $name and not ref $ns and not ref $name
keys %$enum == 2 defined $ns defined $name not ref $ns not ref $name
Truth table33%2/6
A and B and C and not(D) and not(E)
A= keys %$enum == 2B= defined $nsC= defined $nameD= not ref $nsE= not ref $name
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
698 3 return {namespace => $ns, name => $name};
699 }
700 14 ▶ croak "column '$column': 'enum' takes an enum's name or an arrayref of values" unless defined($enum) && !ref($enum) && length($enum);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $enum and not ref $enum and length $enum 1 13 14
Condition50%3/6
defined $enum and not ref $enum and length $enum
!ll&&!rl&&r
0113
defined $enum and not ref $enum
!ll&&!rl&&r
0014
MC/DC33%1/3
defined $enum and not ref $enum and length $enum
defined $enum not ref $enum length $enum
Truth table50%2/4
A and not(B) and C
A= defined $enumB= not ref $enumC= length $enum
ABCresult
0XX0
10X0
1100
1111
701 13 return $enum;
702 }
703  
704 26 ▶sub _check_references {
Subroutine _check_references: called CC=13 SCAR=26.2
705 26 my $self = shift;
706 26 my ($column, $refs) = @_;
707 26 ▶ my @args = ref($refs) eq 'ARRAY' ? @$refs : ($refs);
Branch: 1 branch
BranchTrueFalseTotal
ref $refs eq 'ARRAY' ? : 21 5 26
708 26 ▶ my $opts = ref($args[-1]) eq 'HASH' ? pop(@args) : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[-1] eq 'HASH' ? : 10 16 26
709 26 my ($table, $their, @extra) = @args;
710 26 ▶ croak "column '$column': 'references' takes a table, then optionally its column and a hashref of 'on_delete' and 'on_update'" if @extra || !(defined($table) && !ref($table) && length($table)) || (defined($their) && (ref($their) || !length($their)));
Branch: 1 branch
BranchTrueFalseTotal
if @extra or not defined $table && !ref($table) && length $table or defined $their and ref $their || !length($their) 2 24 26
Condition66%12/18
@extra or not defined $table && !ref($table) && length $table or defined $their and ref $their || !length($their)
l!l&&r!l&&!r
2024
@extra or not defined $table && !ref($table) && length $table
l!l&&r!l&&!r
1124
defined $table && !ref($table) && length $table
!ll&&!rl&&r
1024
defined $table and not ref $table
!ll&&!rl&&r
1024
defined $their and ref $their || !length($their)
!ll&&!rl&&r
8160
ref $their or not length $their
l!l&&r!l&&!r
0016
MC/DC28%2/7
@extra or not defined $table && !ref($table) && length $table or defined $their and ref $their || !length($their)
@extra defined $table not ref $table length $table defined $their ref $their not length $their
Truth table50%4/8
A or not(B and not(C) && D) or E and F or not(G)
A= @extraB= defined $tableC= not ref $tableD= length $tableE= defined $theirF= ref $theirG= not length $their
ABCDEFGresult
1XXXXXX1
00XXXXX1
010XXXX1
0110XXX1
011111X1
01111011
01110XX0
01111000
711 24 ▶ my @bad = sort grep { $_ ne 'on_delete' && $_ ne 'on_update' } keys %$opts;
Condition100%3/3
$_ ne 'on_delete' && $_ ne 'on_update'
!ll&&!rl&&r
731
MC/DC100%2/2
$_ ne 'on_delete' && $_ ne 'on_update'
$_ ne 'on_delete' $_ ne 'on_update'
Truth table100%3/3
A && B
A= $_ ne 'on_delete'B= $_ ne 'on_update'
ABresult
0X0
100
111
712 24 ▶ croak "column '$column': 'references' got unknown option(s): " . join(', ' => @bad) . ", it takes 'on_delete' and 'on_update'" if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 23 24
713 23 ▶ return {%$opts, table => $table, column => $their // $column};
Condition66%2/3
$their // $column
l!l&&r!l&&!r
1580
MC/DC0%0/2
$their // $column
$their $column
Truth table66%2/3
A // B
A= $theirB= $column
ABresult
1X1
011
000
714 }
715  
716 9 ▶sub _check_using {
Subroutine _check_using: called CC=4 SCAR=13.9
717 9 my $self = shift;
718 9 my ($what, $using) = @_;
719 9 ▶ return if ref($using) eq 'SCALAR' || (defined($using) && $using =~ m/^\w+\z/);
Branch: 1 branch
BranchTrueFalseTotal
if ref $using eq "SCALAR" or defined $using and $using =~ /^\w+\z/ 8 1 9
Condition83%5/6
ref $using eq "SCALAR" or defined $using and $using =~ /^\w+\z/
l!l&&r!l&&!r
531
defined $using and $using =~ /^\w+\z/
!ll&&!rl&&r
013
MC/DC66%2/3
ref $using eq "SCALAR" or defined $using and $using =~ /^\w+\z/
ref $using eq "SCALAR" defined $using $using =~ /^\w+\z/
Truth table75%3/4
A or B and C
A= ref $using eq "SCALAR"B= defined $usingC= $using =~ /^\w+\z/
ABCresult
1XX1
0111
00X0
0100
720 1 croak "$what: 'using' must be an index type name, such as 'btree', or a reference to the SQL that follows USING";
721 }
722  
723 12 ▶sub _check_index_dialects {
Subroutine _check_index_dialects: called CC=9 SCAR=22.0
724 12 my $self = shift;
725 12 my ($what, $by) = @_;
726 12 ▶ croak "$what must be 1, or a hashref of dialect => index options, undef, or a reference to the index's SQL" unless ref($by) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $by eq "HASH" 1 11 12
727 11 my @keys = sort grep { !$DB_TYPE{$_} } keys %$by;
728 11 ▶ croak "$what names unknown dialect(s): " . join(', ' => @keys) . ", known: " . join(', ' => sort keys %DB_TYPE) if @keys;
Branch: 1 branch
BranchTrueFalseTotal
if @keys 1 10 11
729 10 for my $key (sort keys %$by) {
730 18 my $entry = $by->{$key};
731 18 ▶ next if !defined($entry) || ref($entry) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $entry or ref $entry eq "SCALAR" 9 9 18
Condition100%3/3
not defined $entry or ref $entry eq "SCALAR"
l!l&&r!l&&!r
639
MC/DC100%2/2
not defined $entry or ref $entry eq "SCALAR"
not defined $entry ref $entry eq "SCALAR"
Truth table100%3/3
not(A) or B
A= not defined $entryB= ref $entry eq "SCALAR"
ABresult
1X1
011
000
732 9 ▶ croak "$what: the '$key' entry must be an options hash, undef, or a reference to the index's SQL" unless ref($entry) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry eq "HASH" 2 7 9
733 7 my @bad = sort grep { !$ENTRY_OPTS{$_} } keys %$entry;
734 7 ▶ croak "$what: the '$key' entry got unknown option(s): " . join(', ' => @bad) . ", it takes 'name', 'unique', and 'using'" if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 6 7
735 6 ▶ $self->_check_using("$what: the '$key' entry", $entry->{using}) if exists $entry->{using};
Branch: 1 branch
BranchTrueFalseTotal
if exists $entry->{"using"} 6 0 6
736 }
737 7 return;
738 }
739  
740 310 ▶sub _check_open {
Subroutine _check_open: called CC=2 SCAR=6.9
741 310 my $self = shift;
742 310 my ($method) = @_;
743 310 ▶ croak "$method() cannot change a table that was built already" if $self->{+BUILT};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"built"} 3 307 310
744 }
745  
746 77 ▶sub _fill { $_[0]->{+FILL} }
Subroutine _fill: called CC=1 SCAR=0.0
747  
748 44 ▶sub _finish {
Subroutine _finish: called CC=3 SCAR=11.0
749 44 my $self = shift;
750 44 my ($create) = @_;
751 44 ▶ croak "This table builder was built already" if $self->{+BUILT};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"built"} 2 42 44
752 42 ▶ croak "A table needs at least one column" unless @{$self->{+COLUMNS}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"columns"};} 1 41 42
753 41 my $out = $create->();
754 34 $self->{+BUILT} = 1;
755 34 return $out;
756 }
757  
758 211 ▶sub _is_sql { blessed($_[1]) && $_[1]->isa('DBIx::QuickORM::Value') && $_[1]->kind eq 'sql' ? 1 : 0 }
Subroutine _is_sql: called CC=4 SCAR=14.3
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[1]) && $_[1]->isa('DBIx::QuickORM::Value') && $_[1]->kind eq 'sql' ? : 2 209 211
Condition66%4/6
builtin::blessed($_[1]) and $_[1]->isa("DBIx::QuickORM::Value") and $_[1]->kind eq "sql"
!ll&&!rl&&r
20902
builtin::blessed($_[1]) and $_[1]->isa("DBIx::QuickORM::Value")
!ll&&!rl&&r
20902
MC/DC33%1/3
builtin::blessed($_[1]) and $_[1]->isa("DBIx::QuickORM::Value") and $_[1]->kind eq "sql"
builtin::blessed($_[1]) $_[1]->isa("DBIx::QuickORM::Value") $_[1]->kind eq "sql"
Truth table50%2/4
A and B and C
A= builtin::blessed($_[1])B= $_[1]->isa("DBIx::QuickORM::Value")C= $_[1]->kind eq "sql"
ABCresult
0XX0
10X0
1100
1111
759  
760 260 1;
761  
762 __END__
« blib/lib/DBIx/QuickORM/AggregateRow.pm ↑ Summary blib/lib/DBIx/QuickORM/Source/Join.pm »