Commit | Line | Data |
758ab1cd |
1 | package SQL::Translator::Producer::SQLite; |
2 | |
3 | # ------------------------------------------------------------------- |
478f608d |
4 | # Copyright (C) 2002-2009 SQLFairy Authors |
758ab1cd |
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 | |
20770e44 |
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 | |
758ab1cd |
38 | use strict; |
d0fcb05d |
39 | use warnings; |
758ab1cd |
40 | use Data::Dumper; |
b21bf652 |
41 | use SQL::Translator::Schema::Constants; |
5ee19df8 |
42 | use SQL::Translator::Utils qw(debug header_comment); |
758ab1cd |
43 | |
da06ac74 |
44 | use vars qw[ $VERSION $DEBUG $WARN ]; |
5ee19df8 |
45 | |
da06ac74 |
46 | $VERSION = '1.99'; |
5ee19df8 |
47 | $DEBUG = 0 unless defined $DEBUG; |
48 | $WARN = 0 unless defined $WARN; |
758ab1cd |
49 | |
d0fcb05d |
50 | our %used_identifiers = (); |
51 | our $max_id_length = 30; |
52 | our %global_names; |
53 | our %truncated; |
758ab1cd |
54 | |
758ab1cd |
55 | sub produce { |
a1d94525 |
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; |
d12f72ff |
62 | my $producer_args = $translator->producer_args; |
f9c96971 |
63 | my $sqlite_version = $producer_args->{sqlite_version} || 0; |
64 | my $no_txn = $producer_args->{no_transaction}; |
758ab1cd |
65 | |
1a24938d |
66 | debug("PKG: Beginning production\n"); |
758ab1cd |
67 | |
24d9fe69 |
68 | my @create = (); |
69 | push @create, header_comment unless ($no_comments); |
f9c96971 |
70 | $create[0] .= "\n\nBEGIN TRANSACTION" unless $no_txn; |
758ab1cd |
71 | |
b21bf652 |
72 | for my $table ( $schema->get_tables ) { |
24d9fe69 |
73 | push @create, create_table($table, { no_comments => $no_comments, |
d12f72ff |
74 | sqlite_version => $sqlite_version, |
4d438549 |
75 | add_drop_table => $add_drop_table,}); |
a5a882f0 |
76 | } |
77 | |
ec59a597 |
78 | for my $view ( $schema->get_views ) { |
24d9fe69 |
79 | push @create, create_view($view, { |
a25ac5d2 |
80 | add_drop_view => $add_drop_table, |
81 | no_comments => $no_comments, |
82 | }); |
ec59a597 |
83 | } |
84 | |
d0fcb05d |
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 | |
f9c96971 |
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 | } |
758ab1cd |
99 | } |
100 | |
758ab1cd |
101 | # ------------------------------------------------------------------- |
102 | sub mk_name { |
103 | my ($basename, $type, $scope, $critical) = @_; |
104 | my $basename_orig = $basename; |
d0fcb05d |
105 | my $max_name = !$max_id_length |
106 | ? length($type) + 1 |
107 | : $type |
108 | ? $max_id_length - (length($type) + 1) |
109 | : $max_id_length; |
758ab1cd |
110 | $basename = substr( $basename, 0, $max_name ) |
111 | if length( $basename ) > $max_name; |
cc74bccc |
112 | $basename =~ s/\./_/g; |
758ab1cd |
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 | |
ec59a597 |
139 | sub create_view { |
140 | my ($view, $options) = @_; |
a25ac5d2 |
141 | my $add_drop_view = $options->{add_drop_view}; |
ec59a597 |
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}; |
a25ac5d2 |
150 | $create .= "DROP VIEW IF EXISTS $view_name;\n" if $add_drop_view; |
ec59a597 |
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 ){ |
a25ac5d2 |
158 | $create .= " AS\n ${sql}"; |
ec59a597 |
159 | } |
ec59a597 |
160 | return $create; |
161 | } |
162 | |
163 | |
bfb5a568 |
164 | sub create_table |
165 | { |
166 | my ($table, $options) = @_; |
758ab1cd |
167 | |
bfb5a568 |
168 | my $table_name = $table->name; |
169 | my $no_comments = $options->{no_comments}; |
170 | my $add_drop_table = $options->{add_drop_table}; |
3db73ef8 |
171 | my $sqlite_version = $options->{sqlite_version} || 0; |
bfb5a568 |
172 | |
173 | debug("PKG: Looking at table '$table_name'\n"); |
174 | |
d0fcb05d |
175 | my ( @index_defs, @constraint_defs ); |
bfb5a568 |
176 | my @fields = $table->get_fields or die "No fields in $table_name"; |
177 | |
4d438549 |
178 | my $temp = $options->{temporary_table} ? 'TEMPORARY ' : ''; |
bfb5a568 |
179 | # |
180 | # Header. |
181 | # |
d12f72ff |
182 | my $exists = ($sqlite_version >= 3.3) ? ' IF EXISTS' : ''; |
24d9fe69 |
183 | my @create; |
d0fcb05d |
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"; |
bfb5a568 |
193 | |
194 | # |
195 | # Comments |
196 | # |
197 | if ( $table->comments and !$no_comments ){ |
24d9fe69 |
198 | $create_table .= "-- Comments: \n-- "; |
199 | $create_table .= join "\n-- ", $table->comments; |
200 | $create_table .= "\n--\n\n"; |
bfb5a568 |
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 | |
24d9fe69 |
242 | $create_table .= join(",\n", map { " $_" } @field_defs ) . "\n)"; |
bfb5a568 |
243 | |
d0fcb05d |
244 | return (@create, $create_table, @index_defs, @constraint_defs ); |
bfb5a568 |
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; |
bc8e2aa1 |
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 | ); |
bfb5a568 |
321 | } |
322 | |
323 | return $field_def; |
324 | |
325 | } |
326 | |
327 | sub create_index |
328 | { |
329 | my ($index, $options) = @_; |
330 | |
331 | my $name = $index->name; |
4d438549 |
332 | $name = mk_name($index->table->name, $name); |
333 | |
334 | my $type = $index->type eq 'UNIQUE' ? "UNIQUE " : ''; |
bfb5a568 |
335 | |
336 | # strip any field size qualifiers as SQLite doesn't like these |
337 | my @fields = map { s/\(\d+\)$//; $_ } $index->fields; |
cc74bccc |
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; |
bfb5a568 |
340 | my $index_def = |
907f8cea |
341 | "CREATE ${type}INDEX $name ON " . $index_table_name . |
342 | ' (' . join( ', ', @fields ) . ')'; |
bfb5a568 |
343 | |
344 | return $index_def; |
345 | } |
346 | |
347 | sub create_constraint |
348 | { |
349 | my ($c, $options) = @_; |
350 | |
351 | my $name = $c->name; |
4d438549 |
352 | $name = mk_name($c->table->name, $name); |
bfb5a568 |
353 | my @fields = $c->fields; |
cc74bccc |
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; |
bfb5a568 |
356 | |
357 | my $c_def = |
907f8cea |
358 | "CREATE UNIQUE INDEX $name ON " . $index_table_name . |
359 | ' (' . join( ', ', @fields ) . ')'; |
bfb5a568 |
360 | |
361 | return $c_def; |
362 | } |
363 | |
d0fcb05d |
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 = ""; |
d0fcb05d |
377 | |
f9c96971 |
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}; |
d0fcb05d |
384 | |
f9c96971 |
385 | $action = $trigger->action->{when} . " " |
386 | if $trigger->action->{when}; |
d0fcb05d |
387 | |
f9c96971 |
388 | my $steps = $trigger->action->{steps} || []; |
389 | |
390 | $action .= "BEGIN "; |
391 | for (@$steps) { |
392 | $action .= $_ . "; " |
393 | } |
394 | $action .= "END"; |
d0fcb05d |
395 | } |
d0fcb05d |
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 | |
4d438549 |
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 | |
ecf7f25d |
434 | return sprintf("DROP INDEX %s", |
435 | $constraint->name); |
4d438549 |
436 | } |
437 | |
438 | sub batch_alter_table { |
439 | my ($table, $diffs) = @_; |
440 | |
4d438549 |
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? |
46bf5655 |
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}}; |
4d438549 |
464 | |
465 | if ( @{$diffs->{rename_field}} == 0 && |
466 | @{$diffs->{alter_field}} == 0 && |
46bf5655 |
467 | @{$diffs->{drop_field}} == 0 |
468 | ) { |
24d9fe69 |
469 | # return join("\n", map { |
470 | return map { |
4d438549 |
471 | my $meth = __PACKAGE__->can($_) or die __PACKAGE__ . " cant $_"; |
24d9fe69 |
472 | map { my $sql = $meth->(ref $_ eq 'ARRAY' ? @$_ : $_); $sql ? ("$sql") : () } @{ $diffs->{$_} } |
4d438549 |
473 | |
46bf5655 |
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 |
24d9fe69 |
484 | alter_table/; |
4d438549 |
485 | } |
486 | |
487 | |
488 | my @sql; |
46bf5655 |
489 | my $old_table = $renaming ? $diffs->{rename_table}[0][0] : $table; |
4d438549 |
490 | |
491 | do { |
46bf5655 |
492 | local $table->{name} = $table_name . '_temp_alter'; |
4d438549 |
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 | |
46bf5655 |
498 | push @sql, "INSERT INTO @{[$table_name]}_temp_alter SELECT @{[ join(', ', $old_table->get_fields)]} FROM @{[$old_table]}", |
499 | "DROP TABLE @{[$old_table]}", |
4d438549 |
500 | create_table($table, { no_comments => 1 }), |
46bf5655 |
501 | "INSERT INTO @{[$table_name]} SELECT @{[ join(', ', $old_table->get_fields)]} FROM @{[$table_name]}_temp_alter", |
502 | "DROP TABLE @{[$table_name]}_temp_alter"; |
4d438549 |
503 | |
24d9fe69 |
504 | return @sql; |
505 | # return join("", @sql, ""); |
4d438549 |
506 | } |
507 | |
508 | sub drop_table { |
509 | my ($table) = @_; |
24d9fe69 |
510 | return "DROP TABLE $table"; |
4d438549 |
511 | } |
512 | |
46bf5655 |
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 | |
934e1b39 |
522 | # No-op. Just here to signify that we are a new style parser. |
523 | sub preproces_schema { } |
524 | |
bfb5a568 |
525 | 1; |
758ab1cd |
526 | |
20770e44 |
527 | =pod |
528 | |
529 | =head1 SEE ALSO |
530 | |
531 | SQL::Translator, http://www.sqlite.org/. |
758ab1cd |
532 | |
533 | =head1 AUTHOR |
534 | |
4d438549 |
535 | Ken Y. Clark C<< <kclark@cpan.orgE> >>. |
536 | |
537 | Diff code added by Ash Berlin C<< <ash@cpan.org> >>. |
20770e44 |
538 | |
539 | =cut |