blib/lib/DBIx/QuickORM/Process/BuildSchema.pm

filter: Statementstmt 100% 298 / 298 Branchbran 97.7% 88 / 90 Conditioncond 79.3% 46 / 58 MC/DCmcdc 57.5% 19 / 33 Subroutinesub 100% 22 / 22 Podpod 80.0% 4 / 5 totaltotal 94.2% 477 / 506 CC 86 SCAR 21.7
CC
86
Cov
97%
_table_record
15.0
_resolve_spec
12.0
_handle_view
12.0
CRAP 8.8
« blib/lib/DBIx/QuickORM/ORM.pm ↑ Summary blib/lib/DBIx/QuickORM/DSL.pm »
1 package DBIx::QuickORM::Process::BuildSchema;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 our $VERSION = '0.000033';
6  
7 4 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
11 <dialect
12 <tables
13 <types
14 <orm
15 <pretty
16 <namespace
17 +enums
18 +one_off
19 +resolved
20 +handle_views
21 +items
22 4 };
23  
24 =pod
25  
26 =encoding UTF-8
27  
28 =head1 NAME
29  
30 DBIx::QuickORM::Process::BuildSchema - Turn table definitions into the SQL that creates them.
31  
32 =head1 DESCRIPTION
33  
34 Takes the tables an ORM class defines for schema building and a dialect, and
35 gives the statements that create them on that database, in an order that
36 works: every table after the tables its foreign keys name, and the views
37 after every table, each after the views it reads. Keys between
38 tables that reference each other are added after all the tables, with
39 C<ALTER TABLE>, which SQLite and DuckDB cannot do, so they croak there.
40  
41 Use it through C<build_sql> and C<deploy> on an ORM class (see
42 L<DBIx::QuickORM::ORM>).
43  
44 =head1 SYNOPSIS
45  
46 my $build = DBIx::QuickORM::Process::BuildSchema->new(
47 dialect => DBIx::QuickORM::Dialect->for_name('postgresql'),
48 tables => [map { DBIx::QuickORM::DSL->table_schema($_) } @table_classes],
49 );
50 my @sql = $build->statements;
51  
52 =head1 ATTRIBUTES
53  
54 =over 4
55  
56 =item $dialect = $build->dialect
57  
58 The L<DBIx::QuickORM::Dialect> object to write SQL for. Required.
59  
60 =item \@tables = $build->tables
61  
62 What L<DBIx::QuickORM::DSL>'s C<table_schema> gives for each table class:
63 C<< {name => $table, spec => \%spec} >> from a C<schema> block,
64 C<< {name => $view, view => {sql => $sql, depends => \@names}} >> from one
65 giving C<view_sql>, or C<< {name => $table, sql => $sql, depends => \@tables} >>
66 from C<schema_sql>.
67 Required.
68  
69 =item \%spec = $build->orm
70  
71 What the C<schema> block inside the ORM's C<orm> block describes, as
72 L<DBIx::QuickORM::SchemaBuilder::ORM>'s C<spec>, or undef: its settings,
73 extensions, and SQL go around the tables.
74  
75 =item $class = $build->namespace
76  
77 The ORM class whose enums (L<DBIx::QuickORM::DSL>'s C<enum>) a column's
78 C<enum> names, or undef for none.
79  
80 =item $bool = $build->pretty
81  
82 Writes each table's columns and constraints on lines of their own.
83  
84 =item $class = $build->types
85  
86 The class whose C<require_type> loads field types, the ORM class. Default
87 L<DBIx::QuickORM>.
88  
89 =back
90  
91 =cut
92  
93 89 ▶sub init {
Subroutine init: called CC=4 SCAR=13.9
Pod undocumented
94 89 my $self = shift;
95 89 ▶ croak "'dialect' is required" unless $self->{+DIALECT};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dialect"} 1 88 89
96 88 ▶ croak "'tables' must be an arrayref of tables" unless ref($self->{+TABLES}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"tables"} eq "ARRAY" 1 87 88
97 87 ▶ $self->{+TYPES} //= do { require DBIx::QuickORM; 'DBIx::QuickORM' };
Condition66%2/3
$self->{"types"} //= do { require DBIx::QuickORM; "DBIx::QuickORM" }
l!l&&r!l&&!r
42450
MC/DC0%0/2
$self->{"types"} //= do { require DBIx::QuickORM; "DBIx::QuickORM" }
$self->{"types"} do { require DBIx::QuickORM; "DBIx::QuickORM" }
Truth table66%2/3
A //= B
A= $self->{"types"}B= do { require DBIx::QuickORM; "DBIx::QuickORM" }
ABresult
1X1
011
000
98 87 $self->{+ENUMS} = {};
99 87 $self->{+ONE_OFF} = {};
100 }
101  
102 =head1 PUBLIC METHODS
103  
104 =over 4
105  
106 =item @sql = $build->statements
107  
108 =item @groups = $build->groups
109  
110 The statements, in order, or the same as arrayrefs in groups: the ORM's
111 statements and the named enums the tables use before the tables, each table
112 with its one-off enums, sequences, and indexes, the keys added afterwards,
113 each view, and the ORM's SQL given C<after>. Empty groups are left out.
114 Croaks for two definitions of one table, a column type or collation missing
115 for the database, an enum nothing defines, raw SQL missing for it, a cycle
116 among raw SQL tables, or keys between tables that reference each other on a
117 database that cannot add them later.
118  
119 =back
120  
121 =cut
122  
123 43 ▶sub statements {
Subroutine statements: called CC=1 SCAR=0.0
Pod documented
124 43 my $self = shift;
125 43 return map { @$_ } $self->groups;
126 }
127  
128 70 ▶sub groups {
Subroutine groups: called CC=11 SCAR=24.0
Pod documented
129 70 my $self = shift;
130 70 my $dialect = $self->{+DIALECT};
131  
132 70 my ($tables, $views) = $self->_items;
133 66 my (%table, %deps, %fks);
134 66 for my $name (keys %$tables) {
135 94 my $t = $table{$name} = $tables->{$name};
136 94 ▶ if ($t->{spec}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'spec'}) { } 78 16 94
137 78 $fks{$name} = [$self->_foreign_keys($t->{spec})];
138 78 $deps{$name} = [map { $_->{table} } @{$fks{$name}}];
139 }
140 else {
141 16 $deps{$name} = $t->{depends};
142 }
143 }
144  
145 # Create whatever has its dependencies in place. When nothing does, the rest
146 # are in cycles: one builder table among them takes its keys to them later.
147 66 my (@order, %later);
148 66 my %left = map { $_ => 1 } keys %table;
149 66 ▶ while (%left) {
Branch: 1 branch
BranchTrueFalseTotal
if (%left) 92 65 157
150 my @ready = grep {
151 92 my $n = $_;
152 141 ▶ !grep { $left{$_} && $_ ne $n } @{$deps{$n}}
Condition100%3/3
$left{$_} && $_ ne $n
!ll&&!rl&&r
26149
MC/DC100%2/2
$left{$_} && $_ ne $n
$left{$_} $_ ne $n
Truth table100%3/3
A && B
A= $left{$_}B= $_ ne $n
ABresult
0X0
100
111
153 } sort keys %left;
154 92 ▶ if (@ready) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ready) 86 6 92
155 86 push @order => @ready;
156 86 delete @left{@ready};
157 86 next;
158 }
159 12 my ($name) = grep { $table{$_}->{spec} } sort keys %left
160 6 ▶ or croak "The tables " . join(', ' => map { "'$_'" } sort keys %left) . " depend on each other, and raw SQL tables cannot add their keys later";
Branch: 1 branch
BranchTrueFalseTotal
my($name) = grep({$table{$_}{"spec"};} sort(keys %left)) or croak('The tables ' . join(", ", map({"'${_}'";} sort(keys %left))) . ' depend on each other, and raw SQL tables cannot add their keys later') 5 1 6
161 5 ▶ $later{$name}->{$_} = 1 for grep { $left{$_} && $_ ne $name } @{$deps{$name}};
Condition33%1/3
$left{$_} && $_ ne $name
!ll&&!rl&&r
005
MC/DC0%0/2
$left{$_} && $_ ne $name
$left{$_} $_ ne $name
Truth table33%1/3
A && B
A= $left{$_}B= $_ ne $name
ABresult
0X0
100
111
162 5 $deps{$name} = [grep { !$later{$name}->{$_} } @{$deps{$name}}];
163 }
164  
165 65 my (@tables, @alter);
166 65 for my $name (@order) {
167 90 my $t = $table{$name};
168 90 ▶ unless ($t->{spec}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t->{"spec"}) 14 76 90
169 14 push @tables => [$t->{sql}];
170 14 next;
171 }
172 76 my @now = grep { !$later{$name}->{$_->{table}} } @{$fks{$name}};
173 76 my @after = grep { $later{$name}->{$_->{table}} } @{$fks{$name}};
174 76 my $spec = $self->_resolve($t->{spec}, $name);
175 70 push @tables => [(map { $dialect->create_enum_sql(@$_) } @{$self->{+ONE_OFF}->{$name}}), $dialect->create_table_sql($name, $spec, foreign_keys => \@now, pretty => $self->{+PRETTY})];
176 70 push @alter => map { $dialect->add_foreign_key_sql($name, $_) } @after;
177 }
178  
179 57 my ($before, $after) = $self->_orm_statements;
180 57 return grep { @$_ } ($before, @tables, \@alter, (map { [$_] } $self->_view_statements($views)), $after);
181 }
182  
183 =pod
184  
185 =over 4
186  
187 =item @sql = $build->enum_statements
188  
189 The statements that create the named enums every builder table uses, sorted
190 by name: what C<groups> puts before the tables, alone.
191  
192 =back
193  
194 =cut
195  
196 8 ▶sub enum_statements {
Subroutine enum_statements: called CC=2 SCAR=6.9
Pod documented
197 8 my $self = shift;
198 8 my ($tables) = $self->_items;
199 8 $self->_resolve($_->{spec}, $_->{name}) for grep { $_->{spec} } map { $tables->{$_} } sort keys %$tables;
200 8 return $self->_enum_sql;
201 }
202  
203 65 ▶sub _enum_sql {
Subroutine _enum_sql: called CC=1 SCAR=0.0
204 65 my $self = shift;
205 65 my $enums = $self->{+ENUMS};
206 65 return map { $self->{+DIALECT}->create_enum_sql($_, $enums->{$_}) } sort keys %$enums;
207 }
208  
209 96 ▶sub _items {
Subroutine _items: called CC=2 SCAR=6.9
210 96 my $self = shift;
211 96 ▶ return @{$self->{+ITEMS} //= [$self->_pick_items]};
Condition100%2/2
$self->{"items"} //= [$self->_pick_items]
l!l
987
MC/DC100%1/1
$self->{"items"} //= [$self->_pick_items]
$self->{"items"}
Truth table100%2/2
A
A= $self->{"items"}
Aresult
11
00
212 }
213  
214 87 ▶sub _pick_items {
Subroutine _pick_items: called CC=10 SCAR=23.0
215 87 my $self = shift;
216 87 my $dialect = $self->{+DIALECT};
217 87 my $on = "the " . $dialect->name . " dialect";
218  
219 87 my (%tables, %views, %seen);
220 87 for my $t (@{$self->{+TABLES}}) {
221 177 my $name = $t->{name};
222 177 ▶ croak "The table '$name' is defined twice" if $seen{$name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$name}++ 2 175 177
223  
224 175 ▶ if ($t->{spec}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"spec"}) 118 57 175
225 118 $tables{$name} = $t;
226 118 next;
227 }
228  
229 57 ▶ my ($kind, $entries) = $t->{view} ? ('view', $t->{view}->{entries}) : ('table', $t->{raw});
Branch: 1 branch
BranchTrueFalseTotal
$t->{'view'} ? : 33 24 57
230 57 ▶ my ($entry, $sql) = $self->_pick_entry($entries) or croak "The $kind '$name' gives no SQL for $on: give it, or '" . lc($dialect->name) . " => undef' to build without it there";
Branch: 1 branch
BranchTrueFalseTotal
() = $self->_pick_entry($entries) or croak("The $kind '${name}' gives no SQL for ${on}: give it, or '" . lc($dialect->name) . q[ => undef' to build without it there]) 55 2 57
231 55 ▶ next unless defined $sql; # not on this dialect
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sql 6 49 55
232 49 ▶ $sql = $$sql if ref($sql) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $sql eq "SCALAR" 5 44 49
233 49 ▶ $sql =~ s/;\s*\z// unless ref $sql;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sql 34 15 49
234  
235 49 ▶ if ($kind eq 'view') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq 'view') { } 31 18 49
236 31 $views{$name} = {sql => $sql, depends => $entry->{depends}, columns => $entry->{columns}};
237 }
238 else {
239 18 $tables{$name} = {name => $name, sql => $sql, depends => $entry->{depends}};
240 }
241 }
242  
243 83 return (\%tables, \%views);
244 }
245  
246 57 ▶sub _pick_entry {
Subroutine _pick_entry: called CC=4 SCAR=13.9
247 57 my $self = shift;
248 57 my ($entries) = @_;
249 57 for my $key ($self->{+DIALECT}->db_type_keys) {
250 109 for my $entry (@$entries) {
251 131 ▶ return ($entry, $entry->{sql}->{$key}) if exists $entry->{sql}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $entry->{"sql"}{$key} 55 76 131
252 }
253 }
254 2 return;
255 }
256  
257 57 ▶sub _orm_statements {
Subroutine _orm_statements: called CC=9 SCAR=22.0
258 57 my $self = shift;
259 57 my $dialect = $self->{+DIALECT};
260 57 ▶ my $orm = $self->{+ORM} // {};
Condition100%2/2
$self->{"orm"} // {}
l!l
651
MC/DC100%1/1
$self->{"orm"} // {}
$self->{"orm"}
Truth table100%2/2
A
A= $self->{"orm"}
Aresult
11
00
261  
262 my @before = (
263 4 ▶ (map { $dialect->setting_sql(@$_) } @{$orm->{settings} // []}),
Condition100%2/2
$orm->{"settings"} // []
l!l
651
MC/DC100%1/1
$orm->{"settings"} // []
$orm->{"settings"}
Truth table100%2/2
A
A= $orm->{"settings"}
Aresult
11
00
264 57 ▶ (map { $dialect->extension_sql($_) } @{$orm->{extensions} // []}),
Condition100%2/2
$orm->{"extensions"} // []
l!l
651
MC/DC100%1/1
$orm->{"extensions"} // []
$orm->{"extensions"}
Truth table100%2/2
A
A= $orm->{"extensions"}
Aresult
11
00
265 $self->_enum_sql,
266 );
267 57 my @after;
268 57 ▶ for my $item (@{$orm->{sql} // []}) {
Condition100%2/2
$orm->{"sql"} // []
l!l
651
MC/DC100%1/1
$orm->{"sql"} // []
$orm->{"sql"}
Truth table100%2/2
A
A= $orm->{"sql"}
Aresult
11
00
269 13 my ($found, $sql) = $dialect->pick_entry($item->{sql});
270 13 ▶ next unless defined $sql; # SQL for other databases only
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sql 1 12 13
271 12 ▶ $sql = $$sql if ref $sql;
Branch: 1 branch
BranchTrueFalseTotal
if ref $sql 2 10 12
272 12 ▶ push @{$item->{after} ? \@after : \@before} => $sql =~ s/;\s*\z//r;
Branch: 1 branch
BranchTrueFalseTotal
$item->{'after'} ? : 6 6 12
273 }
274 57 return (\@before, \@after);
275 }
276  
277 57 ▶sub _view_statements {
Subroutine _view_statements: called CC=4 SCAR=13.9
278 57 my $self = shift;
279 57 my ($views) = @_;
280 57 my $dialect = $self->{+DIALECT};
281  
282 57 my @sql;
283 57 for my $name ($self->_view_order($views)) {
284 20 my $sql = $views->{$name}->{sql};
285 20 ▶ if (ref($sql) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sql eq "CODE") 9 11 20
286 9 ▶ $self->map_tables unless $self->{+HANDLE_VIEWS};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"handle_views"} 9 0 9
287 9 $sql = $self->{+HANDLE_VIEWS}->{$name};
288 }
289 20 push @sql => $dialect->create_view_sql($name, $sql);
290 }
291  
292 56 return @sql;
293 }
294  
295 75 ▶sub _view_order {
Subroutine _view_order: called CC=4 SCAR=13.9
296 75 my $self = shift;
297 75 my ($views) = @_;
298 75 my (@order, %left);
299 75 %left = map { $_ => 1 } keys %$views;
300 75 ▶ while (%left) {
Branch: 1 branch
BranchTrueFalseTotal
if (%left) 33 74 107
301 my @ready = grep {
302 33 my $n = $_;
303 41 ▶ !grep { $left{$_} && $_ ne $n } @{$views->{$n}->{depends}}
Condition66%2/3
$left{$_} && $_ ne $n
!ll&&!rl&&r
3106
MC/DC50%1/2
$left{$_} && $_ ne $n
$left{$_} $_ ne $n
Truth table66%2/3
A && B
A= $left{$_}B= $_ ne $n
ABresult
0X0
100
111
304 } sort keys %left;
305 33 ▶ croak "The views " . join(', ' => map { "'$_'" } sort keys %left) . " depend on each other" unless @ready;
Branch: 1 branch
BranchTrueFalseTotal
unless @ready 1 32 33
306 32 push @order => @ready;
307 32 delete @left{@ready};
308 }
309 74 return @order;
310 }
311  
312 =pod
313  
314 =over 4
315  
316 =item \%tables = $build->map_tables
317  
318 The map the definitions describe, as the tables of a L<DBIx::QuickORM::Map>,
319 before the ORM's setup: a record for each table a C<schema> block describes
320 and each view built from a handle. Tables and views given as SQL are left
321 out. A column given as SQL gets the facts of the type its SQL starts with,
322 and no fill facts.
323  
324 A field record holds the dialect's C<field_facts> for its column type, with
325 C<db_fills> for a default, identity, generated column, or rowid alias, and
326 C<db_refuses_value> for a generated column. A table holds its C<pk>, its
327 C<unique> keys (each sorted, the primary key not repeated), and its C<fk>
328 links. A view built from a handle holds the fields it selects, with no fill
329 facts, and C<view> set to 1.
330  
331 =back
332  
333 =cut
334  
335 18 ▶sub map_tables {
Subroutine map_tables: called CC=5 SCAR=16.1
Pod documented
336 18 my $self = shift;
337 18 my $dialect = $self->{+DIALECT};
338  
339 18 my ($items, $all_views) = $self->_items;
340 18 ▶ my %views = map { ref($all_views->{$_}->{sql}) eq 'CODE' ? ($_ => $all_views->{$_}) : () } keys %$all_views;
Branch: 1 branch
BranchTrueFalseTotal
ref $all_views->{$_}{'sql'} eq 'CODE' ? : 15 9 24
341  
342 18 my %tables;
343 18 for my $t (values %$items) {
344 36 ▶ next unless $t->{spec};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"spec"} 5 31 36
345 31 $tables{$t->{name}} = $self->_table_record($t->{name}, $self->_resolve($t->{spec}, $t->{name}));
346 }
347  
348 18 $self->{+HANDLE_VIEWS} = {};
349 18 for my $name ($self->_view_order(\%views)) {
350 15 my ($select, $record) = $self->_handle_view($name, $views{$name}, \%tables);
351 12 $self->{+HANDLE_VIEWS}->{$name} = $select;
352 12 $tables{$name} = $record;
353 }
354  
355 15 return \%tables;
356 }
357  
358 31 ▶sub _table_record {
Subroutine _table_record: called CC=15 SCAR=27.1
359 31 my $self = shift;
360 31 my ($name, $spec) = @_;
361 31 my $dialect = $self->{+DIALECT};
362 31 ▶ my $rowid = $dialect->rowid_alias($spec) // '';
Condition100%2/2
$dialect->rowid_alias($spec) // ""
l!l
2011
MC/DC100%1/1
$dialect->rowid_alias($spec) // ""
$dialect->rowid_alias($spec)
Truth table100%2/2
A
A= $dialect->rowid_alias($spec)
Aresult
11
00
363  
364 31 my %fields;
365 31 for my $f (@{$spec->{columns}}) {
366 91 ▶ if (exists $f->{sql}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $f->{"sql"}) 5 86 91
367 5 my ($type) = $dialect->_literal_sql($f->{sql}) =~ m/^\s*(\w+(?:\s*\([^)]*\))?)/;
368 5 ▶ $fields{$f->{name}} = {%{$dialect->field_facts($type // '')}, table => $name, name => $f->{name}};
Condition50%1/2
$type // ""
l!l
50
MC/DC0%0/1
$type // ""
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
369 5 next;
370 }
371 86 ▶ my $fills = exists($f->{default}) || $f->{identity} || defined($f->{generated}) || $f->{name} eq $rowid;
Condition100%9/9
exists $f->{'default'} || $f->{'identity'} || defined $f->{'generated'} || $f->{'name'} eq $rowid
l!l&&r!l&&!r
181652
exists $f->{"default"} or $f->{"identity"} or defined $f->{"generated"}
l!l&&r!l&&!r
17168
exists $f->{"default"} or $f->{"identity"}
l!l&&r!l&&!r
13469
MC/DC100%4/4
exists $f->{'default'} || $f->{'identity'} || defined $f->{'generated'} || $f->{'name'} eq $rowid
exists $f->{"default"} $f->{"identity"} defined $f->{"generated"} $f->{'name'} eq $rowid
Truth table100%5/5
A or B or C || D
A= exists $f->{"default"}B= $f->{"identity"}C= defined $f->{"generated"}D= $f->{'name'} eq $rowid
ABCDresult
1XXX1
01XX1
001X1
00011
00000
372 86 my $refuses = defined($f->{generated});
373 86 $fields{$f->{name}} = {%{$dialect->field_facts($f->{db_type}, undef, $fills, $refuses)}, table => $name, name => $f->{name}};
374 }
375  
376 31 ▶ my $pk = $spec->{pk} ? [@{$spec->{pk}}] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$spec->{'pk'} ? : 30 1 31
377 31 ▶ my $pk_key = $pk ? join("\0" => sort @$pk) : '';
Branch: 1 branch
BranchTrueFalseTotal
$pk ? : 30 1 31
378 31 my (%seen, @unique);
379 31 ▶ for my $u ((map { [$_->{name}] } grep { $_->{unique} } @{$spec->{columns}}), (map { $_->{columns} } @{$spec->{uniques} // []})) {
Condition100%2/2
$spec->{"uniques"} // []
l!l
1714
MC/DC100%1/1
$spec->{"uniques"} // []
$spec->{"uniques"}
Truth table100%2/2
A
A= $spec->{"uniques"}
Aresult
11
00
380 7 my @sorted = sort @$u;
381 7 my $key = join "\0" => @sorted;
382 7 ▶ next if $key eq $pk_key || $seen{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq $pk_key or $seen{$key}++ 1 6 7
Condition66%2/3
$key eq $pk_key or $seen{$key}++
l!l&&r!l&&!r
106
MC/DC50%1/2
$key eq $pk_key or $seen{$key}++
$key eq $pk_key $seen{$key}++
Truth table66%2/3
A or B
A= $key eq $pk_keyB= $seen{$key}++
ABresult
1X1
011
000
383 6 push @unique => \@sorted;
384 }
385  
386 31 my %fk;
387 31 for my $fk ($self->_foreign_keys($spec)) {
388 13 push @{$fk{$fk->{table}}} => {map { ($fk->{columns}->[$_] => $fk->{their_columns}->[$_]) } 0 .. $#{$fk->{columns}}};
389 }
390  
391 31 return {fields => \%fields, pk => $pk, unique => \@unique, fk => \%fk};
392 }
393  
394 15 ▶sub _handle_view {
Subroutine _handle_view: called CC=12 SCAR=24.9
395 15 my $self = shift;
396 15 my ($name, $view, $tables) = @_;
397  
398 15 require DBIx::QuickORM::Map;
399 15 my $map = DBIx::QuickORM::Map->new(tables => $tables)->clone;
400 15 my $class = $self->{+TYPES};
401 15 ▶ my $orm = $class->new(_offline => {dialect => $self->{+DIALECT}, db_map => $map}, $class->isa('DBIx::QuickORM::ORM') ? () : (row_class => undef));
Branch: 1 branch
BranchTrueFalseTotal
$class->isa('DBIx::QuickORM::ORM') ? : 4 11 15
402  
403 15 my $h = $view->{sql}->($orm);
404 15 ▶ croak "The view '$name' gives a code reference, which must return a handle of the ORM it is given" unless blessed($h) && $h->isa('DBIx::QuickORM::Handle');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($h) and $h->isa("DBIx::QuickORM::Handle") 1 14 15
Condition66%2/3
builtin::blessed($h) and $h->isa("DBIx::QuickORM::Handle")
!ll&&!rl&&r
1014
MC/DC50%1/2
builtin::blessed($h) and $h->isa("DBIx::QuickORM::Handle")
builtin::blessed($h) $h->isa("DBIx::QuickORM::Handle")
Truth table66%2/3
A and B
A= builtin::blessed($h)B= $h->isa("DBIx::QuickORM::Handle")
ABresult
0X0
100
111
405 14 my $source = $h->source;
406 14 ▶ croak "The view '$name' is built from a handle on a query, whose columns cannot be traced; give it as SQL" if $source->isa('DBIx::QuickORM::Source::Query');
Branch: 1 branch
BranchTrueFalseTotal
if $source->isa("DBIx::QuickORM::Source::Query") 1 13 14
407  
408 13 my ($src, $sql, $binds) = $orm->_temp_select(view_sql => $h, 0);
409 13 my $select = $orm->connection->inline_binds($src, $sql, $binds);
410  
411 13 my %request = $h->_select_request;
412 13 my $fields = $request{fields};
413 13 ▶ my %columns = ref($fields) eq 'HASH' ? %$fields : map { ($_ => $_) } @$fields;
Branch: 1 branch
BranchTrueFalseTotal
ref $fields eq 'HASH' ? : 6 7 13
414 13 my $records = $source->field_records;
415 13 ▶ my $types = $view->{columns} // {};
Condition50%1/2
$view->{"columns"} // {}
l!l
130
MC/DC0%0/1
$view->{"columns"} // {}
$view->{"columns"}
Truth table50%1/2
A
A= $view->{"columns"}
Aresult
11
00
416  
417 13 my (%record, @missing);
418 13 for my $column (sort keys %columns) {
419 28 my $expr = $columns{$column};
420 28 my %rec;
421 28 ▶ if (defined $types->{$column}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $types->{$column}) { } 5 23 28
elsif (not ref $expr and $records->{$expr}) { } 22 1 23
422 5 %rec = %{$self->{+DIALECT}->field_facts($types->{$column})};
423 }
424 ▶ elsif (!ref($expr) && $records->{$expr}) {
Condition66%2/3
not ref $expr and $records->{$expr}
!ll&&!rl&&r
1022
MC/DC50%1/2
not ref $expr and $records->{$expr}
not ref $expr $records->{$expr}
Truth table66%2/3
not(A) and B
A= not ref $exprB= $records->{$expr}
ABresult
0X0
100
111
425 22 %rec = %{$records->{$expr}};
426 22 delete @rec{qw/db_fills db_refuses_value orm_type passthrough format generate/};
427 }
428 else {
429 1 push @missing => $column;
430 1 next;
431 }
432 27 $record{$column} = {%rec, table => $name, name => $column};
433 }
434 13 ▶ croak "The view '$name' selects " . join(', ' => map { "'$_'" } @missing) . ", which no table field gives: give the column type in 'columns'" if @missing;
Branch: 1 branch
BranchTrueFalseTotal
if @missing 1 12 13
435  
436 12 return ($select, {fields => \%record, pk => undef, unique => [], fk => {}, view => 1});
437 }
438  
439 =head1 PRIVATE METHODS
440  
441 =over 4
442  
443 =item @fks = $build->_foreign_keys(\%spec)
444  
445 The table's foreign keys: one for each column's C<references> (with its
446 C<on_delete> and C<on_update>), then those from C<add_foreign_key>.
447  
448 =item @names = $build->_view_order(\%views)
449  
450 The views' names, each after the views it depends on. Croaks for views that
451 depend on each other.
452  
453 =item \%record = $build->_table_record($name, \%resolved_spec)
454  
455 =item ($select_sql, \%record) = $build->_handle_view($name, \%view, \%tables)
456  
457 A table's map record, and a view built from a handle: the code gets an
458 offline ORM of the C<types> class over the tables so far, its handle's
459 C<SELECT> has its binds written in, and the record holds the columns it
460 selects. A column that is one source field keeps that field's facts, a
461 column in the view's C<columns> gets that column type's, and any other
462 croaks.
463  
464 =item ($tables, $views) = $build->_items
465  
466 =item ($tables, $views) = $build->_pick_items
467  
468 =item ($entry, $sql) = $build->_pick_entry(\@entries)
469  
470 The tables and views for the dialect: builder tables as given, raw tables
471 and views from the entry for the dialect (the exact one, then C<any_mysql>,
472 then C<any>, across every call's entries), left out where that entry is
473 undef. Croaks for a name defined twice, or a raw table or view with no entry
474 for the dialect.
475  
476 =item @sql = $build->_enum_sql
477  
478 The C<CREATE TYPE> statements of the named enums recorded so far.
479  
480 =item (\@before, \@after) = $build->_orm_statements
481  
482 The ORM's settings, extensions, the named enums the tables use (sorted by
483 name), and its SQL without C<after>, which go before the tables, and its SQL with C<after>, which goes last. SQL with
484 no entry for the database is left out.
485  
486 =item @sql = $build->_view_statements(\%views)
487  
488 The C<CREATE VIEW> statements, each view after the views it depends on.
489 Croaks for views that depend on each other, or a query missing for the
490 database.
491  
492 =item \%spec = $build->_resolve(\%spec, $table)
493  
494 A copy of the spec with each column's type loaded and its C<db_type> and
495 C<collate> chosen for the dialect. An enum column gets the dialect's enum
496 column, and its check, where the dialect gives one, joins the table's. Its
497 enum is recorded: a named one for the statements before the tables (two
498 enums of one name with different values croak), a one-off one, named
499 C<${table}_enum_N>, for the table's own statements.
500  
501 =back
502  
503 =cut
504  
505 109 ▶sub _foreign_keys {
Subroutine _foreign_keys: called CC=4 SCAR=13.9
506 109 my $self = shift;
507 109 my ($spec) = @_;
508 109 my @fks;
509 109 for my $column (grep { $_->{references} } @{$spec->{columns}}) {
510 31 my $refs = $column->{references};
511 31 ▶ push @fks => {columns => [$column->{name}], table => $refs->{table}, their_columns => [$refs->{column}], map { exists($refs->{$_}) ? ($_ => $refs->{$_}) : () } qw/on_delete on_update/};
Branch: 1 branch
BranchTrueFalseTotal
exists $refs->{$_} ? : 7 55 62
512 }
513 109 ▶ return (@fks, @{$spec->{foreign_keys} // []});
Condition100%2/2
$spec->{"foreign_keys"} // []
l!l
6742
MC/DC100%1/1
$spec->{"foreign_keys"} // []
$spec->{"foreign_keys"}
Truth table100%2/2
A
A= $spec->{"foreign_keys"}
Aresult
11
00
514 }
515  
516 129 ▶sub _resolve {
Subroutine _resolve: called CC=2 SCAR=7.0
517 129 my $self = shift;
518 129 my ($spec, $table) = @_;
519 129 ▶ return $self->{+RESOLVED}->{$table} //= $self->_resolve_spec($spec, $table);
Condition66%2/3
$self->{"resolved"}{$table} //= $self->_resolve_spec($spec, $table)
l!l&&r!l&&!r
151140
MC/DC0%0/2
$self->{"resolved"}{$table} //= $self->_resolve_spec($spec, $table)
$self->{"resolved"}{$table} $self->_resolve_spec($spec, $table)
Truth table66%2/3
A //= B
A= $self->{"resolved"}{$table}B= $self->_resolve_spec($spec, $table)
ABresult
1X1
011
000
520 }
521  
522 114 ▶sub _resolve_spec {
Subroutine _resolve_spec: called CC=12 SCAR=24.9
523 114 my $self = shift;
524 114 my ($spec, $table) = @_;
525 114 my $dialect = $self->{+DIALECT};
526 114 my $types = $self->{+TYPES};
527  
528 114 my (@columns, @checks, @one_off);
529 114 for my $c (@{$spec->{columns}}) {
530 282 my $column = {%$c};
531 282 ▶ if (exists $column->{enum}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $column->{"enum"}) 75 207 282
532 75 require DBIx::QuickORM::DSL;
533 75 my $what = "The column '$column->{name}' of the table '$table'";
534 75 my ($name, $values) = DBIx::QuickORM::DSL->enum_values($self->{+NAMESPACE}, delete($column->{enum}), $what);
535 71 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 40 31 71
536 40 my $have = $self->{+ENUMS}->{$name};
537 40 ▶ croak "$what uses the enum '$name', and another enum of that name has other values" if $have && join("\0" => @$have) ne join("\0" => @$values);
Branch: 1 branch
BranchTrueFalseTotal
if $have and join("\0", @$have) ne join("\0", @$values) 0 40 40
Condition33%1/3
$have and join("\0", @$have) ne join("\0", @$values)
!ll&&!rl&&r
4000
MC/DC0%0/2
$have and join("\0", @$have) ne join("\0", @$values)
$have join("\0", @$have) ne join("\0", @$values)
Truth table33%1/3
A and B
A= $haveB= join("\0", @$have) ne join("\0", @$values)
ABresult
0X0
100
111
538 40 $self->{+ENUMS}->{$name} = $values;
539 }
540 else {
541 31 push @one_off => [$name = "${table}_enum_" . (@one_off + 1), $values];
542 }
543 71 my ($db_type, $check) = $dialect->enum_column($column->{name}, $name, $values);
544 71 $column->{db_type} = $db_type;
545 71 ▶ push @checks => {sql => \$check} if defined $check;
Branch: 1 branch
BranchTrueFalseTotal
if defined $check 24 47 71
546 }
547 278 ▶ unless (exists $column->{sql}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $column->{"sql"}) 273 5 278
548 273 ▶ $column->{type} = $types->require_type($column->{type}, "column '$column->{name}'") if defined $column->{type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $column->{"type"} 8 265 273
549 273 $column->{db_type} = $dialect->column_type($column);
550 272 ▶ if (exists $column->{collate}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $column->{"collate"}) 6 266 272
551 6 my $collate = $dialect->pick_value($column->{collate});
552 6 ▶ croak "The column '$column->{name}' of the table '$table' has no collation for " . $dialect->name unless defined $collate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $collate 1 5 6
553 5 $column->{collate} = $collate;
554 }
555 }
556 276 push @columns => $column;
557 }
558 108 $self->{+ONE_OFF}->{$table} = \@one_off;
559  
560 108 ▶ return {%$spec, columns => \@columns, checks => [@{$spec->{checks} // []}, @checks]};
Condition100%2/2
$spec->{"checks"} // []
l!l
7632
MC/DC100%1/1
$spec->{"checks"} // []
$spec->{"checks"}
Truth table100%2/2
A
A= $spec->{"checks"}
Aresult
11
00
561 }
562  
563 4 1;
564  
565 __END__
« blib/lib/DBIx/QuickORM/ORM.pm ↑ Summary blib/lib/DBIx/QuickORM/DSL.pm »