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