From: Justin Hunter Date: Wed, 8 Jul 2009 01:47:25 +0000 (-0700) Subject: _very_ basic working SQLite producer X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=053fd7e50846a3ed1c151007922f56e7a66ef236;p=dbsrgits%2FSQL-Translator-2.0-ish.git _very_ basic working SQLite producer --- diff --git a/lib/SQL/Translator/Producer/SQLite.pm b/lib/SQL/Translator/Producer/SQLite.pm index 56392c0..9335906 100644 --- a/lib/SQL/Translator/Producer/SQLite.pm +++ b/lib/SQL/Translator/Producer/SQLite.pm @@ -1,6 +1,15 @@ package SQL::Translator::Producer::SQLite; use namespace::autoclean; use Moose::Role; +use SQL::Translator::Constants qw(:sqlt_types); + +my %data_type_mapping = ( + SQL_LONGVARCHAR() => 'text', + SQL_TIMESTAMP() => 'timestamp', + SQL_INTEGER() => 'integer', + SQL_CHAR() => 'character', + SQL_VARCHAR() => 'varchar', +); sub _create_table { my $self = shift; @@ -19,7 +28,7 @@ sub _create_table { push @column_defs, $self->_create_column($_) for values %{$table->columns}; $create_table .= join(",\n", map { ' ' . $_ } @column_defs ) . "\n)"; - print $create_table . "\n"; + print $create_table . ";\n"; return (@create, $create_table, @index_defs, @constraint_defs ); } @@ -27,13 +36,19 @@ sub _create_column { my $self = shift; my $column = shift; - my $size = $column->data_type =~ /^(timestamp)/i ? undef : $column->size; + my $size = $column->data_type == SQL_TIMESTAMP() ? undef : $column->size; + my $default_value = $column->default_value; + $default_value =~ s/^now[()]*/CURRENT_TIMESTAMP/i if $default_value; my $column_def; - $column_def = $column->name . ' ' . $column->data_type; - $column_def .= '(' . $column->size . ')' if $size; + $column_def = $column->name . ' '; + $column_def .= defined $data_type_mapping{$column->data_type} + ? $data_type_mapping{$column->data_type} + : $column->data_type; + #$column_def .= '(' . $column->size . ')' if $size; $column_def .= ' NOT NULL' unless $column->is_nullable; - $column_def .= ' DEFAULT ' . $column->default_value if $column->default_value; + $column_def .= ' PRIMARY KEY' if $column->is_auto_increment; + $column_def .= ' DEFAULT ' . $default_value if $column->default_value && !$column->is_auto_increment; $column_def; }