remove some unnecessary code
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Producer / SQL / MySQL.pm
CommitLineData
24e6a53a 1use MooseX::Declare;
2role SQL::Translator::Producer::SQL::MySQL {
3 use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants);
4 use SQL::Translator::Types qw(Column Constraint Index Table View);
5 my $DEFAULT_MAX_ID_LENGTH = 64;
6 my %used_names;
7
8 #
9 # Use only lowercase for the keys (e.g. "long" and not "LONG")
10 #
11 my %translate = (
12 #
13 # Oracle types
14 #
15 varchar2 => 'varchar',
16 long => 'text',
17 clob => 'longtext',
18
19 #
20 # Sybase types
21 #
22 int => 'integer',
23 money => 'float',
24 real => 'double',
25 comment => 'text',
26 bit => 'tinyint',
27
28 #
29 # Access types
30 #
31 'long integer' => 'integer',
32 'text' => 'text',
33 'datetime' => 'datetime',
34
35 #
36 # PostgreSQL types
37 #
38 bytea => 'BLOB',
39 );
40
41 #
42 # Column types that do not support lenth attribute
43 #
44 my @no_length_attr = qw/ date time timestamp datetime year /;
45
46 method preprocess_schema($schema) {
47# my ($schema) = @_;
48
49 # extra->{mysql_table_type} used to be the type. It belongs in options, so
50 # move it if we find it. Return Engine type if found in extra or options
51 # Similarly for mysql_charset and mysql_collate
52 my $extra_to_options = sub {
53 my ($table, $extra_name, $opt_name) = @_;
54
55 my $extra = $table->extra;
56
57 my $extra_type = delete $extra->{$extra_name};
58
59 # Now just to find if there is already an Engine or Type option...
60 # and lets normalize it to ENGINE since:
61 #
62 # The ENGINE table option specifies the storage engine for the table.
63 # TYPE is a synonym, but ENGINE is the preferred option name.
64 #
65
66 # We have to use the hash directly here since otherwise there is no way
67 # to remove options.
68 my $options = ( $table->{options} ||= []);
69
70 # If multiple option names, normalize to the first one
71 if (ref $opt_name) {
72 OPT_NAME: for ( @$opt_name[1..$#$opt_name] ) {
73 for my $idx ( 0..$#{$options} ) {
74 my ($key, $value) = %{ $options->[$idx] };
75
76 if (uc $key eq $_) {
77 $options->[$idx] = { $opt_name->[0] => $value };
78 last OPT_NAME;
79 }
80 }
81 }
82 $opt_name = $opt_name->[0];
83
84 }
85
86
87 # This assumes that there isn't both a Type and an Engine option.
88 OPTION:
89 for my $idx ( 0..$#{$options} ) {
90 my ($key, $value) = %{ $options->[$idx] };
91
92 next unless uc $key eq $opt_name;
93
94 # make sure case is right on option name
95 delete $options->[$idx]{$key};
96 return $options->[$idx]{$opt_name} = $value || $extra_type;
97
98 }
99
100 if ($extra_type) {
101 push @$options, { $opt_name => $extra_type };
102 return $extra_type;
103 }
104
105 };
106
107 # Names are only specific to a given schema
108 my %used_names = ();
109
110 #
111 # Work out which tables need to be InnoDB to support foreign key
112 # constraints. We do this first as we need InnoDB at both ends.
113 #
114 foreach my $table ( $schema->get_tables ) {
115
116 $extra_to_options->($table, 'mysql_table_type', ['ENGINE', 'TYPE'] );
117 $extra_to_options->($table, 'mysql_charset', 'CHARACTER SET' );
118 $extra_to_options->($table, 'mysql_collate', 'COLLATE' );
119
120 foreach my $c ( $table->get_constraints ) {
121 next unless $c->type eq FOREIGN_KEY;
122
123 # Normalize constraint names here.
124 my $c_name = $c->name;
125 # Give the constraint a name if it doesn't have one, so it doens't feel
126 # left out
127 $c_name = $table->name . '_fk' unless length $c_name;
128
129 $c->name( $self->next_unused_name($c_name) );
130
131 for my $meth (qw/table reference_table/) {
132 my $table = $schema->get_table($c->$meth) || next;
133 # This normalizes the types to ENGINE and returns the value if its there
5590bc43 134 next if $extra_to_options->($table, 'mysql_table_type', ['ENGINE', 'TYPE']);
135# $table->options( [ { ENGINE => 'InnoDB' } ] );
24e6a53a 136 }
137 } # foreach constraints
138
139 my %map = ( mysql_collate => 'collate', mysql_charset => 'character set');
140 foreach my $f ( $table->get_fields ) {
141 my $extra = $f->extra;
142 for (keys %map) {
143 $extra->{$map{$_}} = delete $extra->{$_} if exists $extra->{$_};
144 }
145
146 my @size = $f->size;
147 if ( !$size[0] && $f->data_type =~ /char$/ ) {
148 $f->size( (255) );
149 }
150 }
151
152 }
153 }
154
94f42333 155 method produce {
24e6a53a 156 my $translator = $self->translator;
157 my $DEBUG = 0;# = $translator->debug;
158 #local %used_names;
159 my $no_comments = $translator->no_comments;
160 my $add_drop_table = $translator->add_drop_table;
161 my $schema = $translator->schema;
162 my $show_warnings = $translator->show_warnings || 0;
163 my $producer_args = $translator->producer_args;
f48e6f55 164 my $mysql_version = $translator->engine_version ($producer_args->{mysql_version}, 'perl') || 0;
24e6a53a 165 my $max_id_length = $producer_args->{mysql_max_id_length} || $DEFAULT_MAX_ID_LENGTH;
166
167 my ($qt, $qf, $qc) = ('','', '');
168 $qt = '`' if $translator->quote_table_names;
169 $qf = '`' if $translator->quote_field_names;
170
171 #debug("PKG: Beginning production\n");
172 %used_names = ();
173 my $create = '';
174 $create .= $self->header_comment unless ($no_comments);
175 # \todo Don't set if MySQL 3.x is set on command line
176 my @create = "SET foreign_key_checks=0";
177
178 $self->preprocess_schema($schema);
179
180 #
181 # Generate sql
182 #
183 my @table_defs =();
184
185 for my $table ( $schema->get_tables ) {
186 # print $table->name, "\n";
187 push @table_defs, $self->create_table($table,
188 { add_drop_table => $add_drop_table,
189 show_warnings => $show_warnings,
190 no_comments => $no_comments,
191 quote_table_names => $qt,
192 quote_field_names => $qf,
193 max_id_length => $max_id_length,
194 mysql_version => $mysql_version
195 });
196 }
197
198 if ($mysql_version >= 5.000001) {
199 for my $view ( $schema->get_views ) {
200 push @table_defs, $self->create_view($view,
201 { add_replace_view => $add_drop_table,
202 show_warnings => $show_warnings,
203 no_comments => $no_comments,
204 quote_table_names => $qt,
205 quote_field_names => $qf,
206 max_id_length => $max_id_length,
207 mysql_version => $mysql_version
208 });
209 }
210 }
211
212
94f42333 213 #warn "@table_defs\n";
24e6a53a 214 push @table_defs, "SET foreign_key_checks=1";
24e6a53a 215 return wantarray ? ($create ? $create : (), @create, @table_defs) : ($create . join('', map { $_ ? "$_;\n\n" : () } (@create, @table_defs)));
216 }
217
218 method create_view($view, $options) {
24e6a53a 219 my $qt = $options->{quote_table_names} || '';
220 my $qf = $options->{quote_field_names} || '';
221
222 my $view_name = $view->name;
223 #debug("PKG: Looking at view '${view_name}'\n");
224
225 # Header. Should this look like what mysqldump produces?
226 my $create = '';
227 $create .= "--\n-- View: ${qt}${view_name}${qt}\n--\n" unless $options->{no_comments};
228 $create .= 'CREATE';
229 $create .= ' OR REPLACE' if $options->{add_replace_view};
230 $create .= "\n";
231
232 my $extra = $view->extra;
233 # ALGORITHM
234 if( exists($extra->{mysql_algorithm}) && defined(my $algorithm = $extra->{mysql_algorithm}) ){
235 $create .= " ALGORITHM = ${algorithm}\n" if $algorithm =~ /(?:UNDEFINED|MERGE|TEMPTABLE)/i;
236 }
237 # DEFINER
238 if( exists($extra->{mysql_definer}) && defined(my $user = $extra->{mysql_definer}) ){
239 $create .= " DEFINER = ${user}\n";
240 }
241 # SECURITY
242 if( exists($extra->{mysql_security}) && defined(my $security = $extra->{mysql_security}) ){
243 $create .= " SQL SECURITY ${security}\n" if $security =~ /(?:DEFINER|INVOKER)/i;
244 }
245
246 #Header, cont.
247 $create .= " VIEW ${qt}${view_name}${qt}";
248
249 if( my @fields = $view->fields ){
250 my $list = join ', ', map { "${qf}${_}${qf}"} @fields;
251 $create .= " ( ${list} )";
252 }
253 if( my $sql = $view->sql ){
254 $create .= " AS (\n ${sql}\n )";
255 }
24e6a53a 256 return $create;
257 }
258
259 method create_table($table, $options) {
24e6a53a 260 my $qt = $options->{quote_table_names} || '';
261 my $qf = $options->{quote_field_names} || '';
262
263 my $table_name = $self->quote_table_name($table->name, $qt);
264 #debug("PKG: Looking at table '$table_name'\n");
265
266 #
267 # Header. Should this look like what mysqldump produces?
268 #
269 my $create = '';
270 my $drop;
271 $create .= "--\n-- Table: $table_name\n--\n" unless $options->{no_comments};
272 $drop = qq[DROP TABLE IF EXISTS $table_name] if $options->{add_drop_table};
273 $create .= "CREATE TABLE $table_name (\n";
274
275 #
276 # Fields
277 #
278 my @field_defs;
279 for my $field ( $table->get_fields ) {
280 push @field_defs, $self->create_field($field, $options);
281 }
282
283 #
284 # Indices
285 #
286 my @index_defs;
287 my %indexed_fields;
288 for my $index ( $table->get_indices ) {
289 push @index_defs, $self->create_index($index, $options);
290 $indexed_fields{ $_ } = 1 for $index->fields;
291 }
292
293 #
294 # Constraints -- need to handle more than just FK. -ky
295 #
296 my @constraint_defs;
297 my @constraints = $table->get_constraints;
298 for my $c ( @constraints ) {
94f42333 299 my $constr = $self->create_constraint($c, $options);
300 push @constraint_defs, $constr if($constr); #use Data::Dumper; warn Dumper($c->columns) if $constr =~ /^CONSTRAINT/; # unless $c->fields;
24e6a53a 301 next unless $c->fields;
94f42333 302
24e6a53a 303 unless ( $indexed_fields{ ($c->fields())[0] } || $c->type ne FOREIGN_KEY ) {
304 push @index_defs, "INDEX ($qf" . ($c->fields())[0] . "$qf)";
305 $indexed_fields{ ($c->fields())[0] } = 1;
306 }
307 }
94f42333 308
24e6a53a 309 $create .= join(",\n", map { " $_" }
310 @field_defs, @index_defs, @constraint_defs
311 );
94f42333 312
24e6a53a 313 #
314 # Footer
315 #
316 $create .= "\n)";
317 $create .= $self->generate_table_options($table, $options) || '';
318 # $create .= ";\n\n";
319
320 return $drop ? ($drop,$create) : $create;
321 }
322
323 method quote_table_name(Str $table_name, Str $qt) {
324 $table_name =~ s/\./$qt.$qt/g;
325
326 return "$qt$table_name$qt";
327 }
328
329 method generate_table_options(Table $table, $options?) {
330 my $create;
331
332 my $table_type_defined = 0;
333 my $qf = $options->{quote_field_names} ||= '';
334 my $charset = $table->extra->{'mysql_charset'};
335 my $collate = $table->extra->{'mysql_collate'};
336 my $union = undef;
94f42333 337
5590bc43 338 for my $t1_option_ref ($table->options) {
24e6a53a 339 my($key, $value) = %{$t1_option_ref};
340 $table_type_defined = 1
341 if uc $key eq 'ENGINE' or uc $key eq 'TYPE';
342 if (uc $key eq 'CHARACTER SET') {
343 $charset = $value;
344 next;
345 } elsif (uc $key eq 'COLLATE') {
346 $collate = $value;
347 next;
348 } elsif (uc $key eq 'UNION') {
349 $union = "($qf". join("$qf, $qf", @$value) ."$qf)";
350 next;
351 }
352 $create .= " $key=$value";
353 }
354
355 my $mysql_table_type = $table->extra->{'mysql_table_type'};
356 $create .= " ENGINE=$mysql_table_type"
357 if $mysql_table_type && !$table_type_defined;
358 my $comments = $table->comments;
359
360 $create .= " DEFAULT CHARACTER SET $charset" if $charset;
361 $create .= " COLLATE $collate" if $collate;
362 $create .= " UNION=$union" if $union;
363 $create .= qq[ comment='$comments'] if $comments;
364 return $create;
365 }
366
367 method create_field($field, $options?) {
368 my $qf = $options->{quote_field_names} ||= '';
369
370 my $field_name = $field->name;
371 #debug("PKG: Looking at field '$field_name'\n");
372 my $field_def = "$qf$field_name$qf";
373
374 # data type and size
375 my $data_type = $field->data_type;
376 my @size = $field->size;
377 my %extra = $field->extra;
378 my $list = $extra{'list'} || [];
379 # \todo deal with embedded quotes
380 my $commalist = join( ', ', map { qq['$_'] } @$list );
381 my $charset = $extra{'mysql_charset'};
382 my $collate = $extra{'mysql_collate'};
383
384 my $mysql_version = $options->{mysql_version} || 0;
385 #
386 # Oracle "number" type -- figure best MySQL type
387 #
388 if ( lc $data_type eq 'number' ) {
389 # not an integer
390 if ( scalar @size > 1 ) {
391 $data_type = 'double';
392 }
393 elsif ( $size[0] && $size[0] >= 12 ) {
394 $data_type = 'bigint';
395 }
396 elsif ( $size[0] && $size[0] <= 1 ) {
397 $data_type = 'tinyint';
398 }
399 else {
400 $data_type = 'int';
401 }
402 }
403 #
404 # Convert a large Oracle varchar to "text"
405 # (not necessary as of 5.0.3 http://dev.mysql.com/doc/refman/5.0/en/char.html)
406 #
407 elsif ( $data_type =~ /char/i && $size[0] > 255 ) {
408 unless ($size[0] <= 65535 && $mysql_version >= 5.000003 ) {
409 $data_type = 'text';
410 @size = ();
411 }
412 }
413 elsif ( $data_type =~ /boolean/i ) {
414 if ($mysql_version >= 4) {
415 $data_type = 'boolean';
416 } else {
417 $data_type = 'enum';
418 $commalist = "'0','1'";
419 }
420 }
421# elsif ( exists $translate{ lc $data_type } ) {
422# $data_type = $translate{ lc $data_type };
423# }
424
425 @size = () if $data_type =~ /(text|blob)/i;
426
427 if ( $data_type =~ /(double|float)/ && scalar @size == 1 ) {
428 push @size, '0';
429 }
430
431 $field_def .= " $data_type";
432
433 if ( lc($data_type) eq 'enum' || lc($data_type) eq 'set') {
434 $field_def .= '(' . $commalist . ')';
435 }
436 elsif (
437 defined $size[0] && $size[0] > 0
438 &&
439 ! grep lc($data_type) eq $_, @no_length_attr
440 ) {
441 $field_def .= '(' . join( ', ', @size ) . ')';
442 }
443
444 # char sets
445 $field_def .= " CHARACTER SET $charset" if $charset;
446 $field_def .= " COLLATE $collate" if $collate;
447
448 # MySQL qualifiers
449 for my $qual ( qw[ binary unsigned zerofill ] ) {
450 my $val = $extra{ $qual } || $extra{ uc $qual } or next;
451 $field_def .= " $qual";
452 }
453 for my $qual ( 'character set', 'collate', 'on update' ) {
454 my $val = $extra{ $qual } || $extra{ uc $qual } or next;
455 $field_def .= " $qual $val";
456 }
457
458 # Null?
459 $field_def .= ' NOT NULL' unless $field->is_nullable;
460
461 # Default? XXX Need better quoting!
462 my $default = $field->default_value;
e20b43c1 463
464# if ( defined $default ) {
465# SQL::Translator::Producer->_apply_default_value(
466# \$field_def,
467# $default,
468# [
469# 'NULL' => \'NULL',
470# ],
471# );
472# }
24e6a53a 473
474 if ( my $comments = $field->comments ) {
475 $field_def .= qq[ comment '$comments'];
476 }
477
478 # auto_increment?
479 $field_def .= " auto_increment" if $field->is_auto_increment;
480
481 return $field_def;
482 }
483
484 method alter_create_index(Index $index, $options?) {
485 my $qt = $options->{quote_table_names} || '';
486 my $qf = $options->{quote_field_names} || '';
487
488 return join( ' ',
489 'ALTER TABLE',
490 $qt.$index->table->name.$qt,
491 'ADD',
492 create_index(@_)
493 );
494 }
495
496 method create_index($index, $options?) {
497# my ( $index, $options ) = @_;
498
499 my $qf = $options->{quote_field_names} || '';
500
501 return join(
502 ' ',
503 map { $_ || () }
504 lc $index->type eq 'normal' ? 'INDEX' : $index->type . ' INDEX',
505 $index->name
506 ? (
507 $self->truncate_id_uniquely(
508 $index->name,
509 $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH
510 )
511 )
512 : '',
513 '(' . $qf . join( "$qf, $qf", $index->fields ) . $qf . ')'
514 );
515 }
516
517 method alter_drop_index(Index $index, $options?) {
518 my $qt = $options->{quote_table_names} || '';
519 my $qf = $options->{quote_field_names} || '';
520
521 return join( ' ',
522 'ALTER TABLE',
523 $qt.$index->table->name.$qt,
524 'DROP',
525 'INDEX',
526 $index->name || $index->fields
527 );
528
529 }
530
531 method alter_drop_constraint(Constraint $c, $options?) {
532 my $qt = $options->{quote_table_names} || '';
533 my $qc = $options->{quote_field_names} || '';
534
535 my $out = sprintf('ALTER TABLE %s DROP %s %s',
536 $qt . $c->table->name . $qt,
537 $c->type eq FOREIGN_KEY ? $c->type : "INDEX",
538 $qc . $c->name . $qc );
539
540 return $out;
541 }
542
543 method alter_create_constraint($index, $options?) {
544 my $qt = $options->{quote_table_names} || '';
545 return join( ' ',
546 'ALTER TABLE',
547 $qt.$index->table->name.$qt,
548 'ADD',
549 $self->create_constraint(@_) );
550 }
551
552 method create_constraint(Constraint $c, $options?) {
553 my $qf = $options->{quote_field_names} || '';
554 my $qt = $options->{quote_table_names} || '';
555 my $leave_name = $options->{leave_name} || undef;
556
557 my @fields = $c->fields or return;
558
559 if ( $c->type eq PRIMARY_KEY ) {
560 return 'PRIMARY KEY (' . $qf . join("$qf, $qf", @fields). $qf . ')';
561 }
562 elsif ( $c->type eq UNIQUE ) {
563 return
564 'UNIQUE '.
565 (defined $c->name ? $qf.$self->truncate_id_uniquely( $c->name, $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH ).$qf.' ' : '').
566 '(' . $qf . join("$qf, $qf", @fields). $qf . ')';
567 }
568 elsif ( $c->type eq FOREIGN_KEY ) {
569 #
570 # Make sure FK field is indexed or MySQL complains.
571 #
24e6a53a 572 my $table = $c->table;
573 my $c_name = $self->truncate_id_uniquely( $c->name, $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH );
574
575 my $def = join(' ',
576 map { $_ || () }
577 'CONSTRAINT',
578 $qf . $c_name . $qf,
579 'FOREIGN KEY'
580 );
581
582
583 $def .= ' ('.$qf . join( "$qf, $qf", @fields ) . $qf . ')';
94f42333 584
24e6a53a 585 $def .= ' REFERENCES ' . $qt . $c->reference_table . $qt;
24e6a53a 586 my @rfields = map { $_ || () } $c->reference_fields;
94f42333 587
24e6a53a 588 unless ( @rfields ) {
589 my $rtable_name = $c->reference_table;
590 if ( my $ref_table = $table->schema->get_table( $rtable_name ) ) {
591 push @rfields, $ref_table->primary_key;
592 }
593 else {
594 warn "Can't find reference table '$rtable_name' " .
595 "in schema\n" if $options->{show_warnings};
596 }
597 }
598
599 if ( @rfields ) {
600 $def .= ' (' . $qf . join( "$qf, $qf", @rfields ) . $qf . ')';
601 }
602 else {
603 warn "FK constraint on " . $table->name . '.' .
604 join('', @fields) . " has no reference fields\n"
605 if $options->{show_warnings};
606 }
607
608 if ( $c->match_type ) {
94f42333 609 $def .= ' MATCH ';
610 $def .= ( $c->match_type =~ /full/i ) ? 'FULL' : 'PARTIAL';
24e6a53a 611 }
94f42333 612# if ( $c->on_delete ) {
613# $def .= ' ON DELETE '.join( ' ', $c->on_delete );
614# }
24e6a53a 615
94f42333 616# if ( $c->on_update ) {
617# $def .= ' ON UPDATE '.join( ' ', $c->on_update );
618# }
24e6a53a 619 return $def;
620 }
621
622 return undef;
623 }
624
625 method alter_table(Str $to_table, $options?) {
626 my $qt = $options->{quote_table_names} || '';
627
628 my $table_options = $self->generate_table_options($to_table, $options) || '';
629 my $out = sprintf('ALTER TABLE %s%s',
630 $qt . $to_table->name . $qt,
631 $table_options);
632
633 return $out;
634 }
635
636 method rename_field(@args) { alter_field(@args) }
637 method alter_field(Str $from_field, Str $to_field, $options?) {
638 my $qf = $options->{quote_field_names} || '';
639 my $qt = $options->{quote_table_names} || '';
640
641 my $out = sprintf('ALTER TABLE %s CHANGE COLUMN %s %s',
642 $qt . $to_field->table->name . $qt,
643 $qf . $from_field->name . $qf,
644 $self->create_field($to_field, $options));
645
646 return $out;
647 }
648
649 method add_field(Str $new_field, $options?) {
650 my $qt = $options->{quote_table_names} || '';
651
652 my $out = sprintf('ALTER TABLE %s ADD COLUMN %s',
653 $qt . $new_field->table->name . $qt,
654 create_field($new_field, $options));
655
656 return $out;
657
658 }
659
660 method drop_field(Str $old_field, $options?) {
661 my $qf = $options->{quote_field_names} || '';
662 my $qt = $options->{quote_table_names} || '';
663
664 my $out = sprintf('ALTER TABLE %s DROP COLUMN %s',
665 $qt . $old_field->table->name . $qt,
666 $qf . $old_field->name . $qf);
667
668 return $out;
669
670 }
671
672 method batch_alter_table($table, $diff_hash, $options?) {
673 # InnoDB has an issue with dropping and re-adding a FK constraint under the
674 # name in a single alter statment, see: http://bugs.mysql.com/bug.php?id=13741
675 #
676 # We have to work round this.
677
678 my %fks_to_alter;
679 my %fks_to_drop = map {
680 $_->type eq FOREIGN_KEY
681 ? ( $_->name => $_ )
682 : ( )
683 } @{$diff_hash->{alter_drop_constraint} };
684
685 my %fks_to_create = map {
686 if ( $_->type eq FOREIGN_KEY) {
687 $fks_to_alter{$_->name} = $fks_to_drop{$_->name} if $fks_to_drop{$_->name};
688 ( $_->name => $_ );
689 } else { ( ) }
690 } @{$diff_hash->{alter_create_constraint} };
691
692 my @drop_stmt;
693 if (scalar keys %fks_to_alter) {
694 $diff_hash->{alter_drop_constraint} = [
695 grep { !$fks_to_alter{$_->name} } @{ $diff_hash->{alter_drop_constraint} }
696 ];
697
698 @drop_stmt = batch_alter_table($table, { alter_drop_constraint => [ values %fks_to_alter ] }, $options);
699
700 }
701
702 my @stmts = map {
703 if (@{ $diff_hash->{$_} || [] }) {
704 my $meth = __PACKAGE__->can($_) or die __PACKAGE__ . " cant $_";
705 map { $meth->( (ref $_ eq 'ARRAY' ? @$_ : $_), $options ) } @{ $diff_hash->{$_} }
706 } else { () }
707 } qw/rename_table
708 alter_drop_constraint
709 alter_drop_index
710 drop_field
711 add_field
712 alter_field
713 rename_field
714 alter_create_index
715 alter_create_constraint
716 alter_table/;
717
718 # rename_table makes things a bit more complex
719 my $renamed_from = "";
720 $renamed_from = $diff_hash->{rename_table}[0][0]->name
721 if $diff_hash->{rename_table} && @{$diff_hash->{rename_table}};
722
723 return unless @stmts;
724 # Just zero or one stmts. return now
725 return (@drop_stmt,@stmts) unless @stmts > 1;
726
727 # Now strip off the 'ALTER TABLE xyz' of all but the first one
728
729 my $qt = $options->{quote_table_names} || '';
730 my $table_name = $qt . $table->name . $qt;
731
732
733 my $re = $renamed_from
734 ? qr/^ALTER TABLE (?:\Q$table_name\E|\Q$qt$renamed_from$qt\E) /
735 : qr/^ALTER TABLE \Q$table_name\E /;
736
737 my $first = shift @stmts;
738 my ($alter_table) = $first =~ /($re)/;
739
740 my $padd = " " x length($alter_table);
741
742 return @drop_stmt, join( ",\n", $first, map { s/$re//; $padd . $_ } @stmts);
743
744 }
745
746 method drop_table(Str $table, $options?) {
747 my $qt = $options->{quote_table_names} || '';
748
749 # Drop (foreign key) constraints so table drops cleanly
750 my @sql = batch_alter_table($table, { alter_drop_constraint => [ grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints ] }, $options);
751
752 return (@sql, "DROP TABLE $qt$table$qt");
753 # return join("\n", @sql, "DROP TABLE $qt$table$qt");
754
755 }
756
757 method rename_table(Str $old_table, Str $new_table, $options?) {
758 my $qt = $options->{quote_table_names} || '';
759
760 return "ALTER TABLE $qt$old_table$qt RENAME TO $qt$new_table$qt";
761 }
762
763 method next_unused_name($name?) {
764# my $name = shift || '';
765 if ( !defined($used_names{$name}) ) {
766 $used_names{$name} = $name;
767 return $name;
768 }
769
770 my $i = 1;
771 while ( defined($used_names{$name . '_' . $i}) ) {
772 ++$i;
773 }
774 $name .= '_' . $i;
775 $used_names{$name} = $name;
776 return $name;
777 }
778
779 method header_comment($producer?, $comment_char?) {
780 $producer ||= caller;
781 my $now = scalar localtime;
782 my $DEFAULT_COMMENT = '-- ';
783
784 $comment_char = $DEFAULT_COMMENT
785 unless defined $comment_char;
786
787 my $header_comment =<<"HEADER_COMMENT";
788 ${comment_char}
789 ${comment_char}Created by $producer
790 ${comment_char}Created on $now
791 ${comment_char}
792HEADER_COMMENT
793
794 # Any additional stuff passed in
795 for my $additional_comment (@_) {
796 $header_comment .= "${comment_char}${additional_comment}\n";
797 }
798
799 return $header_comment;
800 }
801
f48e6f55 802 use constant COLLISION_TAG_LENGTH => 8;
24e6a53a 803
f48e6f55 804 method truncate_id_uniquely(Str $desired_name, Int $max_symbol_length) {
f77b076a 805 use Digest::SHA1 qw(sha1_hex);
f48e6f55 806 return $desired_name
807 unless defined $desired_name && length $desired_name > $max_symbol_length;
24e6a53a 808
f48e6f55 809 my $truncated_name = substr $desired_name, 0,
810 $max_symbol_length - COLLISION_TAG_LENGTH - 1;
24e6a53a 811
f48e6f55 812 # Hex isn't the most space-efficient, but it skirts around allowed
813 # charset issues
814 my $digest = sha1_hex($desired_name);
815 my $collision_tag = substr $digest, 0, COLLISION_TAG_LENGTH;
24e6a53a 816
f48e6f55 817 return $truncated_name
818 . '_'
819 . $collision_tag;
24e6a53a 820 }
24e6a53a 821}