MAss diff changes imported from Ash's local diff-refactor branch
[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 #    $create .= "COMMIT;\n";
80
81     return wantarray ? ($create, @table_defs, "COMMIT;\n") : join("\n", ($create, @table_defs, "COMMIT;\n"));
82 }
83
84 # -------------------------------------------------------------------
85 sub mk_name {
86     my ($basename, $type, $scope, $critical) = @_;
87     my $basename_orig = $basename;
88     my $max_name      = $type 
89                         ? $max_id_length - (length($type) + 1) 
90                         : $max_id_length;
91     $basename         = substr( $basename, 0, $max_name ) 
92                         if length( $basename ) > $max_name;
93     my $name          = $type ? "${type}_$basename" : $basename;
94
95     if ( $basename ne $basename_orig and $critical ) {
96         my $show_type = $type ? "+'$type'" : "";
97         warn "Truncating '$basename_orig'$show_type to $max_id_length ",
98             "character limit to make '$name'\n" if $WARN;
99         $truncated{ $basename_orig } = $name;
100     }
101
102     $scope ||= \%global_names;
103     if ( my $prev = $scope->{ $name } ) {
104         my $name_orig = $name;
105         $name        .= sprintf( "%02d", ++$prev );
106         substr($name, $max_id_length - 3) = "00" 
107             if length( $name ) > $max_id_length;
108
109         warn "The name '$name_orig' has been changed to ",
110              "'$name' to make it unique.\n" if $WARN;
111
112         $scope->{ $name_orig }++;
113     }
114
115     $scope->{ $name }++;
116     return $name;
117 }
118
119 sub create_table
120 {
121     my ($table, $options) = @_;
122
123     my $table_name = $table->name;
124     my $no_comments = $options->{no_comments};
125     my $add_drop_table = $options->{add_drop_table};
126
127     debug("PKG: Looking at table '$table_name'\n");
128
129     my ( @index_defs, @constraint_defs, @trigger_defs );
130     my @fields = $table->get_fields or die "No fields in $table_name";
131
132     my $temp = $options->{temporary_table} ? 'TEMPORARY ' : '';
133     #
134     # Header.
135     #
136     my $create = '';
137     $create .= "--\n-- Table: $table_name\n--\n" unless $no_comments;
138     $create .= qq[DROP TABLE $table_name;\n] if $add_drop_table;
139     $create .= "CREATE ${temp}TABLE $table_name (\n";
140
141     #
142     # Comments
143     #
144     if ( $table->comments and !$no_comments ){
145         $create .= "-- Comments: \n-- ";
146         $create .= join "\n-- ",  $table->comments;
147         $create .= "\n--\n\n";
148     }
149
150     #
151     # How many fields in PK?
152     #
153     my $pk        = $table->primary_key;
154     my @pk_fields = $pk ? $pk->fields : ();
155
156     #
157     # Fields
158     #
159     my ( @field_defs, $pk_set );
160     for my $field ( @fields ) {
161         push @field_defs, create_field($field);
162     }
163
164     if ( 
165          scalar @pk_fields > 1 
166          || 
167          ( @pk_fields && !grep /INTEGER PRIMARY KEY/, @field_defs ) 
168          ) {
169         push @field_defs, 'PRIMARY KEY (' . join(', ', @pk_fields ) . ')';
170     }
171
172     #
173     # Indices
174     #
175     my $idx_name_default = 'A';
176     for my $index ( $table->get_indices ) {
177         push @index_defs, create_index($index);
178     }
179
180     #
181     # Constraints
182     #
183     my $c_name_default = 'A';
184     for my $c ( $table->get_constraints ) {
185         next unless $c->type eq UNIQUE; 
186         push @constraint_defs, create_constraint($c);
187     }
188
189     $create .= join(",\n", map { "  $_" } @field_defs ) . "\n)";
190
191     return ($create, @index_defs, @constraint_defs, @trigger_defs );
192 }
193
194 sub create_field
195 {
196     my ($field, $options) = @_;
197
198     my $field_name = $field->name;
199     debug("PKG: Looking at field '$field_name'\n");
200     my $field_comments = $field->comments 
201         ? "-- " . $field->comments . "\n  " 
202         : '';
203
204     my $field_def = $field_comments.$field_name;
205
206     # data type and size
207     my $size      = $field->size;
208     my $data_type = $field->data_type;
209     $data_type    = 'varchar' if lc $data_type eq 'set';
210     $data_type  = 'blob' if lc $data_type eq 'bytea';
211
212     if ( lc $data_type =~ /(text|blob)/i ) {
213         $size = undef;
214     }
215
216 #             if ( $data_type =~ /timestamp/i ) {
217 #                 push @trigger_defs, 
218 #                     "CREATE TRIGGER ts_${table_name} ".
219 #                     "after insert on $table_name\n".
220 #                     "begin\n".
221 #                     "  update $table_name set $field_name=timestamp() ".
222 #                        "where id=new.id;\n".
223 #                     "end;\n"
224 #                 ;
225 #
226 #            }
227
228     #
229     # SQLite is generally typeless, but newer versions will
230     # make a field autoincrement if it is declared as (and
231     # *only* as) INTEGER PRIMARY KEY
232     #
233     my $pk        = $field->table->primary_key;
234     my @pk_fields = $pk ? $pk->fields : ();
235
236     if ( 
237          $field->is_primary_key && 
238          scalar @pk_fields == 1 &&
239          (
240           $data_type =~ /int(eger)?$/i
241           ||
242           ( $data_type =~ /^number?$/i && $size !~ /,/ )
243           )
244          ) {
245         $data_type = 'INTEGER PRIMARY KEY';
246         $size      = undef;
247 #        $pk_set    = 1;
248     }
249
250     $field_def .= sprintf " %s%s", $data_type, 
251     ( !$field->is_auto_increment && $size ) ? "($size)" : '';
252
253     # Null?
254     $field_def .= ' NOT NULL' unless $field->is_nullable;
255
256     # Default?  XXX Need better quoting!
257     my $default = $field->default_value;
258     if ( defined $default ) {
259         if ( uc $default eq 'NULL') {
260             $field_def .= ' DEFAULT NULL';
261         } elsif ( $default eq 'now()' ||
262                   $default eq 'CURRENT_TIMESTAMP' ) {
263             $field_def .= ' DEFAULT CURRENT_TIMESTAMP';
264         } elsif ( $default =~ /val\(/ ) {
265             next;
266         } else {
267             $field_def .= " DEFAULT '$default'";
268         }
269     }
270
271     return $field_def;
272
273 }
274
275 sub create_index
276 {
277     my ($index, $options) = @_;
278
279     my $name   = $index->name;
280     $name      = mk_name($index->table->name, $name);
281
282     my $type   = $index->type eq 'UNIQUE' ? "UNIQUE " : ''; 
283
284     # strip any field size qualifiers as SQLite doesn't like these
285     my @fields = map { s/\(\d+\)$//; $_ } $index->fields;
286     my $index_def =  
287     "CREATE ${type}INDEX $name ON " . $index->table->name .
288         ' (' . join( ', ', @fields ) . ')';
289
290     return $index_def;
291 }
292
293 sub create_constraint
294 {
295     my ($c, $options) = @_;
296
297     my $name   = $c->name;
298     $name      = mk_name($c->table->name, $name);
299     my @fields = $c->fields;
300
301     my $c_def =  
302     "CREATE UNIQUE INDEX $name ON " . $c->table->name .
303         ' (' . join( ', ', @fields ) . ')';
304
305     return $c_def;
306 }
307
308 sub alter_table { } # Noop
309
310 sub add_field {
311   my ($field) = @_;
312
313   return sprintf("ALTER TABLE %s ADD COLUMN %s",
314       $field->table->name, create_field($field))
315 }
316
317 sub alter_create_index {
318   my ($index) = @_;
319
320   # This might cause name collisions
321   return create_index($index);
322 }
323
324 sub alter_create_constraint {
325   my ($constraint) = @_;
326
327   return create_constraint($constraint) if $constraint->type eq 'UNIQUE';
328 }
329
330 sub alter_drop_constraint { alter_drop_index(@_) }
331
332 sub alter_drop_index {
333   my ($constraint) = @_;
334
335   return sprintf("DROP INDEX %s ON %s",
336       $constraint->name, $constraint->table->name);
337 }
338
339 sub batch_alter_table {
340   my ($table, $diffs) = @_;
341
342   $DB::single = 1 if $table->name eq 'deleted'; 
343
344   # If we have any of the following
345   #
346   #  rename_field
347   #  alter_field
348   #  drop_field
349   #
350   # we need to do the following <http://www.sqlite.org/faq.html#q11>
351   #
352   # BEGIN TRANSACTION;
353   # CREATE TEMPORARY TABLE t1_backup(a,b);
354   # INSERT INTO t1_backup SELECT a,b FROM t1;
355   # DROP TABLE t1;
356   # CREATE TABLE t1(a,b);
357   # INSERT INTO t1 SELECT a,b FROM t1_backup;
358   # DROP TABLE t1_backup;
359   # COMMIT;
360   #
361   # Fun, eh?
362
363   if ( @{$diffs->{rename_field}} == 0 &&
364        @{$diffs->{alter_field}}  == 0 &&
365        @{$diffs->{drop_field}}   == 0) {
366     return join("\n", map { 
367         my $meth = __PACKAGE__->can($_) or die __PACKAGE__ . " cant $_";
368         map { my $sql = $meth->(ref $_ eq 'ARRAY' ? @$_ : $_); $sql ?  ("$sql;") : () } @{ $diffs->{$_} }
369         
370       } grep { @{$diffs->{$_}} } keys %$diffs);
371   }
372
373
374   my @sql;
375   
376   do {
377     local $table->{name} = $table->name . '_temp_alter';
378     # We only want the table - dont care about indexes on tmp table
379     my ($table_sql) = create_table($table, {no_comments => 1, temporary_table => 1});
380     push @sql,$table_sql;
381   };
382
383   push @sql, "INSERT INTO @{[$table]}_temp_alter SELECT @{[ join(', ', $table->get_fields)]} FROM @{[$table]}",
384              "DROP TABLE @{[$table]}",
385              create_table($table, { no_comments => 1 }),
386              "INSERT INTO @{[$table]} SELECT @{[ join(', ', $table->get_fields)]} FROM @{[$table]}_temp_alter",
387              "DROP TABLE @{[$table]}_temp_alter";
388
389   return join(";\n", @sql, "");
390 }
391
392 sub drop_table {
393   my ($table) = @_;
394   return "DROP TABLE $table;";
395 }
396
397 1;
398
399 =pod
400
401 =head1 SEE ALSO
402
403 SQL::Translator, http://www.sqlite.org/.
404
405 =head1 AUTHOR
406
407 Ken Y. Clark C<< <kclark@cpan.orgE> >>.
408
409 Diff code added by Ash Berlin C<< <ash@cpan.org> >>.
410
411 =cut