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