1 package SQL::Translator::Producer::SQLite;
3 # -------------------------------------------------------------------
4 # $Id: SQLite.pm,v 1.9 2003-10-15 19:09:15 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7 # darren chamberlain <darren@cpan.org>,
8 # Chris Mungall <cjm@fruitfly.org>
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License as
12 # published by the Free Software Foundation; version 2.
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 # -------------------------------------------------------------------
27 SQL::Translator::Producer::SQLite - SQLite producer for SQL::Translator
33 my $t = SQL::Translator->new( parser => '...', producer => 'SQLite' );
38 This module will produce text output of the schema suitable for SQLite.
44 use SQL::Translator::Schema::Constants;
45 use SQL::Translator::Utils qw(debug header_comment);
47 use vars qw[ $VERSION $DEBUG $WARN ];
49 $VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/;
50 $DEBUG = 0 unless defined $DEBUG;
51 $WARN = 0 unless defined $WARN;
53 my %used_identifiers = ();
54 my $max_id_length = 30;
59 my $translator = shift;
60 local $DEBUG = $translator->debug;
61 local $WARN = $translator->show_warnings;
62 my $no_comments = $translator->no_comments;
63 my $add_drop_table = $translator->add_drop_table;
64 my $schema = $translator->schema;
66 debug("PKG: Beginning production\n");
69 $create .= header_comment unless ($no_comments);
70 $create .= "BEGIN TRANSACTION;\n\n";
72 my ( @index_defs, @constraint_defs, @trigger_defs );
73 for my $table ( $schema->get_tables ) {
74 my $table_name = $table->name;
75 debug("PKG: Looking at table '$table_name'\n");
77 my @fields = $table->get_fields or die "No fields in $table_name";
82 $create .= "--\n-- Table: $table_name\n--\n" unless $no_comments;
83 $create .= qq[DROP TABLE $table_name;\n] if $add_drop_table;
84 $create .= "CREATE TABLE $table_name (\n";
87 # How many fields in PK?
89 my $pk = $table->primary_key;
90 my @pk_fields = $pk ? $pk->fields : ();
95 my ( @field_defs, $pk_set );
96 for my $field ( @fields ) {
97 my $field_name = $field->name;
98 debug("PKG: Looking at field '$field_name'\n");
99 my $field_def = $field_name;
102 my $size = $field->size;
103 my $data_type = $field->data_type;
104 $data_type = 'varchar' if lc $data_type eq 'set';
106 if ( $data_type =~ /timestamp/i ) {
108 "CREATE TRIGGER ts_${table_name} ".
109 "after insert on $table_name\n".
111 " update $table_name set $field_name=timestamp() ".
112 "where id=new.id;\n".
119 # SQLite is generally typeless, but newer versions will
120 # make a field autoincrement if it is declared as (and
121 # *only* as) INTEGER PRIMARY KEY
124 $field->is_primary_key &&
125 scalar @pk_fields == 1 &&
127 $data_type =~ /^int(eger)?$/i
129 ( $data_type =~ /^number?$/i && $size !~ /,/ )
132 $data_type = 'INTEGER PRIMARY KEY';
137 $field_def .= sprintf " %s%s", $data_type,
138 ( !$field->is_auto_increment && $size ) ? "($size)" : '';
141 $field_def .= ' NOT NULL' unless $field->is_nullable;
143 # Default? XXX Need better quoting!
144 my $default = $field->default_value;
145 if ( defined $default ) {
146 if ( uc $default eq 'NULL') {
147 $field_def .= ' DEFAULT NULL';
149 $field_def .= " DEFAULT '$default'";
153 push @field_defs, $field_def;
157 scalar @pk_fields > 1
159 ( @pk_fields && !$pk_set )
161 push @field_defs, 'PRIMARY KEY (' . join(', ', @pk_fields ) . ')';
167 my $idx_name_default = 'A';
168 for my $index ( $table->get_indices ) {
169 my $name = $index->name;
170 $name = mk_name($table_name, $name || ++$idx_name_default);
172 # strip any field size qualifiers as SQLite doesn't like these
173 my @fields = map { s/\(\d+\)$//; $_ } $index->fields;
175 "CREATE INDEX $name on $table_name ".
176 '(' . join( ', ', @fields ) . ');';
182 my $c_name_default = 'A';
183 for my $c ( $table->get_constraints ) {
184 next unless $c->type eq UNIQUE;
186 $name = mk_name($table_name, $name || ++$idx_name_default);
187 my @fields = $c->fields;
189 push @constraint_defs,
190 "CREATE UNIQUE INDEX $name on $table_name ".
191 '(' . join( ', ', @fields ) . ');';
194 $create .= join(",\n", map { " $_" } @field_defs ) . "\n);\n";
199 for my $def ( @index_defs, @constraint_defs, @trigger_defs ) {
203 $create .= "COMMIT;\n";
208 # -------------------------------------------------------------------
210 my ($basename, $type, $scope, $critical) = @_;
211 my $basename_orig = $basename;
213 ? $max_id_length - (length($type) + 1)
215 $basename = substr( $basename, 0, $max_name )
216 if length( $basename ) > $max_name;
217 my $name = $type ? "${type}_$basename" : $basename;
219 if ( $basename ne $basename_orig and $critical ) {
220 my $show_type = $type ? "+'$type'" : "";
221 warn "Truncating '$basename_orig'$show_type to $max_id_length ",
222 "character limit to make '$name'\n" if $WARN;
223 $truncated{ $basename_orig } = $name;
226 $scope ||= \%global_names;
227 if ( my $prev = $scope->{ $name } ) {
228 my $name_orig = $name;
229 $name .= sprintf( "%02d", ++$prev );
230 substr($name, $max_id_length - 3) = "00"
231 if length( $name ) > $max_id_length;
233 warn "The name '$name_orig' has been changed to ",
234 "'$name' to make it unique.\n" if $WARN;
236 $scope->{ $name_orig }++;
245 # -------------------------------------------------------------------
251 SQL::Translator, http://www.sqlite.org/.
255 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.