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