From: Peter Rabbitson Date: Mon, 4 May 2009 08:16:19 +0000 (+0000) Subject: SQLite improvements: X-Git-Tag: v0.11008~163^2~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ae6027378e090ce10e6a84d97e0ce247a9b62c73;p=dbsrgits%2FSQL-Translator.git SQLite improvements: - Support parsing of all DROP clauses - Support parsing of field-level comments - When producing do not append table names to constraint/index names - it's useless --- diff --git a/lib/SQL/Translator/Parser/SQLite.pm b/lib/SQL/Translator/Parser/SQLite.pm index 88302f2..0de6e12 100644 --- a/lib/SQL/Translator/Parser/SQLite.pm +++ b/lib/SQL/Translator/Parser/SQLite.pm @@ -198,7 +198,13 @@ begin_transaction : /begin/i TRANSACTION(?) SEMICOLON commit : /commit/i SEMICOLON -drop : /drop/i TABLE table_name SEMICOLON +drop : /drop/i (tbl_drop | view_drop | trg_drop) SEMICOLON + +tbl_drop: TABLE table_name + +view_drop: VIEW if_exists(?) view_name + +trg_drop: TRIGGER if_exists(?) trigger_name comment : /^\s*(?:#|-{2}).*\n/ { @@ -265,22 +271,24 @@ create : CREATE TEMPORARY(?) TABLE table_name '(' definition(s /,/) ')' SEMICOLO definition : constraint_def | column_def -column_def: NAME type(?) column_constraint(s?) +column_def: comment(s?) NAME type(?) column_constraint(s?) { my $column = { supertype => 'column', - name => $item[1], - data_type => $item[2][0]->{'type'}, - size => $item[2][0]->{'size'}, + name => $item[2], + data_type => $item[3][0]->{'type'}, + size => $item[3][0]->{'size'}, is_nullable => 1, is_primary_key => 0, is_unique => 0, check => '', default => undef, - constraints => $item[3], + constraints => $item[4], + comments => $item[1], }; - for my $c ( @{ $item[3] } ) { + + for my $c ( @{ $item[4] } ) { if ( $c->{'type'} eq 'not_null' ) { $column->{'is_nullable'} = 0; } @@ -470,8 +478,12 @@ before_or_after : /(before|after)/i { $return = lc $1 } instead_of : /instead of/i +if_exists : /if exists/i + view_name : qualified_name +trigger_name : qualified_name + # # Create View # diff --git a/lib/SQL/Translator/Producer/SQLite.pm b/lib/SQL/Translator/Producer/SQLite.pm index 64f0bfb..41a80c4 100644 --- a/lib/SQL/Translator/Producer/SQLite.pm +++ b/lib/SQL/Translator/Producer/SQLite.pm @@ -47,10 +47,8 @@ $VERSION = '1.59'; $DEBUG = 0 unless defined $DEBUG; $WARN = 0 unless defined $WARN; -our %used_identifiers = (); our $max_id_length = 30; -our %global_names; -our %truncated; +my %global_names; sub produce { my $translator = shift; @@ -65,6 +63,8 @@ sub produce { debug("PKG: Beginning production\n"); + %global_names = (); #reset + my @create = (); push @create, header_comment unless ($no_comments); $create[0] .= "\n\nBEGIN TRANSACTION" unless $no_txn; @@ -100,24 +100,7 @@ sub produce { # ------------------------------------------------------------------- sub mk_name { - my ($basename, $type, $scope, $critical) = @_; - my $basename_orig = $basename; - my $max_name = !$max_id_length - ? length($type) + 1 - : $type - ? $max_id_length - (length($type) + 1) - : $max_id_length; - $basename = substr( $basename, 0, $max_name ) - if length( $basename ) > $max_name; - $basename =~ s/\./_/g; - my $name = $type ? "${type}_$basename" : $basename; - - if ( $basename ne $basename_orig and $critical ) { - my $show_type = $type ? "+'$type'" : ""; - warn "Truncating '$basename_orig'$show_type to $max_id_length ", - "character limit to make '$name'\n" if $WARN; - $truncated{ $basename_orig } = $name; - } + my ($name, $scope, $critical) = @_; $scope ||= \%global_names; if ( my $prev = $scope->{ $name } ) { @@ -329,7 +312,7 @@ sub create_index my ($index, $options) = @_; my $name = $index->name; - $name = mk_name($index->table->name, $name); + $name = mk_name($name); my $type = $index->type eq 'UNIQUE' ? "UNIQUE " : ''; @@ -349,7 +332,7 @@ sub create_constraint my ($c, $options) = @_; my $name = $c->name; - $name = mk_name($c->table->name, $name); + $name = mk_name($name); my @fields = $c->fields; (my $index_table_name = $c->table->name) =~ s/^.+?\.//; # table name may not specify schema warn "removing schema name from '" . $c->table->name . "' to make '$index_table_name'\n" if $WARN;