Oracle/DDL.pm

filter: Statementstmt 98.7% 244 / 247 Branchbran 81.0% 128 / 158 Conditioncond 61.8% 55 / 89 MC/DCmcdc 46.0% 23 / 50 Subroutinesub 100% 18 / 18 Podpod 100% 3 / 3 totaltotal 83.3% 471 / 565 CC 128 SCAR 37.4
CC
128
Cov
86%
_build
61.1
_table_foreign_keys
60.7
_cascading_has_many
16.7
CRAP 42.0
« Oracle/Storage/AutoIncrement.pm ↑ Summary Oracle/Type.pm »
1 package DBIO::Oracle::DDL;
2 # ABSTRACT: Generate Oracle DDL from DBIO Result classes
3  
4 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 26 our $VERSION = '0.900007';
8  
9 26 ▶use DBIO::Oracle::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 26 ▶use DBIO::Oracle::Identifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 # Every identifier is written quoted, exactly as the result class spells it
14 # (karr k42): the storage quotes with sql_quote_char '"' and so asks the
15 # dictionary for the lower-case name, while an unquoted CREATE folds it to
16 # upper case. DBIO::SQL::Util::_quote_ident leaves a simple name unquoted and
17 # is therefore the wrong tool here.
18 708 ▶sub _q {
Subroutine _q: called CC=1 SCAR=0.0
19 708 my ($name) = @_;
20 708 $name =~ s/"/""/g;
21 708 return qq{"$name"};
22 }
23  
24 # A table name that holds a dot is schema.table (karr k58): the table lives in
25 # that schema, which on Oracle is a user. Schema and table are quoted apart,
26 # each as spelled -- "OWNX"."item", never "OWNX.item".
27 192 ▶sub _qt { DBIO::Oracle::Identifier::quote_table($_[0]) }
Subroutine _qt: called CC=1 SCAR=0.0
28  
29 # A name in the schema of the table whose key is $table: sequence, trigger,
30 # index.
31 89 ▶sub _qin { DBIO::Oracle::Identifier::quote_in(@_) }
Subroutine _qin: called CC=1 SCAR=0.0
32  
33 # Records an object of the install in $objects. A name of the connected
34 # user's schema goes to $objects->{$type}; one in another schema ('schema.x',
35 # $name still qualified) to $objects->{schema}{$schema}{$type} by its bare
36 # name, which is how the cleanup finds it. $index_table, for an index, is the
37 # bare table it is on.
38 233 ▶sub _note_object {
Subroutine _note_object: called CC=3 SCAR=11.0
39 233 my ($objects, $type, $name, $index_table) = @_;
40 233 my ($schema, $bare) = DBIO::Oracle::Identifier::split_table($name);
41 233 ▶ my $slot = defined $schema ? \@{ $objects->{schema}{$schema}{$type} } : \@{ $objects->{$type} };
Branch: 1 branch
BranchTrueFalseTotal
defined $schema ? : 89 144 233
42 233 ▶ push @$slot, defined $index_table ? [ $bare, $index_table ] : $bare;
Branch: 1 branch
BranchTrueFalseTotal
defined $index_table ? : 36 197 233
43 233 return;
44 }
45  
46 # What Oracle cannot hold is refused here, as a DBIO exception that names the
47 # table and the column, before any statement is built (core ADR 0060, karr
48 # k56): the server's answer to each of them is an error of its own
49 # (ORA-00902, ORA-25716, ORA-03060), or, for a dot in a table name, a table
50 # nobody can reach. The names are the ones of the declaration.
51 176 ▶sub _refuse_unsupported {
Subroutine _refuse_unsupported: called CC=13 SCAR=25.7
52 176 my ($source, $table_name, $literal) = @_;
53 176 my @cols = $source->columns;
54  
55 # one message for every offending name, so that it names them all
56 176 my @bad;
57 176 ▶ push @bad, "the table name '$table_name'" if !$literal && $table_name =~ /["\0]/;
Branch: 1 branch
BranchTrueFalseTotal
if not $literal and $table_name =~ /["\0]/ 2 174 176
Condition100%3/3
not $literal and $table_name =~ /["\0]/
!ll&&!rl&&r
21722
MC/DC100%2/2
not $literal and $table_name =~ /["\0]/
not $literal $table_name =~ /["\0]/
Truth table100%3/3
not(A) and B
A= not $literalB= $table_name =~ /["\0]/
ABresult
0X0
100
111
58 176 push @bad, map { "the column name '$_'" } grep { /["\0]/ } @cols;
59 176 ▶ $source->throw_exception(sprintf
Branch: 2 branches
BranchTrueFalseTotal
if @bad 2 174 176
@bad == 1 ? : 0 2 2
60 "%s: %s hold%s a double quote or NUL character; Oracle refuses that in any identifier, "
61 . "quoted or not (ORA-25716) -- refused, nothing is installed",
62 $table_name, join(' and ', @bad), (@bad == 1 ? 's' : ''))
63 if @bad;
64  
65 174 ▶ $source->throw_exception(sprintf
Branch: 1 branch
BranchTrueFalseTotal
if not $literal and $table_name =~ /\./ and not $table_name =~ /\A[^.]+\.[^.]/s 3 171 174
Condition100%6/6
not $literal and $table_name =~ /\./ and not $table_name =~ /\A[^.]+\.[^.]/s
!ll&&!rl&&r
132393
not $literal and $table_name =~ /\./
!ll&&!rl&&r
213042
MC/DC100%3/3
not $literal and $table_name =~ /\./ and not $table_name =~ /\A[^.]+\.[^.]/s
not $literal $table_name =~ /\./ not $table_name =~ /\A[^.]+\.[^.]/s
Truth table100%4/4
not(A) and B and not(C)
A= not $literalB= $table_name =~ /\./C= not $table_name =~ /\A[^.]+\.[^.]/s
ABCresult
0XX0
10X0
1100
1111
66 "%s: the table name is schema-qualified (schema.table) but a half of it is empty; "
67 . "refused -- a table in another schema is written schema.table, the dot is not part of a name",
68 $table_name)
69 if !$literal && $table_name =~ /\./ && !($table_name =~ /\A[^.]+\.[^.]/s);
70  
71 171 for my $col (@cols) {
72 363 my $type = $source->column_info($col)->{data_type};
73 363 ▶ next unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 363 363
74 # compared as it means: letter case, blanks and a trailing (n) or (a,b)
75 # are not part of the type
76 363 (my $kind = lc $type) =~ s/\s+/ /g;
77 363 $kind =~ s/\A | \z//g;
78 363 $kind =~ s/ ?\(.*\)\z//s;
79 363 ▶ $source->throw_exception(sprintf
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "enum" or $kind eq "set" 9 354 363
Condition100%3/3
$kind eq "enum" or $kind eq "set"
l!l&&r!l&&!r
72354
MC/DC100%2/2
$kind eq "enum" or $kind eq "set"
$kind eq "enum" $kind eq "set"
Truth table100%3/3
A or B
A= $kind eq "enum"B= $kind eq "set"
ABresult
1X1
011
000
80 "%s: column '%s' is of type %s, which has no counterpart on Oracle (Oracle has no "
81 . "%s type); the driver refuses it rather than write a type the server rejects",
82 $table_name, $col, $type, $kind)
83 if $kind eq 'enum' || $kind eq 'set';
84 354 ▶ $source->throw_exception(sprintf
Branch: 1 branch
BranchTrueFalseTotal
if $kind =~ /\A(?:bit(?: varying)?|varbit)\z/ 8 346 354
85 "%s: column '%s' is of type %s, which has no counterpart on Oracle (Oracle has no BIT "
86 . "type); the driver refuses it rather than write a type the server rejects",
87 $table_name, $col, $type)
88 if $kind =~ /\A(?:bit(?: varying)?|varbit)\z/;
89 }
90 154 return;
91 }
92  
93  
94 89 ▶sub install_ddl {
Subroutine install_ddl: called CC=1 SCAR=0.0
Pod documented
95 89 my ($class, $schema) = @_;
96 89 return $class->_build($schema, {});
97 }
98  
99  
100 33 ▶sub declared_objects {
Subroutine declared_objects: called CC=1 SCAR=0.0
Pod documented
101 33 my ($class, $schema) = @_;
102 33 my $objects = {};
103 33 $class->_build($schema, $objects);
104 32 return $objects;
105 }
106  
107  
108 21 ▶sub foreign_schemas {
Subroutine foreign_schemas: called CC=2 SCAR=6.9
Pod documented
109 21 my ($class, $schema) = @_;
110 21 ▶ return sort keys %{ $class->declared_objects($schema)->{schema} || {} };
Condition100%2/2
$class->declared_objects($schema)->{'schema'} || {}
l!l
1011
MC/DC100%1/1
$class->declared_objects($schema)->{'schema'} || {}
$class->declared_objects($schema)->{'schema'}
Truth table100%2/2
A
A= $class->declared_objects($schema)->{'schema'}
Aresult
11
00
111 }
112  
113 122 ▶sub _build {
Subroutine _build: called CC=59 SCAR=41.1
114 122 my ($class, $schema, $objects) = @_;
115 122 my @stmts;
116 my @view_stmts;
117 122 my %seen_table;
118 122 my %seen_view;
119 122 my @fk_sources;
120  
121 122 for my $source_name (sort $schema->sources) {
122 184 my $source = $schema->source($source_name);
123 184 my $table_name = $source->name;
124  
125 # Views: emit CREATE VIEW after all tables; skip virtual views.
126 184 ▶ if ($source->isa('DBIO::ResultSource::View')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->isa("DBIO::ResultSource::View")) 8 176 184
127 8 ▶ next if $source->can('is_virtual') && $source->is_virtual;
Branch: 1 branch
BranchTrueFalseTotal
if $source->can("is_virtual") and $source->is_virtual 1 7 8
Condition66%2/3
$source->can("is_virtual") and $source->is_virtual
!ll&&!rl&&r
071
MC/DC50%1/2
$source->can("is_virtual") and $source->is_virtual
$source->can("is_virtual") $source->is_virtual
Truth table66%2/3
A and B
A= $source->can("is_virtual")B= $source->is_virtual
ABresult
0X0
100
111
128 7 ▶ my $vname = ref $table_name eq 'SCALAR' ? $$table_name : $table_name;
Branch: 1 branch
BranchTrueFalseTotal
ref $table_name eq 'SCALAR' ? : 0 7 7
129 7 ▶ next if ref $vname || $seen_view{$vname}++;
Branch: 1 branch
BranchTrueFalseTotal
if ref $vname or $seen_view{$vname}++ 0 7 7
Condition33%1/3
ref $vname or $seen_view{$vname}++
l!l&&r!l&&!r
007
MC/DC0%0/2
ref $vname or $seen_view{$vname}++
ref $vname $seen_view{$vname}++
Truth table33%1/3
A or B
A= ref $vnameB= $seen_view{$vname}++
ABresult
1X1
011
000
130 7 ▶ my $def = $source->can('view_definition') ? $source->view_definition : undef;
Branch: 1 branch
BranchTrueFalseTotal
$source->can('view_definition') ? : 7 0 7
131 7 ▶ next unless defined $def && length $def;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def and length $def 0 7 7
Condition33%1/3
defined $def and length $def
!ll&&!rl&&r
007
MC/DC0%0/2
defined $def and length $def
defined $def length $def
Truth table33%1/3
A and B
A= defined $defB= length $def
ABresult
0X0
100
111
132 7 push @view_stmts, sprintf 'CREATE VIEW %s AS %s;', _qt($vname), $def;
133 7 _note_object($objects, 'view', $vname);
134 7 next;
135 }
136  
137 # A scalar-ref name is either a literal table name (\'cd', emitted
138 # unquoted) or an inline subquery source; deref the former, skip the latter.
139 176 my $literal_name;
140 176 ▶ if (ref $table_name eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $table_name eq "SCALAR") 2 174 176
141 2 $table_name = $$table_name;
142 2 ▶ next if $table_name =~ /\s|\(/;
Branch: 1 branch
BranchTrueFalseTotal
if $table_name =~ /\s|\(/ 0 2 2
143 2 $literal_name = 1;
144 }
145 176 ▶ next if ref $table_name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name 0 176 176
146  
147 176 _refuse_unsupported($source, $table_name, $literal_name);
148  
149 # Several result sources can map to one physical table; emit it once.
150 154 ▶ next if $seen_table{$table_name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_table{$table_name}++ 0 154 154
151  
152 # Detect autoincrement columns
153 154 my @autoinc_cols;
154 154 for my $col_name ($source->columns) {
155 330 my $info = $source->column_info($col_name);
156 330 ▶ push @autoinc_cols, $col_name if $info->{is_auto_increment};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_auto_increment"} 24 306 330
157 }
158  
159 # Column definitions
160 154 my @col_defs;
161 my %is_pk;
162 154 my @pk_cols = $source->primary_columns;
163 154 @is_pk{@pk_cols} = (1) x @pk_cols;
164  
165 154 for my $col_name ($source->columns) {
166 330 my $info = $source->column_info($col_name);
167 # a type the mapping refuses (float of a precision Oracle does not have)
168 # is refused with the table and the column of the declaration
169 330 my $type = eval {
170 330 DBIO::Oracle::Type::map_dbio_type_to_oracle($info->{data_type}, size => $info->{size});
171 };
172 330 ▶ unless (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 1 329 330
173 1 (my $why = "$@") =~ s/\s+\z//;
174 1 $source->throw_exception(sprintf "%s: column '%s': %s", $table_name, $col_name, $why);
175 }
176 329 my $def = sprintf ' %s %s', _q($col_name), $type;
177  
178 329 ▶ if ($info->{is_auto_increment}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"is_auto_increment"}) 24 305 329
179 # SERIAL/BIGSERIAL mapped to NUMBER + sequence (type already set)
180 }
181  
182 # DEFAULT before NOT NULL: Oracle's column definition is
183 # "datatype [DEFAULT expr] [inline_constraint ...]" and it refuses the
184 # other order.
185 329 ▶ if (defined $info->{default_value} && !$info->{is_auto_increment}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{"default_value"} and not $info->{"is_auto_increment"}) 15 314 329
Condition66%2/3
defined $info->{"default_value"} and not $info->{"is_auto_increment"}
!ll&&!rl&&r
314015
MC/DC50%1/2
defined $info->{"default_value"} and not $info->{"is_auto_increment"}
defined $info->{"default_value"} not $info->{"is_auto_increment"}
Truth table66%2/3
A and not(B)
A= defined $info->{"default_value"}B= not $info->{"is_auto_increment"}
ABresult
0X0
100
111
186 15 $def .= ' DEFAULT ' . DBIO::Oracle::Type::render_default_value($info->{default_value});
187 }
188  
189 329 ▶ $def .= ' NOT NULL' if !$info->{is_nullable};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"is_nullable"} 272 57 329
190  
191 329 push @col_defs, $def;
192 }
193  
194 # The canonical index declarations of the source (Cake idx, indices;
195 # ADR 0061 in core, karr k51). The helper refuses an entry that carries
196 # only another driver's block and an unknown key in the oracle block;
197 # its message is passed through. An empty oracle block is the plain
198 # index. type (other than unique) and options have no counterpart here
199 # and are refused, never written as a plainer index.
200 153 my @declared;
201 153 ▶ if ($source->can('index_declarations_for')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->can("index_declarations_for")) 149 4 153
202 149 for my $decl ($source->index_declarations_for('oracle', [qw(expression)])) {
203 27 ▶ if (defined $decl->{type} && $decl->{type} !~ /^unique$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decl->{"type"} and not $decl->{"type"} =~ /^unique$/i) 2 25 27
Condition100%3/3
defined $decl->{"type"} and not $decl->{"type"} =~ /^unique$/i
!ll&&!rl&&r
2412
MC/DC100%2/2
defined $decl->{"type"} and not $decl->{"type"} =~ /^unique$/i
defined $decl->{"type"} not $decl->{"type"} =~ /^unique$/i
Truth table100%3/3
A and not(B)
A= defined $decl->{"type"}B= not $decl->{"type"} =~ /^unique$/i
ABresult
0X0
100
111
204 $source->throw_exception(sprintf
205 "%s: index '%s': type '%s' has no Oracle counterpart; refused, not written as a plainer index",
206 2 $table_name, $decl->{name}, $decl->{type});
207 }
208 25 ▶ if (defined $decl->{options}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decl->{"options"}) 2 23 25
209 $source->throw_exception(sprintf
210 "%s: index '%s': options are not supported by the Oracle DDL; refused, not dropped silently",
211 2 $table_name, $decl->{name});
212 }
213 23 push @declared, $decl;
214 }
215 }
216  
217 # The unique constraints of the source (add_unique_constraint, Cake
218 # uniq; karr k46), the primary one excepted: it is the PRIMARY KEY below.
219 # A table-level constraint makes Oracle create the unique index of the
220 # same name. An index declaration of the same name, or over exactly the
221 # columns of a constraint, is refused (below) rather than left to the
222 # server (ORA-01408) or let to displace the constraint. A column set that
223 # is already the primary key or an earlier constraint is not written
224 # again -- Oracle refuses it (ORA-02261).
225 147 my (@unique_defs, @unique_names);
226 {
227 147 my %seen;
228 147 ▶ $seen{ join "\0", sort @pk_cols } = 1 if @pk_cols;
Branch: 1 branch
BranchTrueFalseTotal
if @pk_cols 142 5 147
229 147 my %uc = $source->unique_constraints;
230 147 for my $uc_name (sort keys %uc) {
231 168 ▶ next if $uc_name eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
if $uc_name eq "primary" 141 27 168
232 27 my @cols = @{ $uc{$uc_name} };
233 27 ▶ next unless @cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @cols 0 27 27
234 27 my $cname = DBIO::Oracle::Identifier::shorten($uc_name);
235 27 for my $decl (@declared) {
236 20 ▶ my $own = ($decl->{driver} || {})->{oracle} || {};
Condition75%3/4
+($decl->{'driver'} || {})->{'oracle'} || {}
l!l
218
$decl->{'driver'} || {}
l!l
200
MC/DC0%0/1
+($decl->{'driver'} || {})->{'oracle'} || {}
$decl->{'driver'}
Truth table50%1/2
A
A= $decl->{'driver'}
Aresult
11
00
237 20 ▶ my $same_name = $decl->{name} eq $cname || $decl->{name} eq $uc_name;
Condition66%2/3
$decl->{'name'} eq $cname || $decl->{'name'} eq $uc_name
l!l&&r!l&&!r
1019
MC/DC50%1/2
$decl->{'name'} eq $cname || $decl->{'name'} eq $uc_name
$decl->{'name'} eq $cname $decl->{'name'} eq $uc_name
Truth table66%2/3
A || B
A= $decl->{'name'} eq $cnameB= $decl->{'name'} eq $uc_name
ABresult
1X1
011
000
238 my $same_cols = !defined $own->{expression}
239 20 ▶ && join("\0", @{ $decl->{columns} || [] }) eq join("\0", @cols);
Condition80%4/5
!defined($own->{'expression'}) && join("\000", @{$decl->{'columns'} or [];}) eq join("\000", @cols)
!ll&&!rl&&r
2144
$decl->{'columns'} || []
l!l
180
MC/DC100%2/2
!defined($own->{'expression'}) && join("\000", @{$decl->{'columns'} or [];}) eq join("\000", @cols)
!defined($own->{'expression'}) join("\000", @{$decl->{'columns'} or [];}) eq join("\000", @cols)
MC/DC0%0/1
$decl->{'columns'} || []
$decl->{'columns'}
Truth table100%3/3
not(A) && B
A= !defined($own->{'expression'})B= join("\000", @{$decl->{'columns'} or [];}) eq join("\000", @cols)
ABresult
0X0
100
111
Truth table50%1/2
A
A= $decl->{'columns'}
Aresult
11
00
240 20 ▶ next unless $same_name || $same_cols;
Branch: 1 branch
BranchTrueFalseTotal
unless $same_name or $same_cols 15 5 20
Condition100%3/3
$same_name or $same_cols
l!l&&r!l&&!r
1415
MC/DC100%2/2
$same_name or $same_cols
$same_name $same_cols
Truth table100%3/3
A or B
A= $same_nameB= $same_cols
ABresult
1X1
011
000
241 $source->throw_exception(sprintf
242 "%s: refused, index '%s' %s unique constraint '%s' (%s); Oracle holds one index "
243 . "there (ORA-01408) -- declare the key once, as the unique constraint or as the index",
244 $table_name, $decl->{name},
245 5 ▶ ($same_name ? 'has the name of' : 'is over the columns of'),
Branch: 1 branch
BranchTrueFalseTotal
$same_name ? : 1 4 5
246 $uc_name, join(', ', @cols));
247 }
248 22 ▶ next if $seen{ join "\0", sort @cols }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{join "\0", sort(@cols)}++ 2 20 22
249 20 push @unique_names, $cname;
250 push @unique_defs, sprintf ' CONSTRAINT %s UNIQUE (%s)',
251 20 _q($cname), join(', ', map { _q($_) } @cols);
252 }
253 }
254  
255 # Primary key constraint
256 142 ▶ if (@pk_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols) 137 5 142
257 push @col_defs, sprintf ' PRIMARY KEY (%s)',
258 137 join(', ', map { _q($_) } @pk_cols);
259 }
260  
261 142 push @col_defs, @unique_defs;
262  
263 # A literal table name (\'cd') is the user's own SQL and stays as given,
264 # which is what the storage writes for it as well.
265 142 ▶ my $qualified = $literal_name ? $table_name : _qt($table_name);
Branch: 1 branch
BranchTrueFalseTotal
$literal_name ? : 2 140 142
266 142 ▶ my ($tschema, $bare) = $literal_name
Branch: 1 branch
BranchTrueFalseTotal
$literal_name ? : 2 140 142
267 ? (undef, $table_name) : DBIO::Oracle::Identifier::split_table($table_name);
268 142 my $sql = sprintf "CREATE TABLE %s (\n%s\n);", $qualified, join(",\n", @col_defs);
269 142 push @stmts, $sql;
270 142 ▶ my $own_table = $literal_name ? uc $table_name : $bare;
Branch: 1 branch
BranchTrueFalseTotal
$literal_name ? : 2 140 142
271 142 ▶ _note_object($objects, "table", $literal_name ? $own_table : $table_name);
Branch: 1 branch
BranchTrueFalseTotal
$literal_name ? : 2 140 142
272 # a unique constraint is backed by the index of its name
273 142 ▶ _note_object($objects, "index", defined $tschema ? "$tschema.$_" : $_, $own_table) for @unique_names;
Branch: 1 branch
BranchTrueFalseTotal
defined $tschema ? : 8 11 19
274  
275 # Sequences for autoincrement columns, each with the BEFORE INSERT
276 # trigger that fills the column from it (karr k42). The storage finds the
277 # sequence of a column by reading exactly that trigger (ALL_TRIGGERS),
278 # so a create() without RETURNING can read the key back. The trigger
279 # leaves a value the caller gave alone.
280 142 for my $col_name (@autoinc_cols) {
281 24 my $seq_name = DBIO::Oracle::Identifier::shorten("${bare}_${col_name}_seq");
282 24 push @stmts, sprintf "CREATE SEQUENCE %s;", _qin($table_name, $seq_name);
283 24 ▶ _note_object($objects, "sequence", defined $tschema ? "$tschema.$seq_name" : $seq_name);
Branch: 1 branch
BranchTrueFalseTotal
defined $tschema ? : 17 7 24
284 24 my $trg_name = DBIO::Oracle::Identifier::shorten("${bare}_${col_name}_trg");
285 24 ▶ _note_object($objects, "trigger", defined $tschema ? "$tschema.$trg_name" : $trg_name);
Branch: 1 branch
BranchTrueFalseTotal
defined $tschema ? : 17 7 24
286 24 push @stmts, sprintf
287 "CREATE OR REPLACE TRIGGER %s\n"
288 . "BEFORE INSERT ON %s FOR EACH ROW WHEN (new.%s IS NULL)\n"
289 . "BEGIN\n"
290 . " SELECT %s.NEXTVAL INTO :new.%s FROM DUAL;\n"
291 . "END;",
292 _qin($table_name, $trg_name), $qualified, _q($col_name),
293 _qin($table_name, $seq_name), _q($col_name);
294 }
295  
296 # The declared name is the user's and is used as given; Oracle itself
297 # refuses one that is too long.
298 142 for my $decl (@declared) {
299 17 ▶ my $own = ($decl->{driver} || {})->{oracle} || {};
Condition75%3/4
+($decl->{'driver'} || {})->{'oracle'} || {}
l!l
314
$decl->{'driver'} || {}
l!l
170
MC/DC0%0/1
+($decl->{'driver'} || {})->{'oracle'} || {}
$decl->{'driver'}
Truth table50%1/2
A
A= $decl->{'driver'}
Aresult
11
00
300 my $cols = defined $own->{expression}
301 ? $own->{expression}
302 17 ▶ : join ', ', map { _q($_) } @{ $decl->{columns} };
Branch: 1 branch
BranchTrueFalseTotal
defined $own->{'expression'} ? : 2 15 17
303 push @stmts, sprintf 'CREATE %sINDEX %s ON %s (%s);',
304 17 ▶ $decl->{unique} ? 'UNIQUE ' : '', _qin($table_name, $decl->{name}),
Branch: 1 branch
BranchTrueFalseTotal
$decl->{'unique'} ? : 2 15 17
305 $qualified, $cols;
306 17 ▶ _note_object($objects, "index", defined $tschema ? "$tschema.$decl->{name}" : $decl->{name}, $own_table);
Branch: 1 branch
BranchTrueFalseTotal
defined $tschema ? : 8 9 17
307 }
308  
309 142 push @fk_sources, [ $table_name, $source, $qualified ];
310 }
311  
312 # Foreign keys, after every CREATE TABLE (karr k42): a table may refer to
313 # one that sorts after it, or to itself, so no table order would do.
314 88 for my $triple (@fk_sources) {
315 137 my ($table_name, $source, $qualified) = @$triple;
316 push @stmts, map {
317 137 sprintf 'ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;',
318 $qualified, _q($_->{constraint_name}),
319 44 join(', ', map { _q($_) } @{ $_->{from_columns} }),
320 $_->{to_qualified},
321 44 join(', ', map { _q($_) } @{ $_->{to_columns} }),
322 44 $_->{actions};
323 } _table_foreign_keys($schema, $source, \%seen_table);
324 }
325  
326 83 return join "
327  
328 ", @stmts, @view_stmts;
329 }
330  
331 # True when $target (the table a belongs_to points at) has a has_many back to
332 # $table over the same column pairs with cascade_delete: the other side of
333 # the belongs_to, which is what Relationship::Base describes as the
334 # autodetection of on_delete.
335 44 ▶sub _cascading_has_many {
Subroutine _cascading_has_many: called CC=15 SCAR=28.1
336 44 my ($target, $table, $from, $to) = @_;
337 44 my $want = join "\0", sort map { "$from->[$_]\t$to->[$_]" } 0 .. $#$from;
338 44 for my $rel ($target->relationships) {
339 22 ▶ my $info = $target->relationship_info($rel) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $info = $target->relationship_info($rel) or next 22 0 22
340 22 ▶ next unless $info->{attrs} && $info->{attrs}{cascade_delete};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"attrs"} and $info->{"attrs"}{"cascade_delete"} 19 3 22
Condition66%2/3
$info->{"attrs"} and $info->{"attrs"}{"cascade_delete"}
!ll&&!rl&&r
0193
MC/DC50%1/2
$info->{"attrs"} and $info->{"attrs"}{"cascade_delete"}
$info->{"attrs"} $info->{"attrs"}{"cascade_delete"}
Truth table66%2/3
A and B
A= $info->{"attrs"}B= $info->{"attrs"}{"cascade_delete"}
ABresult
0X0
100
111
341 3 ▶ next unless ref $info->{cond} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $info->{"cond"} eq "HASH" 0 3 3
342 3 ▶ my $other = eval { $target->related_source($rel) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $other = eval { do { $target->related_source($rel) } } or next 2 1 3
343 2 my $oname = $other->name;
344 2 ▶ $oname = $$oname if ref $oname eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $oname eq "SCALAR" 0 2 2
345 2 ▶ next unless !ref $oname && $oname eq $table;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $oname and $oname eq $table 1 1 2
Condition66%2/3
not ref $oname and $oname eq $table
!ll&&!rl&&r
011
MC/DC50%1/2
not ref $oname and $oname eq $table
not ref $oname $oname eq $table
Truth table66%2/3
not(A) and B
A= not ref $onameB= $oname eq $table
ABresult
0X0
100
111
346 1 my @pairs;
347 1 for my $k (keys %{ $info->{cond} }) {
348 1 ▶ my ($f) = $k =~ /\Aforeign\.(\w+)\z/ or next;
Branch: 1 branch
BranchTrueFalseTotal
my($f) = $k =~ /\Aforeign\.(\w+)\z/ or next 1 0 1
349 1 ▶ my ($s) = ($info->{cond}{$k} // '') =~ /\Aself\.(\w+)\z/ or next;
Branch: 1 branch
BranchTrueFalseTotal
my($s) = ($info->{"cond"}{$k} // "") =~ /\Aself\.(\w+)\z/ or next 1 0 1
Condition50%1/2
$info->{"cond"}{$k} // ""
l!l
10
MC/DC0%0/1
$info->{"cond"}{$k} // ""
$info->{"cond"}{$k}
Truth table50%1/2
A
A= $info->{"cond"}{$k}
Aresult
11
00
350 1 push @pairs, "$f\t$s"; # our column, the target's column
351 }
352 1 ▶ return 1 if (join "\0", sort @pairs) eq $want;
Branch: 1 branch
BranchTrueFalseTotal
if join("\0", sort(@pairs)) eq $want 1 0 1
353 }
354 43 return 0;
355 }
356  
357 4 ▶sub _source_table {
Subroutine _source_table: called CC=2 SCAR=7.1
358 4 my ($source) = @_;
359 4 my $name = $source->name;
360 4 ▶ return ref $name eq 'SCALAR' ? $$name : $name;
Branch: 1 branch
BranchTrueFalseTotal
ref $name eq 'SCALAR' ? : 0 4 4
361 }
362  
363 52 ▶sub _fk_on_delete {
Subroutine _fk_on_delete: called CC=5 SCAR=16.1
364 52 my ($source, $rel, $action) = @_;
365 52 ▶ return '' unless defined $action && length $action;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action and length $action 45 7 52
Condition100%3/3
defined $action and length $action
!ll&&!rl&&r
4417
MC/DC100%2/2
defined $action and length $action
defined $action length $action
Truth table100%3/3
A and B
A= defined $actionB= length $action
ABresult
0X0
100
111
366 7 ▶ return ' ON DELETE ' . uc $action if $action =~ /\A(?:cascade|set null)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $action =~ /\A(?:cascade|set null)\z/i 5 2 7
367 2 ▶ return '' if $action =~ /\A(?:no action|restrict)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $action =~ /\A(?:no action|restrict)\z/i 0 2 2
368 2 $source->throw_exception(sprintf
369 "%s: relationship '%s': on_delete '%s' has no Oracle counterpart (ON DELETE takes CASCADE and SET NULL); "
370 . "refused, not left out silently", _source_table($source), $rel, $action);
371 }
372  
373 50 ▶sub _fk_on_update {
Subroutine _fk_on_update: called CC=4 SCAR=13.9
374 50 my ($source, $rel, $action) = @_;
375 50 ▶ return '' unless defined $action && length $action;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action and length $action 47 3 50
Condition66%2/3
defined $action and length $action
!ll&&!rl&&r
4703
MC/DC50%1/2
defined $action and length $action
defined $action length $action
Truth table66%2/3
A and B
A= defined $actionB= length $action
ABresult
0X0
100
111
376 3 ▶ return '' if $action =~ /\A(?:no action|restrict)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $action =~ /\A(?:no action|restrict)\z/i 1 2 3
377 2 $source->throw_exception(sprintf
378 "%s: relationship '%s': on_update '%s' has no Oracle counterpart (Oracle has no ON UPDATE); "
379 . "refused, not left out silently", _source_table($source), $rel, $action);
380 }
381  
382 # The FK constraints of one source, from its relationships marked
383 # is_foreign_key_constraint (belongs_to sets it unless told otherwise), the
384 # signal the other drivers' DDL reads too. A relationship is left out when it
385 # cannot be one constraint:
386 #
387 # - its condition is not plain 'foreign.x' => 'self.y' pairs (a coderef);
388 # - its target table is not part of this deploy (%$deployed): not in the
389 # schema, a view, or an inline subquery source;
390 # - its target columns are neither the target's primary key nor one of its
391 # unique constraints (Oracle refuses a reference without a key behind it,
392 # ORA-02270, and a belongs_to may legitimately join on any column).
393 #
394 # Two relationships over the same columns to the same target are one
395 # constraint. Pairs are ordered by the target's key order, so a composite
396 # reference matches its key. The name is fk_<table>_<columns>_<target>,
397 # shortened to the 30 characters of Identifier::shorten like every generated
398 # name. ON DELETE and DEFERRABLE follow the relationship (see POD).
399 137 ▶sub _table_foreign_keys {
Subroutine _table_foreign_keys: called CC=33 SCAR=41.1
400 137 my ($schema, $source, $deployed) = @_;
401 137 ▶ return unless $source->can('relationships');
Branch: 1 branch
BranchTrueFalseTotal
unless $source->can("relationships") 4 133 137
402 133 my $table_name = $source->name;
403 133 ▶ $table_name = $$table_name if ref $table_name eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name eq "SCALAR" 2 131 133
404 133 my (@fks, %named);
405  
406 133 for my $rel (sort $source->relationships) {
407 55 my $info = $source->relationship_info($rel);
408 55 ▶ next unless $info && $info->{attrs} && $info->{attrs}{is_foreign_key_constraint};
Branch: 1 branch
BranchTrueFalseTotal
unless $info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"} 3 52 55
Condition50%3/6
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
!ll&&!rl&&r
0352
$info and $info->{"attrs"}
!ll&&!rl&&r
0055
MC/DC33%1/3
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
$info $info->{"attrs"} $info->{"attrs"}{"is_foreign_key_constraint"}
Truth table50%2/4
A and B and C
A= $infoB= $info->{"attrs"}C= $info->{"attrs"}{"is_foreign_key_constraint"}
ABCresult
0XX0
10X0
1100
1111
409 52 my $cond = $info->{cond};
410 52 ▶ next unless ref $cond eq 'HASH' && %$cond;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cond eq "HASH" and %$cond 0 52 52
Condition33%1/3
ref $cond eq "HASH" and %$cond
!ll&&!rl&&r
0052
MC/DC0%0/2
ref $cond eq "HASH" and %$cond
ref $cond eq "HASH" %$cond
Truth table33%1/3
A and B
A= ref $cond eq "HASH"B= %$cond
ABresult
0X0
100
111
411  
412 52 my @pairs;
413 52 for my $key (keys %$cond) {
414 52 my $val = $cond->{$key};
415 52 ▶ next if ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 0 52 52
416 52 ▶ my ($to) = $key =~ /\Aforeign\.(\w+)\z/ or next;
Branch: 1 branch
BranchTrueFalseTotal
my($to) = $key =~ /\Aforeign\.(\w+)\z/ or next 52 0 52
417 52 ▶ my ($from) = $val =~ /\Aself\.(\w+)\z/ or next;
Branch: 1 branch
BranchTrueFalseTotal
my($from) = $val =~ /\Aself\.(\w+)\z/ or next 52 0 52
418 52 push @pairs, [ $from, $to ];
419 }
420 52 ▶ next unless @pairs == keys %$cond;
Branch: 1 branch
BranchTrueFalseTotal
unless @pairs == keys %$cond 0 52 52
421  
422 52 my $fs = eval { $schema->source($info->{source}) }
423 0 // eval { $schema->source($info->{class}) }
424 0 ▶ // eval { $schema->source(($info->{source} // '') =~ s/.*:://r) }
Branch: 1 branch
BranchTrueFalseTotal
my $fs = (eval { do { $schema->source($info->{"source"}) } } // eval { do { $schema->source($info->{"class"}) } }) // eval { do { $schema->source(($info->{"source"} // "") =~ s/.*:://r) } } or next 52 0 52
Condition25%2/8
(eval { do { $schema->source($info->{"source"}) } } // eval { do { $schema->source($info->{"class"}) } }) // eval { do { $schema->source(($info->{"source"} // "") =~ s/.*:://r) } }
l!l&&r!l&&!r
5200
eval { do { $schema->source($info->{"source"}) } } // eval { do { $schema->source($info->{"class"}) } }
l!l&&r!l&&!r
5200
$info->{"source"} // ""
l!l
00
MC/DC0%0/3
(eval { do { $schema->source($info->{"source"}) } } // eval { do { $schema->source($info->{"class"}) } }) // eval { do { $schema->source(($info->{"source"} // "") =~ s/.*:://r) } }
eval { do { $schema->source($info->{"source"}) } } eval { do { $schema->source($info->{"class"}) } } eval { do { $schema->source(($info->{"source"} // "") =~ s/.*:://r) } }
MC/DC0%0/1
$info->{"source"} // ""
$info->{"source"}
Truth table25%1/4
A // B // C
A= eval { do { $schema->source($info->{"source"}) } }B= eval { do { $schema->source($info->{"class"}) } }C= eval { do { $schema->source(($info->{"source"} // "") =~ s/.*:://r) } }
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/2
A
A= $info->{"source"}
Aresult
11
00
425 or next;
426 52 ▶ next if $fs->isa('DBIO::ResultSource::View');
Branch: 1 branch
BranchTrueFalseTotal
if $fs->isa("DBIO::ResultSource::View") 0 52 52
427 52 my $to_table = $fs->name;
428 52 my $to_literal = ref $to_table eq 'SCALAR';
429 52 ▶ $to_table = $$to_table if $to_literal;
Branch: 1 branch
BranchTrueFalseTotal
if $to_literal 0 52 52
430 52 ▶ next if ref $to_table || !$deployed->{$to_table};
Branch: 1 branch
BranchTrueFalseTotal
if ref $to_table or not $deployed->{$to_table} 0 52 52
Condition33%1/3
ref $to_table or not $deployed->{$to_table}
l!l&&r!l&&!r
0052
MC/DC0%0/2
ref $to_table or not $deployed->{$to_table}
ref $to_table not $deployed->{$to_table}
Truth table33%1/3
A or not(B)
A= ref $to_tableB= not $deployed->{$to_table}
ABresult
1X1
011
000
431  
432 52 my @key = $fs->primary_columns;
433 52 my %pos;
434 52 @pos{@key} = (0 .. $#key);
435 0 ▶ @pairs = sort { ($pos{ $a->[1] } // 1e9) <=> ($pos{ $b->[1] } // 1e9)
Condition0%0/7
($pos{$DBIO::Oracle::DDL::a->[1]} // 1000000000.0) <=> ($pos{$DBIO::Oracle::DDL::b->[1]} // 1000000000.0) || $DBIO::Oracle::DDL::a->[1] cmp $DBIO::Oracle::DDL::b->[1]
l!l&&r!l&&!r
000
$pos{$DBIO::Oracle::DDL::a->[1]} // "1000000000"
l!l
00
$pos{$DBIO::Oracle::DDL::b->[1]} // "1000000000"
l!l
00
MC/DC0%0/2
($pos{$DBIO::Oracle::DDL::a->[1]} // 1000000000.0) <=> ($pos{$DBIO::Oracle::DDL::b->[1]} // 1000000000.0) || $DBIO::Oracle::DDL::a->[1] cmp $DBIO::Oracle::DDL::b->[1]
$pos{$DBIO::Oracle::DDL::a->[1]} $DBIO::Oracle::DDL::a->[1] cmp $DBIO::Oracle::DDL::b->[1]
MC/DC0%0/1
$pos{$DBIO::Oracle::DDL::b->[1]} // "1000000000"
$pos{$DBIO::Oracle::DDL::b->[1]}
Truth table0%0/3
A || B
A= $pos{$DBIO::Oracle::DDL::a->[1]}B= $DBIO::Oracle::DDL::a->[1] cmp $DBIO::Oracle::DDL::b->[1]
ABresult
1X1
011
000
Truth table0%0/2
A
A= $pos{$DBIO::Oracle::DDL::b->[1]}
Aresult
11
00
436 || $a->[1] cmp $b->[1] } @pairs;
437  
438 52 my $to_set = join "\0", sort map { $_->[1] } @pairs;
439 52 my %uniques = $fs->unique_constraints;
440 52 ▶ next unless grep { join("\0", sort @$_) eq $to_set } values %uniques;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {join("\0", sort(@$_)) eq $to_set;} values %uniques 0 52 52
441  
442 52 my @from = map { $_->[0] } @pairs;
443 52 my @to = map { $_->[1] } @pairs;
444 52 my $key = join "\0", @from, '->', $to_table, @to;
445 # The actions the belongs_to declares (karr k42); on_delete that it does
446 # not declare at all is read off the has_many on the other side.
447 52 my $attrs = $info->{attrs};
448 my $on_delete = exists $attrs->{on_delete}
449 ? $attrs->{on_delete}
450 52 ▶ : _cascading_has_many($fs, $table_name, \@from, \@to) ? 'CASCADE' : undef;
Branch: 2 branches
BranchTrueFalseTotal
exists $attrs->{'on_delete'} ? : 8 44 52
_cascading_has_many($fs, $table_name, \@from, \@to) ? : 1 43 44
451 my $actions = _fk_on_delete($source, $rel, $on_delete)
452 . _fk_on_update($source, $rel, $attrs->{on_update})
453 52 ▶ . ($attrs->{is_deferrable} ? ' DEFERRABLE' : '');
Branch: 1 branch
BranchTrueFalseTotal
$attrs->{'is_deferrable'} ? : 1 47 48
454  
455 # The name is made of the table, the columns and the target, so that two
456 # keys over the same columns to different tables do not meet in one name
457 # (ORA-02264). Two relationships to the same target with other actions
458 # would still need one name twice: refused, not dropped.
459 48 ▶ my $cname = DBIO::Oracle::Identifier::shorten(join '_', 'fk',
Branch: 1 branch
BranchTrueFalseTotal
$to_literal ? : 0 48 48
460 (DBIO::Oracle::Identifier::split_table($table_name))[1], @from,
461 $to_literal ? $to_table : (DBIO::Oracle::Identifier::split_table($to_table))[1]);
462 48 ▶ if (defined(my $earlier = $named{$cname})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $earlier = $named{$cname})) 3 45 48
463 3 ▶ next if $earlier->[0] eq $key . $actions;
Branch: 1 branch
BranchTrueFalseTotal
if $earlier->[0] eq $key . $actions 2 1 3
464 1 $source->throw_exception(sprintf
465 "%s: refused, relationships '%s' and '%s' declare the foreign key on (%s) to %s twice with "
466 . "other actions; Oracle holds one constraint of a name -- declare one of them",
467 $table_name, $earlier->[1], $rel, join(', ', @from), $to_table);
468 }
469 45 $named{$cname} = [ $key . $actions, $rel ];
470  
471 45 ▶ push @fks, {
Branch: 1 branch
BranchTrueFalseTotal
$to_literal ? : 0 45 45
472 constraint_name => $cname,
473 from_columns => \@from,
474 to_qualified => ($to_literal ? $to_table : _qt($to_table)),
475 to_columns => \@to,
476 actions => $actions,
477 };
478 }
479 128 return @fks;
480 }
481  
482  
483 26 1;
484  
485 __END__
« Oracle/Storage/AutoIncrement.pm ↑ Summary Oracle/Type.pm »