« blib/lib/DBIx/QuickORM/Command/GenerateSQL.pm
↑ Summary
blib/lib/DBIx/QuickORM/Process/BuildSchema.pm »
| 1 | package DBIx::QuickORM::ORM; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 20 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 20 | our $VERSION = '0.000033'; | |||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 20 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 20 | ▶ | use DBIx::QuickORM::DSL(); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 20 | ▶ | use DBIx::QuickORM::Dialect(); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 20 | ▶ | use parent 'DBIx::QuickORM'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | DBIx::QuickORM::ORM - Base class for an ORM defined as classes. | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | The C<orm> block of L<DBIx::QuickORM::DSL> makes its package a subclass of | ||||||||||||||||||||||||||||||||||||||||||
| 25 | this one. C<new> gathers what the C<orm> block and the table classes collected | ||||||||||||||||||||||||||||||||||||||||||
| 26 | into arguments for L<DBIx::QuickORM's C<new>|DBIx::QuickORM/"PUBLIC METHODS">, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | then constructs the ORM with them. | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | The table classes are the modules directly under the ORM class's namespace | ||||||||||||||||||||||||||||||||||||||||||
| 30 | (C<My::ORM::*> for C<My::ORM>), found in C<@INC>. Every module there is | ||||||||||||||||||||||||||||||||||||||||||
| 31 | loaded, and those with a C<table> block are used. The C<tables> keyword | ||||||||||||||||||||||||||||||||||||||||||
| 32 | replaces that search with another namespace or a list of classes. A table | ||||||||||||||||||||||||||||||||||||||||||
| 33 | that needs nothing adjusted needs no class. | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | package My::ORM; | ||||||||||||||||||||||||||||||||||||||||||
| 38 | use DBIx::QuickORM::DSL ':orm'; | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | orm { | ||||||||||||||||||||||||||||||||||||||||||
| 41 | retain_cache 1; | ||||||||||||||||||||||||||||||||||||||||||
| 42 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | package main; | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | my $orm = My::ORM->new( | ||||||||||||||||||||||||||||||||||||||||||
| 47 | dbh => $dbh, | ||||||||||||||||||||||||||||||||||||||||||
| 48 | types => {users => {prefs => undef}}, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | The schema building examples below use an ORM class whose table classes | ||||||||||||||||||||||||||||||||||||||||||
| 52 | define their tables (see L<DBIx::QuickORM::DSL/":schema">). Each is a file | ||||||||||||||||||||||||||||||||||||||||||
| 53 | of its own. Their identity columns need PostgreSQL 10 or later. | ||||||||||||||||||||||||||||||||||||||||||
| 54 | F<lib/My/Shop.pm>: | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | package My::Shop; | ||||||||||||||||||||||||||||||||||||||||||
| 57 | use DBIx::QuickORM::DSL qw/:orm enum/; | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | enum status => [qw/open shipped/]; | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | orm { }; | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | And F<lib/My/Shop/Orders.pm>: | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | package My::Shop::Orders; | ||||||||||||||||||||||||||||||||||||||||||
| 68 | use DBIx::QuickORM::DSL qw/:table :schema/; | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | table { name 'orders' }; | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | schema { | ||||||||||||||||||||||||||||||||||||||||||
| 73 | column 'id' => sub { db_type 'integer'; primary_key; identity }; | ||||||||||||||||||||||||||||||||||||||||||
| 74 | column 'status' => sub { enum 'status'; not_null; default 'open' }; | ||||||||||||||||||||||||||||||||||||||||||
| 75 | column 'total' => { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | db_type => 'numeric(10,2)', | ||||||||||||||||||||||||||||||||||||||||||
| 77 | check => \'total >= 0', | ||||||||||||||||||||||||||||||||||||||||||
| 78 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 79 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | And F<lib/My/Shop/Customers.pm>: | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | package My::Shop::Customers; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | use DBIx::QuickORM::DSL qw/:table :schema/; | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | table { name 'customers' }; | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | schema { | ||||||||||||||||||||||||||||||||||||||||||
| 91 | column 'id' => sub { db_type 'integer'; primary_key; identity }; | ||||||||||||||||||||||||||||||||||||||||||
| 92 | column 'name' => sub { db_type 'text'; not_null }; | ||||||||||||||||||||||||||||||||||||||||||
| 93 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | =head1 PUBLIC METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | =item $orm = My::ORM->new(%args) | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | =item $orm = My::ORM->new(\%args) | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | Constructs the ORM from the collected specs and C<%args>, which takes the | ||||||||||||||||||||||||||||||||||||||||||
| 106 | arguments of L<DBIx::QuickORM's C<new>|DBIx::QuickORM/"PUBLIC METHODS">, and | ||||||||||||||||||||||||||||||||||||||||||
| 107 | one more: | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | =item deploy => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | Creates the schema the table classes define (see C<build_schema>) on the | ||||||||||||||||||||||||||||||||||||||||||
| 114 | connection's first handle, then scans the database as usual, never reusing | ||||||||||||||||||||||||||||||||||||||||||
| 115 | a scan from before: | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | # A new, empty database: creates the status enum type, then the tables, | ||||||||||||||||||||||||||||||||||||||||||
| 118 | # then scans as usual. | ||||||||||||||||||||||||||||||||||||||||||
| 119 | my $orm = My::Shop->new( | ||||||||||||||||||||||||||||||||||||||||||
| 120 | credentials => \%credentials, | ||||||||||||||||||||||||||||||||||||||||||
| 121 | deploy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 122 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | my $order = $orm->insert(orders => {total => 5}); # status 'open', the default | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | It assumes none of the schema exists, and a failing statement croaks. | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | Precedence, lowest to highest: the C<orm> block's keywords, its C<override>, | ||||||||||||||||||||||||||||||||||||||||||
| 131 | the table classes, then C<%args>. C<time_zone> and its alias C<tz> count as | ||||||||||||||||||||||||||||||||||||||||||
| 132 | one argument, so either spelling in C<%args> beats either in C<override>. | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | C<types> merge per field and C<fks> per target table: a higher source's | ||||||||||||||||||||||||||||||||||||||||||
| 135 | entry for a field (or target table) replaces the lower one's, and the rest | ||||||||||||||||||||||||||||||||||||||||||
| 136 | stay: | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | package My::Blog; | ||||||||||||||||||||||||||||||||||||||||||
| 139 | use DBIx::QuickORM::DSL ':orm'; | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | orm { | ||||||||||||||||||||||||||||||||||||||||||
| 142 | retain_cache 0; | ||||||||||||||||||||||||||||||||||||||||||
| 143 | override(retain_cache => 1); # beats the keyword | ||||||||||||||||||||||||||||||||||||||||||
| 144 | override(types => {users => {prefs => undef}}); | ||||||||||||||||||||||||||||||||||||||||||
| 145 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | package main; | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | my $orm = My::Blog->new( | ||||||||||||||||||||||||||||||||||||||||||
| 150 | dbh => $dbh, | ||||||||||||||||||||||||||||||||||||||||||
| 151 | types => {users => {created => undef}}, # merges with prefs per field | ||||||||||||||||||||||||||||||||||||||||||
| 152 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | # retain_cache is 1. types merge per field: override's users.prefs entry | ||||||||||||||||||||||||||||||||||||||||||
| 155 | # and new's users.created entry both apply, so neither field has a type. | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | C<omit>, C<default_fields>, and C<row_classes> apply per table: a higher | ||||||||||||||||||||||||||||||||||||||||||
| 158 | source's entry for a table replaces the lower one's entry for that table | ||||||||||||||||||||||||||||||||||||||||||
| 159 | whole. C<omit> and C<default_fields> count as one setting there, so a higher | ||||||||||||||||||||||||||||||||||||||||||
| 160 | source's C<omit> for a table also drops a lower one's C<default_fields> for | ||||||||||||||||||||||||||||||||||||||||||
| 161 | it, and the other way around. | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | Croaks when: | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | The class has no C<orm> block of its own. Specs are not inherited, so a | ||||||||||||||||||||||||||||||||||||||||||
| 170 | subclass of an ORM class needs its own. | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | Two table classes name one table. | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | A C<view> class names a table, or a C<table> class a view. | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | A table class names a table the database does not have, unless the table is | ||||||||||||||||||||||||||||||||||||||||||
| 183 | elective (its C<table> block says C<elective>, or C<elective> in C<new> or | ||||||||||||||||||||||||||||||||||||||||||
| 184 | C<override> names it). Elective names from every source add up. | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | C<tables> lists a class without a C<table> block. | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 20 | my %TABLE_ARG = ( | |||||||||||||||||||||||||||||||||||||||||
| 197 | omit => 'omit', | ||||||||||||||||||||||||||||||||||||||||||
| 198 | default_fields => 'default_fields', | ||||||||||||||||||||||||||||||||||||||||||
| 199 | types => 'types', | ||||||||||||||||||||||||||||||||||||||||||
| 200 | fks => 'fks', | ||||||||||||||||||||||||||||||||||||||||||
| 201 | pks => 'pks', | ||||||||||||||||||||||||||||||||||||||||||
| 202 | row_class => 'row_classes', | ||||||||||||||||||||||||||||||||||||||||||
| 203 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | 20 | my %PER_TABLE = map { $_ => 1 } values %TABLE_ARG; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 20 | my %PER_ENTRY = (types => 1, fks => 1); | |||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 50 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=16 SCAR=27.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 50 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 50 | ▶ | my %args = @_ == 1 && ref($_[0]) eq 'HASH' ? %{$_[0]} : @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1
ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | 50 | ▶ | my $spec = DBIx::QuickORM::DSL->orm_spec($class) or croak "$class has no orm block; DBIx::QuickORM::ORM specs are not inherited"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | 49 | my %orm = %$spec; | |||||||||||||||||||||||||||||||||||||||||
| 215 | 49 | my $tables = delete $orm{tables}; | |||||||||||||||||||||||||||||||||||||||||
| 216 | 49 | ▶ | my $override = {%{delete($orm{override}) // {}}}; # a copy: the spec's own hash stays as declared | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $orm{"override"} // {}
MC/DC100%1/1
delete $orm{"override"} // {}
delete $orm{"override"}
Truth table100%2/2
A
A= delete $orm{"override"}
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | 49 | my ($from_tables, $claimed, $elective, $kind) = $class->_table_args($tables); | |||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | # Elective tables add up from every source. An offline map also lacks the tables it cannot map (given as | ||||||||||||||||||||||||||||||||||||||||||
| 221 | # SQL, or with no schema, and while views are built from handles, those not built yet). | ||||||||||||||||||||||||||||||||||||||||||
| 222 | 45 | ▶ | my %elective = map { $_ => 1 } @$elective, @{delete($override->{elective}) // []}, @{$args{elective} // []}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%4/4
delete $override->{"elective"} // []
$args{"elective"} // []
MC/DC100%1/1
delete $override->{"elective"} // []
delete $override->{"elective"}
MC/DC100%1/1
$args{"elective"} // []
$args{"elective"}
Truth table100%2/2
A
A= delete $override->{"elective"}
Truth table100%2/2
A
A= $args{"elective"}
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 45 | ▶ | if (my $offline = $args{_offline}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 7 | $elective{$_} = 1 for grep { !$offline->{db_map}->table($_) } keys %$claimed; | |||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||
| 226 | 45 | ▶ | $args{elective} = [sort keys %elective] if %elective; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 45 | ▶ | if (delete $args{deploy}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 3 | my @classes = $class->_table_classes($tables); | |||||||||||||||||||||||||||||||||||||||||
| 229 | 3 | ▶ | $args{_build_schema} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 3 | my ($dbh) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 3 | my $dialect = DBIx::QuickORM::Dialect->for_dbh($dbh); | |||||||||||||||||||||||||||||||||||||||||
| 232 | 3 | $class->_run_statements($dbh, $class->_build_statements($dialect, \@classes)); | |||||||||||||||||||||||||||||||||||||||||
| 233 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 45 | my $self = $class->SUPER::new($class->_merge({%orm, %$override}, $from_tables, \%args), caller => [caller]); | |||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | 40 | for my $table (sort keys %$claimed) { | |||||||||||||||||||||||||||||||||||||||||
| 238 | 88 | my $record = $self->db_map->table($table); | |||||||||||||||||||||||||||||||||||||||||
| 239 | 88 | ▶ | unless ($record) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 34 | ▶ | next if $elective{$table}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 2 | croak "$claimed->{$table} names the table '$table', which is not in the database map"; | |||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 54 | ▶ | my $is = $record->{view} ? 'view' : 'table'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 54 | ▶ | next if $is eq $kind->{$table}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 2 | croak "$claimed->{$table} names '$table' with a '$kind->{$table}' block, but it is a $is: use a '$is' block (import :$is)"; | |||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 36 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | =item $sql = My::ORM->build_sql(dialect => $name, version => $version, tables => \@tables, pretty => $bool) | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | =item @sql = My::ORM->build_sql(...) | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | The SQL that creates the schema the table classes define, with no database: | ||||||||||||||||||||||||||||||||||||||||||
| 260 | one string of statements, or the statements in list context. | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | # One string: CREATE TYPE "status" ..., then the two CREATE TABLEs | ||||||||||||||||||||||||||||||||||||||||||
| 263 | my $sql = My::Shop->build_sql( | ||||||||||||||||||||||||||||||||||||||||||
| 264 | dialect => 'postgresql', | ||||||||||||||||||||||||||||||||||||||||||
| 265 | pretty => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 266 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | # One statement each, for MariaDB 10.6 | ||||||||||||||||||||||||||||||||||||||||||
| 269 | my @sql = My::Shop->build_sql( | ||||||||||||||||||||||||||||||||||||||||||
| 270 | dialect => 'mariadb', | ||||||||||||||||||||||||||||||||||||||||||
| 271 | version => '10.6', | ||||||||||||||||||||||||||||||||||||||||||
| 272 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | # Only the orders table, and the enum it uses: no customers | ||||||||||||||||||||||||||||||||||||||||||
| 275 | my $orders = My::Shop->build_sql( | ||||||||||||||||||||||||||||||||||||||||||
| 276 | dialect => 'postgresql', | ||||||||||||||||||||||||||||||||||||||||||
| 277 | tables => ['orders'], | ||||||||||||||||||||||||||||||||||||||||||
| 278 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | C<dialect> is required: C<postgresql>, C<mysql>, C<mariadb>, C<sqlite>, or | ||||||||||||||||||||||||||||||||||||||||||
| 281 | C<duckdb>. | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | C<version> is the server version to write for, such as C<'10.6'>. Without | ||||||||||||||||||||||||||||||||||||||||||
| 284 | it, the newest is assumed. | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | C<tables> limits it to those tables, by name. The named enums the tables | ||||||||||||||||||||||||||||||||||||||||||
| 287 | use come before the tables, so the output still creates them. | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | C<pretty> makes the SQL easy to read: each table's columns and constraints | ||||||||||||||||||||||||||||||||||||||||||
| 290 | on lines of their own, and in the string a blank line between groups (the | ||||||||||||||||||||||||||||||||||||||||||
| 291 | statements before the tables, each table, each view, and the SQL after). | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | =item $sql = My::ORM->enum_sql(dialect => $name, version => $version) | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | =item @sql = My::ORM->enum_sql(...) | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | The statements that create the named enums the ORM's columns use, as | ||||||||||||||||||||||||||||||||||||||||||
| 298 | C<build_sql> gives them: C<CREATE TYPE> on PostgreSQL and DuckDB, and none | ||||||||||||||||||||||||||||||||||||||||||
| 299 | on MySQL, MariaDB, and SQLite, whose enums are written into the column: | ||||||||||||||||||||||||||||||||||||||||||
| 300 | an C<ENUM(...)> column type on MySQL and MariaDB, a C<text> column with a | ||||||||||||||||||||||||||||||||||||||||||
| 301 | C<CHECK> on SQLite. One-off enums belong to their tables, so they are left | ||||||||||||||||||||||||||||||||||||||||||
| 302 | out. | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | my $sql = My::Shop->enum_sql(dialect => 'postgresql'); # CREATE TYPE "status" AS ENUM ('open', 'shipped'); | ||||||||||||||||||||||||||||||||||||||||||
| 305 | my @none = My::Shop->enum_sql(dialect => 'sqlite'); # (): the column is text with CHECK ("status" IN (...)) | ||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | =item My::ORM->build_schema(%connection) | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | Creates the schema on the database a C<new> connection form names (C<dbh>, | ||||||||||||||||||||||||||||||||||||||||||
| 310 | C<connect>, or C<credentials>), without making an ORM. C<new> does the same | ||||||||||||||||||||||||||||||||||||||||||
| 311 | before scanning when given C<< deploy => 1 >>: | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | My::Shop->build_schema(credentials => \%credentials); | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | my $orm = My::Shop->new(credentials => \%credentials); # the usual scan finds orders | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | It assumes none of the schema exists, and a failing statement croaks, | ||||||||||||||||||||||||||||||||||||||||||
| 318 | leaving what was created before it. | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | =item $orm = My::ORM->offline(dialect => $name, version => $version) | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | An ORM with no database, so no connection and no scan: its map is the one | ||||||||||||||||||||||||||||||||||||||||||
| 331 | the schema definitions describe. Handles build their SQL, and anything that | ||||||||||||||||||||||||||||||||||||||||||
| 332 | would send a query croaks. | ||||||||||||||||||||||||||||||||||||||||||
| 333 | C<dialect> and C<version> are as C<build_sql> takes them. For generating | ||||||||||||||||||||||||||||||||||||||||||
| 334 | SQL, tests, and checking definitions. | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | my $orm = My::Shop->offline(dialect => 'postgresql'); | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | my $pk = $orm->db_map->pk('orders'); # ['id'] | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | # A query croaks: an offline ORM has no database. | ||||||||||||||||||||||||||||||||||||||||||
| 341 | my $ok = eval { $orm->handle('orders')->count; 1 }; # false | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | Tables and views given as SQL, and tables whose class has no C<schema>, are | ||||||||||||||||||||||||||||||||||||||||||
| 344 | not in its map: a handle on one croaks, saying to define it with a | ||||||||||||||||||||||||||||||||||||||||||
| 345 | C<schema> block. Its map differs from a scan | ||||||||||||||||||||||||||||||||||||||||||
| 346 | only in what the database would write differently, such as the spelling | ||||||||||||||||||||||||||||||||||||||||||
| 347 | of column types. | ||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | =item $map = My::ORM->db_map_from_schema(dialect => $name, version => $version) | ||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | That ORM's map, a L<DBIx::QuickORM::Map>, after the usual setup. | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | my $map = My::Shop->db_map_from_schema(dialect => 'postgresql'); | ||||||||||||||||||||||||||||||||||||||||||
| 354 | my $field = $map->field(orders => 'total'); # {type => 'numeric', ...} | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | 20 | my %OFFLINE_ARGS = map { $_ => 1 } qw/dialect version/; | |||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | 4 | ▶ | sub offline { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
offline: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 364 | 4 | my $frame = [caller]; | |||||||||||||||||||||||||||||||||||||||||
| 365 | 4 | my $orm; | |||||||||||||||||||||||||||||||||||||||||
| 366 | 4 | ▶ | eval { $orm = $class->_offline("$class->offline()", @_); 1 } or DBIx::QuickORM::_rethrow_at($frame, "$class->offline()", $@); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 367 | 2 | return $orm; | |||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | 2 | ▶ | sub db_map_from_schema { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
db_map_from_schema: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | 2 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 372 | 2 | my $frame = [caller]; | |||||||||||||||||||||||||||||||||||||||||
| 373 | 2 | my $map; | |||||||||||||||||||||||||||||||||||||||||
| 374 | 2 | ▶ | eval { $map = $class->_offline("$class->db_map_from_schema()", @_)->db_map; 1 } or DBIx::QuickORM::_rethrow_at($frame, "$class->db_map_from_schema()", $@); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 375 | 1 | return $map; | |||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | 6 | ▶ | sub _offline { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_offline: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | 6 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 380 | 6 | my ($what, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 381 | 6 | my @bad = sort grep { !$OFFLINE_ARGS{$_} } keys %args; | |||||||||||||||||||||||||||||||||||||||||
| 382 | 6 | ▶ | croak "$what got unknown argument(s): " . join(', ' => @bad) if @bad; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | 5 | ▶ | croak "$what needs the 'dialect'" unless defined $args{dialect}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | 3 | ▶ | my $spec = DBIx::QuickORM::DSL->orm_spec($class) or croak "$class has no orm block"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 386 | 3 | my $dialect = DBIx::QuickORM::Dialect->for_name($args{dialect}, $args{version}); | |||||||||||||||||||||||||||||||||||||||||
| 387 | 3 | my $build = $class->_schema_process($dialect, [$class->_table_classes($spec->{tables})]); | |||||||||||||||||||||||||||||||||||||||||
| 388 | 3 | my $tables = $build->map_tables; | |||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | # What the map lacks: tables and views given as SQL, and tables with a table class but no schema. | ||||||||||||||||||||||||||||||||||||||||||
| 391 | 3 | my %raw = map { $_->{name} => 'sql' } grep { !$tables->{$_->{name}} } @{$build->tables}; | |||||||||||||||||||||||||||||||||||||||||
| 392 | 3 | for my $table_class ($class->_table_classes($spec->{tables})) { | |||||||||||||||||||||||||||||||||||||||||
| 393 | 14 | my $name = DBIx::QuickORM::DSL->table_spec($table_class)->{name}; | |||||||||||||||||||||||||||||||||||||||||
| 394 | 14 | ▶ | $raw{$name} //= 'none' unless $tables->{$name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$raw{$name} //= "none"
MC/DC100%1/1
$raw{$name} //= "none"
$raw{$name}
Truth table100%2/2
A
A= $raw{$name}
| |||||||||||||||||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | 3 | require DBIx::QuickORM::Map; | |||||||||||||||||||||||||||||||||||||||||
| 398 | 3 | return $class->new(_offline => {dialect => $dialect, db_map => DBIx::QuickORM::Map->new(tables => $tables), raw_tables => \%raw}); | |||||||||||||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | 20 | my %BUILD_SQL_ARGS = map { $_ => 1 } qw/dialect version tables pretty/; | |||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||
| 403 | 33 | ▶ | sub build_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build_sql: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 404 | 33 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 405 | 33 | my $frame = [caller]; | |||||||||||||||||||||||||||||||||||||||||
| 406 | 33 | my $list = wantarray; # an eval block has its own context | |||||||||||||||||||||||||||||||||||||||||
| 407 | 33 | my @out; | |||||||||||||||||||||||||||||||||||||||||
| 408 | 33 | ▶ | eval { @out = $class->_build_sql($list, @_); 1 } or DBIx::QuickORM::_rethrow_at($frame, "$class->build_sql()", $@); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 409 | 24 | ▶ | return $list ? @out : $out[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | 33 | ▶ | sub _build_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_sql: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 413 | 33 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 414 | 33 | my ($list, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 415 | 33 | my @bad = sort grep { !$BUILD_SQL_ARGS{$_} } keys %args; | |||||||||||||||||||||||||||||||||||||||||
| 416 | 33 | ▶ | croak "build_sql() got unknown argument(s): " . join(', ' => @bad) if @bad; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 417 | 32 | ▶ | croak "build_sql() needs the 'dialect' to write for" unless defined $args{dialect}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | 31 | ▶ | my $spec = DBIx::QuickORM::DSL->orm_spec($class) or croak "$class has no orm block"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 420 | 31 | my $dialect = DBIx::QuickORM::Dialect->for_name($args{dialect}, $args{version}); | |||||||||||||||||||||||||||||||||||||||||
| 421 | 30 | my $build = $class->_schema_process($dialect, [$class->_table_classes($spec->{tables})], $args{tables}, pretty => $args{pretty}); | |||||||||||||||||||||||||||||||||||||||||
| 422 | 26 | my @groups = $build->groups; | |||||||||||||||||||||||||||||||||||||||||
| 423 | 24 | ▶ | return map { @$_ } @groups if $list; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 424 | return join( | ||||||||||||||||||||||||||||||||||||||||||
| 425 | $args{pretty} ? "\n" : '' => map { | ||||||||||||||||||||||||||||||||||||||||||
| 426 | 16 | ▶ | join '' => map { "$_;\n" } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 427 | @$_ | ||||||||||||||||||||||||||||||||||||||||||
| 428 | } @groups | ||||||||||||||||||||||||||||||||||||||||||
| 429 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | 20 | my %ENUM_SQL_ARGS = map { $_ => 1 } qw/dialect version/; | |||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | 9 | ▶ | sub enum_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
enum_sql: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 435 | 9 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 436 | 9 | my $frame = [caller]; | |||||||||||||||||||||||||||||||||||||||||
| 437 | 9 | my $list = wantarray; # an eval block has its own context | |||||||||||||||||||||||||||||||||||||||||
| 438 | 9 | my @out; | |||||||||||||||||||||||||||||||||||||||||
| 439 | 9 | ▶ | eval { @out = $class->_enum_sql($list, @_); 1 } or DBIx::QuickORM::_rethrow_at($frame, "$class->enum_sql()", $@); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 440 | 7 | ▶ | return $list ? @out : $out[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | 9 | ▶ | sub _enum_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_enum_sql: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | 9 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 445 | 9 | my ($list, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 446 | 9 | my @bad = sort grep { !$ENUM_SQL_ARGS{$_} } keys %args; | |||||||||||||||||||||||||||||||||||||||||
| 447 | 9 | ▶ | croak "enum_sql() got unknown argument(s): " . join(', ' => @bad) if @bad; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 448 | 8 | ▶ | croak "enum_sql() needs the 'dialect' to write for" unless defined $args{dialect}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | 7 | ▶ | my $spec = DBIx::QuickORM::DSL->orm_spec($class) or croak "$class has no orm block"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 451 | 7 | my $dialect = DBIx::QuickORM::Dialect->for_name($args{dialect}, $args{version}); | |||||||||||||||||||||||||||||||||||||||||
| 452 | 7 | my @sql = $class->_schema_process($dialect, [$class->_table_classes($spec->{tables})])->enum_statements; | |||||||||||||||||||||||||||||||||||||||||
| 453 | 7 | ▶ | return @sql if $list; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 454 | 2 | return join '' => map { "$_;\n" } @sql; | |||||||||||||||||||||||||||||||||||||||||
| 455 | } | ||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||
| 457 | 20 | my %CONNECT_ARGS = map { $_ => 1 } qw/dbh connect credentials/; | |||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | 4 | ▶ | sub build_schema { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build_schema: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 460 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 461 | 4 | my $frame = [caller]; | |||||||||||||||||||||||||||||||||||||||||
| 462 | 4 | ▶ | eval { $class->_build_schema(@_); 1 } or DBIx::QuickORM::_rethrow_at($frame, "$class->build_schema()", $@); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 463 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | 4 | ▶ | sub _build_schema { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_schema: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 467 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 468 | 4 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 469 | 4 | my @bad = sort grep { !$CONNECT_ARGS{$_} } keys %args; | |||||||||||||||||||||||||||||||||||||||||
| 470 | 4 | ▶ | croak "build_schema() takes only a connection form (dbh, connect, or credentials), got: " . join(', ' => @bad) if @bad; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | 3 | ▶ | my $spec = DBIx::QuickORM::DSL->orm_spec($class) or croak "$class has no orm block"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 473 | 3 | my ($connector) = $class->_connector_for({%args}); | |||||||||||||||||||||||||||||||||||||||||
| 474 | 3 | my $dbh = $connector->(); | |||||||||||||||||||||||||||||||||||||||||
| 475 | 3 | $class->_run_statements($dbh, $class->_build_statements(DBIx::QuickORM::Dialect->for_dbh($dbh), [$class->_table_classes($spec->{tables})])); | |||||||||||||||||||||||||||||||||||||||||
| 476 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | =head1 PRIVATE METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||
| 481 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | =item (\%args, \%claimed, \@elective, \%kind) = $class->_table_args($tables) | ||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | The table classes' C<new> arguments, which class named each table, the | ||||||||||||||||||||||||||||||||||||||||||
| 486 | tables marked C<elective>, and whether each class's block was C<table> or | ||||||||||||||||||||||||||||||||||||||||||
| 487 | C<view>. | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||
| 493 | 49 | ▶ | sub _table_args { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_table_args: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 494 | 49 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 495 | 49 | my ($tables) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||
| 497 | 49 | my (%args, %claimed, @elective, %kind); | |||||||||||||||||||||||||||||||||||||||||
| 498 | 49 | for my $table_class ($class->_table_classes($tables)) { | |||||||||||||||||||||||||||||||||||||||||
| 499 | 101 | my $spec = DBIx::QuickORM::DSL->table_spec($table_class); | |||||||||||||||||||||||||||||||||||||||||
| 500 | 101 | my $name = $spec->{name}; | |||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | 101 | ▶ | croak "Both $claimed{$name} and $table_class name the table '$name'" if $claimed{$name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 503 | 100 | $claimed{$name} = $table_class; | |||||||||||||||||||||||||||||||||||||||||
| 504 | 100 | ▶ | push @elective => $name if $spec->{elective}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 505 | 100 | ▶ | $kind{$name} = $spec->{kind} // 'table'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$spec->{"kind"} // "table"
MC/DC0%0/1
$spec->{"kind"} // "table"
$spec->{"kind"}
Truth table50%1/2
A
A= $spec->{"kind"}
| |||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||
| 507 | 100 | for my $key (keys %TABLE_ARG) { | |||||||||||||||||||||||||||||||||||||||||
| 508 | 600 | ▶ | $args{$TABLE_ARG{$key}}->{$name} = $spec->{$key} if exists $spec->{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||
| 512 | 45 | return (\%args, \%claimed, \@elective, \%kind); | |||||||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||
| 515 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||
| 517 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||
| 519 | =item @classes = $class->_table_classes($tables) | ||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||
| 521 | The table classes: those listed, or those found under a namespace (the class's | ||||||||||||||||||||||||||||||||||||||||||
| 522 | own unless C<$tables> names another). Loads each class it looks at. | ||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||
| 524 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||
| 526 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||
| 528 | 98 | ▶ | sub _table_classes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_table_classes: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||||||||||||||||
| 529 | 98 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 530 | 98 | my ($tables) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||
| 532 | 98 | ▶ | if (ref($tables) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 533 | 79 | for my $table_class (@$tables) { | |||||||||||||||||||||||||||||||||||||||||
| 534 | 223 | ▶ | $class->load_class("'tables' lists $table_class, which", $table_class) unless DBIx::QuickORM::DSL->table_spec($table_class); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 535 | 222 | ▶ | croak "'tables' lists $table_class, which has no 'table' block" unless DBIx::QuickORM::DSL->table_spec($table_class); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||||||||||||||||
| 537 | 77 | return @$tables; | |||||||||||||||||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||
| 540 | 19 | ▶ | my $namespace = $tables // $class; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$tables // $class
MC/DC0%0/2
$tables // $class
$tables
$class
Truth table66%2/3
A // B
A= $tablesB= $class
| |||||||||||||||||||||||||||||||||||||||||||
| 541 | 19 | ▶ | croak "'tables' must be a namespace or an arrayref of class names" if ref($namespace) || !length($namespace); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $namespace or not length $namespace
MC/DC50%1/2
ref $namespace or not length $namespace
ref $namespace
not length $namespace
Truth table66%2/3
A or not(B)
A= ref $namespaceB= not length $namespace
| |||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||
| 543 | 18 | (my $dir = $namespace) =~ s{::}{/}g; | |||||||||||||||||||||||||||||||||||||||||
| 544 | 18 | my %found; | |||||||||||||||||||||||||||||||||||||||||
| 545 | 18 | for my $inc (grep { !ref($_) } @INC) { | |||||||||||||||||||||||||||||||||||||||||
| 546 | 146 | ▶ | opendir(my $dh, "$inc/$dir") or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 547 | 9 | ▶ | $found{"${namespace}::$_"} = 1 for map { m/^(\w+)\.pm$/ ? $1 : () } readdir($dh); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||
| 550 | 18 | my @classes = sort keys %found; | |||||||||||||||||||||||||||||||||||||||||
| 551 | 18 | $class->load_class("The table class '$_'", $_) for @classes; | |||||||||||||||||||||||||||||||||||||||||
| 552 | 18 | return grep { DBIx::QuickORM::DSL->table_spec($_) } @classes; | |||||||||||||||||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||
| 555 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||||||
| 557 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||||||
| 559 | =item $orm = $class->_offline($what, %args) | ||||||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||||||
| 561 | The work of C<offline> and C<db_map_from_schema> (C<$what> names the | ||||||||||||||||||||||||||||||||||||||||||
| 562 | method in errors). | ||||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||
| 564 | =item $build = $class->_schema_process($dialect, \@table_classes, \@only, pretty => $bool) | ||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||
| 566 | =item @sql = $class->_build_statements($dialect, \@table_classes, \@only) | ||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||
| 568 | Croaks for a table class that gives an undef entry for a dialect but is not | ||||||||||||||||||||||||||||||||||||||||||
| 569 | C<elective>. The L<DBIx::QuickORM::Process::BuildSchema> for, and the schema | ||||||||||||||||||||||||||||||||||||||||||
| 570 | building statements of, the ORM's own C<schema> block and the | ||||||||||||||||||||||||||||||||||||||||||
| 571 | table classes that define a schema, through | ||||||||||||||||||||||||||||||||||||||||||
| 572 | L<DBIx::QuickORM::Process::BuildSchema>, the tables limited to those in | ||||||||||||||||||||||||||||||||||||||||||
| 573 | C<@only> when given. Croaks when there is no schema at all, and for a name in | ||||||||||||||||||||||||||||||||||||||||||
| 574 | C<@only> no table class defines. | ||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||
| 576 | =item @sql = $class->_build_sql($list, %args) | ||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||
| 578 | =item @sql = $class->_enum_sql($list, %args) | ||||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||
| 580 | =item $class->_build_schema(%args) | ||||||||||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||||||||||
| 582 | The work of C<build_sql> and C<enum_sql> (C<$list> true for list context) | ||||||||||||||||||||||||||||||||||||||||||
| 583 | and C<build_schema>, which run it so any error also names the caller's line. | ||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||
| 585 | =item $class->_run_statements($dbh, @sql) | ||||||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||||||
| 587 | Runs each statement on the handle, in order, then empties the scan cache | ||||||||||||||||||||||||||||||||||||||||||
| 588 | (even when a statement fails), so no connection reuses a scan from before. | ||||||||||||||||||||||||||||||||||||||||||
| 589 | A failure dies naming the statement, since drivers do not reliably say. | ||||||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||||||
| 591 | =item %args = $class->_merge(\%lowest, ..., \%highest) | ||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | Merges argument sets, a higher one replacing a lower one's argument, or for | ||||||||||||||||||||||||||||||||||||||||||
| 594 | the per-table arguments its entry for each table it names. C<types> and | ||||||||||||||||||||||||||||||||||||||||||
| 595 | C<fks> go one level deeper: per field and per target table. A table named in a | ||||||||||||||||||||||||||||||||||||||||||
| 596 | higher set's C<omit> or C<default_fields> loses its entries in both from the | ||||||||||||||||||||||||||||||||||||||||||
| 597 | lower sets. C<time_zone> and its alias C<tz> count as one argument. | ||||||||||||||||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||||||||||||||||
| 599 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||
| 601 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||||||||||
| 603 | 6 | ▶ | sub _build_statements { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_statements: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 604 | 6 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 605 | 6 | return $class->_schema_process(@_)->statements; | |||||||||||||||||||||||||||||||||||||||||
| 606 | } | ||||||||||||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||||||||||||
| 608 | 46 | ▶ | sub _schema_process { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_schema_process: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||||||||||||||||
| 609 | 46 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 610 | 46 | my ($dialect, $table_classes, $only, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 611 | 46 | my @tables; | |||||||||||||||||||||||||||||||||||||||||
| 612 | 46 | for my $table_class (@$table_classes) { | |||||||||||||||||||||||||||||||||||||||||
| 613 | 122 | ▶ | my $schema = DBIx::QuickORM::DSL->table_schema($table_class) or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 614 | 112 | ▶ | my $entries = $schema->{raw} // ($schema->{view} ? $schema->{view}->{entries} : []); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$schema->{"raw"} // ($schema->{"view"} ? $schema->{"view"}{"entries"} : [])
MC/DC0%0/2
$schema->{"raw"} // ($schema->{"view"} ? $schema->{"view"}{"entries"} : [])
$schema->{"raw"}
($schema->{"view"} ? $schema->{"view"}{"entries"} : [])
Truth table66%2/3
A // B
A= $schema->{"raw"}B= ($schema->{"view"} ? $schema->{"view"}{"entries"} : [])
| |||||||||||||||||||||||||||||||||||||||||||
| 615 | my @skips = sort map { | ||||||||||||||||||||||||||||||||||||||||||
| 616 | 112 | my $e = $_; | |||||||||||||||||||||||||||||||||||||||||
| 617 | 30 | grep { !defined $e->{sql}->{$_} } keys %{$e->{sql}} | |||||||||||||||||||||||||||||||||||||||||
| 618 | } @$entries; | ||||||||||||||||||||||||||||||||||||||||||
| 619 | 2 | croak "$table_class gives " . join(', ' => map { "$_ => undef" } @skips) . ", which builds without its table there, so its table block must say 'elective'" | |||||||||||||||||||||||||||||||||||||||||
| 620 | 112 | ▶ | if @skips && !DBIx::QuickORM::DSL->table_spec($table_class)->{elective}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@skips and not "DBIx::QuickORM::DSL"->table_spec($table_class)->{"elective"}
MC/DC100%2/2
@skips and not "DBIx::QuickORM::DSL"->table_spec($table_class)->{"elective"}
@skips
not "DBIx::QuickORM::DSL"->table_spec($table_class)->{"elective"}
Truth table100%3/3
A and not(B)
A= @skipsB= not "DBIx::QuickORM::DSL"->table_spec($table_class)->{"elective"}
| |||||||||||||||||||||||||||||||||||||||||||
| 621 | 110 | push @tables => $schema; | |||||||||||||||||||||||||||||||||||||||||
| 622 | } | ||||||||||||||||||||||||||||||||||||||||||
| 623 | 44 | my $orm = DBIx::QuickORM::DSL->orm_schema($class); | |||||||||||||||||||||||||||||||||||||||||
| 624 | 44 | ▶ | croak "$class has no schema: no 'schema' block in its orm block, and no table class with a 'schema' block or 'schema_sql'" unless @tables || $orm; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@tables or $orm
MC/DC50%1/2
@tables or $orm
@tables
$orm
Truth table66%2/3
A or B
A= @tablesB= $orm
| |||||||||||||||||||||||||||||||||||||||||||
| 625 | 43 | ▶ | if ($only) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 626 | 6 | my %have = map { $_->{name} => 1 } @tables; | |||||||||||||||||||||||||||||||||||||||||
| 627 | 6 | ▶ | $have{$_} or croak "build_sql() lists the table '$_', which no table class of $class defines a schema for" for @$only; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 628 | 5 | my %want = map { $_ => 1 } @$only; | |||||||||||||||||||||||||||||||||||||||||
| 629 | 5 | @tables = grep { $want{$_->{name}} } @tables; | |||||||||||||||||||||||||||||||||||||||||
| 630 | } | ||||||||||||||||||||||||||||||||||||||||||
| 631 | 42 | require DBIx::QuickORM::Process::BuildSchema; | |||||||||||||||||||||||||||||||||||||||||
| 632 | 42 | return DBIx::QuickORM::Process::BuildSchema->new(dialect => $dialect, tables => \@tables, types => $class, namespace => $class, orm => $orm, pretty => $opts{pretty}); | |||||||||||||||||||||||||||||||||||||||||
| 633 | } | ||||||||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||||||||
| 635 | 5 | ▶ | sub _run_statements { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_statements: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 636 | 5 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 637 | 5 | my ($dbh, @sql) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 638 | # The error is rethrown below, naming the statement: DBI printing it too only doubles it. | ||||||||||||||||||||||||||||||||||||||||||
| 639 | 5 | local $dbh->{PrintError} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 640 | 5 | local $dbh->{RaiseError} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 641 | # Even a failed build may have created some tables. | ||||||||||||||||||||||||||||||||||||||||||
| 642 | 5 | my $failed; | |||||||||||||||||||||||||||||||||||||||||
| 643 | 5 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 644 | 5 | for my $sql (@sql) { | |||||||||||||||||||||||||||||||||||||||||
| 645 | 21 | $failed = $sql; | |||||||||||||||||||||||||||||||||||||||||
| 646 | 21 | $dbh->do($sql); | |||||||||||||||||||||||||||||||||||||||||
| 647 | } | ||||||||||||||||||||||||||||||||||||||||||
| 648 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 649 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 650 | 5 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 651 | 5 | require DBIx::QuickORM::Connection; | |||||||||||||||||||||||||||||||||||||||||
| 652 | 5 | DBIx::QuickORM::Connection->_forget_scans; | |||||||||||||||||||||||||||||||||||||||||
| 653 | 5 | ▶ | return if $ok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 654 | 2 | ▶ | die $err if ref $err; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 655 | 2 | chomp($err); | |||||||||||||||||||||||||||||||||||||||||
| 656 | # Drivers do not reliably name the statement, and the SQL may be the user's own (raw index SQL, using, schema_sql). | ||||||||||||||||||||||||||||||||||||||||||
| 657 | 2 | die "Building the schema failed on this statement:\n$failed\n$err\n"; | |||||||||||||||||||||||||||||||||||||||||
| 658 | } | ||||||||||||||||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||||||||||||||||
| 660 | 45 | ▶ | sub _merge { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_merge: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||||||||||||||||
| 661 | 45 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||||||||||||
| 663 | 45 | my %merged; | |||||||||||||||||||||||||||||||||||||||||
| 664 | 45 | for my $args (@_) { | |||||||||||||||||||||||||||||||||||||||||
| 665 | 135 | ▶ | delete @merged{qw/time_zone tz/} if grep { exists $args->{$_} } qw/time_zone tz/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||||||||||
| 667 | 135 | ▶ | my @named = map { ref($args->{$_}) eq 'HASH' ? keys %{$args->{$_}} : () } qw/omit default_fields/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 668 | 135 | for my $key (grep { ref($merged{$_}) eq 'HASH' } qw/omit default_fields/) { | |||||||||||||||||||||||||||||||||||||||||
| 669 | 19 | my %kept = %{$merged{$key}}; | |||||||||||||||||||||||||||||||||||||||||
| 670 | 19 | delete @kept{@named}; | |||||||||||||||||||||||||||||||||||||||||
| 671 | 19 | $merged{$key} = \%kept; | |||||||||||||||||||||||||||||||||||||||||
| 672 | } | ||||||||||||||||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||||||||||||||||
| 674 | 135 | for my $key (keys %$args) { | |||||||||||||||||||||||||||||||||||||||||
| 675 | 161 | my ($have, $new) = ($merged{$key}, $args->{$key}); | |||||||||||||||||||||||||||||||||||||||||
| 676 | 161 | ▶ | unless ($PER_TABLE{$key} && ref($have) eq 'HASH' && ref($new) eq 'HASH') { $merged{$key} = $new; next } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$PER_TABLE{$key} and ref $have eq "HASH" and ref $new eq "HASH"
$PER_TABLE{$key} and ref $have eq "HASH"
MC/DC66%2/3
$PER_TABLE{$key} and ref $have eq "HASH" and ref $new eq "HASH"
$PER_TABLE{$key}
ref $have eq "HASH"
ref $new eq "HASH"
Truth table75%3/4
A and B and C
A= $PER_TABLE{$key}B= ref $have eq "HASH"C= ref $new eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||||||||||||||||
| 678 | 13 | my %both = (%$have, %$new); | |||||||||||||||||||||||||||||||||||||||||
| 679 | 13 | ▶ | if ($PER_ENTRY{$key}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 680 | 4 | ▶ | for my $table (grep { ref($have->{$_}) eq 'HASH' && ref($new->{$_}) eq 'HASH' } keys %$new) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $have->{$_} eq 'HASH' && ref $new->{$_} eq 'HASH'
MC/DC50%1/2
ref $have->{$_} eq 'HASH' && ref $new->{$_} eq 'HASH'
ref $have->{$_} eq 'HASH'
ref $new->{$_} eq 'HASH'
Truth table66%2/3
A && B
A= ref $have->{$_} eq 'HASH'B= ref $new->{$_} eq 'HASH'
| |||||||||||||||||||||||||||||||||||||||||||
| 681 | 3 | $both{$table} = {%{$have->{$table}}, %{$new->{$table}}}; | |||||||||||||||||||||||||||||||||||||||||
| 682 | } | ||||||||||||||||||||||||||||||||||||||||||
| 683 | } | ||||||||||||||||||||||||||||||||||||||||||
| 684 | 13 | $merged{$key} = \%both; | |||||||||||||||||||||||||||||||||||||||||
| 685 | } | ||||||||||||||||||||||||||||||||||||||||||
| 686 | } | ||||||||||||||||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||||||||||||||||
| 688 | 45 | return %merged; | |||||||||||||||||||||||||||||||||||||||||
| 689 | } | ||||||||||||||||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||||||||||||||||
| 691 | 20 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||||||||||||||||
| 693 | __END__ | ||||||||||||||||||||||||||||||||||||||||||