Downgrade global version - highest version in 9002 on cpan is 1.58 - thus go with...
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / SQLite.pm
1 package SQL::Translator::Producer::SQLite;
2
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 # 02111-1307  USA
19 # -------------------------------------------------------------------
20
21 =head1 NAME
22
23 SQL::Translator::Producer::SQLite - SQLite producer for SQL::Translator
24
25 =head1 SYNOPSIS
26
27   use SQL::Translator;
28
29   my $t = SQL::Translator->new( parser => '...', producer => 'SQLite' );
30   $t->translate;
31
32 =head1 DESCRIPTION
33
34 This module will produce text output of the schema suitable for SQLite.
35
36 =cut
37
38 use strict;
39 use warnings;
40 use Data::Dumper;
41 use SQL::Translator::Schema::Constants;
42 use SQL::Translator::Utils qw(debug header_comment);
43
44 use vars qw[ $VERSION $DEBUG $WARN ];
45
46 $VERSION = '1.59';
47 $DEBUG = 0 unless defined $DEBUG;
48 $WARN = 0 unless defined $WARN;
49
50 our %used_identifiers = ();
51 our $max_id_length    = 30;
52 our %global_names;
53 our %truncated;
54
55 sub produce {
56     my $translator     = shift;
57     local $DEBUG       = $translator->debug;
58     local $WARN        = $translator->show_warnings;
59     my $no_comments    = $translator->no_comments;
60     my $add_drop_table = $translator->add_drop_table;
61     my $schema         = $translator->schema;
62     my $producer_args  = $translator->producer_args;
63     my $sqlite_version = $producer_args->{sqlite_version} || 0;
64     my $no_txn         = $producer_args->{no_transaction};
65
66     debug("PKG: Beginning production\n");
67
68     my @create = ();
69     push @create, header_comment unless ($no_comments);
70     $create[0] .= "\n\nBEGIN TRANSACTION" unless $no_txn;
71
72     for my $table ( $schema->get_tables ) {
73         push @create, create_table($table, { no_comments => $no_comments,
74                                              sqlite_version => $sqlite_version,
75                                           add_drop_table => $add_drop_table,});
76     }
77
78     for my $view ( $schema->get_views ) {
79       push @create, create_view($view, {
80         add_drop_view => $add_drop_table,
81         no_comments   => $no_comments,
82       });
83     }
84
85     for my $trigger ( $schema->get_triggers ) {
86       push @create, create_trigger($trigger, {
87         add_drop_trigger => $add_drop_table,
88         no_comments   => $no_comments,
89       });
90     }
91
92     if (wantarray) {
93       push @create, "COMMIT" unless $no_txn;
94       return @create;
95     } else {
96       push @create, "COMMIT;\n" unless $no_txn;
97       return join(";\n\n", @create );
98     }
99 }
100
101 # -------------------------------------------------------------------
102 sub mk_name {
103     my ($basename, $type, $scope, $critical) = @_;
104     my $basename_orig = $basename;
105     my $max_name      = !$max_id_length 
106                       ? length($type) + 1
107                       : $type 
108                       ? $max_id_length - (length($type) + 1) 
109                       : $max_id_length;
110     $basename         = substr( $basename, 0, $max_name ) 
111                         if length( $basename ) > $max_name;
112     $basename         =~ s/\./_/g;
113     my $name          = $type ? "${type}_$basename" : $basename;
114
115     if ( $basename ne $basename_orig and $critical ) {
116         my $show_type = $type ? "+'$type'" : "";
117         warn "Truncating '$basename_orig'$show_type to $max_id_length ",
118             "character limit to make '$name'\n" if $WARN;
119         $truncated{ $basename_orig } = $name;
120     }
121
122     $scope ||= \%global_names;
123     if ( my $prev = $scope->{ $name } ) {
124         my $name_orig = $name;
125         $name        .= sprintf( "%02d", ++$prev );
126         substr($name, $max_id_length - 3) = "00" 
127             if length( $name ) > $max_id_length;
128
129         warn "The name '$name_orig' has been changed to ",
130              "'$name' to make it unique.\n" if $WARN;
131
132         $scope->{ $name_orig }++;
133     }
134
135     $scope->{ $name }++;
136     return $name;
137 }
138
139 sub create_view {
140     my ($view, $options) = @_;
141     my $add_drop_view = $options->{add_drop_view};
142
143     my $view_name = $view->name;
144     debug("PKG: Looking at view '${view_name}'\n");
145
146     # Header.  Should this look like what mysqldump produces?
147     my $extra = $view->extra;
148     my $create = '';
149     $create .= "--\n-- View: ${view_name}\n--\n" unless $options->{no_comments};
150     $create .= "DROP VIEW IF EXISTS $view_name;\n" if $add_drop_view;
151     $create .= 'CREATE';
152     $create .= " TEMPORARY" if exists($extra->{temporary}) && $extra->{temporary};
153     $create .= ' VIEW';
154     $create .= " IF NOT EXISTS" if exists($extra->{if_not_exists}) && $extra->{if_not_exists};
155     $create .= " ${view_name}";
156
157     if( my $sql = $view->sql ){
158       $create .= " AS\n    ${sql}";
159     }
160     return $create;
161 }
162
163
164 sub create_table
165 {
166     my ($table, $options) = @_;
167
168     my $table_name = $table->name;
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.3) ? ' 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(', ', @pk_fields ) . ')';
223     }
224
225     #
226     # Indices
227     #
228     my $idx_name_default = 'A';
229     for my $index ( $table->get_indices ) {
230         push @index_defs, create_index($index);
231     }
232
233     #
234     # Constraints
235     #
236     my $c_name_default = 'A';
237     for my $c ( $table->get_constraints ) {
238         next unless $c->type eq UNIQUE; 
239         push @constraint_defs, create_constraint($c);
240     }
241
242     $create_table .= join(",\n", map { "  $_" } @field_defs ) . "\n)";
243
244     return (@create, $create_table, @index_defs, @constraint_defs );
245 }
246
247 sub create_field
248 {
249     my ($field, $options) = @_;
250
251     my $field_name = $field->name;
252     debug("PKG: Looking at field '$field_name'\n");
253     my $field_comments = $field->comments 
254         ? "-- " . $field->comments . "\n  " 
255         : '';
256
257     my $field_def = $field_comments.$field_name;
258
259     # data type and size
260     my $size      = $field->size;
261     my $data_type = $field->data_type;
262     $data_type    = 'varchar' if lc $data_type eq 'set';
263     $data_type  = 'blob' if lc $data_type eq 'bytea';
264
265     if ( lc $data_type =~ /(text|blob)/i ) {
266         $size = undef;
267     }
268
269 #             if ( $data_type =~ /timestamp/i ) {
270 #                 push @trigger_defs, 
271 #                     "CREATE TRIGGER ts_${table_name} ".
272 #                     "after insert on $table_name\n".
273 #                     "begin\n".
274 #                     "  update $table_name set $field_name=timestamp() ".
275 #                        "where id=new.id;\n".
276 #                     "end;\n"
277 #                 ;
278 #
279 #            }
280
281     #
282     # SQLite is generally typeless, but newer versions will
283     # make a field autoincrement if it is declared as (and
284     # *only* as) INTEGER PRIMARY KEY
285     #
286     my $pk        = $field->table->primary_key;
287     my @pk_fields = $pk ? $pk->fields : ();
288
289     if ( 
290          $field->is_primary_key && 
291          scalar @pk_fields == 1 &&
292          (
293           $data_type =~ /int(eger)?$/i
294           ||
295           ( $data_type =~ /^number?$/i && $size !~ /,/ )
296           )
297          ) {
298         $data_type = 'INTEGER PRIMARY KEY';
299         $size      = undef;
300 #        $pk_set    = 1;
301     }
302
303     $field_def .= sprintf " %s%s", $data_type, 
304     ( !$field->is_auto_increment && $size ) ? "($size)" : '';
305
306     # Null?
307     $field_def .= ' NOT NULL' unless $field->is_nullable;
308
309     # Default?  XXX Need better quoting!
310     my $default = $field->default_value;
311     if (defined $default) {
312         SQL::Translator::Producer->_apply_default_value(
313             \$field_def,
314             $default, 
315             [
316              'NULL'              => \'NULL',
317              'now()'             => 'now()',
318              'CURRENT_TIMESTAMP' => 'CURRENT_TIMESTAMP',
319             ],
320         );
321     }
322
323     return $field_def;
324
325 }
326
327 sub create_index
328 {
329     my ($index, $options) = @_;
330
331     my $name   = $index->name;
332     $name      = mk_name($index->table->name, $name);
333
334     my $type   = $index->type eq 'UNIQUE' ? "UNIQUE " : ''; 
335
336     # strip any field size qualifiers as SQLite doesn't like these
337     my @fields = map { s/\(\d+\)$//; $_ } $index->fields;
338     (my $index_table_name = $index->table->name) =~ s/^.+?\.//; # table name may not specify schema
339     warn "removing schema name from '" . $index->table->name . "' to make '$index_table_name'\n" if $WARN;
340     my $index_def =  
341     "CREATE ${type}INDEX $name ON " . $index_table_name .
342         ' (' . join( ', ', @fields ) . ')';
343
344     return $index_def;
345 }
346
347 sub create_constraint
348 {
349     my ($c, $options) = @_;
350
351     my $name   = $c->name;
352     $name      = mk_name($c->table->name, $name);
353     my @fields = $c->fields;
354     (my $index_table_name = $c->table->name) =~ s/^.+?\.//; # table name may not specify schema
355     warn "removing schema name from '" . $c->table->name . "' to make '$index_table_name'\n" if $WARN;
356
357     my $c_def =  
358     "CREATE UNIQUE INDEX $name ON " . $index_table_name .
359         ' (' . join( ', ', @fields ) . ')';
360
361     return $c_def;
362 }
363
364 sub create_trigger {
365   my ($trigger, $options) = @_;
366   my $add_drop = $options->{add_drop_trigger};
367
368   my $name = $trigger->name;
369   my @create;
370
371   push @create,  "DROP TRIGGER IF EXISTS $name" if $add_drop;
372
373   my $events = $trigger->database_events;
374   die "Can't handle multiple events in triggers" if @$events > 1;
375
376   my $action = "";
377
378   $DB::single = 1;
379   unless (ref $trigger->action) {
380     $action .= "BEGIN " . $trigger->action . " END";
381   } else {
382     $action = $trigger->action->{for_each} . " "
383       if $trigger->action->{for_each};
384
385     $action = $trigger->action->{when} . " "
386       if $trigger->action->{when};
387
388     my $steps = $trigger->action->{steps} || [];
389
390     $action .= "BEGIN ";
391     for (@$steps) {
392       $action .= $_ . "; "
393     }
394     $action .= "END";
395   }
396
397   push @create, "CREATE TRIGGER $name " .
398                 $trigger->perform_action_when . " " .
399                 $events->[0] .
400                 " on " . $trigger->on_table . " " .
401                 $action;
402
403   return @create;
404             
405 }
406
407 sub alter_table { } # Noop
408
409 sub add_field {
410   my ($field) = @_;
411
412   return sprintf("ALTER TABLE %s ADD COLUMN %s",
413       $field->table->name, create_field($field))
414 }
415
416 sub alter_create_index {
417   my ($index) = @_;
418
419   # This might cause name collisions
420   return create_index($index);
421 }
422
423 sub alter_create_constraint {
424   my ($constraint) = @_;
425
426   return create_constraint($constraint) if $constraint->type eq 'UNIQUE';
427 }
428
429 sub alter_drop_constraint { alter_drop_index(@_) }
430
431 sub alter_drop_index {
432   my ($constraint) = @_;
433
434   return sprintf("DROP INDEX %s",
435       $constraint->name);
436 }
437
438 sub batch_alter_table {
439   my ($table, $diffs) = @_;
440
441   # If we have any of the following
442   #
443   #  rename_field
444   #  alter_field
445   #  drop_field
446   #
447   # we need to do the following <http://www.sqlite.org/faq.html#q11>
448   #
449   # BEGIN TRANSACTION;
450   # CREATE TEMPORARY TABLE t1_backup(a,b);
451   # INSERT INTO t1_backup SELECT a,b FROM t1;
452   # DROP TABLE t1;
453   # CREATE TABLE t1(a,b);
454   # INSERT INTO t1 SELECT a,b FROM t1_backup;
455   # DROP TABLE t1_backup;
456   # COMMIT;
457   #
458   # Fun, eh?
459   #
460   # If we have rename_field we do similarly.
461
462   my $table_name = $table->name;
463   my $renaming = $diffs->{rename_table} && @{$diffs->{rename_table}};
464
465   if ( @{$diffs->{rename_field}} == 0 &&
466        @{$diffs->{alter_field}}  == 0 &&
467        @{$diffs->{drop_field}}   == 0
468        ) {
469 #    return join("\n", map { 
470     return map { 
471         my $meth = __PACKAGE__->can($_) or die __PACKAGE__ . " cant $_";
472         map { my $sql = $meth->(ref $_ eq 'ARRAY' ? @$_ : $_); $sql ?  ("$sql") : () } @{ $diffs->{$_} }
473         
474       } grep { @{$diffs->{$_}} } 
475     qw/rename_table
476        alter_drop_constraint
477        alter_drop_index
478        drop_field
479        add_field
480        alter_field
481        rename_field
482        alter_create_index
483        alter_create_constraint
484        alter_table/;
485   }
486
487
488   my @sql;
489   my $old_table = $renaming ? $diffs->{rename_table}[0][0] : $table;
490   
491   do {
492     local $table->{name} = $table_name . '_temp_alter';
493     # We only want the table - dont care about indexes on tmp table
494     my ($table_sql) = create_table($table, {no_comments => 1, temporary_table => 1});
495     push @sql,$table_sql;
496   };
497
498   push @sql, "INSERT INTO @{[$table_name]}_temp_alter SELECT @{[ join(', ', $old_table->get_fields)]} FROM @{[$old_table]}",
499              "DROP TABLE @{[$old_table]}",
500              create_table($table, { no_comments => 1 }),
501              "INSERT INTO @{[$table_name]} SELECT @{[ join(', ', $old_table->get_fields)]} FROM @{[$table_name]}_temp_alter",
502              "DROP TABLE @{[$table_name]}_temp_alter";
503
504   return @sql;
505 #  return join("", @sql, "");
506 }
507
508 sub drop_table {
509   my ($table) = @_;
510   return "DROP TABLE $table";
511 }
512
513 sub rename_table {
514   my ($old_table, $new_table, $options) = @_;
515
516   my $qt = $options->{quote_table_names} || '';
517
518   return "ALTER TABLE $qt$old_table$qt RENAME TO $qt$new_table$qt";
519
520 }
521
522 # No-op. Just here to signify that we are a new style parser.
523 sub preproces_schema { }
524
525 1;
526
527 =pod
528
529 =head1 SEE ALSO
530
531 SQL::Translator, http://www.sqlite.org/.
532
533 =head1 AUTHOR
534
535 Ken Y. Clark C<< <kclark@cpan.orgE> >>.
536
537 Diff code added by Ash Berlin C<< <ash@cpan.org> >>.
538
539 =cut