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