Fixed copyrights.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / SQLite.pm
CommitLineData
758ab1cd 1package SQL::Translator::Producer::SQLite;
2
3# -------------------------------------------------------------------
20770e44 4# $Id: SQLite.pm,v 1.9 2003-10-15 19:09:15 kycl4rk Exp $
758ab1cd 5# -------------------------------------------------------------------
6# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7# darren chamberlain <darren@cpan.org>,
8# Chris Mungall <cjm@fruitfly.org>
9#
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.
13#
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.
18#
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
22# 02111-1307 USA
23# -------------------------------------------------------------------
24
20770e44 25=head1 NAME
26
27SQL::Translator::Producer::SQLite - SQLite producer for SQL::Translator
28
29=head1 SYNOPSIS
30
31 use SQL::Translator;
32
33 my $t = SQL::Translator->new( parser => '...', producer => 'SQLite' );
34 $t->translate;
35
36=head1 DESCRIPTION
37
38This module will produce text output of the schema suitable for SQLite.
39
40=cut
41
758ab1cd 42use strict;
43use Data::Dumper;
b21bf652 44use SQL::Translator::Schema::Constants;
5ee19df8 45use SQL::Translator::Utils qw(debug header_comment);
758ab1cd 46
47use vars qw[ $VERSION $DEBUG $WARN ];
5ee19df8 48
20770e44 49$VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/;
5ee19df8 50$DEBUG = 0 unless defined $DEBUG;
51$WARN = 0 unless defined $WARN;
758ab1cd 52
53my %used_identifiers = ();
54my $max_id_length = 30;
55my %global_names;
56my %truncated;
57
758ab1cd 58sub produce {
a1d94525 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;
758ab1cd 65
1a24938d 66 debug("PKG: Beginning production\n");
758ab1cd 67
a5a882f0 68 my $create = '';
5ee19df8 69 $create .= header_comment unless ($no_comments);
a5a882f0 70 $create .= "BEGIN TRANSACTION;\n\n";
758ab1cd 71
a5a882f0 72 my ( @index_defs, @constraint_defs, @trigger_defs );
b21bf652 73 for my $table ( $schema->get_tables ) {
74 my $table_name = $table->name;
75 debug("PKG: Looking at table '$table_name'\n");
76
77 my @fields = $table->get_fields or die "No fields in $table_name";
758ab1cd 78
79 #
b21bf652 80 # Header.
758ab1cd 81 #
b21bf652 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";
758ab1cd 85
86 #
a5a882f0 87 # How many fields in PK?
88 #
89 my $pk = $table->primary_key;
90 my @pk_fields = $pk ? $pk->fields : ();
91
92 #
758ab1cd 93 # Fields
94 #
a5a882f0 95 my ( @field_defs, $pk_set );
b21bf652 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;
758ab1cd 100
101 # data type and size
b21bf652 102 my $size = $field->size;
103 my $data_type = $field->data_type;
104 $data_type = 'varchar' if lc $data_type eq 'set';
758ab1cd 105
a5a882f0 106 if ( $data_type =~ /timestamp/i ) {
107 push @trigger_defs,
108 "CREATE TRIGGER ts_${table_name} ".
109 "after insert on $table_name\n".
110 "begin\n".
111 " update $table_name set $field_name=timestamp() ".
112 "where id=new.id;\n".
113 "end;\n"
114 ;
115
116 }
117
b21bf652 118 #
5ee19df8 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
b21bf652 122 #
a5a882f0 123 if (
124 $field->is_primary_key &&
125 scalar @pk_fields == 1 &&
126 (
127 $data_type =~ /^int(eger)?$/i
128 ||
129 ( $data_type =~ /^number?$/i && $size !~ /,/ )
130 )
131 ) {
5ee19df8 132 $data_type = 'INTEGER PRIMARY KEY';
a5a882f0 133 $size = undef;
134 $pk_set = 1;
5ee19df8 135 }
136
b21bf652 137 $field_def .= sprintf " %s%s", $data_type,
138 ( !$field->is_auto_increment && $size ) ? "($size)" : '';
758ab1cd 139
140 # Null?
b21bf652 141 $field_def .= ' NOT NULL' unless $field->is_nullable;
758ab1cd 142
143 # Default? XXX Need better quoting!
b21bf652 144 my $default = $field->default_value;
758ab1cd 145 if ( defined $default ) {
146 if ( uc $default eq 'NULL') {
b21bf652 147 $field_def .= ' DEFAULT NULL';
758ab1cd 148 } else {
b21bf652 149 $field_def .= " DEFAULT '$default'";
758ab1cd 150 }
151 }
152
b21bf652 153 push @field_defs, $field_def;
758ab1cd 154 }
b21bf652 155
a5a882f0 156 if (
157 scalar @pk_fields > 1
158 ||
159 ( @pk_fields && !$pk_set )
160 ) {
161 push @field_defs, 'PRIMARY KEY (' . join(', ', @pk_fields ) . ')';
162 }
163
758ab1cd 164 #
165 # Indices
166 #
5ee19df8 167 my $idx_name_default = 'A';
b21bf652 168 for my $index ( $table->get_indices ) {
169 my $name = $index->name;
170 $name = mk_name($table_name, $name || ++$idx_name_default);
34d2319c 171
172 # strip any field size qualifiers as SQLite doesn't like these
173 my @fields = map { s/\(\d+\)$//; $_ } $index->fields;
b21bf652 174 push @index_defs,
175 "CREATE INDEX $name on $table_name ".
a5a882f0 176 '(' . join( ', ', @fields ) . ');';
b21bf652 177 }
178
179 #
180 # Constraints
181 #
b21bf652 182 my $c_name_default = 'A';
183 for my $c ( $table->get_constraints ) {
184 next unless $c->type eq UNIQUE;
185 my $name = $c->name;
186 $name = mk_name($table_name, $name || ++$idx_name_default);
187 my @fields = $c->fields;
188
189 push @constraint_defs,
190 "CREATE UNIQUE INDEX $name on $table_name ".
e49d2158 191 '(' . join( ', ', @fields ) . ');';
758ab1cd 192 }
193
b21bf652 194 $create .= join(",\n", map { " $_" } @field_defs ) . "\n);\n";
758ab1cd 195
758ab1cd 196 $create .= "\n";
197 }
198
a5a882f0 199 for my $def ( @index_defs, @constraint_defs, @trigger_defs ) {
200 $create .= "$def\n";
201 }
202
203 $create .= "COMMIT;\n";
204
758ab1cd 205 return $create;
206}
207
758ab1cd 208# -------------------------------------------------------------------
209sub mk_name {
210 my ($basename, $type, $scope, $critical) = @_;
211 my $basename_orig = $basename;
212 my $max_name = $type
213 ? $max_id_length - (length($type) + 1)
214 : $max_id_length;
215 $basename = substr( $basename, 0, $max_name )
216 if length( $basename ) > $max_name;
217 my $name = $type ? "${type}_$basename" : $basename;
218
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;
224 }
225
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;
232
233 warn "The name '$name_orig' has been changed to ",
234 "'$name' to make it unique.\n" if $WARN;
235
236 $scope->{ $name_orig }++;
237 }
238
239 $scope->{ $name }++;
240 return $name;
241}
242
2431;
758ab1cd 244
20770e44 245# -------------------------------------------------------------------
758ab1cd 246
20770e44 247=pod
248
249=head1 SEE ALSO
250
251SQL::Translator, http://www.sqlite.org/.
758ab1cd 252
253=head1 AUTHOR
254
20770e44 255Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
256
257=cut