1 package SQL::Translator::Producer::SQLite;
3 # -------------------------------------------------------------------
4 # $Id: SQLite.pm,v 1.12 2005-06-13 18:23:10 mwz444 Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
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.
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.
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
21 # -------------------------------------------------------------------
25 SQL::Translator::Producer::SQLite - SQLite producer for SQL::Translator
31 my $t = SQL::Translator->new( parser => '...', producer => 'SQLite' );
36 This module will produce text output of the schema suitable for SQLite.
42 use SQL::Translator::Schema::Constants;
43 use SQL::Translator::Utils qw(debug header_comment);
45 use vars qw[ $VERSION $DEBUG $WARN ];
47 $VERSION = sprintf "%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/;
48 $DEBUG = 0 unless defined $DEBUG;
49 $WARN = 0 unless defined $WARN;
51 my %used_identifiers = ();
52 my $max_id_length = 30;
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;
64 debug("PKG: Beginning production\n");
67 $create .= header_comment unless ($no_comments);
68 $create .= "BEGIN TRANSACTION;\n\n";
70 my ( @index_defs, @constraint_defs, @trigger_defs );
71 for my $table ( $schema->get_tables ) {
72 my $table_name = $table->name;
73 debug("PKG: Looking at table '$table_name'\n");
75 my @fields = $table->get_fields or die "No fields in $table_name";
80 $create .= "--\n-- Table: $table_name\n--\n" unless $no_comments;
81 $create .= qq[DROP TABLE $table_name;\n] if $add_drop_table;
82 $create .= "CREATE TABLE $table_name (\n";
85 # How many fields in PK?
87 my $pk = $table->primary_key;
88 my @pk_fields = $pk ? $pk->fields : ();
93 my ( @field_defs, $pk_set );
94 for my $field ( @fields ) {
95 my $field_name = $field->name;
96 debug("PKG: Looking at field '$field_name'\n");
97 my $field_def = $field_name;
100 my $size = $field->size;
101 my $data_type = $field->data_type;
102 $data_type = 'varchar' if lc $data_type eq 'set';
103 $data_type = 'blob' if lc $data_type eq 'bytea';
105 if ( lc $data_type =~ /(text|blob)/i ) {
109 if ( $data_type =~ /timestamp/i ) {
111 "CREATE TRIGGER ts_${table_name} ".
112 "after insert on $table_name\n".
114 " update $table_name set $field_name=timestamp() ".
115 "where id=new.id;\n".
122 # SQLite is generally typeless, but newer versions will
123 # make a field autoincrement if it is declared as (and
124 # *only* as) INTEGER PRIMARY KEY
127 $field->is_primary_key &&
128 scalar @pk_fields == 1 &&
130 $data_type =~ /int(eger)?$/i
132 ( $data_type =~ /^number?$/i && $size !~ /,/ )
135 $data_type = 'INTEGER PRIMARY KEY';
140 $field_def .= sprintf " %s%s", $data_type,
141 ( !$field->is_auto_increment && $size ) ? "($size)" : '';
144 $field_def .= ' NOT NULL' unless $field->is_nullable;
146 # Default? XXX Need better quoting!
147 my $default = $field->default_value;
148 if ( defined $default ) {
149 if ( uc $default eq 'NULL') {
150 $field_def .= ' DEFAULT NULL';
151 } elsif ( $default eq 'now()' ) {
152 $field_def .= ' DEFAULT CURRENT_TIMESTAMP';
153 } elsif ( $default =~ /val\(/ ) {
156 $field_def .= " DEFAULT '$default'";
160 push @field_defs, $field_def;
164 scalar @pk_fields > 1
166 ( @pk_fields && !$pk_set )
168 push @field_defs, 'PRIMARY KEY (' . join(', ', @pk_fields ) . ')';
174 my $idx_name_default = 'A';
175 for my $index ( $table->get_indices ) {
176 my $name = $index->name;
177 $name = mk_name($table_name, $name || ++$idx_name_default);
179 # strip any field size qualifiers as SQLite doesn't like these
180 my @fields = map { s/\(\d+\)$//; $_ } $index->fields;
182 "CREATE INDEX $name on $table_name ".
183 '(' . join( ', ', @fields ) . ');';
189 my $c_name_default = 'A';
190 for my $c ( $table->get_constraints ) {
191 next unless $c->type eq UNIQUE;
193 $name = mk_name($table_name, $name || ++$idx_name_default);
194 my @fields = $c->fields;
196 push @constraint_defs,
197 "CREATE UNIQUE INDEX $name on $table_name ".
198 '(' . join( ', ', @fields ) . ');';
201 $create .= join(",\n", map { " $_" } @field_defs ) . "\n);\n";
206 for my $def ( @index_defs, @constraint_defs, @trigger_defs ) {
210 $create .= "COMMIT;\n";
215 # -------------------------------------------------------------------
217 my ($basename, $type, $scope, $critical) = @_;
218 my $basename_orig = $basename;
220 ? $max_id_length - (length($type) + 1)
222 $basename = substr( $basename, 0, $max_name )
223 if length( $basename ) > $max_name;
224 my $name = $type ? "${type}_$basename" : $basename;
226 if ( $basename ne $basename_orig and $critical ) {
227 my $show_type = $type ? "+'$type'" : "";
228 warn "Truncating '$basename_orig'$show_type to $max_id_length ",
229 "character limit to make '$name'\n" if $WARN;
230 $truncated{ $basename_orig } = $name;
233 $scope ||= \%global_names;
234 if ( my $prev = $scope->{ $name } ) {
235 my $name_orig = $name;
236 $name .= sprintf( "%02d", ++$prev );
237 substr($name, $max_id_length - 3) = "00"
238 if length( $name ) > $max_id_length;
240 warn "The name '$name_orig' has been changed to ",
241 "'$name' to make it unique.\n" if $WARN;
243 $scope->{ $name_orig }++;
252 # -------------------------------------------------------------------
258 SQL::Translator, http://www.sqlite.org/.
262 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.