Improve trigger 'scope' attribute support (RT#119997)
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / SQLite.pm
1 package SQL::Translator::Producer::SQLite;
2
3 =head1 NAME
4
5 SQL::Translator::Producer::SQLite - SQLite producer for SQL::Translator
6
7 =head1 SYNOPSIS
8
9   use SQL::Translator;
10
11   my $t = SQL::Translator->new( parser => '...', producer => 'SQLite' );
12   $t->translate;
13
14 =head1 DESCRIPTION
15
16 This module will produce text output of the schema suitable for SQLite.
17
18 =cut
19
20 use strict;
21 use warnings;
22 use Data::Dumper;
23 use SQL::Translator::Schema::Constants;
24 use SQL::Translator::Utils qw(debug header_comment parse_dbms_version batch_alter_table_statements);
25 use SQL::Translator::Generator::DDL::SQLite;
26
27 our ( $DEBUG, $WARN );
28 our $VERSION = '1.59';
29 $DEBUG = 0 unless defined $DEBUG;
30 $WARN = 0 unless defined $WARN;
31
32 our $max_id_length    = 30;
33 my %global_names;
34
35 # HIDEOUS TEMPORARY DEFAULT WITHOUT QUOTING!
36 our $NO_QUOTES = 1;
37 {
38
39   my ($quoting_generator, $nonquoting_generator);
40   sub _generator {
41     $NO_QUOTES
42       ? $nonquoting_generator ||= SQL::Translator::Generator::DDL::SQLite->new(quote_chars => [])
43       : $quoting_generator ||= SQL::Translator::Generator::DDL::SQLite->new
44   }
45 }
46
47 sub produce {
48     my $translator     = shift;
49     local $DEBUG       = $translator->debug;
50     local $WARN        = $translator->show_warnings;
51     my $no_comments    = $translator->no_comments;
52     my $add_drop_table = $translator->add_drop_table;
53     my $schema         = $translator->schema;
54     my $producer_args  = $translator->producer_args;
55     my $sqlite_version = parse_dbms_version(
56         $producer_args->{sqlite_version}, 'perl'
57     );
58     my $no_txn         = $producer_args->{no_transaction};
59
60     debug("PKG: Beginning production\n");
61
62     %global_names = ();   #reset
63
64     # only quote if quotes were requested for real
65     # 0E0 indicates "the default of true" was assumed
66     local $NO_QUOTES = 0
67       if $translator->quote_identifiers and $translator->quote_identifiers ne '0E0';
68
69     my $head = (header_comment() . "\n") unless $no_comments;
70
71     my @create = ();
72
73     push @create, "BEGIN TRANSACTION" unless $no_txn;
74
75     for my $table ( $schema->get_tables ) {
76         push @create, create_table($table, { no_comments => $no_comments,
77                                              sqlite_version => $sqlite_version,
78                                           add_drop_table => $add_drop_table,});
79     }
80
81     for my $view ( $schema->get_views ) {
82       push @create, create_view($view, {
83         add_drop_view => $add_drop_table,
84         no_comments   => $no_comments,
85       });
86     }
87
88     for my $trigger ( $schema->get_triggers ) {
89       push @create, create_trigger($trigger, {
90         add_drop_trigger => $add_drop_table,
91         no_comments   => $no_comments,
92       });
93     }
94
95     push @create, "COMMIT" unless $no_txn;
96
97     if (wantarray) {
98       return ($head||(), @create);
99     } else {
100       return join ('',
101         $head||(),
102         join(";\n\n", @create ),
103         ";\n",
104       );
105     }
106 }
107
108 sub mk_name {
109     my ($name, $scope, $critical) = @_;
110
111     $scope ||= \%global_names;
112     if ( my $prev = $scope->{ $name } ) {
113         my $name_orig = $name;
114         $name        .= sprintf( "%02d", ++$prev );
115         substr($name, $max_id_length - 3) = "00"
116             if length( $name ) > $max_id_length;
117
118         warn "The name '$name_orig' has been changed to ",
119              "'$name' to make it unique.\n" if $WARN;
120
121         $scope->{ $name_orig }++;
122     }
123
124     $scope->{ $name }++;
125     return _generator()->quote($name);
126 }
127
128 sub create_view {
129     my ($view, $options) = @_;
130     my $add_drop_view = $options->{add_drop_view};
131
132     my $view_name = _generator()->quote($view->name);
133     $global_names{$view->name} = 1;
134
135     debug("PKG: Looking at view '${view_name}'\n");
136
137     # Header.  Should this look like what mysqldump produces?
138     my $extra = $view->extra;
139     my @create;
140     push @create, "DROP VIEW IF EXISTS $view_name" if $add_drop_view;
141
142     my $create_view = 'CREATE';
143     $create_view .= " TEMPORARY" if exists($extra->{temporary}) && $extra->{temporary};
144     $create_view .= ' VIEW';
145     $create_view .= " IF NOT EXISTS" if exists($extra->{if_not_exists}) && $extra->{if_not_exists};
146     $create_view .= " ${view_name}";
147
148     if( my $sql = $view->sql ){
149       $create_view .= " AS\n    ${sql}";
150     }
151     push @create, $create_view;
152
153     # Tack the comment onto the first statement.
154     unless ($options->{no_comments}) {
155       $create[0] = "--\n-- View: ${view_name}\n--\n" . $create[0];
156     }
157
158     return @create;
159 }
160
161
162 sub create_table
163 {
164     my ($table, $options) = @_;
165
166     my $table_name = _generator()->quote($table->name);
167     $global_names{$table->name} = 1;
168
169     my $no_comments = $options->{no_comments};
170     my $add_drop_table = $options->{add_drop_table};
171     my $sqlite_version = $options->{sqlite_version} || 0;
172
173     debug("PKG: Looking at table '$table_name'\n");
174
175     my ( @index_defs, @constraint_defs );
176     my @fields = $table->get_fields or die "No fields in $table_name";
177
178     my $temp = $options->{temporary_table} ? 'TEMPORARY ' : '';
179     #
180     # Header.
181     #
182     my $exists = ($sqlite_version >= 3.003) ? ' IF EXISTS' : '';
183     my @create;
184     my ($comment, $create_table) = "";
185     $comment =  "--\n-- Table: $table_name\n--\n" unless $no_comments;
186     if ($add_drop_table) {
187       push @create, $comment . qq[DROP TABLE$exists $table_name];
188     } else {
189       $create_table = $comment;
190     }
191
192     $create_table .= "CREATE ${temp}TABLE $table_name (\n";
193
194     #
195     # Comments
196     #
197     if ( $table->comments and !$no_comments ){
198         $create_table .= "-- Comments: \n-- ";
199         $create_table .= join "\n-- ",  $table->comments;
200         $create_table .= "\n--\n\n";
201     }
202
203     #
204     # How many fields in PK?
205     #
206     my $pk        = $table->primary_key;
207     my @pk_fields = $pk ? $pk->fields : ();
208
209     #
210     # Fields
211     #
212     my ( @field_defs, $pk_set );
213     for my $field ( @fields ) {
214         push @field_defs, create_field($field);
215     }
216
217     if (
218          scalar @pk_fields > 1
219          ||
220          ( @pk_fields && !grep /INTEGER PRIMARY KEY/, @field_defs )
221          ) {
222         push @field_defs, 'PRIMARY KEY (' . join(', ', map _generator()->quote($_), @pk_fields ) . ')';
223     }
224
225     #
226     # Indices
227     #
228     for my $index ( $table->get_indices ) {
229         push @index_defs, create_index($index);
230     }
231
232     #
233     # Constraints
234     #
235     for my $c ( $table->get_constraints ) {
236         if ($c->type eq "FOREIGN KEY") {
237             push @field_defs, create_foreignkey($c);
238         }
239         elsif ($c->type eq "CHECK") {
240             push @field_defs, create_check_constraint($c);
241         }
242         next unless $c->type eq UNIQUE;
243         push @constraint_defs, create_constraint($c);
244     }
245
246     $create_table .= join(",\n", map { "  $_" } @field_defs ) . "\n)";
247
248     return (@create, $create_table, @index_defs, @constraint_defs );
249 }
250
251 sub create_check_constraint {
252     my $c     = shift;
253     my $check = '';
254     $check .= 'CONSTRAINT ' . _generator->quote( $c->name ) . ' ' if $c->name;
255     $check .= 'CHECK(' . $c->expression . ')';
256     return $check;
257 }
258
259 sub create_foreignkey {
260     my $c = shift;
261
262     my @fields = $c->fields;
263     my @rfields = map { $_ || () } $c->reference_fields;
264     unless ( @rfields ) {
265         my $rtable_name = $c->reference_table;
266         if ( my $ref_table = $c->schema->get_table( $rtable_name ) ) {
267             push @rfields, $ref_table->primary_key;
268
269             die "FK constraint on " . $rtable_name . '.' . join('', @fields) . " has no reference fields\n"
270               unless @rfields;
271         }
272         else {
273             die "Can't find reference table '$rtable_name' in schema\n";
274         }
275     }
276
277     my $fk_sql = sprintf 'FOREIGN KEY (%s) REFERENCES %s(%s)',
278         join (', ', map { _generator()->quote($_) } @fields ),
279         _generator()->quote($c->reference_table),
280         join (', ', map { _generator()->quote($_) } @rfields )
281     ;
282
283     $fk_sql .= " ON DELETE " . $c->{on_delete} if $c->{on_delete};
284     $fk_sql .= " ON UPDATE " . $c->{on_update} if $c->{on_update};
285
286     return $fk_sql;
287 }
288
289 sub create_field { return _generator()->field($_[0]) }
290
291 sub create_index
292 {
293     my ($index, $options) = @_;
294
295     (my $index_table_name = $index->table->name) =~ s/^.+?\.//; # table name may not specify schema
296     my $name   = mk_name($index->name || "${index_table_name}_idx");
297
298     my $type   = $index->type eq 'UNIQUE' ? "UNIQUE " : '';
299
300     # strip any field size qualifiers as SQLite doesn't like these
301     my @fields = map { s/\(\d+\)$//; _generator()->quote($_) } $index->fields;
302     $index_table_name = _generator()->quote($index_table_name);
303     warn "removing schema name from '" . $index->table->name . "' to make '$index_table_name'\n" if $WARN;
304     my $index_def =
305     "CREATE ${type}INDEX $name ON " . $index_table_name .
306         ' (' . join( ', ', @fields ) . ')';
307
308     return $index_def;
309 }
310
311 sub create_constraint
312 {
313     my ($c, $options) = @_;
314
315     (my $index_table_name = $c->table->name) =~ s/^.+?\.//; # table name may not specify schema
316     my $name   = mk_name($c->name || "${index_table_name}_idx");
317     my @fields = map _generator()->quote($_), $c->fields;
318     $index_table_name = _generator()->quote($index_table_name);
319     warn "removing schema name from '" . $c->table->name . "' to make '$index_table_name'\n" if $WARN;
320
321     my $c_def =
322     "CREATE UNIQUE INDEX $name ON " . $index_table_name .
323         ' (' . join( ', ', @fields ) . ')';
324
325     return $c_def;
326 }
327
328 sub create_trigger {
329   my ($trigger, $options) = @_;
330   my $add_drop = $options->{add_drop_trigger};
331
332   my @statements;
333
334   my $trigger_name = $trigger->name;
335   $global_names{$trigger_name} = 1;
336
337   my $events = $trigger->database_events;
338   for my $evt ( @$events ) {
339
340     my $trig_name = $trigger_name;
341     if (@$events > 1) {
342       $trig_name .= "_$evt";
343
344       warn "Multiple database events supplied for trigger '$trigger_name', ",
345         "creating trigger '$trig_name' for the '$evt' event.\n" if $WARN;
346     }
347
348     $trig_name = _generator()->quote($trig_name);
349     push @statements,  "DROP TRIGGER IF EXISTS $trig_name" if $add_drop;
350
351
352     $DB::single = 1;
353     my $action = "";
354     if (not ref $trigger->action) {
355       $action = $trigger->action;
356       $action = "BEGIN " . $action . " END"
357         unless $action =~ /^ \s* BEGIN [\s\;] .*? [\s\;] END [\s\;]* $/six;
358     }
359     else {
360       $action = $trigger->action->{for_each} . " "
361         if $trigger->action->{for_each};
362
363       $action = $trigger->action->{when} . " "
364         if $trigger->action->{when};
365
366       my $steps = $trigger->action->{steps} || [];
367
368       $action .= "BEGIN ";
369       $action .= $_ . "; " for (@$steps);
370       $action .= "END";
371     }
372
373     push @statements, sprintf (
374       'CREATE TRIGGER %s %s %s on %s %s',
375       $trig_name,
376       $trigger->perform_action_when,
377       $evt,
378       _generator()->quote($trigger->on_table),
379       $action
380     );
381   }
382
383   return @statements;
384 }
385
386 sub alter_table { () } # Noop
387
388 sub add_field {
389   my ($field) = @_;
390
391   return sprintf("ALTER TABLE %s ADD COLUMN %s",
392       _generator()->quote($field->table->name), create_field($field))
393 }
394
395 sub alter_create_index {
396   my ($index) = @_;
397
398   # This might cause name collisions
399   return create_index($index);
400 }
401
402 sub alter_create_constraint {
403   my ($constraint) = @_;
404
405   return create_constraint($constraint) if $constraint->type eq 'UNIQUE';
406 }
407
408 sub alter_drop_constraint { alter_drop_index(@_) }
409
410 sub alter_drop_index {
411   my ($constraint) = @_;
412
413   return sprintf("DROP INDEX %s",
414       _generator()->quote($constraint->name));
415 }
416
417 sub batch_alter_table {
418   my ($table, $diffs, $options) = @_;
419
420   # If we have any of the following
421   #
422   #  rename_field
423   #  alter_field
424   #  drop_field
425   #
426   # we need to do the following <http://www.sqlite.org/faq.html#q11>
427   #
428   # BEGIN TRANSACTION;
429   # CREATE TEMPORARY TABLE t1_backup(a,b);
430   # INSERT INTO t1_backup SELECT a,b FROM t1;
431   # DROP TABLE t1;
432   # CREATE TABLE t1(a,b);
433   # INSERT INTO t1 SELECT a,b FROM t1_backup;
434   # DROP TABLE t1_backup;
435   # COMMIT;
436   #
437   # Fun, eh?
438   #
439   # If we have rename_field we do similarly.
440   #
441   # We create the temporary table as a copy of the new table, copy all data
442   # to temp table, create new table and then copy as appropriate taking note
443   # of renamed fields.
444
445   my $table_name = $table->name;
446
447   if ( @{$diffs->{rename_field}} == 0 &&
448        @{$diffs->{alter_field}}  == 0 &&
449        @{$diffs->{drop_field}}   == 0
450        ) {
451     return batch_alter_table_statements($diffs, $options);
452   }
453
454   my @sql;
455
456   # $table is the new table but we may need an old one
457   # TODO: this is NOT very well tested at the moment so add more tests
458
459   my $old_table = $table;
460
461   if ( $diffs->{rename_table} && @{$diffs->{rename_table}} ) {
462     $old_table = $diffs->{rename_table}[0][0];
463   }
464
465   my $temp_table_name = $table_name . '_temp_alter';
466
467   # CREATE TEMPORARY TABLE t1_backup(a,b);
468
469   my %temp_table_fields;
470   do {
471     local $table->{name} = $temp_table_name;
472     # We only want the table - don't care about indexes on tmp table
473     my ($table_sql) = create_table($table, {no_comments => 1, temporary_table => 1});
474     push @sql,$table_sql;
475
476     %temp_table_fields = map { $_ => 1} $table->get_fields;
477   };
478
479   # record renamed fields for later
480   my %rename_field = map { $_->[1]->name => $_->[0]->name } @{$diffs->{rename_field}};
481
482   # drop added fields from %temp_table_fields
483   delete @temp_table_fields{@{$diffs->{add_field}}};
484
485   # INSERT INTO t1_backup SELECT a,b FROM t1;
486
487   push @sql, sprintf( 'INSERT INTO %s( %s) SELECT %s FROM %s',
488
489     _generator()->quote( $temp_table_name ),
490
491     join( ', ',
492         map _generator()->quote($_),
493         grep { $temp_table_fields{$_} } $table->get_fields ),
494
495     join( ', ',
496         map _generator()->quote($_),
497         map { $rename_field{$_} ? $rename_field{$_} : $_ }
498         grep { $temp_table_fields{$_} } $table->get_fields ),
499
500     _generator()->quote( $old_table->name )
501   );
502
503   # DROP TABLE t1;
504
505   push @sql, sprintf('DROP TABLE %s', _generator()->quote($old_table->name));
506
507   # CREATE TABLE t1(a,b);
508
509   push @sql, create_table($table, { no_comments => 1 });
510
511   # INSERT INTO t1 SELECT a,b FROM t1_backup;
512
513   push @sql, sprintf('INSERT INTO %s SELECT %s FROM %s',
514     _generator()->quote($table_name),
515     join(', ', map _generator()->quote($_), $table->get_fields),
516     _generator()->quote($temp_table_name)
517   );
518
519   # DROP TABLE t1_backup;
520
521   push @sql, sprintf('DROP TABLE %s', _generator()->quote($temp_table_name));
522
523   return wantarray ? @sql : join(";\n", @sql);
524 }
525
526 sub drop_table {
527   my ($table) = @_;
528   $table = _generator()->quote($table);
529   return "DROP TABLE $table";
530 }
531
532 sub rename_table {
533   my ($old_table, $new_table, $options) = @_;
534
535   $old_table = _generator()->quote($old_table);
536   $new_table = _generator()->quote($new_table);
537
538   return "ALTER TABLE $old_table RENAME TO $new_table";
539
540 }
541
542 # No-op. Just here to signify that we are a new style parser.
543 sub preproces_schema { }
544
545 1;
546
547 =pod
548
549 =head1 SEE ALSO
550
551 SQL::Translator, http://www.sqlite.org/.
552
553 =head1 AUTHOR
554
555 Ken Youens-Clark C<< <kclark@cpan.orgE> >>.
556
557 Diff code added by Ash Berlin C<< <ash@cpan.org> >>.
558
559 =cut