From: Andrew Rodland Date: Wed, 2 Jun 2010 00:03:00 +0000 (+0000) Subject: Fix SQLite producer create_view so it doesn't generate statements with semicolons. X-Git-Tag: v0.11008~48 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4c0d31c125be1c212c2b984f4a4d806a61b2bd6e;p=dbsrgits%2FSQL-Translator.git Fix SQLite producer create_view so it doesn't generate statements with semicolons. --- diff --git a/lib/SQL/Translator/Producer/SQLite.pm b/lib/SQL/Translator/Producer/SQLite.pm index cc8c44c..a2c6c32 100644 --- a/lib/SQL/Translator/Producer/SQLite.pm +++ b/lib/SQL/Translator/Producer/SQLite.pm @@ -137,19 +137,26 @@ sub create_view { # Header. Should this look like what mysqldump produces? my $extra = $view->extra; - my $create = ''; - $create .= "--\n-- View: ${view_name}\n--\n" unless $options->{no_comments}; - $create .= "DROP VIEW IF EXISTS $view_name;\n" if $add_drop_view; - $create .= 'CREATE'; - $create .= " TEMPORARY" if exists($extra->{temporary}) && $extra->{temporary}; - $create .= ' VIEW'; - $create .= " IF NOT EXISTS" if exists($extra->{if_not_exists}) && $extra->{if_not_exists}; - $create .= " ${view_name}"; + my @create; + push @create, "DROP VIEW IF EXISTS $view_name" if $add_drop_view; + + my $create_view = 'CREATE'; + $create_view .= " TEMPORARY" if exists($extra->{temporary}) && $extra->{temporary}; + $create_view .= ' VIEW'; + $create_view .= " IF NOT EXISTS" if exists($extra->{if_not_exists}) && $extra->{if_not_exists}; + $create_view .= " ${view_name}"; if( my $sql = $view->sql ){ - $create .= " AS\n ${sql}"; + $create_view .= " AS\n ${sql}"; } - return $create; + push @create, $create_view; + + # Tack the comment onto the first statement. + unless ($options->{no_comments}) { + $create[0] = "--\n-- View: ${view_name}\n--\n" . $create[0]; + } + + return @create; } diff --git a/t/48xml-to-sqlite.t b/t/48xml-to-sqlite.t index f1029eb..7226723 100644 --- a/t/48xml-to-sqlite.t +++ b/t/48xml-to-sqlite.t @@ -63,6 +63,7 @@ CREATE TABLE Another ( ); DROP VIEW IF EXISTS email_list; + CREATE VIEW email_list AS SELECT email FROM Basic WHERE (email IS NOT NULL); @@ -111,8 +112,8 @@ eq_or_diff(\@sql, id INTEGER PRIMARY KEY NOT NULL, num numeric(10,2) )', - 'DROP VIEW IF EXISTS email_list; -CREATE VIEW email_list AS + 'DROP VIEW IF EXISTS email_list', + 'CREATE VIEW email_list AS SELECT email FROM Basic WHERE (email IS NOT NULL)', 'DROP TRIGGER IF EXISTS foo_trigger', 'CREATE TRIGGER foo_trigger after insert on Basic BEGIN update modified=timestamp(); END', diff --git a/t/56-sqlite-producer.t b/t/56-sqlite-producer.t index 4e2a969..945a8bd 100644 --- a/t/56-sqlite-producer.t +++ b/t/56-sqlite-producer.t @@ -18,19 +18,19 @@ use SQL::Translator::Producer::SQLite; if_not_exists => 1, }); my $create_opts = { no_comments => 1 }; - my $view1_sql1 = SQL::Translator::Producer::SQLite::create_view($view1, $create_opts); + my $view1_sql1 = [ SQL::Translator::Producer::SQLite::create_view($view1, $create_opts) ]; - my $view_sql_replace = "CREATE TEMPORARY VIEW IF NOT EXISTS view_foo AS - SELECT id, name FROM thing"; - is($view1_sql1, $view_sql_replace, 'correct "CREATE TEMPORARY VIEW" SQL'); + my $view_sql_replace = [ "CREATE TEMPORARY VIEW IF NOT EXISTS view_foo AS + SELECT id, name FROM thing" ]; + is_deeply($view1_sql1, $view_sql_replace, 'correct "CREATE TEMPORARY VIEW" SQL'); my $view2 = SQL::Translator::Schema::View->new( name => 'view_foo', fields => [qw/id name/], sql => 'SELECT id, name FROM thing',); - my $view1_sql2 = SQL::Translator::Producer::SQLite::create_view($view2, $create_opts); - my $view_sql_noreplace = "CREATE VIEW view_foo AS - SELECT id, name FROM thing"; - is($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL'); + my $view1_sql2 = [ SQL::Translator::Producer::SQLite::create_view($view2, $create_opts) ]; + my $view_sql_noreplace = [ "CREATE VIEW view_foo AS + SELECT id, name FROM thing" ]; + is_deeply($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL'); } diff --git a/t/57-class-dbi.t b/t/57-class-dbi.t index 4d19dbc..ace3f53 100644 --- a/t/57-class-dbi.t +++ b/t/57-class-dbi.t @@ -21,11 +21,11 @@ use SQL::Translator::Producer::SQLite; ); my $create_opts = { no_comments => 1 }; my $view1_sql1 = - SQL::Translator::Producer::SQLite::create_view( $view1, $create_opts ); + [ SQL::Translator::Producer::SQLite::create_view( $view1, $create_opts ) ]; - my $view_sql_replace = "CREATE TEMPORARY VIEW IF NOT EXISTS view_foo AS - SELECT id, name FROM thing"; - is( $view1_sql1, $view_sql_replace, 'correct "CREATE TEMPORARY VIEW" SQL' ); + my $view_sql_replace = [ "CREATE TEMPORARY VIEW IF NOT EXISTS view_foo AS + SELECT id, name FROM thing" ]; + is_deeply( $view1_sql1, $view_sql_replace, 'correct "CREATE TEMPORARY VIEW" SQL' ); my $view2 = SQL::Translator::Schema::View->new( name => 'view_foo', @@ -34,8 +34,8 @@ use SQL::Translator::Producer::SQLite; ); my $view1_sql2 = - SQL::Translator::Producer::SQLite::create_view( $view2, $create_opts ); - my $view_sql_noreplace = "CREATE VIEW view_foo AS - SELECT id, name FROM thing"; - is( $view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL' ); + [ SQL::Translator::Producer::SQLite::create_view( $view2, $create_opts ) ]; + my $view_sql_noreplace = [ "CREATE VIEW view_foo AS + SELECT id, name FROM thing" ]; + is_deeply( $view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL' ); }