blib/lib/SQL/Translator/Producer/Oracle.pm

filter: Statementstmt 90.1% 329 / 365 Branchbran 68.0% 147 / 216 Conditioncond 63.1% 65 / 103 MC/DCmcdc 37.7% 23 / 61 Subroutinesub 100% 22 / 22 Podpod 56.2% 9 / 16 totaltotal 75.9% 595 / 783 CC 160 SCAR 38.5
CC
160
Cov
79%
create_table
96.3
create_field
57.8
create_constraint
51.4
CRAP 47.0
« blib/lib/SQL/Translator/Schema.pm ↑ Summary script/sqlt-diff-old »
1 package SQL::Translator::Producer::Oracle;
2  
3 =head1 NAME
4  
5 SQL::Translator::Producer::Oracle - Oracle SQL producer
6  
7 =head1 SYNOPSIS
8  
9 use SQL::Translator;
10  
11 my $t = SQL::Translator->new( parser => '...', producer => 'Oracle' );
12 print $translator->translate( $file );
13  
14 =head1 DESCRIPTION
15  
16 Creates an SQL DDL suitable for Oracle.
17  
18 =head1 producer_args
19  
20 =over
21  
22 =item delay_constraints
23  
24 This option remove the primary key and other key constraints from the
25 CREATE TABLE statement and adds ALTER TABLEs at the end with it.
26  
27 =item quote_field_names
28  
29 Controls whether quotes are being used around column names in generated DDL.
30  
31 =item quote_table_names
32  
33 Controls whether quotes are being used around table, sequence and trigger names in
34 generated DDL.
35  
36 =back
37  
38 =head1 NOTES
39  
40 =head2 Autoincremental primary keys
41  
42 This producer uses sequences and triggers to autoincrement primary key
43 columns, if necessary. SQLPlus and DBI expect a slightly different syntax
44 of CREATE TRIGGER statement. You might have noticed that this
45 producer returns a scalar containing all statements concatenated by
46 newlines or an array of single statements depending on the context
47 (scalar, array) it has been called in.
48  
49 SQLPlus expects following trigger syntax:
50  
51 CREATE OR REPLACE TRIGGER ai_person_id
52 BEFORE INSERT ON person
53 FOR EACH ROW WHEN (
54 new.id IS NULL OR new.id = 0
55 )
56 BEGIN
57 SELECT sq_person_id.nextval
58 INTO :new.id
59 FROM dual;
60 END;
61 /
62  
63 Whereas if you want to create the same trigger using L<DBI/do>, you need
64 to omit the last slash:
65  
66 my $dbh = DBI->connect('dbi:Oracle:mysid', 'scott', 'tiger');
67 $dbh->do("
68 CREATE OR REPLACE TRIGGER ai_person_id
69 BEFORE INSERT ON person
70 FOR EACH ROW WHEN (
71 new.id IS NULL OR new.id = 0
72 )
73 BEGIN
74 SELECT sq_person_id.nextval
75 INTO :new.id
76 FROM dual;
77 END;
78 ");
79  
80 If you call this producer in array context, we expect you want to process
81 the returned array of statements using L<DBI> like
82 L<DBIx::Class::Schema/deploy> does.
83  
84 To get this working we removed the slash in those statements in version
85 0.09002 of L<SQL::Translator> when called in array context. In scalar
86 context the slash will be still there to ensure compatibility with SQLPlus.
87  
88 =cut
89  
90 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 12 our ($DEBUG, $WARN);
93 12 our $VERSION = '1.66';
94 12 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $SQL::Translator::Producer::Oracle::DEBUG 12 0 12
95  
96 12 ▶use base 'SQL::Translator::Producer';
Subroutine BEGIN: called CC=1 SCAR=0.0
97 12 ▶use SQL::Translator::Schema::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 12 ▶use SQL::Translator::Utils qw(debug header_comment);
Subroutine BEGIN: called CC=1 SCAR=0.0
99 12 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 12 my %translate = (
102 #
103 # MySQL types
104 #
105 bigint => 'number',
106 double => 'float',
107 decimal => 'number',
108 float => 'float',
109 int => 'number',
110 integer => 'number',
111 mediumint => 'number',
112 smallint => 'number',
113 tinyint => 'number',
114 char => 'char',
115 varchar => 'varchar2',
116 tinyblob => 'blob',
117 blob => 'blob',
118 mediumblob => 'blob',
119 longblob => 'blob',
120 tinytext => 'varchar2',
121 text => 'clob',
122 longtext => 'clob',
123 mediumtext => 'clob',
124 enum => 'varchar2',
125 set => 'varchar2',
126 date => 'date',
127 datetime => 'date',
128 time => 'date',
129 timestamp => 'date',
130 year => 'date',
131  
132 #
133 # PostgreSQL types
134 #
135 numeric => 'number',
136 'double precision' => 'number',
137 serial => 'number',
138 bigserial => 'number',
139 money => 'number',
140 character => 'char',
141 'character varying' => 'varchar2',
142 bytea => 'BLOB',
143 interval => 'number',
144 boolean => 'number',
145 point => 'number',
146 line => 'number',
147 lseg => 'number',
148 box => 'number',
149 path => 'number',
150 polygon => 'number',
151 circle => 'number',
152 cidr => 'number',
153 inet => 'varchar2',
154 macaddr => 'varchar2',
155 bit => 'number',
156 'bit varying' => 'number',
157  
158 #
159 # Oracle types
160 #
161 number => 'number',
162 varchar2 => 'varchar2',
163 long => 'clob',
164 );
165  
166 #
167 # Oracle 8/9 max size of data types from:
168 # http://www.ss64.com/orasyntax/datatypes.html
169 #
170 12 my %max_size = (
171 char => 2000,
172 float => 126,
173 nchar => 2000,
174 nvarchar2 => 4000,
175 number => [ 38, 127 ],
176 raw => 2000,
177 varchar => 4000, # only synonym for varchar2
178 varchar2 => 4000,
179 );
180  
181 12 my $max_id_length = 30;
182 12 my %used_identifiers = ();
183 12 my %global_names;
184 my %truncated;
185  
186 # Quote used to escape table, field, sequence and trigger names
187 12 my $quote_char = '"';
188  
189 11 ▶sub produce {
Subroutine produce: called CC=13 SCAR=25.7
Pod documented
190 11 my $translator = shift;
191 11 $DEBUG = $translator->debug;
192 11 ▶ $WARN = $translator->show_warnings || 0;
Condition50%1/2
$translator->show_warnings || 0
l!l
011
MC/DC0%0/1
$translator->show_warnings || 0
$translator->show_warnings
Truth table50%1/2
A
A= $translator->show_warnings
Aresult
11
00
193 11 my $no_comments = $translator->no_comments;
194 11 my $add_drop_table = $translator->add_drop_table;
195 11 my $schema = $translator->schema;
196 11 ▶ my $oracle_version = $translator->producer_args->{oracle_version} || 0;
Condition50%1/2
$translator->producer_args->{'oracle_version'} || 0
l!l
011
MC/DC0%0/1
$translator->producer_args->{'oracle_version'} || 0
$translator->producer_args->{'oracle_version'}
Truth table50%1/2
A
A= $translator->producer_args->{'oracle_version'}
Aresult
11
00
197 11 my $delay_constraints = $translator->producer_args->{delay_constraints};
198 11 my ($output, $create, @table_defs, @fk_defs, @trigger_defs, @index_defs, @constraint_defs);
199  
200 11 debug("ORA: Beginning production");
201 11 ▶ $create .= header_comment unless ($no_comments);
Branch: 1 branch
BranchTrueFalseTotal
unless $no_comments 4 7 11
202 11 ▶ my $qt = 1 if $translator->quote_table_names;
Branch: 1 branch
BranchTrueFalseTotal
if $translator->quote_table_names 6 5 11
203 11 ▶ my $qf = 1 if $translator->quote_field_names;
Branch: 1 branch
BranchTrueFalseTotal
if $translator->quote_field_names 6 5 11
204  
205 11 ▶ if ($translator->parser_type =~ /mysql/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($translator->parser_type =~ /mysql/i) 1 10 11
206 1 ▶ $create
Branch: 1 branch
BranchTrueFalseTotal
unless $no_comments 1 0 1
207 .= "-- We assume that default NLS_DATE_FORMAT has been changed\n"
208 . "-- but we set it here anyway to be self-consistent.\n"
209 unless $no_comments;
210  
211 1 $create .= "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';\n\n";
212 }
213  
214 11 for my $table ($schema->get_tables) {
215 21 debug("ORA: Producing for table " . $table->name);
216 21 my ($table_def, $fk_def, $trigger_def, $index_def, $constraint_def) = create_table(
217 $table,
218 {
219 add_drop_table => $add_drop_table,
220 show_warnings => $WARN,
221 no_comments => $no_comments,
222 delay_constraints => $delay_constraints,
223 quote_table_names => $qt,
224 quote_field_names => $qf,
225 }
226 );
227 21 push @table_defs, @$table_def;
228 21 push @fk_defs, @$fk_def;
229 21 push @trigger_defs, @$trigger_def;
230 21 push @index_defs, @$index_def;
231 21 push @constraint_defs, @$constraint_def;
232 }
233  
234 11 my (@view_defs);
235 11 foreach my $view ($schema->get_views) {
236 5 my ($view_def) = create_view(
237 $view,
238 {
239 add_drop_view => $add_drop_table,
240 quote_table_names => $qt,
241 }
242 );
243 5 push @view_defs, @$view_def;
244 }
245  
246 11 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 3 8 11
247 3 ▶ return defined $create ? $create : (), @table_defs, @view_defs,
Branch: 1 branch
BranchTrueFalseTotal
defined $create ? : 0 3 3
248 @fk_defs, @trigger_defs, @index_defs, @constraint_defs;
249 } else {
250 8 $create .= join(";\n\n", @table_defs, @view_defs, @fk_defs, @index_defs, @constraint_defs);
251 8 $create .= ";\n\n";
252  
253 # If wantarray is not set we have to add "/" in this statement
254 # DBI->do() needs them omitted
255 # triggers may NOT end with a semicolon but a "/" instead
256 8 $create .= "$_/\n\n" for @trigger_defs;
257 8 return $create;
258 }
259 }
260  
261 22 ▶sub create_table {
Subroutine create_table: called CC=38 SCAR=45.7
Pod documented
262 22 my ($table, $options) = @_;
263 22 my $qt = $options->{quote_table_names};
264 22 my $qf = $options->{quote_field_names};
265 22 my $table_name = $table->name;
266 22 my $table_name_q = quote($table_name, $qt);
267  
268 22 my $item = '';
269 22 my $drop;
270 22 my (@create, @field_defs, @constraint_defs, @fk_defs, @trigger_defs);
271  
272 push @create, "--\n-- Table: $table_name\n--"
273 22 ▶ unless $options->{no_comments};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"no_comments"} 5 17 22
274 push @create, qq[DROP TABLE $table_name_q CASCADE CONSTRAINTS]
275 22 ▶ if $options->{add_drop_table};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"add_drop_table"} 16 6 22
276  
277 22 my (%field_name_scope, @field_comments);
278 22 for my $field ($table->get_fields) {
279 97 debug("ORA: Creating field " . $field->name . "(" . $field->data_type . ")");
280 97 my ($field_create, $field_defs, $trigger_defs, $field_comments)
281 = create_field($field, $options, \%field_name_scope);
282 97 ▶ push @create, @$field_create if ref $field_create;
Branch: 1 branch
BranchTrueFalseTotal
if ref $field_create 97 0 97
283 97 ▶ push @field_defs, @$field_defs if ref $field_defs;
Branch: 1 branch
BranchTrueFalseTotal
if ref $field_defs 97 0 97
284 97 ▶ push @trigger_defs, @$trigger_defs if ref $trigger_defs;
Branch: 1 branch
BranchTrueFalseTotal
if ref $trigger_defs 97 0 97
285 97 ▶ push @field_comments, @$field_comments if ref $field_comments;
Branch: 1 branch
BranchTrueFalseTotal
if ref $field_comments 97 0 97
286 }
287  
288 #
289 # Table options
290 #
291 22 my @table_options;
292 22 for my $opt ($table->options) {
293 7 ▶ if (ref $opt eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt eq "HASH") 5 2 7
294 5 my ($key, $value) = each %$opt;
295 5 ▶ if (ref $value eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 5 5
elsif (not defined $value) { } 0 5 5
296 push @table_options,
297 "$key\n(\n"
298 . join(
299 0 "\n", map {" $_->[0]\t$_->[1]"}
300 0 map { [ each %$_ ] } @$value
301 ) . "\n)";
302 } elsif (!defined $value) {
303 0 push @table_options, $key;
304 } else {
305 5 push @table_options, "$key $value";
306 }
307 }
308 }
309  
310 #
311 # Table constraints
312 #
313 22 for my $c ($table->get_constraints) {
314 36 my $constr = create_constraint($c, $options);
315 36 ▶ if ($constr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constr) 36 0 36
316 36 ▶ if ($c->type eq FOREIGN_KEY) { # FK defs always come later as alters
Branch: 1 branch
BranchTrueFalseTotal
if ($c->type eq 'FOREIGN KEY') { } 7 29 36
317 7 push @fk_defs, sprintf("ALTER TABLE %s ADD %s", $table_name_q, $constr);
318 } else {
319 29 push @constraint_defs, $constr;
320 }
321 }
322 }
323  
324 #
325 # Index Declarations
326 #
327 22 my @index_defs = ();
328 22 for my $index ($table->get_indices) {
329 13 ▶ my $index_name = $index->name || '';
Condition50%1/2
$index->name || ''
l!l
130
MC/DC0%0/1
$index->name || ''
$index->name
Truth table50%1/2
A
A= $index->name
Aresult
11
00
330 13 ▶ my $index_type = $index->type || NORMAL;
Condition50%1/2
$index->type || 'NORMAL'
l!l
130
MC/DC0%0/1
$index->type || 'NORMAL'
$index->type
Truth table50%1/2
A
A= $index->type
Aresult
11
00
331 13 my @fields = map { quote($_, $qf) } $index->fields;
332 13 ▶ next unless @fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields 0 13 13
333 13 debug("ORA: Creating $index_type index on fields (" . join(', ', @fields) . ") named $index_name");
334 13 my @index_options;
335 13 for my $opt ($index->options) {
336 3 ▶ if (ref $opt eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt eq "HASH") 3 0 3
337 3 my ($key, $value) = each %$opt;
338 3 ▶ if (ref $value eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 3 3
elsif (not defined $value) { } 0 3 3
339 push @table_options,
340 "$key\n(\n"
341 . join(
342 0 "\n", map {" $_->[0]\t$_->[1]"}
343 0 map { [ each %$_ ] } @$value
344 ) . "\n)";
345 } elsif (!defined $value) {
346 0 push @index_options, $key;
347 } else {
348 3 push @index_options, "$key $value";
349 }
350 }
351 }
352 13 ▶ my $index_options = @index_options ? "\n" . join("\n", @index_options) : '';
Branch: 1 branch
BranchTrueFalseTotal
@index_options ? : 3 10 13
353  
354 13 ▶ if ($index_type eq PRIMARY_KEY) {
Branch: 2 branches
BranchTrueFalseTotal
if ($index_type eq 'PRIMARY KEY') { } 0 13 13
elsif ($index_type eq 'NORMAL' or $index_type eq 'UNIQUE') { } 13 0 13
355 0 ▶ $index_name
Branch: 1 branch
BranchTrueFalseTotal
$index_name ? : 0 0 0
356 = $index_name
357 ? mk_name($index_name)
358 : mk_name($table_name, 'pk');
359 0 $index_name = quote($index_name, $qf);
360 0 push @field_defs, 'CONSTRAINT ' . $index_name . ' PRIMARY KEY ' . '(' . join(', ', @fields) . ')';
361 ▶ } elsif ($index_type eq NORMAL or $index_type eq UNIQUE) {
Condition66%2/3
$index_type eq "NORMAL" or $index_type eq "UNIQUE"
l!l&&r!l&&!r
1210
MC/DC0%0/2
$index_type eq "NORMAL" or $index_type eq "UNIQUE"
$index_type eq "NORMAL" $index_type eq "UNIQUE"
Truth table66%2/3
A or B
A= $index_type eq "NORMAL"B= $index_type eq "UNIQUE"
ABresult
1X1
011
000
362 13 push @index_defs, create_index($index, $options, $index_options);
363 } else {
364 0 ▶ warn "Unknown index type ($index_type) on table $table_name.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $SQL::Translator::Producer::Oracle::WARN 0 0 0
365 if $WARN;
366 }
367 }
368  
369 22 ▶ if (my @table_comments = $table->comments) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@table_comments) = $table->comments) 0 22 22
370 0 for my $comment (@table_comments) {
371 0 ▶ next unless $comment;
Branch: 1 branch
BranchTrueFalseTotal
unless $comment 0 0 0
372 0 $comment = __PACKAGE__->_quote_string($comment);
373 push @field_comments, "COMMENT ON TABLE $table_name_q is\n $comment"
374 0 ▶ unless $options->{no_comments};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"no_comments"} 0 0 0
375 }
376 }
377  
378 22 ▶ my $table_options = @table_options ? "\n" . join("\n", @table_options) : '';
Branch: 1 branch
BranchTrueFalseTotal
@table_options ? : 5 17 22
379 push @create,
380 "CREATE TABLE $table_name_q (\n"
381 22 ▶ . join(",\n", map {" $_"} @field_defs, ($options->{delay_constraints} ? () : @constraint_defs))
Branch: 1 branch
BranchTrueFalseTotal
$options->{'delay_constraints'} ? : 2 20 22
382 . "\n)$table_options";
383  
384 22 @constraint_defs = map {"ALTER TABLE $table_name_q ADD $_"} @constraint_defs;
385  
386 22 ▶ if ($WARN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SQL::Translator::Producer::Oracle::WARN) 0 22 22
387 0 ▶ if (%truncated) {
Branch: 1 branch
BranchTrueFalseTotal
if (%truncated) 0 0 0
388 0 warn "Truncated " . keys(%truncated) . " names:\n";
389 0 warn "\t" . join("\n\t", sort keys %truncated) . "\n";
390 }
391 }
392  
393 22 ▶ return \@create, \@fk_defs, \@trigger_defs, \@index_defs, ($options->{delay_constraints} ? \@constraint_defs : []);
Branch: 1 branch
BranchTrueFalseTotal
$options->{'delay_constraints'} ? : 2 20 22
394 }
395  
396 4 ▶sub alter_field {
Subroutine alter_field: called CC=6 SCAR=18.0
Pod documented
397 4 my ($from_field, $to_field, $options) = @_;
398  
399 4 my $qt = $options->{quote_table_names};
400 4 my ($field_create, $field_defs, $trigger_defs, $field_comments)
401 = create_field($to_field, $options, {});
402  
403 # Fix ORA-01442
404 4 ▶ if (!$from_field->is_nullable && $to_field->is_nullable) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $from_field->is_nullable and $to_field->is_nullable) { } 1 3 4
elsif (not $from_field->is_nullable || $to_field->is_nullable) { } 1 2 3
Condition100%3/3
not $from_field->is_nullable and $to_field->is_nullable
!ll&&!rl&&r
211
MC/DC100%2/2
not $from_field->is_nullable and $to_field->is_nullable
not $from_field->is_nullable $to_field->is_nullable
Truth table100%3/3
not(A) and B
A= not $from_field->is_nullableB= $to_field->is_nullable
ABresult
0X0
100
111
405 1 ▶ if ($from_field->data_type =~ /text/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_field->data_type =~ /text/) { } 0 1 1
406 0 die 'Cannot alter CLOB field in this way';
407 } else {
408 1 @$field_defs = map { $_ .= ' NULL' } @$field_defs;
409 }
410 ▶ } elsif (!$from_field->is_nullable && !$to_field->is_nullable) {
Condition66%2/3
$from_field->is_nullable || $to_field->is_nullable
l!l&&r!l&&!r
201
MC/DC50%1/2
$from_field->is_nullable || $to_field->is_nullable
$from_field->is_nullable $to_field->is_nullable
Truth table66%2/3
A || B
A= $from_field->is_nullableB= $to_field->is_nullable
ABresult
1X1
011
000
411 1 @$field_defs = map { s/ NOT NULL//; $_ } @$field_defs;
412 }
413  
414 4 my $table_name = quote($to_field->table->name, $qt);
415  
416 4 return 'ALTER TABLE ' . $table_name . ' MODIFY ( ' . join('', @$field_defs) . ' )';
417 }
418  
419 2 ▶sub drop_field {
Subroutine drop_field: called CC=1 SCAR=0.0
Pod documented
420 2 my ($old_field, $options) = @_;
421 2 my $qi = $options->{quote_identifiers};
422 2 my $table_name = quote($old_field->table->name, $qi);
423  
424 2 my $out = sprintf('ALTER TABLE %s DROP COLUMN %s', $table_name, quote($old_field->name, $qi));
425  
426 2 return $out;
427 }
428  
429 3 ▶sub add_field {
Subroutine add_field: called CC=1 SCAR=0.0
Pod documented
430 3 my ($new_field, $options) = @_;
431  
432 3 my $qt = $options->{quote_table_names};
433 3 my ($field_create, $field_defs, $trigger_defs, $field_comments)
434 = create_field($new_field, $options, {});
435  
436 3 my $table_name = quote($new_field->table->name, $qt);
437  
438 3 my $out = sprintf('ALTER TABLE %s ADD ( %s )', $table_name, join('', @$field_defs));
439 3 return $out;
440 }
441  
442 104 ▶sub create_field {
Subroutine create_field: called CC=43 SCAR=40.6
Pod documented
443 104 my ($field, $options, $field_name_scope) = @_;
444 104 my $qf = $options->{quote_field_names};
445 104 my $qt = $options->{quote_table_names};
446  
447 104 my (@create, @field_defs, @trigger_defs, @field_comments);
448  
449 104 my $table_name = $field->table->name;
450 104 my $table_name_q = quote($table_name, $qt);
451  
452 #
453 # Field name
454 #
455 104 my $field_name = mk_name($field->name, '', $field_name_scope, 1);
456 104 my $field_name_q = quote($field_name, $qf);
457 104 my $field_def = quote($field_name, $qf);
458 104 $field->name($field_name);
459  
460 #
461 # Datatype
462 #
463 104 my $check;
464 104 my $data_type = lc $field->data_type;
465 104 my @size = $field->size;
466 104 my %extra = $field->extra;
467 104 ▶ my $list = $extra{'list'} || [];
Condition100%2/2
$extra{'list'} || []
l!l
1103
MC/DC100%1/1
$extra{'list'} || []
$extra{'list'}
Truth table100%2/2
A
A= $extra{'list'}
Aresult
11
00
468 104 my $commalist = join(', ', map { __PACKAGE__->_quote_string($_) } @$list);
469  
470 104 ▶ if ($data_type eq 'enum') {
Branch: 2 branches
BranchTrueFalseTotal
if ($data_type eq 'enum') { } 1 103 104
elsif ($data_type eq 'set') { } 0 103 103
471 1 $check = "CHECK ($field_name_q IN ($commalist))";
472 1 $data_type = 'varchar2';
473 } elsif ($data_type eq 'set') {
474  
475 # XXX add a CHECK constraint maybe
476 # (trickier and slower, than enum :)
477 0 $data_type = 'varchar2';
478 } else {
479 103 ▶ if (defined $translate{$data_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $translate{$data_type}) 97 6 103
480 97 ▶ if (ref $translate{$data_type} eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $translate{$data_type} eq 'ARRAY') { } 0 97 97
481 0 ($data_type, $size[0]) = @{ $translate{$data_type} };
482 } else {
483 97 $data_type = $translate{$data_type};
484 }
485 }
486 103 ▶ $data_type ||= 'varchar2';
Condition50%1/2
$data_type ||= "varchar2"
l!l
1030
MC/DC0%0/1
$data_type ||= "varchar2"
$data_type
Truth table50%1/2
A
A= $data_type
Aresult
11
00
487 }
488  
489 # ensure size is not bigger than max size oracle allows for data type
490 104 ▶ if (defined $max_size{$data_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_size{$data_type}) 87 17 104
491 87 ▶ for (my $i = 0; $i < scalar @size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @size) 95 87 182
492 my $max
493 = ref($max_size{$data_type}) eq 'ARRAY'
494 ? $max_size{$data_type}->[$i]
495 95 ▶ : $max_size{$data_type};
Branch: 1 branch
BranchTrueFalseTotal
ref $max_size{$data_type} eq 'ARRAY' ? : 46 49 95
496 95 ▶ $size[$i] = $max if $size[$i] > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $size[$i] > $max 0 95 95
497 }
498 }
499  
500 #
501 # Fixes ORA-02329: column of datatype LOB cannot be
502 # unique or a primary key
503 #
504 104 ▶ if ($data_type eq 'clob' && $field->is_primary_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type eq "clob" and $field->is_primary_key) 1 103 104
Condition100%3/3
$data_type eq "clob" and $field->is_primary_key
!ll&&!rl&&r
9671
MC/DC100%2/2
$data_type eq "clob" and $field->is_primary_key
$data_type eq "clob" $field->is_primary_key
Truth table100%3/3
A and B
A= $data_type eq "clob"B= $field->is_primary_key
ABresult
0X0
100
111
505 1 $data_type = 'varchar2';
506 1 $size[0] = 4000;
507 1 ▶ warn "CLOB cannot be a primary key, changing to VARCHAR2\n"
Branch: 1 branch
BranchTrueFalseTotal
if $SQL::Translator::Producer::Oracle::WARN 0 1 1
508 if $WARN;
509 }
510  
511 104 ▶ if ($data_type eq 'clob' && $field->is_unique) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type eq "clob" and $field->is_unique) 0 104 104
Condition66%2/3
$data_type eq "clob" and $field->is_unique
!ll&&!rl&&r
9770
MC/DC0%0/2
$data_type eq "clob" and $field->is_unique
$data_type eq "clob" $field->is_unique
Truth table66%2/3
A and B
A= $data_type eq "clob"B= $field->is_unique
ABresult
0X0
100
111
512 0 $data_type = 'varchar2';
513 0 $size[0] = 4000;
514 0 ▶ warn "CLOB cannot be a unique key, changing to VARCHAR2\n"
Branch: 1 branch
BranchTrueFalseTotal
if $SQL::Translator::Producer::Oracle::WARN 0 0 0
515 if $WARN;
516 }
517  
518 #
519 # Fixes ORA-00907: missing right parenthesis
520 #
521 104 ▶ if ($data_type =~ /(date|clob)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type =~ /(date|clob)/i) 16 88 104
522 16 undef @size;
523 }
524  
525 #
526 # Fixes ORA-00906: missing right parenthesis
527 # if size is 0 or undefined
528 #
529 104 for (qw/varchar2/) {
530 104 ▶ if ($data_type =~ /^($_)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type =~ /^($_)$/i) 46 58 104
531 46 ▶ $size[0] ||= $max_size{$_};
Condition66%2/3
$size[0] ||= $max_size{$_}
l!l&&r!l&&!r
31150
MC/DC0%0/2
$size[0] ||= $max_size{$_}
$size[0] $max_size{$_}
Truth table66%2/3
A ||= B
A= $size[0]B= $max_size{$_}
ABresult
1X1
011
000
532 }
533 }
534  
535 104 $field_def .= " $data_type";
536 104 ▶ if (defined $size[0] && $size[0] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size[0] and $size[0] > 0) 85 19 104
Condition100%3/3
defined $size[0] and $size[0] > 0
!ll&&!rl&&r
16385
MC/DC100%2/2
defined $size[0] and $size[0] > 0
defined $size[0] $size[0] > 0
Truth table100%3/3
A and B
A= defined $size[0]B= $size[0] > 0
ABresult
0X0
100
111
537 85 $field_def .= '(' . join(',', @size) . ')';
538 }
539  
540 #
541 # Default value
542 #
543 104 my $default = $field->default_value;
544 104 ▶ if (defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) 36 68 104
545 36 debug("ORA: Handling default value: $default");
546 #
547 # Wherein we try to catch a string being used as
548 # a default value for a numerical field. If "true/false,"
549 # then sub "1/0," otherwise just test the truthity of the
550 # argument and use that (naive?).
551 #
552 36 ▶ if (ref $default and defined $$default) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $default and defined $$default) { } 1 35 36
elsif (ref $default) { } 0 35 35
elsif ($data_type =~ /^number$/i and not $default =~ /^-?\d+$/ and not $default =~ /null/i) { } 1 34 35
elsif ($data_type =~ /date/ and $default eq 'current_timestamp' || $default eq 'now()') { } 0 34 34
Condition66%2/3
ref $default and defined $$default
!ll&&!rl&&r
3501
MC/DC50%1/2
ref $default and defined $$default
ref $default defined $$default
Truth table66%2/3
A and B
A= ref $defaultB= defined $$default
ABresult
0X0
100
111
553 1 $default = $$default;
554 } elsif (ref $default) {
555 0 $default = 'NULL';
556 ▶ } elsif ($data_type =~ /^number$/i
Condition83%5/6
$data_type =~ /^number$/i and not $default =~ /^-?\d+$/ and not $default =~ /null/i
!ll&&!rl&&r
3401
$data_type =~ /^number$/i and not $default =~ /^-?\d+$/
!ll&&!rl&&r
2771
MC/DC66%2/3
$data_type =~ /^number$/i and not $default =~ /^-?\d+$/ and not $default =~ /null/i
$data_type =~ /^number$/i not $default =~ /^-?\d+$/ not $default =~ /null/i
Truth table75%3/4
A and not(B) and not(C)
A= $data_type =~ /^number$/iB= not $default =~ /^-?\d+$/C= not $default =~ /null/i
ABCresult
0XX0
10X0
1100
1111
557 && $default !~ /^-?\d+$/
558 && $default !~ m/null/i) {
559 1 ▶ if ($default =~ /^true$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($default =~ /^true$/i) { } 0 1 1
elsif ($default =~ /^false$/i) { } 0 1 1
560 0 $default = "'1'";
561 } elsif ($default =~ /^false$/i) {
562 0 $default = "'0'";
563 } else {
564 1 ▶ $default = $default ? "'1'" : "'0'";
Branch: 1 branch
BranchTrueFalseTotal
$default ? : 1 0 1
565 }
566 ▶ } elsif (
Condition16%1/6
$data_type =~ /date/ and $default eq "current_timestamp" || $default eq "now()"
!ll&&!rl&&r
3400
$default eq "current_timestamp" or $default eq "now()"
l!l&&r!l&&!r
000
MC/DC0%0/3
$data_type =~ /date/ and $default eq "current_timestamp" || $default eq "now()"
$data_type =~ /date/ $default eq "current_timestamp" $default eq "now()"
Truth table25%1/4
A and B or C
A= $data_type =~ /date/B= $default eq "current_timestamp"C= $default eq "now()"
ABCresult
0XX0
1000
11X1
1011
567 $data_type =~ /date/
568 && ( $default eq 'current_timestamp'
569 || $default eq 'now()')
570 ) {
571 0 $default = 'SYSDATE';
572 } else {
573 34 ▶ $default
Branch: 1 branch
BranchTrueFalseTotal
$default =~ /null/i ? : 0 34 34
574 = $default =~ m/null/i
575 ? 'NULL'
576 : __PACKAGE__->_quote_string($default);
577 }
578  
579 36 $field_def .= " DEFAULT $default",;
580 }
581  
582 #
583 # Not null constraint
584 #
585 104 ▶ unless ($field->is_nullable) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field->is_nullable) 47 57 104
586 47 debug("ORA: Field is NOT NULL");
587 47 $field_def .= ' NOT NULL';
588 }
589  
590 104 ▶ $field_def .= " $check" if $check;
Branch: 1 branch
BranchTrueFalseTotal
if $check 1 103 104
591  
592 #
593 # Auto_increment
594 #
595 104 ▶ if ($field->is_auto_increment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->is_auto_increment) 15 89 104
596 15 debug("ORA: Handling auto increment");
597 15 my $base_name = $table_name . "_" . $field_name;
598 15 my $seq_name = quote(mk_name($base_name, 'sq'), $qt);
599 15 my $trigger_name = quote(mk_name($base_name, 'ai'), $qt);
600  
601 15 ▶ push @create, qq[DROP SEQUENCE $seq_name] if $options->{add_drop_table};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"add_drop_table"} 11 4 15
602 15 push @create, "CREATE SEQUENCE $seq_name";
603 15 my $trigger
604 = "CREATE OR REPLACE TRIGGER $trigger_name\n"
605 . "BEFORE INSERT ON $table_name_q\n"
606 . "FOR EACH ROW WHEN (\n"
607 . " new.$field_name_q IS NULL"
608 . " OR new.$field_name_q = 0\n" . ")\n"
609 . "BEGIN\n"
610 . " SELECT $seq_name.nextval\n"
611 . " INTO :new."
612 . $field_name_q . "\n"
613 . " FROM dual;\n"
614 . "END;\n";
615  
616 15 push @trigger_defs, $trigger;
617 }
618  
619 104 push @field_defs, $field_def;
620  
621 104 ▶ if (my $comment = $field->comments) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $comment = $field->comments) 5 99 104
622 5 debug("ORA: Handling comment");
623 5 $comment =~ __PACKAGE__->_quote_string($comment);
624 push @field_comments, "COMMENT ON COLUMN $table_name_q.$field_name_q is\n $comment;"
625 5 ▶ unless $options->{no_comments};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"no_comments"} 0 5 5
626 }
627  
628 104 return \@create, \@field_defs, \@trigger_defs, \@field_comments;
629  
630 }
631  
632 2 ▶sub drop_table {
Subroutine drop_table: called CC=2 SCAR=6.9
Pod undocumented
633 2 my ($table, $options) = @_;
634  
635 2 my $qi = $options->{quote_identifiers};
636 2 my @foreign_key_constraints = grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints;
637 2 my @statements;
638 2 for my $constraint (@foreign_key_constraints) {
639 1 push @statements, alter_drop_constraint($constraint, $options);
640 }
641  
642 2 return @statements, 'DROP TABLE ' . quote($table, $qi);
643 }
644  
645 1 ▶sub alter_create_index {
Subroutine alter_create_index: called CC=1 SCAR=0.0
Pod undocumented
646 1 my ($index, $options) = @_;
647 1 return create_index($index, $options);
648 }
649  
650 14 ▶sub create_index {
Subroutine create_index: called CC=10 SCAR=25.4
Pod documented
651 14 my ($index, $options, $index_options) = @_;
652 14 ▶ $index_options = $index_options || '';
Condition100%2/2
$index_options || ''
l!l
311
MC/DC100%1/1
$index_options || ''
$index_options
Truth table100%2/2
A
A= $index_options
Aresult
11
00
653 14 ▶ my $qf = $options->{quote_field_names} || $options->{quote_identifiers};
Condition66%2/3
$options->{'quote_field_names'} || $options->{'quote_identifiers'}
l!l&&r!l&&!r
608
MC/DC50%1/2
$options->{'quote_field_names'} || $options->{'quote_identifiers'}
$options->{'quote_field_names'} $options->{'quote_identifiers'}
Truth table66%2/3
A || B
A= $options->{'quote_field_names'}B= $options->{'quote_identifiers'}
ABresult
1X1
011
000
654 14 ▶ my $qt = $options->{quote_table_names} || $options->{quote_identifiers};
Condition66%2/3
$options->{'quote_table_names'} || $options->{'quote_identifiers'}
l!l&&r!l&&!r
608
MC/DC50%1/2
$options->{'quote_table_names'} || $options->{'quote_identifiers'}
$options->{'quote_table_names'} $options->{'quote_identifiers'}
Truth table66%2/3
A || B
A= $options->{'quote_table_names'}B= $options->{'quote_identifiers'}
ABresult
1X1
011
000
655 14 ▶ my $index_name = $index->name || '';
Condition50%1/2
$index->name || ''
l!l
140
MC/DC0%0/1
$index->name || ''
$index->name
Truth table50%1/2
A
A= $index->name
Aresult
11
00
656 14 ▶ $index_name
Branch: 1 branch
BranchTrueFalseTotal
$index_name ? : 14 0 14
Condition0%0/2
$index_name || 'i'
l!l
00
MC/DC0%0/1
$index_name || 'i'
$index_name
Truth table0%0/2
A
A= $index_name
Aresult
11
00
657 = $index_name
658 ? mk_name($index_name)
659 : mk_name($index->table, $index_name || 'i');
660 return join(' ',
661 84 ▶ map { $_ || () } 'CREATE',
Branch: 2 branches
BranchTrueFalseTotal
lc $index->type eq 'normal' ? : 13 1 14
$index_name ? : 14 0 14
Condition33%1/3
$_ || ()
l!l&&r!l&&!r
8400
MC/DC0%0/2
$_ || ()
$_ ()
Truth table33%1/3
A || B
A= $_B= ()
ABresult
1X1
011
000
662 lc $index->type eq 'normal' ? 'INDEX' : $index->type . ' INDEX',
663 $index_name ? quote($index_name, $qf) : '',
664 'ON',
665 quote($index->table, $qt),
666 14 '(' . join(', ', map { quote($_, $qf) } $index->fields) . ")$index_options");
667 }
668  
669 1 ▶sub alter_drop_index {
Subroutine alter_drop_index: called CC=1 SCAR=0.0
Pod undocumented
670 1 my ($index, $options) = @_;
671 1 return 'DROP INDEX ' . $index->name;
672 }
673  
674 9 ▶sub alter_drop_constraint {
Subroutine alter_drop_constraint: called CC=3 SCAR=11.0
Pod undocumented
675 9 my ($c, $options) = @_;
676 9 my $qi = $options->{quote_identifiers};
677 9 my $table_name = quote($c->table->name, $qi);
678 9 my @out = ('ALTER', 'TABLE', $table_name, 'DROP',);
679 9 ▶ if ($c->name) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c->name) { } 8 1 9
elsif ($c->type eq 'PRIMARY KEY') { } 1 0 1
680 8 push @out, ('CONSTRAINT', quote($c->name, $qi));
681 } elsif ($c->type eq PRIMARY_KEY) {
682 1 push @out, 'PRIMARY KEY';
683 }
684 9 return join(' ', @out);
685 }
686  
687 5 ▶sub alter_create_constraint {
Subroutine alter_create_constraint: called CC=1 SCAR=0.0
Pod undocumented
688 5 my ($c, $options) = @_;
689 5 my $qi = $options->{quote_identifiers};
690 5 my $table_name = quote($c->table->name, $qi);
691 5 return join(' ', 'ALTER TABLE', $table_name, 'ADD', create_constraint($c, $options));
692 }
693  
694 41 ▶sub create_constraint {
Subroutine create_constraint: called CC=32 SCAR=39.4
Pod documented
695 41 my ($c, $options) = @_;
696  
697 41 my $qt = $options->{quote_table_names};
698 41 my $qf = $options->{quote_field_names};
699 41 my $table = $c->table;
700 41 my $table_name = $table->name;
701 41 my $table_name_q = quote($table_name, $qt);
702 41 ▶ my $name = $c->name || '';
Condition100%2/2
$c->name || ''
l!l
1724
MC/DC100%1/1
$c->name || ''
$c->name
Truth table100%2/2
A
A= $c->name
Aresult
11
00
703 41 my @fields = map { quote($_, $qf) } $c->fields;
704 41 my @rfields = map { quote($_, $qf) } $c->reference_fields;
705  
706 41 ▶ return undef if !@fields && $c->type ne 'CHECK';
Branch: 1 branch
BranchTrueFalseTotal
if not @fields and $c->type ne "CHECK" 0 41 41
Condition33%1/3
not @fields and $c->type ne "CHECK"
!ll&&!rl&&r
4100
MC/DC0%0/2
not @fields and $c->type ne "CHECK"
not @fields $c->type ne "CHECK"
Truth table33%1/3
not(A) and B
A= not @fieldsB= $c->type ne "CHECK"
ABresult
0X0
100
111
707  
708 41 my $definition;
709  
710 41 ▶ if ($c->type eq PRIMARY_KEY) {
Branch: 4 branches
BranchTrueFalseTotal
if ($c->type eq 'PRIMARY KEY') { } 20 21 41
elsif ($c->type eq 'UNIQUE') { } 10 11 21
elsif ($c->type eq 'CHECK') { } 1 10 11
elsif ($c->type eq 'FOREIGN KEY') { } 10 0 10
711 20 debug("ORA: Creating PK constraint on fields (" . join(', ', @fields) . ")");
712  
713 # create a name if delay_constraints
714 $name ||= mk_name($table_name, 'pk')
715 20 ▶ if $options->{delay_constraints};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"delay_constraints"} 2 18 20
Condition33%1/3
$name ||= mk_name($table_name, "pk")
l!l&&r!l&&!r
020
MC/DC0%0/2
$name ||= mk_name($table_name, "pk")
$name mk_name($table_name, "pk")
Truth table33%1/3
A ||= B
A= $nameB= mk_name($table_name, "pk")
ABresult
1X1
011
000
716 20 $name = quote($name, $qf);
717 20 ▶ $definition = ($name ? "CONSTRAINT $name " : '') . 'PRIMARY KEY (' . join(', ', @fields) . ')';
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 2 18 20
718 } elsif ($c->type eq UNIQUE) {
719  
720 # Don't create UNIQUE constraints identical to the primary key
721 10 ▶ if (my $pk = $table->primary_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pk = $table->primary_key) 10 0 10
722 10 my $u_fields = join(":", @fields);
723 10 my $pk_fields = join(":", $pk->fields);
724 10 ▶ next if $u_fields eq $pk_fields;
Branch: 1 branch
BranchTrueFalseTotal
if $u_fields eq $pk_fields 0 10 10
725 }
726  
727 10 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 10 0 10
728  
729 # Force prepend of table_name as ORACLE doesn't allow duplicate
730 # CONSTRAINT names even for different tables (ORA-02264)
731 10 ▶ $name = mk_name("${table_name}_$name", 'u')
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^$table_name/ 10 0 10
732 unless $name =~ /^$table_name/;
733 } else {
734 0 $name = mk_name($table_name, 'u');
735 }
736 10 debug("ORA: Creating UNIQUE constraint on fields (" . join(', ', @fields) . ") named $name");
737 10 $name = quote($name, $qf);
738  
739 10 for my $f ($c->fields) {
740 10 ▶ my $field_def = $table->get_field($f) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $field_def = $table->get_field($f) or next 10 0 10
741 my $dtype = $translate{
742 10 ▶ ref $field_def->data_type eq "ARRAY"
Branch: 2 branches
BranchTrueFalseTotal
my $dtype = $translate{ref $field_def->data_type eq "ARRAY" ? $field_def->data_type->[0] : $field_def->data_type} or next 10 0 10
ref $field_def->data_type eq 'ARRAY' ? : 0 10 10
743 ? $field_def->data_type->[0]
744 : $field_def->data_type
745 }
746 or next;
747 10 ▶ if ($WARN && $dtype =~ /clob/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SQL::Translator::Producer::Oracle::WARN and $dtype =~ /clob/i) 0 10 10
Condition33%1/3
$SQL::Translator::Producer::Oracle::WARN and $dtype =~ /clob/i
!ll&&!rl&&r
1000
MC/DC0%0/2
$SQL::Translator::Producer::Oracle::WARN and $dtype =~ /clob/i
$SQL::Translator::Producer::Oracle::WARN $dtype =~ /clob/i
Truth table33%1/3
A and B
A= $SQL::Translator::Producer::Oracle::WARNB= $dtype =~ /clob/i
ABresult
0X0
100
111
748 0 warn "Oracle will not allow UNIQUE constraints on "
749 . "CLOB field '"
750 . $field_def->table->name . '.'
751 . $field_def->name . ".'\n";
752 }
753 }
754  
755 10 $definition = "CONSTRAINT $name UNIQUE " . '(' . join(', ', @fields) . ')';
756 } elsif ($c->type eq CHECK_C) {
757 1 ▶ $name ||= mk_name($name || $table_name, 'ck');
Condition16%1/6
$name ||= mk_name($name || $table_name, "ck")
l!l&&r!l&&!r
100
$name || $table_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$name ||= mk_name($name || $table_name, "ck")
$name mk_name($name || $table_name, "ck")
MC/DC0%0/2
$name || $table_name
$name $table_name
Truth table33%1/3
A ||= B
A= $nameB= mk_name($name || $table_name, "ck")
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $nameB= $table_name
ABresult
1X1
011
000
758 1 $name = quote($name, $qf);
759 1 ▶ my $expression = $c->expression || '';
Condition50%1/2
$c->expression || ''
l!l
10
MC/DC0%0/1
$c->expression || ''
$c->expression
Truth table50%1/2
A
A= $c->expression
Aresult
11
00
760 1 debug("ORA: Creating CHECK constraint on fields (" . join(', ', @fields) . ") named $name");
761 1 $definition = "CONSTRAINT $name CHECK ($expression)";
762 } elsif ($c->type eq FOREIGN_KEY) {
763 10 $name = mk_name(join('_', $table_name, $c->fields) . '_fk');
764 10 $name = quote($name, $qf);
765 10 ▶ my $on_delete = uc($c->on_delete || '');
Condition100%2/2
$c->on_delete || ''
l!l
19
MC/DC100%1/1
$c->on_delete || ''
$c->on_delete
Truth table100%2/2
A
A= $c->on_delete
Aresult
11
00
766  
767 10 $definition = "CONSTRAINT $name FOREIGN KEY ";
768  
769 10 ▶ if (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) 10 0 10
770 10 $definition .= '(' . join(', ', @fields) . ')';
771 }
772  
773 10 my $ref_table = quote($c->reference_table, $qt);
774 10 debug("ORA: Creating FK constraint on fields (" . join(', ', @fields) . ") named $name referencing $ref_table");
775 10 $definition .= " REFERENCES $ref_table";
776  
777 10 ▶ if (@rfields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rfields) 10 0 10
778 10 $definition .= ' (' . join(', ', @rfields) . ')';
779 }
780  
781 10 ▶ if ($c->match_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->match_type) 0 10 10
782 0 ▶ $definition .= ' MATCH ' . ($c->match_type =~ /full/i) ? 'FULL' : 'PARTIAL';
Branch: 1 branch
BranchTrueFalseTotal
' MATCH ' . $c->match_type =~ /full/i ? : 0 0 0
783 }
784  
785 10 ▶ if ($on_delete && $on_delete ne "RESTRICT") {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_delete and $on_delete ne "RESTRICT") 1 9 10
Condition66%2/3
$on_delete and $on_delete ne "RESTRICT"
!ll&&!rl&&r
901
MC/DC50%1/2
$on_delete and $on_delete ne "RESTRICT"
$on_delete $on_delete ne "RESTRICT"
Truth table66%2/3
A and B
A= $on_deleteB= $on_delete ne "RESTRICT"
ABresult
0X0
100
111
786 1 $definition .= ' ON DELETE ' . $c->on_delete;
787 }
788 }
789  
790 41 ▶ return $definition ? $definition : undef;
Branch: 1 branch
BranchTrueFalseTotal
$definition ? : 41 0 41
791 }
792  
793 6 ▶sub create_view {
Subroutine create_view: called CC=3 SCAR=11.0
Pod documented
794 6 my ($view, $options) = @_;
795 6 my $qt = $options->{quote_table_names};
796 6 my $view_name = quote($view->name, $qt);
797 6 my $extra = $view->extra;
798  
799 6 my $view_type = 'VIEW';
800 6 my $view_options = '';
801 6 ▶ if (my $materialized = $extra->{materialized}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $materialized = $extra->{"materialized"}) 1 5 6
802 1 $view_type = 'MATERIALIZED VIEW';
803 1 $view_options .= ' ' . $materialized;
804 }
805  
806 6 my @create;
807 push @create, qq[DROP $view_type $view_name]
808 6 ▶ if $options->{add_drop_view};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"add_drop_view"} 5 1 6
809  
810 6 push @create, sprintf("CREATE %s %s%s AS\n%s", $view_type, $view_name, $view_options, $view->sql);
811  
812 6 return \@create;
813 }
814  
815 170 ▶sub mk_name {
Subroutine mk_name: called CC=17 SCAR=30.0
Pod undocumented
816 170 ▶ my $basename = shift || '';
Condition50%1/2
shift() || ''
l!l
1700
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
817 170 ▶ my $type = shift || '';
Condition100%2/2
shift() || ''
l!l
42128
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
818 170 ▶ $type = '' if $type =~ /^\d/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^\d/ 0 170 170
819 170 ▶ my $scope = shift || '';
Condition100%2/2
shift() || ''
l!l
10466
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
820 170 ▶ my $critical = shift || '';
Condition100%2/2
shift() || ''
l!l
10466
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
821 170 my $basename_orig = $basename;
822 170 ▶ my $max_name
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 42 128 170
823 = $type
824 ? $max_id_length - (length($type) + 1)
825 : $max_id_length;
826 170 ▶ $basename = substr($basename, 0, $max_name)
Branch: 1 branch
BranchTrueFalseTotal
if length $basename > $max_name 8 162 170
827 if length($basename) > $max_name;
828 170 ▶ my $name = $type ? "${type}_$basename" : $basename;
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 42 128 170
829  
830 170 ▶ if ($basename ne $basename_orig and $critical) {
Branch: 1 branch
BranchTrueFalseTotal
if ($basename ne $basename_orig and $critical) 0 170 170
Condition66%2/3
$basename ne $basename_orig and $critical
!ll&&!rl&&r
16280
MC/DC0%0/2
$basename ne $basename_orig and $critical
$basename ne $basename_orig $critical
Truth table66%2/3
A and B
A= $basename ne $basename_origB= $critical
ABresult
0X0
100
111
831 0 ▶ my $show_type = $type ? "+'$type'" : "";
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 0 0 0
832 0 ▶ warn "Truncating '$basename_orig'$show_type to $max_id_length ", "character limit to make '$name'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $SQL::Translator::Producer::Oracle::WARN 0 0 0
833 if $WARN;
834 0 $truncated{$basename_orig} = $name;
835 }
836  
837 170 ▶ $scope ||= \%global_names;
Condition100%2/2
$scope ||= \%global_names
l!l
10466
MC/DC100%1/1
$scope ||= \%global_names
$scope
Truth table100%2/2
A
A= $scope
Aresult
11
00
838 170 ▶ if (my $prev = $scope->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $scope->{$name}) 18 152 170
839 18 my $name_orig = $name;
840 18 ▶ substr($name, $max_id_length - 2) = ""
Branch: 1 branch
BranchTrueFalseTotal
if length $name >= $max_id_length - 1 2 16 18
841 if length($name) >= $max_id_length - 1;
842 18 $name .= sprintf("%02d", $prev++);
843  
844 18 ▶ warn "The name '$name_orig' has been changed to ", "'$name' to make it unique.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $SQL::Translator::Producer::Oracle::WARN 0 18 18
845 if $WARN;
846  
847 18 $scope->{$name_orig}++;
848 }
849  
850 170 $scope->{$name}++;
851 170 return $name;
852 }
853  
854 12 1;
855  
856 607 ▶sub quote {
Subroutine quote: called CC=3 SCAR=11.0
Pod undocumented
857 607 my ($name, $q) = @_;
858 607 ▶ return $name unless $q && $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $q and $name 323 284 607
Condition100%3/3
$q and $name
!ll&&!rl&&r
31222273
MC/DC100%2/2
$q and $name
$q $name
Truth table100%3/3
A and B
A= $qB= $name
ABresult
0X0
100
111
859 284 $name =~ s/\Q$quote_char/$quote_char$quote_char/g;
860 284 return "$quote_char$name$quote_char";
861 }
862  
863 # -------------------------------------------------------------------
864 # All bad art is the result of good intentions.
865 # Oscar Wilde
866 # -------------------------------------------------------------------
867  
868 =pod
869  
870 =head1 CREDITS
871  
872 Mad props to Tim Bunce for much of the logic stolen from his "mysql2ora"
873 script.
874  
875 =head1 AUTHORS
876  
877 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
878 Alexander Hartmaier E<lt>abraxxa@cpan.orgE<gt>,
879 Fabien Wernli E<lt>faxmodem@cpan.orgE<gt>.
880  
881 =head1 SEE ALSO
882  
883 SQL::Translator, DDL::Oracle, mysql2ora.
884  
885 =cut
« blib/lib/SQL/Translator/Schema.pm ↑ Summary script/sqlt-diff-old »