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

filter: Statementstmt 90.1% 329 / 365 Branchbran 67.8% 148 / 218 Conditioncond 64.0% 64 / 100 MC/DCmcdc 38.9% 23 / 59 Subroutinesub 100% 22 / 22 Podpod 56.2% 9 / 16 totaltotal 76.2% 595 / 780 SCAR 59.7
CC
160
Cov
79%
create_table
96.3
create_field
57.8
create_constraint
51.4
CRAP 390.1
« blib/lib/SQL/Translator/Parser/DBI/Sybase.pm ↑ Summary blib/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm »
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.63';
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 sub produce {
190 11 ▶ my $translator = shift;
Subroutine produce: called CC=13 SCAR=25.7
Pod documented
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 .=
212 "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';\n\n";
213 }
214  
215 11 for my $table ( $schema->get_tables ) {
216 21 debug("ORA: Producing for table " . $table->name);
217 21 my ( $table_def, $fk_def, $trigger_def, $index_def, $constraint_def ) = create_table(
218 $table,
219 {
220 add_drop_table => $add_drop_table,
221 show_warnings => $WARN,
222 no_comments => $no_comments,
223 delay_constraints => $delay_constraints,
224 quote_table_names => $qt,
225 quote_field_names => $qf,
226 }
227 );
228 21 push @table_defs, @$table_def;
229 21 push @fk_defs, @$fk_def;
230 21 push @trigger_defs, @$trigger_def;
231 21 push @index_defs, @$index_def;
232 21 push @constraint_defs, @$constraint_def;
233 }
234  
235 11 my (@view_defs);
236 11 foreach my $view ( $schema->get_views ) {
237 5 my ( $view_def ) = create_view(
238 $view,
239 {
240 add_drop_view => $add_drop_table,
241 quote_table_names => $qt,
242 }
243 );
244 5 push @view_defs, @$view_def;
245 }
246  
247 11 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 3 8 11
248 3 ▶ return defined $create ? $create : (), @table_defs, @view_defs, @fk_defs, @trigger_defs, @index_defs, @constraint_defs;
Branch: 1 branch
BranchTrueFalseTotal
defined $create ? : 0 3 3
249 }
250 else {
251 8 $create .= join (";\n\n", @table_defs, @view_defs, @fk_defs, @index_defs, @constraint_defs);
252 8 $create .= ";\n\n";
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 $create .= "$_/\n\n"
257 8 for @trigger_defs;
258 8 return $create;
259 }
260 }
261  
262 sub create_table {
263 22 ▶ my ($table, $options) = @_;
Subroutine create_table: called CC=38 SCAR=45.7
Pod documented
264 22 my $qt = $options->{quote_table_names};
265 22 my $qf = $options->{quote_field_names};
266 22 my $table_name = $table->name;
267 22 my $table_name_q = quote($table_name,$qt);
268  
269 22 my $item = '';
270 22 my $drop;
271 22 my (@create, @field_defs, @constraint_defs, @fk_defs, @trigger_defs);
272  
273 22 ▶ push @create, "--\n-- Table: $table_name\n--" unless $options->{no_comments};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"no_comments"} 5 17 22
274 22 ▶ push @create, qq[DROP TABLE $table_name_q CASCADE CONSTRAINTS] if $options->{add_drop_table};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"add_drop_table"} 16 6 22
275  
276 22 my ( %field_name_scope, @field_comments );
277 22 for my $field ( $table->get_fields ) {
278 97 debug("ORA: Creating field " . $field->name . "(" . $field->data_type . ")");
279 97 my ($field_create, $field_defs, $trigger_defs, $field_comments) =
280 create_field($field, $options, \%field_name_scope);
281 97 ▶ push @create, @$field_create if ref $field_create;
Branch: 1 branch
BranchTrueFalseTotal
if ref $field_create 97 0 97
282 97 ▶ push @field_defs, @$field_defs if ref $field_defs;
Branch: 1 branch
BranchTrueFalseTotal
if ref $field_defs 97 0 97
283 97 ▶ push @trigger_defs, @$trigger_defs if ref $trigger_defs;
Branch: 1 branch
BranchTrueFalseTotal
if ref $trigger_defs 97 0 97
284 97 ▶ push @field_comments, @$field_comments if ref $field_comments;
Branch: 1 branch
BranchTrueFalseTotal
if ref $field_comments 97 0 97
285 }
286  
287 #
288 # Table options
289 #
290 22 my @table_options;
291 22 for my $opt ( $table->options ) {
292 7 ▶ if ( ref $opt eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt eq "HASH") 5 2 7
293 5 my ( $key, $value ) = each %$opt;
294 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
295 push @table_options, "$key\n(\n". join ("\n",
296 0 map { " $_->[0]\t$_->[1]" }
297 0 map { [ each %$_ ] }
298 @$value
299 )."\n)";
300 }
301 elsif ( !defined $value ) {
302 0 push @table_options, $key;
303 }
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 }
319 else {
320 29 push @constraint_defs, $constr;
321 }
322 }
323 }
324  
325 #
326 # Index Declarations
327 #
328 22 my @index_defs = ();
329 22 for my $index ( $table->get_indices ) {
330 11 ▶ my $index_name = $index->name || '';
Condition50%1/2
$index->name || ''
l!l
110
MC/DC0%0/1
$index->name || ''
$index->name
Truth table50%1/2
A
A= $index->name
Aresult
11
00
331 11 ▶ my $index_type = $index->type || NORMAL;
Condition50%1/2
$index->type || 'NORMAL'
l!l
110
MC/DC0%0/1
$index->type || 'NORMAL'
$index->type
Truth table50%1/2
A
A= $index->type
Aresult
11
00
332 11 my @fields = map { quote($_, $qf) } $index->fields;
333 11 ▶ next unless @fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields 0 11 11
334 11 debug("ORA: Creating $index_type index on fields (" . join(', ', @fields) . ") named $index_name");
335 11 my @index_options;
336 11 for my $opt ( $index->options ) {
337 3 ▶ if ( ref $opt eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt eq "HASH") 3 0 3
338 3 my ( $key, $value ) = each %$opt;
339 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
340 push @table_options, "$key\n(\n". join ("\n",
341 0 map { " $_->[0]\t$_->[1]" }
342 0 map { [ each %$_ ] }
343 @$value
344 )."\n)";
345 }
346 elsif ( !defined $value ) {
347 0 push @index_options, $key;
348 }
349 else {
350 3 push @index_options, "$key $value";
351 }
352 }
353 }
354 11 ▶ my $index_options = @index_options
Branch: 1 branch
BranchTrueFalseTotal
@index_options ? : 3 8 11
355 ? "\n".join("\n", @index_options) : '';
356  
357 11 ▶ if ( $index_type eq PRIMARY_KEY ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($index_type eq 'PRIMARY KEY') { } 0 11 11
elsif ($index_type eq 'NORMAL' or $index_type eq 'UNIQUE') { } 11 0 11
358 0 ▶ $index_name = $index_name ? mk_name( $index_name )
Branch: 1 branch
BranchTrueFalseTotal
$index_name ? : 0 0 0
359 : mk_name( $table_name, 'pk' );
360 0 $index_name = quote($index_name, $qf);
361 0 push @field_defs, 'CONSTRAINT '.$index_name.' PRIMARY KEY '.
362 '(' . join( ', ', @fields ) . ')';
363 }
364 ▶ 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
1010
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
365 11 push @index_defs, create_index($index, $options, $index_options);
366 }
367 else {
368 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
369 if $WARN;
370 }
371 }
372  
373 22 ▶ if ( my @table_comments = $table->comments ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@table_comments) = $table->comments) 0 22 22
374 0 for my $comment ( @table_comments ) {
375 0 ▶ next unless $comment;
Branch: 1 branch
BranchTrueFalseTotal
unless $comment 0 0 0
376 0 $comment = __PACKAGE__->_quote_string($comment);
377 push @field_comments, "COMMENT ON TABLE $table_name_q is\n $comment"
378 0 ▶ unless $options->{no_comments};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"no_comments"} 0 0 0
379 }
380 }
381  
382 22 ▶ my $table_options = @table_options ? "\n".join("\n", @table_options) : '';
Branch: 1 branch
BranchTrueFalseTotal
@table_options ? : 5 17 22
383 push @create, "CREATE TABLE $table_name_q (\n" .
384 124 ▶ join( ",\n", map { " $_" } @field_defs,
Branch: 1 branch
BranchTrueFalseTotal
$options->{'delay_constraints'} ? : 2 20 22
385 22 ($options->{delay_constraints} ? () : @constraint_defs) ) .
386 "\n)$table_options";
387  
388 22 @constraint_defs = map { "ALTER TABLE $table_name_q ADD $_" } @constraint_defs;
389  
390 22 ▶ if ( $WARN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SQL::Translator::Producer::Oracle::WARN) 0 22 22
391 0 ▶ if ( %truncated ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%truncated) 0 0 0
392 0 warn "Truncated " . keys( %truncated ) . " names:\n";
393 0 warn "\t" . join( "\n\t", sort keys %truncated ) . "\n";
394 }
395 }
396  
397 22 ▶ return \@create, \@fk_defs, \@trigger_defs, \@index_defs, ($options->{delay_constraints} ? \@constraint_defs : []);
Branch: 1 branch
BranchTrueFalseTotal
$options->{'delay_constraints'} ? : 2 20 22
398 }
399  
400 sub alter_field {
401 4 ▶ my ($from_field, $to_field, $options) = @_;
Subroutine alter_field: called CC=6 SCAR=18.0
Pod documented
402  
403 4 my $qt = $options->{quote_table_names};
404 4 my ($field_create, $field_defs, $trigger_defs, $field_comments) =
405 create_field($to_field, $options, {});
406  
407 # Fix ORA-01442
408 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
409 1 ▶ if ($from_field->data_type =~ /text/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_field->data_type =~ /text/) { } 0 1 1
410 0 die 'Cannot alter CLOB field in this way';
411 }
412 else {
413 1 @$field_defs = map { $_ .= ' NULL' } @$field_defs;
414 }
415 ▶ } 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
416 1 @$field_defs = map { s/ NOT NULL//; $_} @$field_defs;
417 }
418  
419 4 my $table_name = quote($to_field->table->name,$qt);
420  
421 4 return 'ALTER TABLE '.$table_name.' MODIFY ( '.join('', @$field_defs).' )';
422 }
423  
424 sub drop_field
425 {
426 2 ▶ my ($old_field, $options) = @_;
Subroutine drop_field: called CC=1 SCAR=0.0
Pod documented
427 2 my $qi = $options->{quote_identifiers};
428 2 my $table_name = quote($old_field->table->name, $qi);
429  
430 2 my $out = sprintf('ALTER TABLE %s DROP COLUMN %s',
431 $table_name,
432 quote($old_field->name, $qi));
433  
434 2 return $out;
435 }
436  
437 sub add_field {
438 3 ▶ my ($new_field, $options) = @_;
Subroutine add_field: called CC=1 SCAR=0.0
Pod documented
439  
440 3 my $qt = $options->{quote_table_names};
441 3 my ($field_create, $field_defs, $trigger_defs, $field_comments) =
442 create_field($new_field, $options, {});
443  
444 3 my $table_name = quote($new_field->table->name,$qt);
445  
446 3 my $out = sprintf('ALTER TABLE %s ADD ( %s )',
447 $table_name,
448 join('', @$field_defs));
449 3 return $out;
450 }
451  
452 sub create_field {
453 104 ▶ my ($field, $options, $field_name_scope) = @_;
Subroutine create_field: called CC=43 SCAR=40.6
Pod documented
454 104 my $qf = $options->{quote_field_names};
455 104 my $qt = $options->{quote_table_names};
456  
457 104 my (@create, @field_defs, @trigger_defs, @field_comments);
458  
459 104 my $table_name = $field->table->name;
460 104 my $table_name_q = quote($table_name, $qt);
461  
462 #
463 # Field name
464 #
465 104 my $field_name = mk_name(
466 $field->name, '', $field_name_scope, 1
467 );
468 104 my $field_name_q = quote($field_name, $qf);
469 104 my $field_def = quote($field_name, $qf);
470 104 $field->name( $field_name );
471  
472 #
473 # Datatype
474 #
475 104 my $check;
476 104 my $data_type = lc $field->data_type;
477 104 my @size = $field->size;
478 104 my %extra = $field->extra;
479 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
480 104 my $commalist = join( ', ', map { __PACKAGE__->_quote_string($_) } @$list );
481  
482 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
483 1 $check = "CHECK ($field_name_q IN ($commalist))";
484 1 $data_type = 'varchar2';
485 }
486 elsif ( $data_type eq 'set' ) {
487 # XXX add a CHECK constraint maybe
488 # (trickier and slower, than enum :)
489 0 $data_type = 'varchar2';
490 }
491 else {
492 103 ▶ if (defined $translate{ $data_type }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $translate{$data_type}) 97 6 103
493 97 ▶ if (ref $translate{ $data_type } eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $translate{$data_type} eq 'ARRAY') { } 0 97 97
494 0 ($data_type,$size[0]) = @{$translate{ $data_type }};
495 } else {
496 97 $data_type = $translate{ $data_type };
497 }
498 }
499 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
500 }
501  
502 # ensure size is not bigger than max size oracle allows for data type
503 104 ▶ if ( defined $max_size{$data_type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_size{$data_type}) 87 17 104
504 87 ▶ for ( my $i = 0 ; $i < scalar @size ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @size) 95 87 182
505 my $max =
506 ref( $max_size{$data_type} ) eq 'ARRAY'
507 ? $max_size{$data_type}->[$i]
508 95 ▶ : $max_size{$data_type};
Branch: 1 branch
BranchTrueFalseTotal
ref $max_size{$data_type} eq 'ARRAY' ? : 46 49 95
509 95 ▶ $size[$i] = $max if $size[$i] > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $size[$i] > $max 0 95 95
510 }
511 }
512  
513 #
514 # Fixes ORA-02329: column of datatype LOB cannot be
515 # unique or a primary key
516 #
517 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
518 1 $data_type = 'varchar2';
519 1 $size[0] = 4000;
520 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
521 if $WARN;
522 }
523  
524 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
525 0 $data_type = 'varchar2';
526 0 $size[0] = 4000;
527 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
528 if $WARN;
529 }
530  
531 #
532 # Fixes ORA-00907: missing right parenthesis
533 #
534 104 ▶ if ( $data_type =~ /(date|clob)/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type =~ /(date|clob)/i) 16 88 104
535 16 undef @size;
536 }
537  
538 #
539 # Fixes ORA-00906: missing right parenthesis
540 # if size is 0 or undefined
541 #
542 104 for (qw/varchar2/) {
543 104 ▶ if ( $data_type =~ /^($_)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type =~ /^($_)$/i) 46 58 104
544 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
545 }
546 }
547  
548 104 $field_def .= " $data_type";
549 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
550 85 $field_def .= '(' . join( ',', @size ) . ')';
551 }
552  
553 #
554 # Default value
555 #
556 104 my $default = $field->default_value;
557 104 ▶ if ( defined $default ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) 36 68 104
558 36 debug("ORA: Handling default value: $default");
559 #
560 # Wherein we try to catch a string being used as
561 # a default value for a numerical field. If "true/false,"
562 # then sub "1/0," otherwise just test the truthity of the
563 # argument and use that (naive?).
564 #
565 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
566 1 $default = $$default;
567 } elsif (ref $default) {
568 0 $default = 'NULL';
569 ▶ } elsif (
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
570 $data_type =~ /^number$/i &&
571 $default !~ /^-?\d+$/ &&
572 $default !~ m/null/i
573 ) {
574 1 ▶ if ( $default =~ /^true$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($default =~ /^true$/i) { } 0 1 1
elsif ($default =~ /^false$/i) { } 0 1 1
575 0 $default = "'1'";
576 } elsif ( $default =~ /^false$/i ) {
577 0 $default = "'0'";
578 } else {
579 1 ▶ $default = $default ? "'1'" : "'0'";
Branch: 1 branch
BranchTrueFalseTotal
$default ? : 1 0 1
580 }
581 ▶ } 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
582 $data_type =~ /date/ && (
583 $default eq 'current_timestamp'
584 ||
585 $default eq 'now()'
586 )
587 ) {
588 0 $default = 'SYSDATE';
589 } else {
590 34 ▶ $default = $default =~ m/null/i ? 'NULL' : __PACKAGE__->_quote_string($default);
Branch: 1 branch
BranchTrueFalseTotal
$default =~ /null/i ? : 0 34 34
591 }
592  
593 36 $field_def .= " DEFAULT $default",
594 }
595  
596 #
597 # Not null constraint
598 #
599 104 ▶ unless ( $field->is_nullable ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field->is_nullable) 47 57 104
600 47 debug("ORA: Field is NOT NULL");
601 47 $field_def .= ' NOT NULL';
602 }
603  
604 104 ▶ $field_def .= " $check" if $check;
Branch: 1 branch
BranchTrueFalseTotal
if $check 1 103 104
605  
606 #
607 # Auto_increment
608 #
609 104 ▶ if ( $field->is_auto_increment ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->is_auto_increment) 15 89 104
610 15 debug("ORA: Handling auto increment");
611 15 my $base_name = $table_name . "_". $field_name;
612 15 my $seq_name = quote(mk_name( $base_name, 'sq' ),$qt);
613 15 my $trigger_name = quote(mk_name( $base_name, 'ai' ),$qt);
614  
615 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
616 15 push @create, "CREATE SEQUENCE $seq_name";
617 15 my $trigger =
618 "CREATE OR REPLACE TRIGGER $trigger_name\n" .
619 "BEFORE INSERT ON $table_name_q\n" .
620 "FOR EACH ROW WHEN (\n" .
621 " new.$field_name_q IS NULL".
622 " OR new.$field_name_q = 0\n".
623 ")\n".
624 "BEGIN\n" .
625 " SELECT $seq_name.nextval\n" .
626 " INTO :new." . $field_name_q."\n" .
627 " FROM dual;\n" .
628 "END;\n";
629  
630 15 push @trigger_defs, $trigger;
631 }
632  
633 104 push @field_defs, $field_def;
634  
635 104 ▶ if ( my $comment = $field->comments ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $comment = $field->comments) 5 99 104
636 5 debug("ORA: Handling comment");
637 5 $comment =~ __PACKAGE__->_quote_string($comment);
638 push @field_comments,
639 "COMMENT ON COLUMN $table_name_q.$field_name_q is\n $comment;"
640 5 ▶ unless $options->{no_comments};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"no_comments"} 0 5 5
641 }
642  
643 104 return \@create, \@field_defs, \@trigger_defs, \@field_comments;
644  
645 }
646  
647 sub drop_table {
648 2 ▶ my ($table, $options) = @_;
Subroutine drop_table: called CC=2 SCAR=6.9
Pod undocumented
649  
650 2 my $qi = $options->{quote_identifiers};
651 2 my @foreign_key_constraints = grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints;
652 2 my @statements;
653 2 for my $constraint(@foreign_key_constraints) {
654 1 push @statements, alter_drop_constraint($constraint, $options);
655 }
656  
657 2 return @statements, 'DROP TABLE ' . quote($table, $qi);
658 }
659  
660 sub alter_create_index {
661 1 ▶ my ($index, $options) = @_;
Subroutine alter_create_index: called CC=1 SCAR=0.0
Pod undocumented
662 1 return create_index($index, $options);
663 }
664  
665 sub create_index {
666 12 ▶ my ( $index, $options, $index_options) = @_;
Subroutine create_index: called CC=10 SCAR=24.9
Pod documented
667 12 ▶ $index_options = $index_options || '';
Condition100%2/2
$index_options || ''
l!l
39
MC/DC100%1/1
$index_options || ''
$index_options
Truth table100%2/2
A
A= $index_options
Aresult
11
00
668 12 ▶ my $qf = $options->{quote_field_names} || $options->{quote_identifiers};
Condition66%2/3
$options->{'quote_field_names'} || $options->{'quote_identifiers'}
l!l&&r!l&&!r
606
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
669 12 ▶ my $qt = $options->{quote_table_names} || $options->{quote_identifiers};
Condition66%2/3
$options->{'quote_table_names'} || $options->{'quote_identifiers'}
l!l&&r!l&&!r
606
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
670 12 ▶ my $index_name = $index->name || '';
Condition50%1/2
$index->name || ''
l!l
120
MC/DC0%0/1
$index->name || ''
$index->name
Truth table50%1/2
A
A= $index->name
Aresult
11
00
671 12 ▶ $index_name = $index_name ? mk_name( $index_name ) : mk_name( $index->table, $index_name || 'i' );
Branch: 1 branch
BranchTrueFalseTotal
$index_name ? : 12 0 12
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
672 return join(
673 ' ',
674 72 ▶ map { $_ || () }
Branch: 3 branches
BranchTrueFalseTotal
$_ or () 0 72 72
lc $index->type eq 'normal' ? : 11 1 12
$index_name ? : 12 0 12
675 'CREATE',
676 lc $index->type eq 'normal' ? 'INDEX' : $index->type . ' INDEX',
677 $index_name ? quote($index_name, $qf): '',
678 'ON',
679 quote($index->table, $qt),
680 12 '(' . join( ', ', map { quote($_, $qf) } $index->fields ) . ")$index_options"
681 );
682 }
683  
684 sub alter_drop_index {
685 1 ▶ my ($index, $options) = @_;
Subroutine alter_drop_index: called CC=1 SCAR=0.0
Pod undocumented
686 1 return 'DROP INDEX ' . $index->name;
687 }
688  
689 sub alter_drop_constraint {
690 9 ▶ my ($c, $options) = @_;
Subroutine alter_drop_constraint: called CC=3 SCAR=11.0
Pod undocumented
691 9 my $qi = $options->{quote_identifiers};
692 9 my $table_name = quote($c->table->name, $qi);
693 9 my @out = ('ALTER','TABLE',$table_name,'DROP',);
694 9 ▶ if ($c->name) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c->name) { } 8 1 9
elsif ($c->type eq 'PRIMARY KEY') { } 1 0 1
695 8 push @out, ('CONSTRAINT',quote($c->name, $qi));
696 }
697 elsif ($c->type eq PRIMARY_KEY) {
698 1 push @out, 'PRIMARY KEY';
699 }
700 9 return join(' ',@out);
701 }
702  
703 sub alter_create_constraint {
704 5 ▶ my ($c, $options) = @_;
Subroutine alter_create_constraint: called CC=1 SCAR=0.0
Pod undocumented
705 5 my $qi = $options->{quote_identifiers};
706 5 my $table_name = quote($c->table->name, $qi);
707 5 return join( ' ',
708 'ALTER TABLE',
709 $table_name,
710 'ADD',
711 create_constraint($c, $options) );
712 }
713  
714 sub create_constraint {
715 41 ▶ my ($c, $options) = @_;
Subroutine create_constraint: called CC=32 SCAR=39.4
Pod documented
716  
717 41 my $qt = $options->{quote_table_names};
718 41 my $qf = $options->{quote_field_names};
719 41 my $table = $c->table;
720 41 my $table_name = $table->name;
721 41 my $table_name_q = quote($table_name,$qt);
722 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
723 41 my @fields = map { quote($_,$qf) } $c->fields;
724 41 my @rfields = map { quote($_,$qf) } $c->reference_fields;
725  
726 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
727  
728 41 my $definition;
729  
730 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
731 20 debug("ORA: Creating PK constraint on fields (" . join(', ', @fields) . ")");
732 # create a name if delay_constraints
733 $name ||= mk_name( $table_name, 'pk' )
734 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
735 20 $name = quote($name,$qf);
736 20 ▶ $definition = ($name ? "CONSTRAINT $name " : '') .
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 2 18 20
737 'PRIMARY KEY (' . join( ', ', @fields ) . ')';
738 }
739 elsif ( $c->type eq UNIQUE ) {
740 # Don't create UNIQUE constraints identical to the primary key
741 10 ▶ if ( my $pk = $table->primary_key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pk = $table->primary_key) 10 0 10
742 10 my $u_fields = join(":", @fields);
743 10 my $pk_fields = join(":", $pk->fields);
744 10 ▶ next if $u_fields eq $pk_fields;
Branch: 1 branch
BranchTrueFalseTotal
if $u_fields eq $pk_fields 0 10 10
745 }
746  
747 10 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 10 0 10
748 # Force prepend of table_name as ORACLE doesn't allow duplicate
749 # CONSTRAINT names even for different tables (ORA-02264)
750 10 ▶ $name = mk_name( "${table_name}_$name", 'u' ) unless $name =~ /^$table_name/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^$table_name/ 10 0 10
751 }
752 else {
753 0 $name = mk_name( $table_name, 'u' );
754 }
755 10 debug("ORA: Creating UNIQUE constraint on fields (" . join(', ', @fields) . ") named $name");
756 10 $name = quote($name, $qf);
757  
758 10 for my $f ( $c->fields ) {
759 10 ▶ my $field_def = $table->get_field( $f ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $field_def = $table->get_field($f) or next 0 10 10
760 10 ▶ my $dtype = $translate{ ref $field_def->data_type eq "ARRAY" ? $field_def->data_type->[0] : $field_def->data_type} or next;
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 0 10 10
ref $field_def->data_type eq 'ARRAY' ? : 0 10 10
761 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
762 0 warn "Oracle will not allow UNIQUE constraints on " .
763 "CLOB field '" . $field_def->table->name . '.' .
764 $field_def->name . ".'\n"
765 }
766 }
767  
768 10 $definition = "CONSTRAINT $name UNIQUE " .
769 '(' . join( ', ', @fields ) . ')';
770 }
771 elsif ( $c->type eq CHECK_C ) {
772 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
773 1 $name = quote($name, $qf);
774 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
775 1 debug("ORA: Creating CHECK constraint on fields (" . join(', ', @fields) . ") named $name");
776 1 $definition = "CONSTRAINT $name CHECK ($expression)";
777 }
778 elsif ( $c->type eq FOREIGN_KEY ) {
779 10 $name = mk_name( join('_', $table_name, $c->fields). '_fk' );
780 10 $name = quote($name, $qf);
781 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
782  
783 10 $definition = "CONSTRAINT $name FOREIGN KEY ";
784  
785 10 ▶ if ( @fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) 10 0 10
786 10 $definition .= '(' . join( ', ', @fields ) . ')';
787 }
788  
789 10 my $ref_table = quote($c->reference_table,$qt);
790 10 debug("ORA: Creating FK constraint on fields (" . join(', ', @fields) . ") named $name referencing $ref_table");
791 10 $definition .= " REFERENCES $ref_table";
792  
793 10 ▶ if ( @rfields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rfields) 10 0 10
794 10 $definition .= ' (' . join( ', ', @rfields ) . ')';
795 }
796  
797 10 ▶ if ( $c->match_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->match_type) 0 10 10
798 0 ▶ $definition .= ' MATCH ' .
Branch: 1 branch
BranchTrueFalseTotal
' MATCH ' . $c->match_type =~ /full/i ? : 0 0 0
799 ( $c->match_type =~ /full/i ) ? 'FULL' : 'PARTIAL';
800 }
801  
802 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
803 1 $definition .= ' ON DELETE '.$c->on_delete;
804 }
805 }
806  
807 41 ▶ return $definition ? $definition : undef;
Branch: 1 branch
BranchTrueFalseTotal
$definition ? : 41 0 41
808 }
809  
810 sub create_view {
811 6 ▶ my ($view, $options) = @_;
Subroutine create_view: called CC=3 SCAR=11.0
Pod documented
812 6 my $qt = $options->{quote_table_names};
813 6 my $view_name = quote($view->name,$qt);
814 6 my $extra = $view->extra;
815  
816 6 my $view_type = 'VIEW';
817 6 my $view_options = '';
818 6 ▶ if ( my $materialized = $extra->{materialized} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $materialized = $extra->{"materialized"}) 1 5 6
819 1 $view_type = 'MATERIALIZED VIEW';
820 1 $view_options .= ' '.$materialized;
821 }
822  
823 6 my @create;
824 push @create, qq[DROP $view_type $view_name]
825 6 ▶ if $options->{add_drop_view};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"add_drop_view"} 5 1 6
826  
827 6 push @create, sprintf("CREATE %s %s%s AS\n%s",
828 $view_type,
829 $view_name,
830 $view_options,
831 $view->sql);
832  
833 6 return \@create;
834 }
835  
836 sub mk_name {
837 168 ▶ my $basename = shift || '';
Subroutine mk_name: called CC=17 SCAR=30.0
Pod undocumented
Condition50%1/2
shift() || ''
l!l
1680
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
838 168 ▶ my $type = shift || '';
Condition100%2/2
shift() || ''
l!l
42126
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
839 168 ▶ $type = '' if $type =~ /^\d/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^\d/ 0 168 168
840 168 ▶ my $scope = shift || '';
Condition100%2/2
shift() || ''
l!l
10464
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
841 168 ▶ my $critical = shift || '';
Condition100%2/2
shift() || ''
l!l
10464
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
842 168 my $basename_orig = $basename;
843 168 ▶ my $max_name = $type
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 42 126 168
844 ? $max_id_length - (length($type) + 1)
845 : $max_id_length;
846 168 ▶ $basename = substr( $basename, 0, $max_name )
Branch: 1 branch
BranchTrueFalseTotal
if length $basename > $max_name 8 160 168
847 if length( $basename ) > $max_name;
848 168 ▶ my $name = $type ? "${type}_$basename" : $basename;
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 42 126 168
849  
850 168 ▶ if ( $basename ne $basename_orig and $critical ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($basename ne $basename_orig and $critical) 0 168 168
Condition66%2/3
$basename ne $basename_orig and $critical
!ll&&!rl&&r
16080
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
851 0 ▶ my $show_type = $type ? "+'$type'" : "";
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 0 0 0
852 0 ▶ warn "Truncating '$basename_orig'$show_type to $max_id_length ",
Branch: 1 branch
BranchTrueFalseTotal
if $SQL::Translator::Producer::Oracle::WARN 0 0 0
853 "character limit to make '$name'\n" if $WARN;
854 0 $truncated{ $basename_orig } = $name;
855 }
856  
857 168 ▶ $scope ||= \%global_names;
Condition100%2/2
$scope ||= \%global_names
l!l
10464
MC/DC100%1/1
$scope ||= \%global_names
$scope
Truth table100%2/2
A
A= $scope
Aresult
11
00
858 168 ▶ if ( my $prev = $scope->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $scope->{$name}) 18 150 168
859 18 my $name_orig = $name;
860 18 ▶ substr($name, $max_id_length - 2) = ""
Branch: 1 branch
BranchTrueFalseTotal
if length $name >= $max_id_length - 1 2 16 18
861 if length( $name ) >= $max_id_length - 1;
862 18 $name .= sprintf( "%02d", $prev++ );
863  
864 18 ▶ warn "The name '$name_orig' has been changed to ",
Branch: 1 branch
BranchTrueFalseTotal
if $SQL::Translator::Producer::Oracle::WARN 0 18 18
865 "'$name' to make it unique.\n" if $WARN;
866  
867 18 $scope->{ $name_orig }++;
868 }
869  
870 168 $scope->{ $name }++;
871 168 return $name;
872 }
873  
874 12 1;
875  
876 sub quote {
877 599 ▶ my ($name, $q) = @_;
Subroutine quote: called CC=3 SCAR=11.0
Pod undocumented
878 599 ▶ return $name unless $q && $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $q and $name 315 284 599
Condition100%3/3
$q and $name
!ll&&!rl&&r
30411284
MC/DC100%2/2
$q and $name
$q $name
Truth table100%3/3
A and B
A= $qB= $name
ABresult
0X0
100
111
879 284 $name =~ s/\Q$quote_char/$quote_char$quote_char/g;
880 284 return "$quote_char$name$quote_char";
881 }
882  
883  
884 # -------------------------------------------------------------------
885 # All bad art is the result of good intentions.
886 # Oscar Wilde
887 # -------------------------------------------------------------------
888  
889 =pod
890  
891 =head1 CREDITS
892  
893 Mad props to Tim Bunce for much of the logic stolen from his "mysql2ora"
894 script.
895  
896 =head1 AUTHORS
897  
898 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
899 Alexander Hartmaier E<lt>abraxxa@cpan.orgE<gt>,
900 Fabien Wernli E<lt>faxmodem@cpan.orgE<gt>.
901  
902 =head1 SEE ALSO
903  
904 SQL::Translator, DDL::Oracle, mysql2ora.
905  
906 =cut
« blib/lib/SQL/Translator/Parser/DBI/Sybase.pm ↑ Summary blib/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm »