From: Arthur Axel 'fREW' Schmidt Date: Tue, 8 Mar 2011 02:29:55 +0000 (-0600) Subject: use future stuff for SQLite field generation X-Git-Tag: v0.11011~27^2~22 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=27df230e4deaa24eac63e6a051080c24ce147e7b;p=dbsrgits%2FSQL-Translator.git use future stuff for SQLite field generation --- diff --git a/lib/SQL/Translator/Generator/DDL/SQLite.pm b/lib/SQL/Translator/Generator/DDL/SQLite.pm index fa60d9a..96ba086 100644 --- a/lib/SQL/Translator/Generator/DDL/SQLite.pm +++ b/lib/SQL/Translator/Generator/DDL/SQLite.pm @@ -32,13 +32,26 @@ sub _build_unquoted_defaults { sub nullable { () } +sub _ipk { + my ($self, $field) = @_; + + my $pk = $field->table->primary_key; + my @pk_fields = $pk ? $pk->fields : (); + + $field->is_primary_key && scalar @pk_fields == 1 && + ( $field->data_type =~ /int(eger)?$/i + || + ( $field->data_type =~ /^number?$/i && $field->size !~ /,/ ) ) +} + sub field { my ($self, $field) = @_; + return join ' ', $self->field_comments($field), $self->field_name($field), - ( $field->is_auto_increment + ( $self->_ipk($field) ? ( 'INTEGER PRIMARY KEY' ) : ( $self->field_type($field) ) ), @@ -55,7 +68,5 @@ sub field_type_size { ) } -sub field_autoinc { ( $_[1]->is_auto_increment ? 'IDENTITY' : () ) } - 1; diff --git a/lib/SQL/Translator/Producer/SQLite.pm b/lib/SQL/Translator/Producer/SQLite.pm index 8d61a24..a4a7fa4 100644 --- a/lib/SQL/Translator/Producer/SQLite.pm +++ b/lib/SQL/Translator/Producer/SQLite.pm @@ -24,6 +24,7 @@ use SQL::Translator::Schema::Constants; use SQL::Translator::Utils qw(debug header_comment parse_dbms_version); use SQL::Translator::Generator::Utils; my $util = SQL::Translator::Generator::Utils->new( quote_chars => q(") ); +use SQL::Translator::Generator::DDL::SQLite; our ( $DEBUG, $WARN ); our $VERSION = '1.59'; @@ -32,6 +33,7 @@ $WARN = 0 unless defined $WARN; our $max_id_length = 30; my %global_names; +my $future = SQL::Translator::Generator::DDL::SQLite->new(); sub produce { my $translator = shift; @@ -262,82 +264,7 @@ sub create_foreignkey { return $fk_sql; } -sub create_field -{ - my ($field, $options) = @_; - - my $field_name = $util->quote($field->name); - debug("PKG: Looking at field '$field_name'\n"); - my $field_comments = $field->comments - ? "-- " . $field->comments . "\n " - : ''; - - my $field_def = $field_comments.$field_name; - - # data type and size - my $size = $field->size; - my $data_type = $field->data_type; - $data_type = 'varchar' if lc $data_type eq 'set'; - $data_type = 'blob' if lc $data_type eq 'bytea'; - - if ( lc $data_type =~ /(text|blob)/i ) { - $size = undef; - } - -# if ( $data_type =~ /timestamp/i ) { -# push @trigger_defs, -# "CREATE TRIGGER ts_${table_name} ". -# "after insert on $table_name\n". -# "begin\n". -# " update $table_name set $field_name=timestamp() ". -# "where id=new.id;\n". -# "end;\n" -# ; -# -# } - - # - # SQLite is generally typeless, but newer versions will - # make a field autoincrement if it is declared as (and - # *only* as) INTEGER PRIMARY KEY - # - my $pk = $field->table->primary_key; - my @pk_fields = $pk ? $pk->fields : (); - - if ( - $field->is_primary_key && - scalar @pk_fields == 1 && - ( - $data_type =~ /int(eger)?$/i - || - ( $data_type =~ /^number?$/i && $size !~ /,/ ) - ) - ) { - $data_type = 'INTEGER PRIMARY KEY'; - $size = undef; -# $pk_set = 1; - } - - $field_def .= sprintf " %s%s", $data_type, - ( !$field->is_auto_increment && $size ) ? "($size)" : ''; - - # Null? - $field_def .= ' NOT NULL' unless $field->is_nullable; - - # Default? - SQL::Translator::Producer->_apply_default_value( - $field, - \$field_def, - [ - 'NULL' => \'NULL', - 'now()' => 'now()', - 'CURRENT_TIMESTAMP' => 'CURRENT_TIMESTAMP', - ], - ); - - return $field_def; - -} +sub create_field { return $future->field($_[0]) } sub create_index {