Commit | Line | Data |
24e6a53a |
1 | use MooseX::Declare; |
2 | role 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 | } |
256 | # $create .= ""; |
257 | return $create; |
258 | } |
259 | |
260 | method create_table($table, $options) { |
261 | # my ($table, $options) = @_; |
262 | |
263 | my $qt = $options->{quote_table_names} || ''; |
264 | my $qf = $options->{quote_field_names} || ''; |
265 | |
266 | my $table_name = $self->quote_table_name($table->name, $qt); |
267 | #debug("PKG: Looking at table '$table_name'\n"); |
268 | |
269 | # |
270 | # Header. Should this look like what mysqldump produces? |
271 | # |
272 | my $create = ''; |
273 | my $drop; |
274 | $create .= "--\n-- Table: $table_name\n--\n" unless $options->{no_comments}; |
275 | $drop = qq[DROP TABLE IF EXISTS $table_name] if $options->{add_drop_table}; |
276 | $create .= "CREATE TABLE $table_name (\n"; |
277 | |
278 | # |
279 | # Fields |
280 | # |
281 | my @field_defs; |
282 | for my $field ( $table->get_fields ) { |
283 | push @field_defs, $self->create_field($field, $options); |
284 | } |
285 | |
286 | # |
287 | # Indices |
288 | # |
289 | my @index_defs; |
290 | my %indexed_fields; |
291 | for my $index ( $table->get_indices ) { |
292 | push @index_defs, $self->create_index($index, $options); |
293 | $indexed_fields{ $_ } = 1 for $index->fields; |
294 | } |
295 | |
296 | # |
297 | # Constraints -- need to handle more than just FK. -ky |
298 | # |
299 | my @constraint_defs; |
300 | my @constraints = $table->get_constraints; |
301 | for my $c ( @constraints ) { |
94f42333 |
302 | my $constr = $self->create_constraint($c, $options); |
303 | push @constraint_defs, $constr if($constr); #use Data::Dumper; warn Dumper($c->columns) if $constr =~ /^CONSTRAINT/; # unless $c->fields; |
24e6a53a |
304 | next unless $c->fields; |
94f42333 |
305 | |
24e6a53a |
306 | unless ( $indexed_fields{ ($c->fields())[0] } || $c->type ne FOREIGN_KEY ) { |
307 | push @index_defs, "INDEX ($qf" . ($c->fields())[0] . "$qf)"; |
308 | $indexed_fields{ ($c->fields())[0] } = 1; |
309 | } |
310 | } |
94f42333 |
311 | |
24e6a53a |
312 | $create .= join(",\n", map { " $_" } |
313 | @field_defs, @index_defs, @constraint_defs |
314 | ); |
94f42333 |
315 | |
24e6a53a |
316 | # |
317 | # Footer |
318 | # |
319 | $create .= "\n)"; |
320 | $create .= $self->generate_table_options($table, $options) || ''; |
321 | # $create .= ";\n\n"; |
322 | |
323 | return $drop ? ($drop,$create) : $create; |
324 | } |
325 | |
326 | method quote_table_name(Str $table_name, Str $qt) { |
327 | $table_name =~ s/\./$qt.$qt/g; |
328 | |
329 | return "$qt$table_name$qt"; |
330 | } |
331 | |
332 | method generate_table_options(Table $table, $options?) { |
333 | my $create; |
334 | |
335 | my $table_type_defined = 0; |
336 | my $qf = $options->{quote_field_names} ||= ''; |
337 | my $charset = $table->extra->{'mysql_charset'}; |
338 | my $collate = $table->extra->{'mysql_collate'}; |
339 | my $union = undef; |
94f42333 |
340 | |
5590bc43 |
341 | for my $t1_option_ref ($table->options) { |
24e6a53a |
342 | my($key, $value) = %{$t1_option_ref}; |
343 | $table_type_defined = 1 |
344 | if uc $key eq 'ENGINE' or uc $key eq 'TYPE'; |
345 | if (uc $key eq 'CHARACTER SET') { |
346 | $charset = $value; |
347 | next; |
348 | } elsif (uc $key eq 'COLLATE') { |
349 | $collate = $value; |
350 | next; |
351 | } elsif (uc $key eq 'UNION') { |
352 | $union = "($qf". join("$qf, $qf", @$value) ."$qf)"; |
353 | next; |
354 | } |
355 | $create .= " $key=$value"; |
356 | } |
357 | |
358 | my $mysql_table_type = $table->extra->{'mysql_table_type'}; |
359 | $create .= " ENGINE=$mysql_table_type" |
360 | if $mysql_table_type && !$table_type_defined; |
361 | my $comments = $table->comments; |
362 | |
363 | $create .= " DEFAULT CHARACTER SET $charset" if $charset; |
364 | $create .= " COLLATE $collate" if $collate; |
365 | $create .= " UNION=$union" if $union; |
366 | $create .= qq[ comment='$comments'] if $comments; |
367 | return $create; |
368 | } |
369 | |
370 | method create_field($field, $options?) { |
371 | my $qf = $options->{quote_field_names} ||= ''; |
372 | |
373 | my $field_name = $field->name; |
374 | #debug("PKG: Looking at field '$field_name'\n"); |
375 | my $field_def = "$qf$field_name$qf"; |
376 | |
377 | # data type and size |
378 | my $data_type = $field->data_type; |
379 | my @size = $field->size; |
380 | my %extra = $field->extra; |
381 | my $list = $extra{'list'} || []; |
382 | # \todo deal with embedded quotes |
383 | my $commalist = join( ', ', map { qq['$_'] } @$list ); |
384 | my $charset = $extra{'mysql_charset'}; |
385 | my $collate = $extra{'mysql_collate'}; |
386 | |
387 | my $mysql_version = $options->{mysql_version} || 0; |
388 | # |
389 | # Oracle "number" type -- figure best MySQL type |
390 | # |
391 | if ( lc $data_type eq 'number' ) { |
392 | # not an integer |
393 | if ( scalar @size > 1 ) { |
394 | $data_type = 'double'; |
395 | } |
396 | elsif ( $size[0] && $size[0] >= 12 ) { |
397 | $data_type = 'bigint'; |
398 | } |
399 | elsif ( $size[0] && $size[0] <= 1 ) { |
400 | $data_type = 'tinyint'; |
401 | } |
402 | else { |
403 | $data_type = 'int'; |
404 | } |
405 | } |
406 | # |
407 | # Convert a large Oracle varchar to "text" |
408 | # (not necessary as of 5.0.3 http://dev.mysql.com/doc/refman/5.0/en/char.html) |
409 | # |
410 | elsif ( $data_type =~ /char/i && $size[0] > 255 ) { |
411 | unless ($size[0] <= 65535 && $mysql_version >= 5.000003 ) { |
412 | $data_type = 'text'; |
413 | @size = (); |
414 | } |
415 | } |
416 | elsif ( $data_type =~ /boolean/i ) { |
417 | if ($mysql_version >= 4) { |
418 | $data_type = 'boolean'; |
419 | } else { |
420 | $data_type = 'enum'; |
421 | $commalist = "'0','1'"; |
422 | } |
423 | } |
424 | # elsif ( exists $translate{ lc $data_type } ) { |
425 | # $data_type = $translate{ lc $data_type }; |
426 | # } |
427 | |
428 | @size = () if $data_type =~ /(text|blob)/i; |
429 | |
430 | if ( $data_type =~ /(double|float)/ && scalar @size == 1 ) { |
431 | push @size, '0'; |
432 | } |
433 | |
434 | $field_def .= " $data_type"; |
435 | |
436 | if ( lc($data_type) eq 'enum' || lc($data_type) eq 'set') { |
437 | $field_def .= '(' . $commalist . ')'; |
438 | } |
439 | elsif ( |
440 | defined $size[0] && $size[0] > 0 |
441 | && |
442 | ! grep lc($data_type) eq $_, @no_length_attr |
443 | ) { |
444 | $field_def .= '(' . join( ', ', @size ) . ')'; |
445 | } |
446 | |
447 | # char sets |
448 | $field_def .= " CHARACTER SET $charset" if $charset; |
449 | $field_def .= " COLLATE $collate" if $collate; |
450 | |
451 | # MySQL qualifiers |
452 | for my $qual ( qw[ binary unsigned zerofill ] ) { |
453 | my $val = $extra{ $qual } || $extra{ uc $qual } or next; |
454 | $field_def .= " $qual"; |
455 | } |
456 | for my $qual ( 'character set', 'collate', 'on update' ) { |
457 | my $val = $extra{ $qual } || $extra{ uc $qual } or next; |
458 | $field_def .= " $qual $val"; |
459 | } |
460 | |
461 | # Null? |
462 | $field_def .= ' NOT NULL' unless $field->is_nullable; |
463 | |
464 | # Default? XXX Need better quoting! |
465 | my $default = $field->default_value; |
e20b43c1 |
466 | |
467 | # if ( defined $default ) { |
468 | # SQL::Translator::Producer->_apply_default_value( |
469 | # \$field_def, |
470 | # $default, |
471 | # [ |
472 | # 'NULL' => \'NULL', |
473 | # ], |
474 | # ); |
475 | # } |
24e6a53a |
476 | |
477 | if ( my $comments = $field->comments ) { |
478 | $field_def .= qq[ comment '$comments']; |
479 | } |
480 | |
481 | # auto_increment? |
482 | $field_def .= " auto_increment" if $field->is_auto_increment; |
483 | |
484 | return $field_def; |
485 | } |
486 | |
487 | method alter_create_index(Index $index, $options?) { |
488 | my $qt = $options->{quote_table_names} || ''; |
489 | my $qf = $options->{quote_field_names} || ''; |
490 | |
491 | return join( ' ', |
492 | 'ALTER TABLE', |
493 | $qt.$index->table->name.$qt, |
494 | 'ADD', |
495 | create_index(@_) |
496 | ); |
497 | } |
498 | |
499 | method create_index($index, $options?) { |
500 | # my ( $index, $options ) = @_; |
501 | |
502 | my $qf = $options->{quote_field_names} || ''; |
503 | |
504 | return join( |
505 | ' ', |
506 | map { $_ || () } |
507 | lc $index->type eq 'normal' ? 'INDEX' : $index->type . ' INDEX', |
508 | $index->name |
509 | ? ( |
510 | $self->truncate_id_uniquely( |
511 | $index->name, |
512 | $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH |
513 | ) |
514 | ) |
515 | : '', |
516 | '(' . $qf . join( "$qf, $qf", $index->fields ) . $qf . ')' |
517 | ); |
518 | } |
519 | |
520 | method alter_drop_index(Index $index, $options?) { |
521 | my $qt = $options->{quote_table_names} || ''; |
522 | my $qf = $options->{quote_field_names} || ''; |
523 | |
524 | return join( ' ', |
525 | 'ALTER TABLE', |
526 | $qt.$index->table->name.$qt, |
527 | 'DROP', |
528 | 'INDEX', |
529 | $index->name || $index->fields |
530 | ); |
531 | |
532 | } |
533 | |
534 | method alter_drop_constraint(Constraint $c, $options?) { |
535 | my $qt = $options->{quote_table_names} || ''; |
536 | my $qc = $options->{quote_field_names} || ''; |
537 | |
538 | my $out = sprintf('ALTER TABLE %s DROP %s %s', |
539 | $qt . $c->table->name . $qt, |
540 | $c->type eq FOREIGN_KEY ? $c->type : "INDEX", |
541 | $qc . $c->name . $qc ); |
542 | |
543 | return $out; |
544 | } |
545 | |
546 | method alter_create_constraint($index, $options?) { |
547 | my $qt = $options->{quote_table_names} || ''; |
548 | return join( ' ', |
549 | 'ALTER TABLE', |
550 | $qt.$index->table->name.$qt, |
551 | 'ADD', |
552 | $self->create_constraint(@_) ); |
553 | } |
554 | |
555 | method create_constraint(Constraint $c, $options?) { |
556 | my $qf = $options->{quote_field_names} || ''; |
557 | my $qt = $options->{quote_table_names} || ''; |
558 | my $leave_name = $options->{leave_name} || undef; |
559 | |
560 | my @fields = $c->fields or return; |
561 | |
562 | if ( $c->type eq PRIMARY_KEY ) { |
563 | return 'PRIMARY KEY (' . $qf . join("$qf, $qf", @fields). $qf . ')'; |
564 | } |
565 | elsif ( $c->type eq UNIQUE ) { |
566 | return |
567 | 'UNIQUE '. |
568 | (defined $c->name ? $qf.$self->truncate_id_uniquely( $c->name, $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH ).$qf.' ' : ''). |
569 | '(' . $qf . join("$qf, $qf", @fields). $qf . ')'; |
570 | } |
571 | elsif ( $c->type eq FOREIGN_KEY ) { |
572 | # |
573 | # Make sure FK field is indexed or MySQL complains. |
574 | # |
24e6a53a |
575 | my $table = $c->table; |
576 | my $c_name = $self->truncate_id_uniquely( $c->name, $options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH ); |
577 | |
578 | my $def = join(' ', |
579 | map { $_ || () } |
580 | 'CONSTRAINT', |
581 | $qf . $c_name . $qf, |
582 | 'FOREIGN KEY' |
583 | ); |
584 | |
585 | |
586 | $def .= ' ('.$qf . join( "$qf, $qf", @fields ) . $qf . ')'; |
94f42333 |
587 | |
24e6a53a |
588 | $def .= ' REFERENCES ' . $qt . $c->reference_table . $qt; |
24e6a53a |
589 | my @rfields = map { $_ || () } $c->reference_fields; |
94f42333 |
590 | |
24e6a53a |
591 | unless ( @rfields ) { |
592 | my $rtable_name = $c->reference_table; |
593 | if ( my $ref_table = $table->schema->get_table( $rtable_name ) ) { |
594 | push @rfields, $ref_table->primary_key; |
595 | } |
596 | else { |
597 | warn "Can't find reference table '$rtable_name' " . |
598 | "in schema\n" if $options->{show_warnings}; |
599 | } |
600 | } |
601 | |
602 | if ( @rfields ) { |
603 | $def .= ' (' . $qf . join( "$qf, $qf", @rfields ) . $qf . ')'; |
604 | } |
605 | else { |
606 | warn "FK constraint on " . $table->name . '.' . |
607 | join('', @fields) . " has no reference fields\n" |
608 | if $options->{show_warnings}; |
609 | } |
610 | |
611 | if ( $c->match_type ) { |
94f42333 |
612 | $def .= ' MATCH '; |
613 | $def .= ( $c->match_type =~ /full/i ) ? 'FULL' : 'PARTIAL'; |
24e6a53a |
614 | } |
94f42333 |
615 | # if ( $c->on_delete ) { |
616 | # $def .= ' ON DELETE '.join( ' ', $c->on_delete ); |
617 | # } |
24e6a53a |
618 | |
94f42333 |
619 | # if ( $c->on_update ) { |
620 | # $def .= ' ON UPDATE '.join( ' ', $c->on_update ); |
621 | # } |
24e6a53a |
622 | return $def; |
623 | } |
624 | |
625 | return undef; |
626 | } |
627 | |
628 | method alter_table(Str $to_table, $options?) { |
629 | my $qt = $options->{quote_table_names} || ''; |
630 | |
631 | my $table_options = $self->generate_table_options($to_table, $options) || ''; |
632 | my $out = sprintf('ALTER TABLE %s%s', |
633 | $qt . $to_table->name . $qt, |
634 | $table_options); |
635 | |
636 | return $out; |
637 | } |
638 | |
639 | method rename_field(@args) { alter_field(@args) } |
640 | method alter_field(Str $from_field, Str $to_field, $options?) { |
641 | my $qf = $options->{quote_field_names} || ''; |
642 | my $qt = $options->{quote_table_names} || ''; |
643 | |
644 | my $out = sprintf('ALTER TABLE %s CHANGE COLUMN %s %s', |
645 | $qt . $to_field->table->name . $qt, |
646 | $qf . $from_field->name . $qf, |
647 | $self->create_field($to_field, $options)); |
648 | |
649 | return $out; |
650 | } |
651 | |
652 | method add_field(Str $new_field, $options?) { |
653 | my $qt = $options->{quote_table_names} || ''; |
654 | |
655 | my $out = sprintf('ALTER TABLE %s ADD COLUMN %s', |
656 | $qt . $new_field->table->name . $qt, |
657 | create_field($new_field, $options)); |
658 | |
659 | return $out; |
660 | |
661 | } |
662 | |
663 | method drop_field(Str $old_field, $options?) { |
664 | my $qf = $options->{quote_field_names} || ''; |
665 | my $qt = $options->{quote_table_names} || ''; |
666 | |
667 | my $out = sprintf('ALTER TABLE %s DROP COLUMN %s', |
668 | $qt . $old_field->table->name . $qt, |
669 | $qf . $old_field->name . $qf); |
670 | |
671 | return $out; |
672 | |
673 | } |
674 | |
675 | method batch_alter_table($table, $diff_hash, $options?) { |
676 | # InnoDB has an issue with dropping and re-adding a FK constraint under the |
677 | # name in a single alter statment, see: http://bugs.mysql.com/bug.php?id=13741 |
678 | # |
679 | # We have to work round this. |
680 | |
681 | my %fks_to_alter; |
682 | my %fks_to_drop = map { |
683 | $_->type eq FOREIGN_KEY |
684 | ? ( $_->name => $_ ) |
685 | : ( ) |
686 | } @{$diff_hash->{alter_drop_constraint} }; |
687 | |
688 | my %fks_to_create = map { |
689 | if ( $_->type eq FOREIGN_KEY) { |
690 | $fks_to_alter{$_->name} = $fks_to_drop{$_->name} if $fks_to_drop{$_->name}; |
691 | ( $_->name => $_ ); |
692 | } else { ( ) } |
693 | } @{$diff_hash->{alter_create_constraint} }; |
694 | |
695 | my @drop_stmt; |
696 | if (scalar keys %fks_to_alter) { |
697 | $diff_hash->{alter_drop_constraint} = [ |
698 | grep { !$fks_to_alter{$_->name} } @{ $diff_hash->{alter_drop_constraint} } |
699 | ]; |
700 | |
701 | @drop_stmt = batch_alter_table($table, { alter_drop_constraint => [ values %fks_to_alter ] }, $options); |
702 | |
703 | } |
704 | |
705 | my @stmts = map { |
706 | if (@{ $diff_hash->{$_} || [] }) { |
707 | my $meth = __PACKAGE__->can($_) or die __PACKAGE__ . " cant $_"; |
708 | map { $meth->( (ref $_ eq 'ARRAY' ? @$_ : $_), $options ) } @{ $diff_hash->{$_} } |
709 | } else { () } |
710 | } qw/rename_table |
711 | alter_drop_constraint |
712 | alter_drop_index |
713 | drop_field |
714 | add_field |
715 | alter_field |
716 | rename_field |
717 | alter_create_index |
718 | alter_create_constraint |
719 | alter_table/; |
720 | |
721 | # rename_table makes things a bit more complex |
722 | my $renamed_from = ""; |
723 | $renamed_from = $diff_hash->{rename_table}[0][0]->name |
724 | if $diff_hash->{rename_table} && @{$diff_hash->{rename_table}}; |
725 | |
726 | return unless @stmts; |
727 | # Just zero or one stmts. return now |
728 | return (@drop_stmt,@stmts) unless @stmts > 1; |
729 | |
730 | # Now strip off the 'ALTER TABLE xyz' of all but the first one |
731 | |
732 | my $qt = $options->{quote_table_names} || ''; |
733 | my $table_name = $qt . $table->name . $qt; |
734 | |
735 | |
736 | my $re = $renamed_from |
737 | ? qr/^ALTER TABLE (?:\Q$table_name\E|\Q$qt$renamed_from$qt\E) / |
738 | : qr/^ALTER TABLE \Q$table_name\E /; |
739 | |
740 | my $first = shift @stmts; |
741 | my ($alter_table) = $first =~ /($re)/; |
742 | |
743 | my $padd = " " x length($alter_table); |
744 | |
745 | return @drop_stmt, join( ",\n", $first, map { s/$re//; $padd . $_ } @stmts); |
746 | |
747 | } |
748 | |
749 | method drop_table(Str $table, $options?) { |
750 | my $qt = $options->{quote_table_names} || ''; |
751 | |
752 | # Drop (foreign key) constraints so table drops cleanly |
753 | my @sql = batch_alter_table($table, { alter_drop_constraint => [ grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints ] }, $options); |
754 | |
755 | return (@sql, "DROP TABLE $qt$table$qt"); |
756 | # return join("\n", @sql, "DROP TABLE $qt$table$qt"); |
757 | |
758 | } |
759 | |
760 | method rename_table(Str $old_table, Str $new_table, $options?) { |
761 | my $qt = $options->{quote_table_names} || ''; |
762 | |
763 | return "ALTER TABLE $qt$old_table$qt RENAME TO $qt$new_table$qt"; |
764 | } |
765 | |
766 | method next_unused_name($name?) { |
767 | # my $name = shift || ''; |
768 | if ( !defined($used_names{$name}) ) { |
769 | $used_names{$name} = $name; |
770 | return $name; |
771 | } |
772 | |
773 | my $i = 1; |
774 | while ( defined($used_names{$name . '_' . $i}) ) { |
775 | ++$i; |
776 | } |
777 | $name .= '_' . $i; |
778 | $used_names{$name} = $name; |
779 | return $name; |
780 | } |
781 | |
782 | method header_comment($producer?, $comment_char?) { |
783 | $producer ||= caller; |
784 | my $now = scalar localtime; |
785 | my $DEFAULT_COMMENT = '-- '; |
786 | |
787 | $comment_char = $DEFAULT_COMMENT |
788 | unless defined $comment_char; |
789 | |
790 | my $header_comment =<<"HEADER_COMMENT"; |
791 | ${comment_char} |
792 | ${comment_char}Created by $producer |
793 | ${comment_char}Created on $now |
794 | ${comment_char} |
795 | HEADER_COMMENT |
796 | |
797 | # Any additional stuff passed in |
798 | for my $additional_comment (@_) { |
799 | $header_comment .= "${comment_char}${additional_comment}\n"; |
800 | } |
801 | |
802 | return $header_comment; |
803 | } |
804 | |
f48e6f55 |
805 | use constant COLLISION_TAG_LENGTH => 8; |
24e6a53a |
806 | |
f48e6f55 |
807 | method truncate_id_uniquely(Str $desired_name, Int $max_symbol_length) { |
f77b076a |
808 | use Digest::SHA1 qw(sha1_hex); |
f48e6f55 |
809 | return $desired_name |
810 | unless defined $desired_name && length $desired_name > $max_symbol_length; |
24e6a53a |
811 | |
f48e6f55 |
812 | my $truncated_name = substr $desired_name, 0, |
813 | $max_symbol_length - COLLISION_TAG_LENGTH - 1; |
24e6a53a |
814 | |
f48e6f55 |
815 | # Hex isn't the most space-efficient, but it skirts around allowed |
816 | # charset issues |
817 | my $digest = sha1_hex($desired_name); |
818 | my $collision_tag = substr $digest, 0, COLLISION_TAG_LENGTH; |
24e6a53a |
819 | |
f48e6f55 |
820 | return $truncated_name |
821 | . '_' |
822 | . $collision_tag; |
24e6a53a |
823 | } |
24e6a53a |
824 | } |