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