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,
}
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,
}
}
}
-for_each : /FOR EACH ROW/i | /FOR EACH STATEMENT/i
+for_each : /FOR EACH ROW/i
when : WHEN expr { $item[2] }
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'},
);
=cut
use strict;
+use warnings;
use Data::Dumper;
use SQL::Translator::Schema::Constants;
use SQL::Translator::Utils qw(debug header_comment);
$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;
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,
});
}
+ 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"));
}
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;
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 ' : '';
#
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
$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
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 {
use base 'SQL::Translator::Schema::Object';
+use Carp;
+
use vars qw($TABLE_COUNT $VIEW_COUNT);
# ----------------------------------------------------------------------
$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];
}
# ----------------------------------------------------------------------
Gets or sets the events that triggers the trigger.
- my $ok = $trigger->database_event('insert');
+ my $ok = $trigger->database_events('insert');
=cut
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;