Remove copyright headers from individual scripts
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / MySQL.pm
CommitLineData
9398955f 1package SQL::Translator::Producer::MySQL;
2
c855a748 3=head1 NAME
4
5SQL::Translator::Producer::MySQL - MySQL-specific producer for SQL::Translator
6
7=head1 SYNOPSIS
8
9Use via SQL::Translator:
10
11 use SQL::Translator;
12
13 my $t = SQL::Translator->new( parser => '...', producer => 'MySQL', '...' );
14 $t->translate;
15
16=head1 DESCRIPTION
17
18This module will produce text output of the schema suitable for MySQL.
19There are still some issues to be worked out with syntax differences
20between MySQL versions 3 and 4 ("SET foreign_key_checks," character sets
21for fields, etc.).
22
5d666b31 23=head1 ARGUMENTS
24
25This producer takes a single optional producer_arg C<mysql_version>, which
26provides the desired version for the target database. By default MySQL v3 is
27assumed, and statements pertaining to any features introduced in later versions
28(e.g. CREATE VIEW) are not produced.
29
94929d12 30Valid version specifiers for C<mysql_version> are listed L<here|SQL::Translator::Utils/parse_mysql_version>
5d666b31 31
5a0c7b43 32=head2 Table Types
33
34Normally the tables will be created without any explicit table type given and
35so will use the MySQL default.
36
37Any tables involved in foreign key constraints automatically get a table type
38of InnoDB, unless this is overridden by setting the C<mysql_table_type> extra
39attribute explicitly on the table.
40
41=head2 Extra attributes.
42
43The producer recognises the following extra attributes on the Schema objects.
44
45=over 4
46
7467c458 47=item B<field.list>
5a0c7b43 48
49Set the list of allowed values for Enum fields.
50
7467c458 51=item B<field.binary>, B<field.unsigned>, B<field.zerofill>
5a0c7b43 52
53Set the MySQL field options of the same name.
54
7467c458 55=item B<field.renamed_from>, B<table.renamed_from>
4d438549 56
7467c458 57Use when producing diffs to indicate that the current table/field has been
58renamed from the old name as given in the attribute value.
4d438549 59
7467c458 60=item B<table.mysql_table_type>
5a0c7b43 61
62Set the type of the table e.g. 'InnoDB', 'MyISAM'. This will be
63automatically set for tables involved in foreign key constraints if it is
64not already set explicitly. See L<"Table Types">.
65
10f70490 66Please note that the C<ENGINE> option is the preferred method of specifying
7467c458 67the MySQL storage engine to use, but this method still works for backwards
10f70490 68compatibility.
7467c458 69
70=item B<table.mysql_charset>, B<table.mysql_collate>
1ded8513 71
72Set the tables default charater set and collation order.
73
7467c458 74=item B<field.mysql_charset>, B<field.mysql_collate>
1ded8513 75
76Set the fields charater set and collation order.
77
5a0c7b43 78=back
79
c855a748 80=cut
81
9398955f 82use strict;
cd0ea0fd 83use warnings;
da06ac74 84use vars qw[ $VERSION $DEBUG %used_names ];
11ad2df9 85$VERSION = '1.59';
5636ed00 86$DEBUG = 0 unless defined $DEBUG;
9398955f 87
f5405d47 88# Maximum length for most identifiers is 64, according to:
89# http://dev.mysql.com/doc/refman/4.1/en/identifiers.html
90# http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
91my $DEFAULT_MAX_ID_LENGTH = 64;
92
9398955f 93use Data::Dumper;
1c14e9f1 94use SQL::Translator::Schema::Constants;
edc4b5da 95use SQL::Translator::Utils qw(debug header_comment
96 truncate_id_uniquely parse_mysql_version);
9398955f 97
d2344c83 98#
99# Use only lowercase for the keys (e.g. "long" and not "LONG")
100#
2620fc1c 101my %translate = (
102 #
103 # Oracle types
104 #
105 varchar2 => 'varchar',
106 long => 'text',
d2344c83 107 clob => 'longtext',
2620fc1c 108
109 #
110 # Sybase types
111 #
112 int => 'integer',
113 money => 'float',
114 real => 'double',
115 comment => 'text',
116 bit => 'tinyint',
87779799 117
118 #
119 # Access types
120 #
121 'long integer' => 'integer',
122 'text' => 'text',
123 'datetime' => 'datetime',
08dd6593 124
125 #
126 # PostgreSQL types
127 #
128 bytea => 'BLOB',
2620fc1c 129);
130
de176728 131#
132# Column types that do not support lenth attribute
133#
134my @no_length_attr = qw/
135 date time timestamp datetime year
136 /;
137
9ab59f87 138
139sub preprocess_schema {
934e1b39 140 my ($schema) = @_;
9ab59f87 141
142 # extra->{mysql_table_type} used to be the type. It belongs in options, so
143 # move it if we find it. Return Engine type if found in extra or options
7725e1e6 144 # Similarly for mysql_charset and mysql_collate
145 my $extra_to_options = sub {
146 my ($table, $extra_name, $opt_name) = @_;
9ab59f87 147
148 my $extra = $table->extra;
149
7725e1e6 150 my $extra_type = delete $extra->{$extra_name};
9ab59f87 151
152 # Now just to find if there is already an Engine or Type option...
153 # and lets normalize it to ENGINE since:
154 #
155 # The ENGINE table option specifies the storage engine for the table.
156 # TYPE is a synonym, but ENGINE is the preferred option name.
157 #
158
159 # We have to use the hash directly here since otherwise there is no way
160 # to remove options.
161 my $options = ( $table->{options} ||= []);
162
7725e1e6 163 # If multiple option names, normalize to the first one
164 if (ref $opt_name) {
165 OPT_NAME: for ( @$opt_name[1..$#$opt_name] ) {
166 for my $idx ( 0..$#{$options} ) {
167 my ($key, $value) = %{ $options->[$idx] };
168
169 if (uc $key eq $_) {
170 $options->[$idx] = { $opt_name->[0] => $value };
171 last OPT_NAME;
172 }
173 }
174 }
175 $opt_name = $opt_name->[0];
176
177 }
178
179
9ab59f87 180 # This assumes that there isn't both a Type and an Engine option.
7725e1e6 181 OPTION:
9ab59f87 182 for my $idx ( 0..$#{$options} ) {
183 my ($key, $value) = %{ $options->[$idx] };
184
7725e1e6 185 next unless uc $key eq $opt_name;
186
187 # make sure case is right on option name
9ab59f87 188 delete $options->[$idx]{$key};
7725e1e6 189 return $options->[$idx]{$opt_name} = $value || $extra_type;
9ab59f87 190
191 }
192
193 if ($extra_type) {
7725e1e6 194 push @$options, { $opt_name => $extra_type };
9ab59f87 195 return $extra_type;
196 }
197
198 };
199
1c680eb9 200 # Names are only specific to a given schema
201 local %used_names = ();
202
9ab59f87 203 #
204 # Work out which tables need to be InnoDB to support foreign key
205 # constraints. We do this first as we need InnoDB at both ends.
206 #
207 foreach my $table ( $schema->get_tables ) {
7725e1e6 208
209 $extra_to_options->($table, 'mysql_table_type', ['ENGINE', 'TYPE'] );
210 $extra_to_options->($table, 'mysql_charset', 'CHARACTER SET' );
211 $extra_to_options->($table, 'mysql_collate', 'COLLATE' );
9ab59f87 212
929ef265 213 foreach my $c ( $table->get_constraints ) {
1c680eb9 214 next unless $c->type eq FOREIGN_KEY;
215
216 # Normalize constraint names here.
217 my $c_name = $c->name;
218 # Give the constraint a name if it doesn't have one, so it doens't feel
219 # left out
220 $c_name = $table->name . '_fk' unless length $c_name;
221
222 $c->name( next_unused_name($c_name) );
223
9ab59f87 224 for my $meth (qw/table reference_table/) {
1c680eb9 225 my $table = $schema->get_table($c->$meth) || next;
7725e1e6 226 # This normalizes the types to ENGINE and returns the value if its there
227 next if $extra_to_options->($table, 'mysql_table_type', ['ENGINE', 'TYPE']);
9ab59f87 228 $table->options( { 'ENGINE' => 'InnoDB' } );
229 }
929ef265 230 } # foreach constraints
231
2c6be67a 232 my %map = ( mysql_collate => 'collate', mysql_charset => 'character set');
929ef265 233 foreach my $f ( $table->get_fields ) {
2c6be67a 234 my $extra = $f->extra;
235 for (keys %map) {
236 $extra->{$map{$_}} = delete $extra->{$_} if exists $extra->{$_};
237 }
238
929ef265 239 my @size = $f->size;
240 if ( !$size[0] && $f->data_type =~ /char$/ ) {
241 $f->size( (255) );
242 }
9ab59f87 243 }
929ef265 244
9ab59f87 245 }
246}
247
9398955f 248sub produce {
a1d94525 249 my $translator = shift;
250 local $DEBUG = $translator->debug;
da5a1bae 251 local %used_names;
a1d94525 252 my $no_comments = $translator->no_comments;
253 my $add_drop_table = $translator->add_drop_table;
254 my $schema = $translator->schema;
2bc23e82 255 my $show_warnings = $translator->show_warnings || 0;
ca1f9923 256 my $producer_args = $translator->producer_args;
5d666b31 257 my $mysql_version = parse_mysql_version ($producer_args->{mysql_version}, 'perl') || 0;
f5405d47 258 my $max_id_length = $producer_args->{mysql_max_id_length} || $DEFAULT_MAX_ID_LENGTH;
d529894e 259
7725e1e6 260 my ($qt, $qf, $qc) = ('','', '');
fe0f47d0 261 $qt = '`' if $translator->quote_table_names;
262 $qf = '`' if $translator->quote_field_names;
263
1a24938d 264 debug("PKG: Beginning production\n");
da5a1bae 265 %used_names = ();
24d9fe69 266 my $create = '';
5ee19df8 267 $create .= header_comment unless ($no_comments);
0823773d 268 # \todo Don't set if MySQL 3.x is set on command line
24d9fe69 269 my @create = "SET foreign_key_checks=0";
9398955f 270
934e1b39 271 preprocess_schema($schema);
5a0c7b43 272
273 #
274 # Generate sql
275 #
cd0ea0fd 276 my @table_defs =();
4d438549 277
1c14e9f1 278 for my $table ( $schema->get_tables ) {
cd0ea0fd 279# print $table->name, "\n";
0013ee25 280 push @table_defs, create_table($table,
fe0f47d0 281 { add_drop_table => $add_drop_table,
282 show_warnings => $show_warnings,
283 no_comments => $no_comments,
284 quote_table_names => $qt,
ca1f9923 285 quote_field_names => $qf,
f5405d47 286 max_id_length => $max_id_length,
ca1f9923 287 mysql_version => $mysql_version
0013ee25 288 });
289 }
9398955f 290
e30b71b8 291 if ($mysql_version >= 5.000001) {
83ddfeac 292 for my $view ( $schema->get_views ) {
d28afa66 293 push @table_defs, create_view($view,
294 { add_replace_view => $add_drop_table,
295 show_warnings => $show_warnings,
296 no_comments => $no_comments,
297 quote_table_names => $qt,
298 quote_field_names => $qf,
299 max_id_length => $max_id_length,
300 mysql_version => $mysql_version
301 });
83ddfeac 302 }
d28afa66 303 }
304
ee22632e 305 if ($mysql_version >= 5.000002) {
306 for my $trigger ( $schema->get_triggers ) {
307 push @table_defs, create_trigger($trigger,
308 { add_drop_trigger => $add_drop_table,
309 show_warnings => $show_warnings,
310 no_comments => $no_comments,
311 quote_table_names => $qt,
312 quote_field_names => $qf,
313 max_id_length => $max_id_length,
314 mysql_version => $mysql_version
315 });
316 }
317 }
318
d28afa66 319
cd0ea0fd 320# print "@table_defs\n";
24d9fe69 321 push @table_defs, "SET foreign_key_checks=1";
cd0ea0fd 322
24d9fe69 323 return wantarray ? ($create ? $create : (), @create, @table_defs) : ($create . join('', map { $_ ? "$_;\n\n" : () } (@create, @table_defs)));
0013ee25 324}
9398955f 325
ee22632e 326sub create_trigger {
327 my ($trigger, $options) = @_;
328 my $qt = $options->{quote_table_names} || '';
329 my $qf = $options->{quote_field_names} || '';
330
331 my $trigger_name = $trigger->name;
332 debug("PKG: Looking at trigger '${trigger_name}'\n");
333
334 my @statements;
335
336 my $events = $trigger->database_events;
337 for my $event ( @$events ) {
338 my $name = $trigger_name;
339 if (@$events > 1) {
340 $name .= "_$event";
341
342 warn "Multiple database events supplied for trigger '${trigger_name}', ",
343 "creating trigger '${name}' for the '${event}' event\n"
344 if $options->{show_warnings};
345 }
346
347 my $action = $trigger->action;
348 $action .= ";" unless $action =~ /;\s*\z/;
349
350 push @statements, "DROP TRIGGER IF EXISTS ${qt}${name}${qt}" if $options->{add_drop_trigger};
351 push @statements, sprintf(
352 "CREATE TRIGGER ${qt}%s${qt} %s %s ON ${qt}%s${qt}\n FOR EACH ROW BEGIN %s END",
353 $name, $trigger->perform_action_when, $event, $trigger->on_table, $action,
354 );
355
356 }
357 # Tack the comment onto the first statement
358 $statements[0] = "--\n-- Trigger ${qt}${trigger_name}${qt}\n--\n" . $statements[0] unless $options->{no_comments};
359 return @statements;
360}
361
d28afa66 362sub create_view {
363 my ($view, $options) = @_;
364 my $qt = $options->{quote_table_names} || '';
365 my $qf = $options->{quote_field_names} || '';
366
367 my $view_name = $view->name;
368 debug("PKG: Looking at view '${view_name}'\n");
369
370 # Header. Should this look like what mysqldump produces?
371 my $create = '';
372 $create .= "--\n-- View: ${qt}${view_name}${qt}\n--\n" unless $options->{no_comments};
373 $create .= 'CREATE';
374 $create .= ' OR REPLACE' if $options->{add_replace_view};
375 $create .= "\n";
376
377 my $extra = $view->extra;
378 # ALGORITHM
379 if( exists($extra->{mysql_algorithm}) && defined(my $algorithm = $extra->{mysql_algorithm}) ){
380 $create .= " ALGORITHM = ${algorithm}\n" if $algorithm =~ /(?:UNDEFINED|MERGE|TEMPTABLE)/i;
381 }
382 # DEFINER
383 if( exists($extra->{mysql_definer}) && defined(my $user = $extra->{mysql_definer}) ){
384 $create .= " DEFINER = ${user}\n";
385 }
386 # SECURITY
387 if( exists($extra->{mysql_security}) && defined(my $security = $extra->{mysql_security}) ){
388 $create .= " SQL SECURITY ${security}\n" if $security =~ /(?:DEFINER|INVOKER)/i;
389 }
390
391 #Header, cont.
392 $create .= " VIEW ${qt}${view_name}${qt}";
393
394 if( my @fields = $view->fields ){
395 my $list = join ', ', map { "${qf}${_}${qf}"} @fields;
396 $create .= " ( ${list} )";
397 }
398 if( my $sql = $view->sql ){
1c8ec56e 399 # do not wrap parenthesis around the selector, mysql doesn't like this
400 # http://bugs.mysql.com/bug.php?id=9198
401 $create .= " AS\n ${sql}\n";
d28afa66 402 }
24d9fe69 403# $create .= "";
d28afa66 404 return $create;
405}
406
0013ee25 407sub create_table
408{
409 my ($table, $options) = @_;
2620fc1c 410
fe0f47d0 411 my $qt = $options->{quote_table_names} || '';
412 my $qf = $options->{quote_field_names} || '';
413
8d693a85 414 my $table_name = quote_table_name($table->name, $qt);
0013ee25 415 debug("PKG: Looking at table '$table_name'\n");
6d3f6379 416
0013ee25 417 #
418 # Header. Should this look like what mysqldump produces?
419 #
cd0ea0fd 420 my $create = '';
fa94b25f 421 my $drop;
8d693a85 422 $create .= "--\n-- Table: $table_name\n--\n" unless $options->{no_comments};
423 $drop = qq[DROP TABLE IF EXISTS $table_name] if $options->{add_drop_table};
424 $create .= "CREATE TABLE $table_name (\n";
2b695517 425
0013ee25 426 #
427 # Fields
428 #
429 my @field_defs;
430 for my $field ( $table->get_fields ) {
fe0f47d0 431 push @field_defs, create_field($field, $options);
0013ee25 432 }
1ded8513 433
0013ee25 434 #
435 # Indices
436 #
437 my @index_defs;
438 my %indexed_fields;
439 for my $index ( $table->get_indices ) {
fe0f47d0 440 push @index_defs, create_index($index, $options);
0013ee25 441 $indexed_fields{ $_ } = 1 for $index->fields;
442 }
d529894e 443
0013ee25 444 #
445 # Constraints -- need to handle more than just FK. -ky
446 #
447 my @constraint_defs;
448 my @constraints = $table->get_constraints;
449 for my $c ( @constraints ) {
cd0ea0fd 450 my $constr = create_constraint($c, $options);
451 push @constraint_defs, $constr if($constr);
0013ee25 452
da5a1bae 453 unless ( $indexed_fields{ ($c->fields())[0] } || $c->type ne FOREIGN_KEY ) {
454 push @index_defs, "INDEX ($qf" . ($c->fields())[0] . "$qf)";
455 $indexed_fields{ ($c->fields())[0] } = 1;
456 }
0013ee25 457 }
1ded8513 458
0013ee25 459 $create .= join(",\n", map { " $_" }
460 @field_defs, @index_defs, @constraint_defs
461 );
9398955f 462
0013ee25 463 #
464 # Footer
465 #
466 $create .= "\n)";
9a96648f 467 $create .= generate_table_options($table, $options) || '';
24d9fe69 468# $create .= ";\n\n";
9398955f 469
fa94b25f 470 return $drop ? ($drop,$create) : $create;
0013ee25 471}
f8b6e804 472
8d693a85 473sub quote_table_name {
474 my ($table_name, $qt) = @_;
475
476 $table_name =~ s/\./$qt.$qt/g;
477
478 return "$qt$table_name$qt";
479}
480
da5a1bae 481sub generate_table_options
482{
9a96648f 483 my ($table, $options) = @_;
da5a1bae 484 my $create;
485
486 my $table_type_defined = 0;
9a96648f 487 my $qf = $options->{quote_field_names} ||= '';
7725e1e6 488 my $charset = $table->extra('mysql_charset');
489 my $collate = $table->extra('mysql_collate');
9a96648f 490 my $union = undef;
da5a1bae 491 for my $t1_option_ref ( $table->options ) {
492 my($key, $value) = %{$t1_option_ref};
493 $table_type_defined = 1
494 if uc $key eq 'ENGINE' or uc $key eq 'TYPE';
7725e1e6 495 if (uc $key eq 'CHARACTER SET') {
496 $charset = $value;
497 next;
498 } elsif (uc $key eq 'COLLATE') {
499 $collate = $value;
500 next;
9a96648f 501 } elsif (uc $key eq 'UNION') {
502 $union = "($qf". join("$qf, $qf", @$value) ."$qf)";
503 next;
7725e1e6 504 }
da5a1bae 505 $create .= " $key=$value";
506 }
9ab59f87 507
da5a1bae 508 my $mysql_table_type = $table->extra('mysql_table_type');
9ab59f87 509 $create .= " ENGINE=$mysql_table_type"
da5a1bae 510 if $mysql_table_type && !$table_type_defined;
da5a1bae 511 my $comments = $table->comments;
512
513 $create .= " DEFAULT CHARACTER SET $charset" if $charset;
514 $create .= " COLLATE $collate" if $collate;
9a96648f 515 $create .= " UNION=$union" if $union;
da5a1bae 516 $create .= qq[ comment='$comments'] if $comments;
517 return $create;
518}
519
0013ee25 520sub create_field
521{
fe0f47d0 522 my ($field, $options) = @_;
523
524 my $qf = $options->{quote_field_names} ||= '';
9398955f 525
0013ee25 526 my $field_name = $field->name;
527 debug("PKG: Looking at field '$field_name'\n");
fe0f47d0 528 my $field_def = "$qf$field_name$qf";
0013ee25 529
530 # data type and size
531 my $data_type = $field->data_type;
532 my @size = $field->size;
533 my %extra = $field->extra;
534 my $list = $extra{'list'} || [];
535 # \todo deal with embedded quotes
536 my $commalist = join( ', ', map { qq['$_'] } @$list );
537 my $charset = $extra{'mysql_charset'};
538 my $collate = $extra{'mysql_collate'};
539
5d666b31 540 my $mysql_version = $options->{mysql_version} || 0;
0013ee25 541 #
542 # Oracle "number" type -- figure best MySQL type
543 #
544 if ( lc $data_type eq 'number' ) {
545 # not an integer
546 if ( scalar @size > 1 ) {
547 $data_type = 'double';
d529894e 548 }
0013ee25 549 elsif ( $size[0] && $size[0] >= 12 ) {
550 $data_type = 'bigint';
551 }
552 elsif ( $size[0] && $size[0] <= 1 ) {
553 $data_type = 'tinyint';
554 }
555 else {
556 $data_type = 'int';
557 }
558 }
559 #
560 # Convert a large Oracle varchar to "text"
5d666b31 561 # (not necessary as of 5.0.3 http://dev.mysql.com/doc/refman/5.0/en/char.html)
0013ee25 562 #
563 elsif ( $data_type =~ /char/i && $size[0] > 255 ) {
5d666b31 564 unless ($size[0] <= 65535 && $mysql_version >= 5.000003 ) {
565 $data_type = 'text';
566 @size = ();
567 }
0013ee25 568 }
0013ee25 569 elsif ( $data_type =~ /boolean/i ) {
ca1f9923 570 if ($mysql_version >= 4) {
571 $data_type = 'boolean';
572 } else {
573 $data_type = 'enum';
574 $commalist = "'0','1'";
575 }
0013ee25 576 }
577 elsif ( exists $translate{ lc $data_type } ) {
578 $data_type = $translate{ lc $data_type };
579 }
580
581 @size = () if $data_type =~ /(text|blob)/i;
582
583 if ( $data_type =~ /(double|float)/ && scalar @size == 1 ) {
584 push @size, '0';
585 }
d529894e 586
0013ee25 587 $field_def .= " $data_type";
588
7c1aae02 589 if ( lc($data_type) eq 'enum' || lc($data_type) eq 'set') {
0013ee25 590 $field_def .= '(' . $commalist . ')';
7c1aae02 591 }
c857e37a 592 elsif (
593 defined $size[0] && $size[0] > 0
594 &&
595 ! grep lc($data_type) eq $_, @no_length_attr
596 ) {
0013ee25 597 $field_def .= '(' . join( ', ', @size ) . ')';
598 }
599
600 # char sets
601 $field_def .= " CHARACTER SET $charset" if $charset;
602 $field_def .= " COLLATE $collate" if $collate;
603
604 # MySQL qualifiers
605 for my $qual ( qw[ binary unsigned zerofill ] ) {
606 my $val = $extra{ $qual } || $extra{ uc $qual } or next;
607 $field_def .= " $qual";
608 }
609 for my $qual ( 'character set', 'collate', 'on update' ) {
610 my $val = $extra{ $qual } || $extra{ uc $qual } or next;
611 $field_def .= " $qual $val";
612 }
613
614 # Null?
615 $field_def .= ' NOT NULL' unless $field->is_nullable;
616
06baeb21 617 # Default?
618 SQL::Translator::Producer->_apply_default_value(
619 $field,
620 \$field_def,
621 [
622 'NULL' => \'NULL',
623 ],
624 );
0013ee25 625
626 if ( my $comments = $field->comments ) {
627 $field_def .= qq[ comment '$comments'];
628 }
629
630 # auto_increment?
631 $field_def .= " auto_increment" if $field->is_auto_increment;
632
633 return $field_def;
634}
635
da5a1bae 636sub alter_create_index
637{
638 my ($index, $options) = @_;
639
640 my $qt = $options->{quote_table_names} || '';
641 my $qf = $options->{quote_field_names} || '';
642
643 return join( ' ',
644 'ALTER TABLE',
645 $qt.$index->table->name.$qt,
646 'ADD',
647 create_index(@_)
648 );
649}
650
0013ee25 651sub create_index
652{
edc4b5da 653 my ( $index, $options ) = @_;
fe0f47d0 654
655 my $qf = $options->{quote_field_names} || '';
0013ee25 656
edc4b5da 657 return join(
658 ' ',
4ec6e692 659 map { $_ || () }
edc4b5da 660 lc $index->type eq 'normal' ? 'INDEX' : $index->type . ' INDEX',
661 $index->name
20476859 662 ? $qf . truncate_id_uniquely(
edc4b5da 663 $index->name,
664 $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH
20476859 665 ) . $qf
edc4b5da 666 : '',
667 '(' . $qf . join( "$qf, $qf", $index->fields ) . $qf . ')'
668 );
0013ee25 669}
670
da5a1bae 671sub alter_drop_index
672{
673 my ($index, $options) = @_;
674
675 my $qt = $options->{quote_table_names} || '';
676 my $qf = $options->{quote_field_names} || '';
677
678 return join( ' ',
679 'ALTER TABLE',
680 $qt.$index->table->name.$qt,
681 'DROP',
682 'INDEX',
683 $index->name || $index->fields
684 );
685
686}
687
688sub alter_drop_constraint
689{
690 my ($c, $options) = @_;
691
692 my $qt = $options->{quote_table_names} || '';
7725e1e6 693 my $qc = $options->{quote_field_names} || '';
da5a1bae 694
695 my $out = sprintf('ALTER TABLE %s DROP %s %s',
7467c458 696 $qt . $c->table->name . $qt,
74ca32ce 697 $c->type eq FOREIGN_KEY ? $c->type : "INDEX",
da5a1bae 698 $qc . $c->name . $qc );
699
700 return $out;
701}
702
703sub alter_create_constraint
704{
705 my ($index, $options) = @_;
706
707 my $qt = $options->{quote_table_names} || '';
708 return join( ' ',
709 'ALTER TABLE',
710 $qt.$index->table->name.$qt,
711 'ADD',
712 create_constraint(@_) );
713}
714
0013ee25 715sub create_constraint
716{
717 my ($c, $options) = @_;
718
fb149f81 719 my $qf = $options->{quote_field_names} || '';
720 my $qt = $options->{quote_table_names} || '';
da5a1bae 721 my $leave_name = $options->{leave_name} || undef;
fe0f47d0 722
0013ee25 723 my @fields = $c->fields or next;
724
725 if ( $c->type eq PRIMARY_KEY ) {
fe0f47d0 726 return 'PRIMARY KEY (' . $qf . join("$qf, $qf", @fields). $qf . ')';
0013ee25 727 }
728 elsif ( $c->type eq UNIQUE ) {
729 return
fe0f47d0 730 'UNIQUE '.
f5405d47 731 (defined $c->name ? $qf.truncate_id_uniquely( $c->name, $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH ).$qf.' ' : '').
fe0f47d0 732 '(' . $qf . join("$qf, $qf", @fields). $qf . ')';
0013ee25 733 }
734 elsif ( $c->type eq FOREIGN_KEY ) {
d529894e 735 #
0013ee25 736 # Make sure FK field is indexed or MySQL complains.
5e56da9a 737 #
0013ee25 738
866d012e 739 my $table = $c->table;
f5405d47 740 my $c_name = truncate_id_uniquely( $c->name, $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH );
866d012e 741
0013ee25 742 my $def = join(' ',
fb149f81 743 map { $_ || () }
744 'CONSTRAINT',
7725e1e6 745 $qf . $c_name . $qf,
fb149f81 746 'FOREIGN KEY'
747 );
0013ee25 748
da5a1bae 749
fe0f47d0 750 $def .= ' ('.$qf . join( "$qf, $qf", @fields ) . $qf . ')';
0013ee25 751
fe0f47d0 752 $def .= ' REFERENCES ' . $qt . $c->reference_table . $qt;
0013ee25 753
754 my @rfields = map { $_ || () } $c->reference_fields;
755 unless ( @rfields ) {
756 my $rtable_name = $c->reference_table;
866d012e 757 if ( my $ref_table = $table->schema->get_table( $rtable_name ) ) {
0013ee25 758 push @rfields, $ref_table->primary_key;
1c14e9f1 759 }
0013ee25 760 else {
761 warn "Can't find reference table '$rtable_name' " .
762 "in schema\n" if $options->{show_warnings};
5e56da9a 763 }
764 }
765
0013ee25 766 if ( @rfields ) {
fe0f47d0 767 $def .= ' (' . $qf . join( "$qf, $qf", @rfields ) . $qf . ')';
0013ee25 768 }
769 else {
866d012e 770 warn "FK constraint on " . $table->name . '.' .
0013ee25 771 join('', @fields) . " has no reference fields\n"
772 if $options->{show_warnings};
773 }
5e56da9a 774
0013ee25 775 if ( $c->match_type ) {
776 $def .= ' MATCH ' .
777 ( $c->match_type =~ /full/i ) ? 'FULL' : 'PARTIAL';
778 }
779
780 if ( $c->on_delete ) {
781 $def .= ' ON DELETE '.join( ' ', $c->on_delete );
782 }
783
784 if ( $c->on_update ) {
785 $def .= ' ON UPDATE '.join( ' ', $c->on_update );
786 }
cd0ea0fd 787 return $def;
9398955f 788 }
789
cd0ea0fd 790 return undef;
0013ee25 791}
792
da5a1bae 793sub alter_table
794{
795 my ($to_table, $options) = @_;
796
7467c458 797 my $qt = $options->{quote_table_names} || '';
da5a1bae 798
9a96648f 799 my $table_options = generate_table_options($to_table, $options) || '';
da5a1bae 800 my $out = sprintf('ALTER TABLE %s%s',
801 $qt . $to_table->name . $qt,
802 $table_options);
803
804 return $out;
805}
806
4d438549 807sub rename_field { alter_field(@_) }
0013ee25 808sub alter_field
809{
fe0f47d0 810 my ($from_field, $to_field, $options) = @_;
811
7467c458 812 my $qf = $options->{quote_field_names} || '';
813 my $qt = $options->{quote_table_names} || '';
0013ee25 814
815 my $out = sprintf('ALTER TABLE %s CHANGE COLUMN %s %s',
fe0f47d0 816 $qt . $to_field->table->name . $qt,
4d438549 817 $qf . $from_field->name . $qf,
fe0f47d0 818 create_field($to_field, $options));
0013ee25 819
820 return $out;
821}
822
823sub add_field
824{
fe0f47d0 825 my ($new_field, $options) = @_;
826
7467c458 827 my $qt = $options->{quote_table_names} || '';
0013ee25 828
829 my $out = sprintf('ALTER TABLE %s ADD COLUMN %s',
fe0f47d0 830 $qt . $new_field->table->name . $qt,
831 create_field($new_field, $options));
0013ee25 832
833 return $out;
834
835}
836
837sub drop_field
838{
fe0f47d0 839 my ($old_field, $options) = @_;
0013ee25 840
7467c458 841 my $qf = $options->{quote_field_names} || '';
842 my $qt = $options->{quote_table_names} || '';
fe0f47d0 843
0013ee25 844 my $out = sprintf('ALTER TABLE %s DROP COLUMN %s',
fe0f47d0 845 $qt . $old_field->table->name . $qt,
846 $qf . $old_field->name . $qf);
0013ee25 847
848 return $out;
849
9398955f 850}
851
4d438549 852sub batch_alter_table {
853 my ($table, $diff_hash, $options) = @_;
854
f9ed5d54 855 # InnoDB has an issue with dropping and re-adding a FK constraint under the
856 # name in a single alter statment, see: http://bugs.mysql.com/bug.php?id=13741
857 #
858 # We have to work round this.
859
860 my %fks_to_alter;
861 my %fks_to_drop = map {
862 $_->type eq FOREIGN_KEY
863 ? ( $_->name => $_ )
864 : ( )
865 } @{$diff_hash->{alter_drop_constraint} };
866
867 my %fks_to_create = map {
868 if ( $_->type eq FOREIGN_KEY) {
869 $fks_to_alter{$_->name} = $fks_to_drop{$_->name} if $fks_to_drop{$_->name};
870 ( $_->name => $_ );
871 } else { ( ) }
872 } @{$diff_hash->{alter_create_constraint} };
873
24d9fe69 874 my @drop_stmt;
f9ed5d54 875 if (scalar keys %fks_to_alter) {
876 $diff_hash->{alter_drop_constraint} = [
877 grep { !$fks_to_alter{$_->name} } @{ $diff_hash->{alter_drop_constraint} }
878 ];
879
24d9fe69 880 @drop_stmt = batch_alter_table($table, { alter_drop_constraint => [ values %fks_to_alter ] }, $options);
f9ed5d54 881
882 }
883
4d438549 884 my @stmts = map {
885 if (@{ $diff_hash->{$_} || [] }) {
886 my $meth = __PACKAGE__->can($_) or die __PACKAGE__ . " cant $_";
f9ed5d54 887 map { $meth->( (ref $_ eq 'ARRAY' ? @$_ : $_), $options ) } @{ $diff_hash->{$_} }
4d438549 888 } else { () }
46bf5655 889 } qw/rename_table
890 alter_drop_constraint
4d438549 891 alter_drop_index
892 drop_field
893 add_field
894 alter_field
895 rename_field
896 alter_create_index
897 alter_create_constraint
898 alter_table/;
899
46bf5655 900 # rename_table makes things a bit more complex
901 my $renamed_from = "";
902 $renamed_from = $diff_hash->{rename_table}[0][0]->name
903 if $diff_hash->{rename_table} && @{$diff_hash->{rename_table}};
904
4d438549 905 return unless @stmts;
906 # Just zero or one stmts. return now
24d9fe69 907 return (@drop_stmt,@stmts) unless @stmts > 1;
4d438549 908
909 # Now strip off the 'ALTER TABLE xyz' of all but the first one
910
7467c458 911 my $qt = $options->{quote_table_names} || '';
4104f82b 912 my $table_name = $qt . $table->name . $qt;
913
914
915 my $re = $renamed_from
916 ? qr/^ALTER TABLE (?:\Q$table_name\E|\Q$qt$renamed_from$qt\E) /
917 : qr/^ALTER TABLE \Q$table_name\E /;
4d438549 918
919 my $first = shift @stmts;
4104f82b 920 my ($alter_table) = $first =~ /($re)/;
46bf5655 921
4d438549 922 my $padd = " " x length($alter_table);
923
24d9fe69 924 return @drop_stmt, join( ",\n", $first, map { s/$re//; $padd . $_ } @stmts);
4104f82b 925
4d438549 926}
927
928sub drop_table {
46bf5655 929 my ($table, $options) = @_;
930
931 my $qt = $options->{quote_table_names} || '';
4d438549 932
933 # Drop (foreign key) constraints so table drops cleanly
46bf5655 934 my @sql = batch_alter_table($table, { alter_drop_constraint => [ grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints ] }, $options);
935
24d9fe69 936 return (@sql, "DROP TABLE $qt$table$qt");
937# return join("\n", @sql, "DROP TABLE $qt$table$qt");
46bf5655 938
939}
940
941sub rename_table {
942 my ($old_table, $new_table, $options) = @_;
4d438549 943
46bf5655 944 my $qt = $options->{quote_table_names} || '';
4d438549 945
46bf5655 946 return "ALTER TABLE $qt$old_table$qt RENAME TO $qt$new_table$qt";
4d438549 947}
948
da5a1bae 949sub next_unused_name {
950 my $name = shift || '';
951 if ( !defined($used_names{$name}) ) {
952 $used_names{$name} = $name;
953 return $name;
954 }
955
956 my $i = 1;
957 while ( defined($used_names{$name . '_' . $i}) ) {
958 ++$i;
959 }
960 $name .= '_' . $i;
961 $used_names{$name} = $name;
962 return $name;
963}
964
9398955f 9651;
9398955f 966
c855a748 967# -------------------------------------------------------------------
9398955f 968
c855a748 969=pod
970
971=head1 SEE ALSO
972
973SQL::Translator, http://www.mysql.com/.
9398955f 974
2d6979da 975=head1 AUTHORS
9398955f 976
758ab1cd 977darren chamberlain E<lt>darren@cpan.orgE<gt>,
f997b9ab 978Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
c855a748 979
980=cut