377dab49cf25916d06a51c2adee61c0ed9f9c462
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / SQLite.pm
1 package SQL::Translator::Producer::SQLite;
2
3 # -------------------------------------------------------------------
4 # $Id$
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$ =~ /(\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     my $producer_args  = $translator->producer_args;
64     my $sqlite_version  = $producer_args->{sqlite_version} || 0;
65
66     debug("PKG: Beginning production\n");
67
68     my @create = ();
69     push @create, header_comment unless ($no_comments);
70     push @create, 'BEGIN TRANSACTION';
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     return wantarray ? (@create, "COMMIT") : join(";\n\n", (@create, "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     my $add_drop_view = $options->{add_drop_view};
127
128     my $view_name = $view->name;
129     debug("PKG: Looking at view '${view_name}'\n");
130
131     # Header.  Should this look like what mysqldump produces?
132     my $extra = $view->extra;
133     my $create = '';
134     $create .= "--\n-- View: ${view_name}\n--\n" unless $options->{no_comments};
135     $create .= "DROP VIEW IF EXISTS $view_name;\n" if $add_drop_view;
136     $create .= 'CREATE';
137     $create .= " TEMPORARY" if exists($extra->{temporary}) && $extra->{temporary};
138     $create .= ' VIEW';
139     $create .= " IF NOT EXISTS" if exists($extra->{if_not_exists}) && $extra->{if_not_exists};
140     $create .= " ${view_name}";
141
142     if( my $sql = $view->sql ){
143       $create .= " AS\n    ${sql}";
144     }
145     return $create;
146 }
147
148
149 sub create_table
150 {
151     my ($table, $options) = @_;
152
153     my $table_name = $table->name;
154     my $no_comments = $options->{no_comments};
155     my $add_drop_table = $options->{add_drop_table};
156     my $sqlite_version = $options->{sqlite_version} || 0;
157
158     debug("PKG: Looking at table '$table_name'\n");
159
160     my ( @index_defs, @constraint_defs, @trigger_defs );
161     my @fields = $table->get_fields or die "No fields in $table_name";
162
163     my $temp = $options->{temporary_table} ? 'TEMPORARY ' : '';
164     #
165     # Header.
166     #
167     my $exists = ($sqlite_version >= 3.3) ? ' IF EXISTS' : '';
168     my @create;
169     push @create, "--\n-- Table: $table_name\n--\n" unless $no_comments;
170     push @create, qq[DROP TABLE$exists $table_name] if $add_drop_table;
171     my $create_table = "CREATE ${temp}TABLE $table_name (\n";
172
173     #
174     # Comments
175     #
176     if ( $table->comments and !$no_comments ){
177         $create_table .= "-- Comments: \n-- ";
178         $create_table .= join "\n-- ",  $table->comments;
179         $create_table .= "\n--\n\n";
180     }
181
182     #
183     # How many fields in PK?
184     #
185     my $pk        = $table->primary_key;
186     my @pk_fields = $pk ? $pk->fields : ();
187
188     #
189     # Fields
190     #
191     my ( @field_defs, $pk_set );
192     for my $field ( @fields ) {
193         push @field_defs, create_field($field);
194     }
195
196     if ( 
197          scalar @pk_fields > 1 
198          || 
199          ( @pk_fields && !grep /INTEGER PRIMARY KEY/, @field_defs ) 
200          ) {
201         push @field_defs, 'PRIMARY KEY (' . join(', ', @pk_fields ) . ')';
202     }
203
204     #
205     # Indices
206     #
207     my $idx_name_default = 'A';
208     for my $index ( $table->get_indices ) {
209         push @index_defs, create_index($index);
210     }
211
212     #
213     # Constraints
214     #
215     my $c_name_default = 'A';
216     for my $c ( $table->get_constraints ) {
217         next unless $c->type eq UNIQUE; 
218         push @constraint_defs, create_constraint($c);
219     }
220
221     $create_table .= join(",\n", map { "  $_" } @field_defs ) . "\n)";
222
223     return (@create, $create_table, @index_defs, @constraint_defs, @trigger_defs );
224 }
225
226 sub create_field
227 {
228     my ($field, $options) = @_;
229
230     my $field_name = $field->name;
231     debug("PKG: Looking at field '$field_name'\n");
232     my $field_comments = $field->comments 
233         ? "-- " . $field->comments . "\n  " 
234         : '';
235
236     my $field_def = $field_comments.$field_name;
237
238     # data type and size
239     my $size      = $field->size;
240     my $data_type = $field->data_type;
241     $data_type    = 'varchar' if lc $data_type eq 'set';
242     $data_type  = 'blob' if lc $data_type eq 'bytea';
243
244     if ( lc $data_type =~ /(text|blob)/i ) {
245         $size = undef;
246     }
247
248 #             if ( $data_type =~ /timestamp/i ) {
249 #                 push @trigger_defs, 
250 #                     "CREATE TRIGGER ts_${table_name} ".
251 #                     "after insert on $table_name\n".
252 #                     "begin\n".
253 #                     "  update $table_name set $field_name=timestamp() ".
254 #                        "where id=new.id;\n".
255 #                     "end;\n"
256 #                 ;
257 #
258 #            }
259
260     #
261     # SQLite is generally typeless, but newer versions will
262     # make a field autoincrement if it is declared as (and
263     # *only* as) INTEGER PRIMARY KEY
264     #
265     my $pk        = $field->table->primary_key;
266     my @pk_fields = $pk ? $pk->fields : ();
267
268     if ( 
269          $field->is_primary_key && 
270          scalar @pk_fields == 1 &&
271          (
272           $data_type =~ /int(eger)?$/i
273           ||
274           ( $data_type =~ /^number?$/i && $size !~ /,/ )
275           )
276          ) {
277         $data_type = 'INTEGER PRIMARY KEY';
278         $size      = undef;
279 #        $pk_set    = 1;
280     }
281
282     $field_def .= sprintf " %s%s", $data_type, 
283     ( !$field->is_auto_increment && $size ) ? "($size)" : '';
284
285     # Null?
286     $field_def .= ' NOT NULL' unless $field->is_nullable;
287
288     # Default?  XXX Need better quoting!
289     my $default = $field->default_value;
290     if (defined $default) {
291         SQL::Translator::Producer->_apply_default_value(
292             \$field_def,
293             $default, 
294             [
295              'NULL'              => \'NULL',
296              'now()'             => 'now()',
297              'CURRENT_TIMESTAMP' => 'CURRENT_TIMESTAMP',
298             ],
299         );
300     }
301
302     return $field_def;
303
304 }
305
306 sub create_index
307 {
308     my ($index, $options) = @_;
309
310     my $name   = $index->name;
311     $name      = mk_name($index->table->name, $name);
312
313     my $type   = $index->type eq 'UNIQUE' ? "UNIQUE " : ''; 
314
315     # strip any field size qualifiers as SQLite doesn't like these
316     my @fields = map { s/\(\d+\)$//; $_ } $index->fields;
317     (my $index_table_name = $index->table->name) =~ s/^.+?\.//; # table name may not specify schema
318     warn "removing schema name from '" . $index->table->name . "' to make '$index_table_name'\n" if $WARN;
319     my $index_def =  
320     "CREATE ${type}INDEX $name ON " . $index_table_name .
321         ' (' . join( ', ', @fields ) . ')';
322
323     return $index_def;
324 }
325
326 sub create_constraint
327 {
328     my ($c, $options) = @_;
329
330     my $name   = $c->name;
331     $name      = mk_name($c->table->name, $name);
332     my @fields = $c->fields;
333     (my $index_table_name = $c->table->name) =~ s/^.+?\.//; # table name may not specify schema
334     warn "removing schema name from '" . $c->table->name . "' to make '$index_table_name'\n" if $WARN;
335
336     my $c_def =  
337     "CREATE UNIQUE INDEX $name ON " . $index_table_name .
338         ' (' . join( ', ', @fields ) . ')';
339
340     return $c_def;
341 }
342
343 sub alter_table { } # Noop
344
345 sub add_field {
346   my ($field) = @_;
347
348   return sprintf("ALTER TABLE %s ADD COLUMN %s",
349       $field->table->name, create_field($field))
350 }
351
352 sub alter_create_index {
353   my ($index) = @_;
354
355   # This might cause name collisions
356   return create_index($index);
357 }
358
359 sub alter_create_constraint {
360   my ($constraint) = @_;
361
362   return create_constraint($constraint) if $constraint->type eq 'UNIQUE';
363 }
364
365 sub alter_drop_constraint { alter_drop_index(@_) }
366
367 sub alter_drop_index {
368   my ($constraint) = @_;
369
370   return sprintf("DROP INDEX %s",
371       $constraint->name);
372 }
373
374 sub batch_alter_table {
375   my ($table, $diffs) = @_;
376
377   # If we have any of the following
378   #
379   #  rename_field
380   #  alter_field
381   #  drop_field
382   #
383   # we need to do the following <http://www.sqlite.org/faq.html#q11>
384   #
385   # BEGIN TRANSACTION;
386   # CREATE TEMPORARY TABLE t1_backup(a,b);
387   # INSERT INTO t1_backup SELECT a,b FROM t1;
388   # DROP TABLE t1;
389   # CREATE TABLE t1(a,b);
390   # INSERT INTO t1 SELECT a,b FROM t1_backup;
391   # DROP TABLE t1_backup;
392   # COMMIT;
393   #
394   # Fun, eh?
395   #
396   # If we have rename_field we do similarly.
397
398   my $table_name = $table->name;
399   my $renaming = $diffs->{rename_table} && @{$diffs->{rename_table}};
400
401   if ( @{$diffs->{rename_field}} == 0 &&
402        @{$diffs->{alter_field}}  == 0 &&
403        @{$diffs->{drop_field}}   == 0
404        ) {
405 #    return join("\n", map { 
406     return map { 
407         my $meth = __PACKAGE__->can($_) or die __PACKAGE__ . " cant $_";
408         map { my $sql = $meth->(ref $_ eq 'ARRAY' ? @$_ : $_); $sql ?  ("$sql") : () } @{ $diffs->{$_} }
409         
410       } grep { @{$diffs->{$_}} } 
411     qw/rename_table
412        alter_drop_constraint
413        alter_drop_index
414        drop_field
415        add_field
416        alter_field
417        rename_field
418        alter_create_index
419        alter_create_constraint
420        alter_table/;
421   }
422
423
424   my @sql;
425   my $old_table = $renaming ? $diffs->{rename_table}[0][0] : $table;
426   
427   do {
428     local $table->{name} = $table_name . '_temp_alter';
429     # We only want the table - dont care about indexes on tmp table
430     my ($table_sql) = create_table($table, {no_comments => 1, temporary_table => 1});
431     push @sql,$table_sql;
432   };
433
434   push @sql, "INSERT INTO @{[$table_name]}_temp_alter SELECT @{[ join(', ', $old_table->get_fields)]} FROM @{[$old_table]}",
435              "DROP TABLE @{[$old_table]}",
436              create_table($table, { no_comments => 1 }),
437              "INSERT INTO @{[$table_name]} SELECT @{[ join(', ', $old_table->get_fields)]} FROM @{[$table_name]}_temp_alter",
438              "DROP TABLE @{[$table_name]}_temp_alter";
439
440   return @sql;
441 #  return join("", @sql, "");
442 }
443
444 sub drop_table {
445   my ($table) = @_;
446   return "DROP TABLE $table";
447 }
448
449 sub rename_table {
450   my ($old_table, $new_table, $options) = @_;
451
452   my $qt = $options->{quote_table_names} || '';
453
454   return "ALTER TABLE $qt$old_table$qt RENAME TO $qt$new_table$qt";
455
456 }
457
458 # No-op. Just here to signify that we are a new style parser.
459 sub preproces_schema { }
460
461 1;
462
463 =pod
464
465 =head1 SEE ALSO
466
467 SQL::Translator, http://www.sqlite.org/.
468
469 =head1 AUTHOR
470
471 Ken Y. Clark C<< <kclark@cpan.orgE> >>.
472
473 Diff code added by Ash Berlin C<< <ash@cpan.org> >>.
474
475 =cut