Modified all filed to quit returning the data structure, now only return "1"
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / SQLite.pm
CommitLineData
758ab1cd 1package SQL::Translator::Producer::SQLite;
2
3# -------------------------------------------------------------------
b21bf652 4# $Id: SQLite.pm,v 1.4 2003-06-09 02:00:01 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
25use strict;
26use Data::Dumper;
b21bf652 27use SQL::Translator::Schema::Constants;
5ee19df8 28use SQL::Translator::Utils qw(debug header_comment);
758ab1cd 29
30use vars qw[ $VERSION $DEBUG $WARN ];
5ee19df8 31
b21bf652 32$VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/;
5ee19df8 33$DEBUG = 0 unless defined $DEBUG;
34$WARN = 0 unless defined $WARN;
758ab1cd 35
36my %used_identifiers = ();
37my $max_id_length = 30;
38my %global_names;
39my %truncated;
40
758ab1cd 41sub produce {
42 my ($translator, $data) = @_;
5ee19df8 43 local $DEBUG = $translator->debug;
44 local $WARN = $translator->show_warnings;
758ab1cd 45 my $no_comments = $translator->no_comments;
46 my $add_drop_table = $translator->add_drop_table;
b21bf652 47 my $schema = $translator->schema;
758ab1cd 48
1a24938d 49 debug("PKG: Beginning production\n");
758ab1cd 50
5ee19df8 51 my $create = '';
52 $create .= header_comment unless ($no_comments);
758ab1cd 53
b21bf652 54 for my $table ( $schema->get_tables ) {
55 my $table_name = $table->name;
56 debug("PKG: Looking at table '$table_name'\n");
57
58 my @fields = $table->get_fields or die "No fields in $table_name";
758ab1cd 59
60 #
b21bf652 61 # Header.
758ab1cd 62 #
b21bf652 63 $create .= "--\n-- Table: $table_name\n--\n" unless $no_comments;
64 $create .= qq[DROP TABLE $table_name;\n] if $add_drop_table;
65 $create .= "CREATE TABLE $table_name (\n";
758ab1cd 66
67 #
68 # Fields
69 #
b21bf652 70 my @field_defs;
71 for my $field ( @fields ) {
72 my $field_name = $field->name;
73 debug("PKG: Looking at field '$field_name'\n");
74 my $field_def = $field_name;
758ab1cd 75
76 # data type and size
b21bf652 77 my $size = $field->size;
78 my $data_type = $field->data_type;
79 $data_type = 'varchar' if lc $data_type eq 'set';
758ab1cd 80
b21bf652 81 #
5ee19df8 82 # SQLite is generally typeless, but newer versions will
83 # make a field autoincrement if it is declared as (and
84 # *only* as) INTEGER PRIMARY KEY
b21bf652 85 #
86 if ( $field->is_auto_increment && $field->is_primary_key ) {
5ee19df8 87 $data_type = 'INTEGER PRIMARY KEY';
5ee19df8 88 }
89
b21bf652 90 $field_def .= sprintf " %s%s", $data_type,
91 ( !$field->is_auto_increment && $size ) ? "($size)" : '';
758ab1cd 92
93 # Null?
b21bf652 94 $field_def .= ' NOT NULL' unless $field->is_nullable;
758ab1cd 95
96 # Default? XXX Need better quoting!
b21bf652 97 my $default = $field->default_value;
758ab1cd 98 if ( defined $default ) {
99 if ( uc $default eq 'NULL') {
b21bf652 100 $field_def .= ' DEFAULT NULL';
758ab1cd 101 } else {
b21bf652 102 $field_def .= " DEFAULT '$default'";
758ab1cd 103 }
104 }
105
b21bf652 106 push @field_defs, $field_def;
758ab1cd 107 }
b21bf652 108
758ab1cd 109 #
110 # Indices
111 #
b21bf652 112 my @index_defs;
5ee19df8 113 my $idx_name_default = 'A';
b21bf652 114 for my $index ( $table->get_indices ) {
115 my $name = $index->name;
116 $name = mk_name($table_name, $name || ++$idx_name_default);
117 my @fields = $index->fields;
118 push @index_defs,
119 "CREATE INDEX $name on $table_name ".
120 '(' . join( ', ', @fields ) . ')';
121 }
122
123 #
124 # Constraints
125 #
126 my @constraint_defs;
127 my $c_name_default = 'A';
128 for my $c ( $table->get_constraints ) {
129 next unless $c->type eq UNIQUE;
130 my $name = $c->name;
131 $name = mk_name($table_name, $name || ++$idx_name_default);
132 my @fields = $c->fields;
133
134 push @constraint_defs,
135 "CREATE UNIQUE INDEX $name on $table_name ".
136 '(' . join( ', ', @fields ) . ')';
758ab1cd 137 }
138
b21bf652 139 $create .= join(",\n", map { " $_" } @field_defs ) . "\n);\n";
758ab1cd 140
b21bf652 141 for my $index_create ( @index_defs, @constraint_defs ) {
758ab1cd 142 $create .= "$index_create;\n";
143 }
144
145 $create .= "\n";
146 }
147
148 return $create;
149}
150
758ab1cd 151
152# -------------------------------------------------------------------
153sub mk_name {
154 my ($basename, $type, $scope, $critical) = @_;
155 my $basename_orig = $basename;
156 my $max_name = $type
157 ? $max_id_length - (length($type) + 1)
158 : $max_id_length;
159 $basename = substr( $basename, 0, $max_name )
160 if length( $basename ) > $max_name;
161 my $name = $type ? "${type}_$basename" : $basename;
162
163 if ( $basename ne $basename_orig and $critical ) {
164 my $show_type = $type ? "+'$type'" : "";
165 warn "Truncating '$basename_orig'$show_type to $max_id_length ",
166 "character limit to make '$name'\n" if $WARN;
167 $truncated{ $basename_orig } = $name;
168 }
169
170 $scope ||= \%global_names;
171 if ( my $prev = $scope->{ $name } ) {
172 my $name_orig = $name;
173 $name .= sprintf( "%02d", ++$prev );
174 substr($name, $max_id_length - 3) = "00"
175 if length( $name ) > $max_id_length;
176
177 warn "The name '$name_orig' has been changed to ",
178 "'$name' to make it unique.\n" if $WARN;
179
180 $scope->{ $name_orig }++;
181 }
182
183 $scope->{ $name }++;
184 return $name;
185}
186
1871;
758ab1cd 188
189=head1 NAME
190
b21bf652 191SQL::Translator::Producer::SQLite - SQLite producer for SQL::Translator
758ab1cd 192
193=head1 AUTHOR
194
195Ken Y. Clark E<lt>kclark@cpan.orgE<gt>