Commit | Line | Data |
758ab1cd |
1 | package SQL::Translator::Producer::SQLite; |
2 | |
44659089 |
3 | # ------------------------------------------------------------------- |
4 | # Copyright (C) 2002-2009 SQLFairy Authors |
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; |
7b4b17aa |
42 | use SQL::Translator::Utils qw(debug header_comment parse_dbms_version); |
758ab1cd |
43 | |
da06ac74 |
44 | use vars qw[ $VERSION $DEBUG $WARN ]; |
5ee19df8 |
45 | |
11ad2df9 |
46 | $VERSION = '1.59'; |
47 | $DEBUG = 0 unless defined $DEBUG; |
48 | $WARN = 0 unless defined $WARN; |
758ab1cd |
49 | |
11ad2df9 |
50 | our $max_id_length = 30; |
ae602737 |
51 | my %global_names; |
758ab1cd |
52 | |
758ab1cd |
53 | sub produce { |
a1d94525 |
54 | my $translator = shift; |
55 | local $DEBUG = $translator->debug; |
56 | local $WARN = $translator->show_warnings; |
57 | my $no_comments = $translator->no_comments; |
58 | my $add_drop_table = $translator->add_drop_table; |
59 | my $schema = $translator->schema; |
d12f72ff |
60 | my $producer_args = $translator->producer_args; |
7b4b17aa |
61 | my $sqlite_version = parse_dbms_version( |
62 | $producer_args->{sqlite_version}, 'perl' |
63 | ); |
f9c96971 |
64 | my $no_txn = $producer_args->{no_transaction}; |
758ab1cd |
65 | |
1a24938d |
66 | debug("PKG: Beginning production\n"); |
758ab1cd |
67 | |
ae602737 |
68 | %global_names = (); #reset |
69 | |
b6b0696f |
70 | |
71 | my $head = (header_comment() . "\n") unless $no_comments; |
72 | |
24d9fe69 |
73 | my @create = (); |
b6b0696f |
74 | |
75 | push @create, "BEGIN TRANSACTION" unless $no_txn; |
758ab1cd |
76 | |
b21bf652 |
77 | for my $table ( $schema->get_tables ) { |
24d9fe69 |
78 | push @create, create_table($table, { no_comments => $no_comments, |
d12f72ff |
79 | sqlite_version => $sqlite_version, |
4d438549 |
80 | add_drop_table => $add_drop_table,}); |
a5a882f0 |
81 | } |
82 | |
ec59a597 |
83 | for my $view ( $schema->get_views ) { |
24d9fe69 |
84 | push @create, create_view($view, { |
a25ac5d2 |
85 | add_drop_view => $add_drop_table, |
86 | no_comments => $no_comments, |
87 | }); |
ec59a597 |
88 | } |
89 | |
d0fcb05d |
90 | for my $trigger ( $schema->get_triggers ) { |
91 | push @create, create_trigger($trigger, { |
92 | add_drop_trigger => $add_drop_table, |
93 | no_comments => $no_comments, |
94 | }); |
95 | } |
96 | |
b6b0696f |
97 | push @create, "COMMIT" unless $no_txn; |
98 | |
f9c96971 |
99 | if (wantarray) { |
b6b0696f |
100 | return ($head||(), @create); |
f9c96971 |
101 | } else { |
b6b0696f |
102 | return join ('', |
103 | $head||(), |
104 | join(";\n\n", @create ), |
105 | ";\n", |
106 | ); |
f9c96971 |
107 | } |
758ab1cd |
108 | } |
109 | |
758ab1cd |
110 | sub mk_name { |
ae602737 |
111 | my ($name, $scope, $critical) = @_; |
758ab1cd |
112 | |
113 | $scope ||= \%global_names; |
114 | if ( my $prev = $scope->{ $name } ) { |
115 | my $name_orig = $name; |
116 | $name .= sprintf( "%02d", ++$prev ); |
ea93df61 |
117 | substr($name, $max_id_length - 3) = "00" |
11ad2df9 |
118 | if length( $name ) > $max_id_length; |
758ab1cd |
119 | |
120 | warn "The name '$name_orig' has been changed to ", |
121 | "'$name' to make it unique.\n" if $WARN; |
122 | |
123 | $scope->{ $name_orig }++; |
124 | } |
125 | |
126 | $scope->{ $name }++; |
127 | return $name; |
128 | } |
129 | |
ec59a597 |
130 | sub create_view { |
131 | my ($view, $options) = @_; |
a25ac5d2 |
132 | my $add_drop_view = $options->{add_drop_view}; |
ec59a597 |
133 | |
134 | my $view_name = $view->name; |
56785c01 |
135 | $global_names{$view_name} = 1; |
136 | |
ec59a597 |
137 | debug("PKG: Looking at view '${view_name}'\n"); |
138 | |
139 | # Header. Should this look like what mysqldump produces? |
140 | my $extra = $view->extra; |
4c0d31c1 |
141 | my @create; |
142 | push @create, "DROP VIEW IF EXISTS $view_name" if $add_drop_view; |
143 | |
144 | my $create_view = 'CREATE'; |
145 | $create_view .= " TEMPORARY" if exists($extra->{temporary}) && $extra->{temporary}; |
146 | $create_view .= ' VIEW'; |
147 | $create_view .= " IF NOT EXISTS" if exists($extra->{if_not_exists}) && $extra->{if_not_exists}; |
148 | $create_view .= " ${view_name}"; |
ec59a597 |
149 | |
150 | if( my $sql = $view->sql ){ |
4c0d31c1 |
151 | $create_view .= " AS\n ${sql}"; |
ec59a597 |
152 | } |
4c0d31c1 |
153 | push @create, $create_view; |
154 | |
155 | # Tack the comment onto the first statement. |
156 | unless ($options->{no_comments}) { |
157 | $create[0] = "--\n-- View: ${view_name}\n--\n" . $create[0]; |
158 | } |
159 | |
160 | return @create; |
ec59a597 |
161 | } |
162 | |
163 | |
bfb5a568 |
164 | sub create_table |
165 | { |
166 | my ($table, $options) = @_; |
758ab1cd |
167 | |
bfb5a568 |
168 | my $table_name = $table->name; |
56785c01 |
169 | $global_names{$table_name} = 1; |
170 | |
bfb5a568 |
171 | my $no_comments = $options->{no_comments}; |
172 | my $add_drop_table = $options->{add_drop_table}; |
3db73ef8 |
173 | my $sqlite_version = $options->{sqlite_version} || 0; |
bfb5a568 |
174 | |
175 | debug("PKG: Looking at table '$table_name'\n"); |
176 | |
d0fcb05d |
177 | my ( @index_defs, @constraint_defs ); |
bfb5a568 |
178 | my @fields = $table->get_fields or die "No fields in $table_name"; |
179 | |
4d438549 |
180 | my $temp = $options->{temporary_table} ? 'TEMPORARY ' : ''; |
bfb5a568 |
181 | # |
182 | # Header. |
183 | # |
7b4b17aa |
184 | my $exists = ($sqlite_version >= 3.003) ? ' IF EXISTS' : ''; |
24d9fe69 |
185 | my @create; |
d0fcb05d |
186 | my ($comment, $create_table) = ""; |
187 | $comment = "--\n-- Table: $table_name\n--\n" unless $no_comments; |
188 | if ($add_drop_table) { |
189 | push @create, $comment . qq[DROP TABLE$exists $table_name]; |
190 | } else { |
191 | $create_table = $comment; |
192 | } |
193 | |
194 | $create_table .= "CREATE ${temp}TABLE $table_name (\n"; |
bfb5a568 |
195 | |
196 | # |
197 | # Comments |
198 | # |
199 | if ( $table->comments and !$no_comments ){ |
24d9fe69 |
200 | $create_table .= "-- Comments: \n-- "; |
201 | $create_table .= join "\n-- ", $table->comments; |
202 | $create_table .= "\n--\n\n"; |
bfb5a568 |
203 | } |
204 | |
205 | # |
206 | # How many fields in PK? |
207 | # |
208 | my $pk = $table->primary_key; |
209 | my @pk_fields = $pk ? $pk->fields : (); |
210 | |
211 | # |
212 | # Fields |
213 | # |
214 | my ( @field_defs, $pk_set ); |
215 | for my $field ( @fields ) { |
216 | push @field_defs, create_field($field); |
217 | } |
218 | |
ea93df61 |
219 | if ( |
220 | scalar @pk_fields > 1 |
221 | || |
222 | ( @pk_fields && !grep /INTEGER PRIMARY KEY/, @field_defs ) |
bfb5a568 |
223 | ) { |
224 | push @field_defs, 'PRIMARY KEY (' . join(', ', @pk_fields ) . ')'; |
225 | } |
226 | |
227 | # |
228 | # Indices |
229 | # |
230 | my $idx_name_default = 'A'; |
231 | for my $index ( $table->get_indices ) { |
232 | push @index_defs, create_index($index); |
233 | } |
234 | |
235 | # |
236 | # Constraints |
237 | # |
238 | my $c_name_default = 'A'; |
239 | for my $c ( $table->get_constraints ) { |
0dbd2362 |
240 | if ($c->type eq "FOREIGN KEY") { |
241 | push @field_defs, create_foreignkey($c); |
242 | } |
ea93df61 |
243 | next unless $c->type eq UNIQUE; |
bfb5a568 |
244 | push @constraint_defs, create_constraint($c); |
245 | } |
246 | |
24d9fe69 |
247 | $create_table .= join(",\n", map { " $_" } @field_defs ) . "\n)"; |
bfb5a568 |
248 | |
d0fcb05d |
249 | return (@create, $create_table, @index_defs, @constraint_defs ); |
bfb5a568 |
250 | } |
251 | |
0dbd2362 |
252 | sub create_foreignkey { |
253 | my $c = shift; |
254 | |
255 | my $fk_sql = "FOREIGN KEY($c->{fields}[0]) REFERENCES "; |
256 | $fk_sql .= ( $c->{reference_table} || '' )."(".( $c->{reference_fields}[0] || '' ).")"; |
257 | |
258 | return $fk_sql; |
259 | } |
260 | |
bfb5a568 |
261 | sub create_field |
262 | { |
263 | my ($field, $options) = @_; |
264 | |
265 | my $field_name = $field->name; |
266 | debug("PKG: Looking at field '$field_name'\n"); |
ea93df61 |
267 | my $field_comments = $field->comments |
268 | ? "-- " . $field->comments . "\n " |
bfb5a568 |
269 | : ''; |
270 | |
271 | my $field_def = $field_comments.$field_name; |
272 | |
273 | # data type and size |
274 | my $size = $field->size; |
275 | my $data_type = $field->data_type; |
276 | $data_type = 'varchar' if lc $data_type eq 'set'; |
277 | $data_type = 'blob' if lc $data_type eq 'bytea'; |
278 | |
279 | if ( lc $data_type =~ /(text|blob)/i ) { |
280 | $size = undef; |
281 | } |
282 | |
283 | # if ( $data_type =~ /timestamp/i ) { |
ea93df61 |
284 | # push @trigger_defs, |
bfb5a568 |
285 | # "CREATE TRIGGER ts_${table_name} ". |
286 | # "after insert on $table_name\n". |
287 | # "begin\n". |
288 | # " update $table_name set $field_name=timestamp() ". |
289 | # "where id=new.id;\n". |
290 | # "end;\n" |
291 | # ; |
292 | # |
293 | # } |
294 | |
295 | # |
296 | # SQLite is generally typeless, but newer versions will |
297 | # make a field autoincrement if it is declared as (and |
298 | # *only* as) INTEGER PRIMARY KEY |
299 | # |
300 | my $pk = $field->table->primary_key; |
301 | my @pk_fields = $pk ? $pk->fields : (); |
302 | |
ea93df61 |
303 | if ( |
304 | $field->is_primary_key && |
bfb5a568 |
305 | scalar @pk_fields == 1 && |
306 | ( |
307 | $data_type =~ /int(eger)?$/i |
308 | || |
309 | ( $data_type =~ /^number?$/i && $size !~ /,/ ) |
310 | ) |
311 | ) { |
312 | $data_type = 'INTEGER PRIMARY KEY'; |
313 | $size = undef; |
314 | # $pk_set = 1; |
315 | } |
316 | |
ea93df61 |
317 | $field_def .= sprintf " %s%s", $data_type, |
bfb5a568 |
318 | ( !$field->is_auto_increment && $size ) ? "($size)" : ''; |
319 | |
320 | # Null? |
321 | $field_def .= ' NOT NULL' unless $field->is_nullable; |
322 | |
06baeb21 |
323 | # Default? |
324 | SQL::Translator::Producer->_apply_default_value( |
325 | $field, |
326 | \$field_def, |
327 | [ |
328 | 'NULL' => \'NULL', |
329 | 'now()' => 'now()', |
330 | 'CURRENT_TIMESTAMP' => 'CURRENT_TIMESTAMP', |
331 | ], |
332 | ); |
bfb5a568 |
333 | |
334 | return $field_def; |
335 | |
336 | } |
337 | |
338 | sub create_index |
339 | { |
340 | my ($index, $options) = @_; |
341 | |
342 | my $name = $index->name; |
ae602737 |
343 | $name = mk_name($name); |
4d438549 |
344 | |
ea93df61 |
345 | my $type = $index->type eq 'UNIQUE' ? "UNIQUE " : ''; |
bfb5a568 |
346 | |
347 | # strip any field size qualifiers as SQLite doesn't like these |
348 | my @fields = map { s/\(\d+\)$//; $_ } $index->fields; |
cc74bccc |
349 | (my $index_table_name = $index->table->name) =~ s/^.+?\.//; # table name may not specify schema |
350 | warn "removing schema name from '" . $index->table->name . "' to make '$index_table_name'\n" if $WARN; |
ea93df61 |
351 | my $index_def = |
907f8cea |
352 | "CREATE ${type}INDEX $name ON " . $index_table_name . |
353 | ' (' . join( ', ', @fields ) . ')'; |
bfb5a568 |
354 | |
355 | return $index_def; |
356 | } |
357 | |
358 | sub create_constraint |
359 | { |
360 | my ($c, $options) = @_; |
361 | |
362 | my $name = $c->name; |
ae602737 |
363 | $name = mk_name($name); |
bfb5a568 |
364 | my @fields = $c->fields; |
cc74bccc |
365 | (my $index_table_name = $c->table->name) =~ s/^.+?\.//; # table name may not specify schema |
366 | warn "removing schema name from '" . $c->table->name . "' to make '$index_table_name'\n" if $WARN; |
bfb5a568 |
367 | |
ea93df61 |
368 | my $c_def = |
907f8cea |
369 | "CREATE UNIQUE INDEX $name ON " . $index_table_name . |
370 | ' (' . join( ', ', @fields ) . ')'; |
bfb5a568 |
371 | |
372 | return $c_def; |
373 | } |
374 | |
d0fcb05d |
375 | sub create_trigger { |
376 | my ($trigger, $options) = @_; |
377 | my $add_drop = $options->{add_drop_trigger}; |
378 | |
410d4a42 |
379 | my @statements; |
d0fcb05d |
380 | |
410d4a42 |
381 | my $trigger_name = $trigger->name; |
56785c01 |
382 | $global_names{$trigger_name} = 1; |
383 | |
d0fcb05d |
384 | my $events = $trigger->database_events; |
410d4a42 |
385 | for my $evt ( @$events ) { |
d0fcb05d |
386 | |
410d4a42 |
387 | my $trig_name = $trigger_name; |
388 | if (@$events > 1) { |
389 | $trig_name .= "_$evt"; |
d0fcb05d |
390 | |
410d4a42 |
391 | warn "Multiple database events supplied for trigger '$trigger_name', ", |
392 | "creating trigger '$trig_name' for the '$evt' event.\n" if $WARN; |
393 | } |
d0fcb05d |
394 | |
410d4a42 |
395 | push @statements, "DROP TRIGGER IF EXISTS $trig_name" if $add_drop; |
d0fcb05d |
396 | |
f9c96971 |
397 | |
410d4a42 |
398 | $DB::single = 1; |
399 | my $action = ""; |
400 | if (not ref $trigger->action) { |
401 | $action .= "BEGIN " . $trigger->action . " END"; |
f9c96971 |
402 | } |
410d4a42 |
403 | else { |
404 | $action = $trigger->action->{for_each} . " " |
405 | if $trigger->action->{for_each}; |
406 | |
407 | $action = $trigger->action->{when} . " " |
408 | if $trigger->action->{when}; |
d0fcb05d |
409 | |
410d4a42 |
410 | my $steps = $trigger->action->{steps} || []; |
411 | |
412 | $action .= "BEGIN "; |
413 | $action .= $_ . "; " for (@$steps); |
414 | $action .= "END"; |
415 | } |
416 | |
417 | push @statements, sprintf ( |
418 | 'CREATE TRIGGER %s %s %s on %s %s', |
419 | $trig_name, |
420 | $trigger->perform_action_when, |
421 | $evt, |
422 | $trigger->on_table, |
423 | $action |
424 | ); |
425 | } |
d0fcb05d |
426 | |
410d4a42 |
427 | return @statements; |
d0fcb05d |
428 | } |
429 | |
4d438549 |
430 | sub alter_table { } # Noop |
431 | |
432 | sub add_field { |
433 | my ($field) = @_; |
434 | |
435 | return sprintf("ALTER TABLE %s ADD COLUMN %s", |
436 | $field->table->name, create_field($field)) |
437 | } |
438 | |
439 | sub alter_create_index { |
440 | my ($index) = @_; |
441 | |
442 | # This might cause name collisions |
443 | return create_index($index); |
444 | } |
445 | |
446 | sub alter_create_constraint { |
447 | my ($constraint) = @_; |
448 | |
449 | return create_constraint($constraint) if $constraint->type eq 'UNIQUE'; |
450 | } |
451 | |
452 | sub alter_drop_constraint { alter_drop_index(@_) } |
453 | |
454 | sub alter_drop_index { |
455 | my ($constraint) = @_; |
456 | |
ecf7f25d |
457 | return sprintf("DROP INDEX %s", |
458 | $constraint->name); |
4d438549 |
459 | } |
460 | |
461 | sub batch_alter_table { |
462 | my ($table, $diffs) = @_; |
463 | |
4d438549 |
464 | # If we have any of the following |
465 | # |
466 | # rename_field |
467 | # alter_field |
468 | # drop_field |
469 | # |
470 | # we need to do the following <http://www.sqlite.org/faq.html#q11> |
471 | # |
472 | # BEGIN TRANSACTION; |
473 | # CREATE TEMPORARY TABLE t1_backup(a,b); |
474 | # INSERT INTO t1_backup SELECT a,b FROM t1; |
475 | # DROP TABLE t1; |
476 | # CREATE TABLE t1(a,b); |
477 | # INSERT INTO t1 SELECT a,b FROM t1_backup; |
478 | # DROP TABLE t1_backup; |
479 | # COMMIT; |
480 | # |
481 | # Fun, eh? |
46bf5655 |
482 | # |
483 | # If we have rename_field we do similarly. |
484 | |
485 | my $table_name = $table->name; |
486 | my $renaming = $diffs->{rename_table} && @{$diffs->{rename_table}}; |
4d438549 |
487 | |
488 | if ( @{$diffs->{rename_field}} == 0 && |
489 | @{$diffs->{alter_field}} == 0 && |
46bf5655 |
490 | @{$diffs->{drop_field}} == 0 |
491 | ) { |
ea93df61 |
492 | # return join("\n", map { |
493 | return map { |
4d438549 |
494 | my $meth = __PACKAGE__->can($_) or die __PACKAGE__ . " cant $_"; |
24d9fe69 |
495 | map { my $sql = $meth->(ref $_ eq 'ARRAY' ? @$_ : $_); $sql ? ("$sql") : () } @{ $diffs->{$_} } |
ea93df61 |
496 | |
497 | } grep { @{$diffs->{$_}} } |
46bf5655 |
498 | qw/rename_table |
499 | alter_drop_constraint |
500 | alter_drop_index |
501 | drop_field |
502 | add_field |
503 | alter_field |
504 | rename_field |
505 | alter_create_index |
506 | alter_create_constraint |
24d9fe69 |
507 | alter_table/; |
4d438549 |
508 | } |
509 | |
510 | |
511 | my @sql; |
46bf5655 |
512 | my $old_table = $renaming ? $diffs->{rename_table}[0][0] : $table; |
ea93df61 |
513 | |
4d438549 |
514 | do { |
46bf5655 |
515 | local $table->{name} = $table_name . '_temp_alter'; |
4d438549 |
516 | # We only want the table - dont care about indexes on tmp table |
517 | my ($table_sql) = create_table($table, {no_comments => 1, temporary_table => 1}); |
518 | push @sql,$table_sql; |
519 | }; |
520 | |
46bf5655 |
521 | push @sql, "INSERT INTO @{[$table_name]}_temp_alter SELECT @{[ join(', ', $old_table->get_fields)]} FROM @{[$old_table]}", |
522 | "DROP TABLE @{[$old_table]}", |
4d438549 |
523 | create_table($table, { no_comments => 1 }), |
46bf5655 |
524 | "INSERT INTO @{[$table_name]} SELECT @{[ join(', ', $old_table->get_fields)]} FROM @{[$table_name]}_temp_alter", |
525 | "DROP TABLE @{[$table_name]}_temp_alter"; |
4d438549 |
526 | |
24d9fe69 |
527 | return @sql; |
528 | # return join("", @sql, ""); |
4d438549 |
529 | } |
530 | |
531 | sub drop_table { |
532 | my ($table) = @_; |
24d9fe69 |
533 | return "DROP TABLE $table"; |
4d438549 |
534 | } |
535 | |
46bf5655 |
536 | sub rename_table { |
537 | my ($old_table, $new_table, $options) = @_; |
538 | |
539 | my $qt = $options->{quote_table_names} || ''; |
540 | |
541 | return "ALTER TABLE $qt$old_table$qt RENAME TO $qt$new_table$qt"; |
542 | |
543 | } |
544 | |
934e1b39 |
545 | # No-op. Just here to signify that we are a new style parser. |
546 | sub preproces_schema { } |
547 | |
bfb5a568 |
548 | 1; |
758ab1cd |
549 | |
20770e44 |
550 | =pod |
551 | |
552 | =head1 SEE ALSO |
553 | |
554 | SQL::Translator, http://www.sqlite.org/. |
758ab1cd |
555 | |
556 | =head1 AUTHOR |
557 | |
11ad2df9 |
558 | Ken Youens-Clark C<< <kclark@cpan.orgE> >>. |
4d438549 |
559 | |
560 | Diff code added by Ash Berlin C<< <ash@cpan.org> >>. |
20770e44 |
561 | |
562 | =cut |