1 package SQL::Translator::Producer::SQLite;
3 # -------------------------------------------------------------------
4 # $Id: SQLite.pm,v 1.2 2003-03-12 14:17:11 dlc 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 use SQL::Translator::Utils qw(debug);
29 use vars qw[ $VERSION $DEBUG $WARN ];
30 $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
32 my %used_identifiers = ();
33 my $max_id_length = 30;
38 warn "loading " . __PACKAGE__ . "...\n";
42 my ($translator, $data) = @_;
43 $DEBUG = $translator->debug;
44 $WARN = $translator->show_warnings;
45 my $no_comments = $translator->no_comments;
46 my $add_drop_table = $translator->add_drop_table;
48 debug("PKG: Beginning production\n");
51 unless ( $no_comments ) {
52 $create .= sprintf "--\n-- Created by %s\n-- Created on %s\n--\n\n",
53 __PACKAGE__, scalar localtime;
56 for my $table ( keys %{ $data } ) {
57 debug("PKG: Looking at table '$table'\n");
58 my $table_data = $data->{$table};
60 $table_data->{'fields'}->{$a}->{'order'}
62 $table_data->{'fields'}->{$b}->{'order'}
63 } keys %{$table_data->{'fields'}};
66 # Header. Should this look like what mysqldump produces?
68 $create .= "--\n-- Table: $table\n--\n" unless $no_comments;
69 $create .= qq[DROP TABLE $table;\n] if $add_drop_table;
70 $create .= "CREATE TABLE $table (";
75 for (my $i = 0; $i <= $#fields; $i++) {
76 my $field = $fields[$i];
77 debug("PKG: Looking at field '$field'\n");
78 my $field_data = $table_data->{'fields'}->{$field};
79 my @fdata = ("", $field);
83 my $data_type = lc $field_data->{'data_type'};
84 my $list = $field_data->{'list'} || [];
85 my $commalist = join ",", @$list;
88 if ( $data_type eq 'set' ) {
89 $data_type = 'varchar';
90 $size = length $commalist;
93 $size = join( ', ', @{ $field_data->{'size'} || [] } );
96 push @fdata, sprintf "%s%s", $data_type, ($size) ? "($size)" : '';
99 # for my $qual ( qw[ binary unsigned zerofill ] ) {
101 # if $field_data->{ $qual } ||
102 # $field_data->{ uc $qual };
106 push @fdata, "NOT NULL" unless $field_data->{'null'};
108 # Default? XXX Need better quoting!
109 my $default = $field_data->{'default'};
110 if ( defined $default ) {
111 if ( uc $default eq 'NULL') {
112 push @fdata, "DEFAULT NULL";
114 push @fdata, "DEFAULT '$default'";
119 # push @fdata, "auto_increment" if $field_data->{'is_auto_inc'};
122 # This is taken care of in the indices, could be duplicated here
123 # push @fdata, "PRIMARY KEY" if $field_data->{'is_primary_key'};
126 $create .= (join " ", '', @fdata);
127 $create .= "," unless ($i == $#fields);
133 my $idx_name_default;
134 for my $index ( @{ $table_data->{'indices'} || [] } ) {
135 my ($name, $type, $fields) = @{ $index }{ qw[ name type fields ] };
138 $type eq 'primary_key' ? 'PRIMARY KEY' :
139 $type eq 'unique' ? 'UNIQUE INDEX' : 'INDEX';
140 if ( $type eq 'primary_key' ) {
141 $create .= join(",\n", '',
142 " $index_type $name (" . join( ', ', @$fields ) . ')'
147 $table, $name || ++$idx_name_default
150 "CREATE $index_type $name on $table ".
151 '(' . join( ', ', @$fields ) . ')';
157 for my $index_create ( @index_creates ) {
158 $create .= "$index_create;\n";
168 # -------------------------------------------------------------------
170 my ($basename, $type, $scope, $critical) = @_;
171 my $basename_orig = $basename;
173 ? $max_id_length - (length($type) + 1)
175 $basename = substr( $basename, 0, $max_name )
176 if length( $basename ) > $max_name;
177 my $name = $type ? "${type}_$basename" : $basename;
179 if ( $basename ne $basename_orig and $critical ) {
180 my $show_type = $type ? "+'$type'" : "";
181 warn "Truncating '$basename_orig'$show_type to $max_id_length ",
182 "character limit to make '$name'\n" if $WARN;
183 $truncated{ $basename_orig } = $name;
186 $scope ||= \%global_names;
187 if ( my $prev = $scope->{ $name } ) {
188 my $name_orig = $name;
189 $name .= sprintf( "%02d", ++$prev );
190 substr($name, $max_id_length - 3) = "00"
191 if length( $name ) > $max_id_length;
193 warn "The name '$name_orig' has been changed to ",
194 "'$name' to make it unique.\n" if $WARN;
196 $scope->{ $name_orig }++;
208 SQL::Translator::Producer::SQLite - SQLite-specific producer for SQL::Translator
212 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>