From: Ash Berlin Date: Sat, 7 Feb 2009 15:53:56 +0000 (+0000) Subject: svk-commitTn2OH.tmp X-Git-Tag: v0.11008~244 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d0fcb05d6b4b6126e080ff90558da0cc31c06b78;p=dbsrgits%2FSQL-Translator.git svk-commitTn2OH.tmp --- diff --git a/Changes b/Changes index a0a2828..afe9021 100644 --- a/Changes +++ b/Changes @@ -1,4 +1,6 @@ * Add support for temporary tables in Pg (nachos) +* Create Trigger support for SQLite + # ---------------------------------------------------------- # 0.09002 2008-12-05 # ---------------------------------------------------------- diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP index 9991106..30fb58b 100644 --- a/MANIFEST.SKIP +++ b/MANIFEST.SKIP @@ -1,7 +1,7 @@ .*CVS.* .#.* -.*.bak -.*.swp +.*\.bak +.*\.swp .*\.old .*\.tar\.gz ^Makefile$ diff --git a/lib/SQL/Translator/Parser/SQLite.pm b/lib/SQL/Translator/Parser/SQLite.pm index 1aa5ef3..1e236d4 100644 --- a/lib/SQL/Translator/Parser/SQLite.pm +++ b/lib/SQL/Translator/Parser/SQLite.pm @@ -418,7 +418,7 @@ create : CREATE TEMPORARY(?) TRIGGER NAME before_or_after(?) database_event ON t is_temporary => $item[2][0] ? 1 : 0, when => $item[5][0], instead_of => 0, - db_event => $item[6], + db_events => [ $item[6] ], action => $item[9], on_table => $table_name, } @@ -432,7 +432,7 @@ create : CREATE TEMPORARY(?) TRIGGER NAME instead_of database_event ON view_name is_temporary => $item[2][0] ? 1 : 0, when => undef, instead_of => 1, - db_event => $item[6], + db_events => [ $item[6] ], action => $item[9], on_table => $table_name, } @@ -451,7 +451,7 @@ trigger_action : for_each(?) when(?) BEGIN_C trigger_step(s) END_C } } -for_each : /FOR EACH ROW/i | /FOR EACH STATEMENT/i +for_each : /FOR EACH ROW/i when : WHEN expr { $item[2] } @@ -634,7 +634,7 @@ sub parse { my $view = $schema->add_trigger( name => $def->{'name'}, perform_action_when => $def->{'when'}, - database_event => $def->{'db_event'}, + database_events => $def->{'db_events'}, action => $def->{'action'}, on_table => $def->{'on_table'}, ); diff --git a/lib/SQL/Translator/Producer/SQLite.pm b/lib/SQL/Translator/Producer/SQLite.pm index 455c5bc..9eeeaa3 100644 --- a/lib/SQL/Translator/Producer/SQLite.pm +++ b/lib/SQL/Translator/Producer/SQLite.pm @@ -38,6 +38,7 @@ This module will produce text output of the schema suitable for SQLite. =cut use strict; +use warnings; use Data::Dumper; use SQL::Translator::Schema::Constants; use SQL::Translator::Utils qw(debug header_comment); @@ -47,10 +48,10 @@ use vars qw[ $DEBUG $WARN ]; $DEBUG = 0 unless defined $DEBUG; $WARN = 0 unless defined $WARN; -my %used_identifiers = (); -my $max_id_length = 30; -my %global_names; -my %truncated; +our %used_identifiers = (); +our $max_id_length = 30; +our %global_names; +our %truncated; sub produce { my $translator = shift; @@ -66,7 +67,7 @@ sub produce { my @create = (); push @create, header_comment unless ($no_comments); - push @create, 'BEGIN TRANSACTION'; + $create[0] .= "\n\nBEGIN TRANSACTION"; for my $table ( $schema->get_tables ) { push @create, create_table($table, { no_comments => $no_comments, @@ -81,6 +82,13 @@ sub produce { }); } + for my $trigger ( $schema->get_triggers ) { + push @create, create_trigger($trigger, { + add_drop_trigger => $add_drop_table, + no_comments => $no_comments, + }); + } + return wantarray ? (@create, "COMMIT") : join(";\n\n", (@create, "COMMIT;\n")); } @@ -88,9 +96,11 @@ sub produce { sub mk_name { my ($basename, $type, $scope, $critical) = @_; my $basename_orig = $basename; - my $max_name = $type - ? $max_id_length - (length($type) + 1) - : $max_id_length; + 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; @@ -156,7 +166,7 @@ sub create_table debug("PKG: Looking at table '$table_name'\n"); - my ( @index_defs, @constraint_defs, @trigger_defs ); + my ( @index_defs, @constraint_defs ); my @fields = $table->get_fields or die "No fields in $table_name"; my $temp = $options->{temporary_table} ? 'TEMPORARY ' : ''; @@ -165,9 +175,15 @@ sub create_table # my $exists = ($sqlite_version >= 3.3) ? ' IF EXISTS' : ''; my @create; - push @create, "--\n-- Table: $table_name\n--\n" unless $no_comments; - push @create, qq[DROP TABLE$exists $table_name] if $add_drop_table; - my $create_table = "CREATE ${temp}TABLE $table_name (\n"; + my ($comment, $create_table) = ""; + $comment = "--\n-- Table: $table_name\n--\n" unless $no_comments; + if ($add_drop_table) { + push @create, $comment . qq[DROP TABLE$exists $table_name]; + } else { + $create_table = $comment; + } + + $create_table .= "CREATE ${temp}TABLE $table_name (\n"; # # Comments @@ -219,7 +235,7 @@ sub create_table $create_table .= join(",\n", map { " $_" } @field_defs ) . "\n)"; - return (@create, $create_table, @index_defs, @constraint_defs, @trigger_defs ); + return (@create, $create_table, @index_defs, @constraint_defs ); } sub create_field @@ -339,6 +355,44 @@ sub create_constraint return $c_def; } +sub create_trigger { + my ($trigger, $options) = @_; + my $add_drop = $options->{add_drop_trigger}; + + my $name = $trigger->name; + my @create; + + push @create, "DROP TRIGGER IF EXISTS $name" if $add_drop; + + my $events = $trigger->database_events; + die "Can't handle multiple events in triggers" if @$events > 1; + + my $action = ""; + + $action = $trigger->ation->{for_each} . " " + if $trigger->action->{for_each}; + + $action = $trigger->action->{when} . " " + if $trigger->action->{when}; + + my $steps = $trigger->action->{steps} || []; + + $action .= "BEGIN "; + for (@$steps) { + $action .= $_ . "; " + } + $action .= "END"; + + push @create, "CREATE TRIGGER $name " . + $trigger->perform_action_when . " " . + $events->[0] . + " on " . $trigger->on_table . " " . + $action; + + return @create; + +} + sub alter_table { } # Noop sub add_field { diff --git a/lib/SQL/Translator/Schema/Trigger.pm b/lib/SQL/Translator/Schema/Trigger.pm index 0091163..92c74d8 100644 --- a/lib/SQL/Translator/Schema/Trigger.pm +++ b/lib/SQL/Translator/Schema/Trigger.pm @@ -53,6 +53,8 @@ use SQL::Translator::Utils 'parse_list_arg'; use base 'SQL::Translator::Schema::Object'; +use Carp; + use vars qw($TABLE_COUNT $VIEW_COUNT); # ---------------------------------------------------------------------- @@ -121,9 +123,10 @@ Obosolete please use database_events! $self->database_events( [$arg] ); } - return $self->error("Please use database_events the trigger has more then one events!") if (scalar @{$self->{'database_events'}} > 1); - - warn 'database_event is obsolete please use database_events'; + return $self->error("Please use database_events the trigger has more then one events!") + if (scalar @{$self->{'database_events'}} > 1); + + carp 'database_event is obsolete please use database_events'; return (@{ $self->{'database_events'} })[0]; } # ---------------------------------------------------------------------- @@ -135,7 +138,7 @@ sub database_events { Gets or sets the events that triggers the trigger. - my $ok = $trigger->database_event('insert'); + my $ok = $trigger->database_events('insert'); =cut diff --git a/t/27sqlite-roundtrip.t b/t/27sqlite-roundtrip.t index 78cb0dc..9e871d0 100644 --- a/t/27sqlite-roundtrip.t +++ b/t/27sqlite-roundtrip.t @@ -19,33 +19,33 @@ BEGIN { my $file = "$Bin/data/sqlite/create.sql"; { - local $/; - open my $fh, "<$file" or die "Can't read file '$file': $!\n"; - my $data = <$fh>; + #local $/; + #open my $fh, "<$file" or die "Can't read file '$file': $!\n"; + #my $data = <$fh>; my $t = SQL::Translator->new; - $t->translate ( + my $schema1 = $t->translate ( parser => 'SQLite', - data => \$data, - ); - my $schema1 = $t->schema; + file => $file, + debug => 1 + ) or die $t->error; isa_ok ($schema1, 'SQL::Translator::Schema', 'First parser pass produced a schema'); my $data2 = $t->translate ( + data => $schema1, producer => 'SQLite', - ); + ) or die $t->error; like ($data2, qr/BEGIN.+COMMIT/is, 'Received some meaningful output from the producer'); # get a new translator $t = SQL::Translator->new; - $t->translate ( + my $schema2 = $t->translate ( parser => 'SQLite', data => \$data2, - ); - my $schema2 = $t->schema; + ) or die $t->error; isa_ok ($schema2, 'SQL::Translator::Schema', 'Second parser pass produced a schema'); my @t1 = $schema1->get_tables;