From: Jess Robinson Date: Mon, 29 Dec 2008 00:53:08 +0000 (+0000) Subject: Support for temporary tables in Pg, from nachos X-Git-Tag: v0.11008~263 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3e98f7d9a1ad37449f7f441c96ca47be8182e496;p=dbsrgits%2FSQL-Translator.git Support for temporary tables in Pg, from nachos --- diff --git a/Changes b/Changes index 1c44470..a0a2828 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,4 @@ +* Add support for temporary tables in Pg (nachos) # ---------------------------------------------------------- # 0.09002 2008-12-05 # ---------------------------------------------------------- diff --git a/lib/SQL/Translator/Parser/PostgreSQL.pm b/lib/SQL/Translator/Parser/PostgreSQL.pm index 525582c..1a80e3f 100644 --- a/lib/SQL/Translator/Parser/PostgreSQL.pm +++ b/lib/SQL/Translator/Parser/PostgreSQL.pm @@ -209,7 +209,7 @@ update : /update/i statement_body ';' # # Create table. # -create : create_table table_name '(' create_definition(s? /,/) ')' table_option(s?) ';' +create : CREATE temporary_table(?) TABLE table_name '(' create_definition(s? /,/) ')' table_option(s?) ';' { my $table_info = $item{'table_name'}; my $schema_name = $table_info->{'schema_name'}; @@ -218,13 +218,15 @@ create : create_table table_name '(' create_definition(s? /,/) ')' table_option( $tables{ $table_name }{'schema_name'} = $schema_name; $tables{ $table_name }{'table_name'} = $table_name; + $tables{ $table_name }{'temporary'} = $item[2][0]; + if ( @table_comments ) { $tables{ $table_name }{'comments'} = [ @table_comments ]; @table_comments = (); } my @constraints; - for my $definition ( @{ $item[4] } ) { + for my $definition ( @{ $item[6] } ) { if ( $definition->{'supertype'} eq 'field' ) { my $field_name = $definition->{'name'}; $tables{ $table_name }{'fields'}{ $field_name } = @@ -244,7 +246,7 @@ create : create_table table_name '(' create_definition(s? /,/) ')' table_option( } } - for my $option ( @{ $item[6] } ) { + for my $option ( @{ $item[8] } ) { $tables{ $table_name }{'table_options(s?)'}{ $option->{'type'} } = $option; } @@ -782,6 +784,13 @@ alter : alter_sequence NAME /owned/i /by/i column_name ';' storage_type : /(plain|external|extended|main)/i +temporary: /temp(orary)?\\b/i + +temporary_table: temporary + { + 1; + } + alter_default_val : SET default_val { $return = { value => $item[2]->{'value'} } @@ -1001,6 +1010,8 @@ sub parse { name => $tdata->{'table_name'}, ) or die "Couldn't create table '$table_name': " . $schema->error; + $table->extra(temporary => 1) if $tdata->{'temporary'}; + $table->comments( $tdata->{'comments'} ); my @fields = sort { diff --git a/lib/SQL/Translator/Producer/PostgreSQL.pm b/lib/SQL/Translator/Producer/PostgreSQL.pm index 17924f3..920a893 100644 --- a/lib/SQL/Translator/Producer/PostgreSQL.pm +++ b/lib/SQL/Translator/Producer/PostgreSQL.pm @@ -392,6 +392,13 @@ sub create_table push @fks, @$fks; } + + my $temporary = ""; + + if(exists $table->{extra}{temporary}) { + $temporary = $table->{extra}{temporary} ? "TEMPORARY " : ""; + } + my $create_statement; $create_statement = join("\n", @comments); if ($add_drop_table) { @@ -405,7 +412,7 @@ sub create_table } $create_statement .= join("\n", @type_defs) . "\n" if $postgres_version >= 8.3; - $create_statement .= qq[CREATE TABLE $qt$table_name_ur$qt (\n]. + $create_statement .= qq[CREATE ${temporary}TABLE $qt$table_name_ur$qt (\n]. join( ",\n", map { " $_" } @field_defs, @constraint_defs ). "\n)" ; diff --git a/t/14postgres-parser.t b/t/14postgres-parser.t index e655614..3bfe6a5 100644 --- a/t/14postgres-parser.t +++ b/t/14postgres-parser.t @@ -8,7 +8,7 @@ use SQL::Translator::Schema::Constants; use Test::SQL::Translator qw(maybe_plan); BEGIN { - maybe_plan(117, 'SQL::Translator::Parser::PostgreSQL'); + maybe_plan(120, 'SQL::Translator::Parser::PostgreSQL'); SQL::Translator::Parser::PostgreSQL->import('parse'); } @@ -40,6 +40,24 @@ my $sql = q[ check (f_int between 1 and 5) ); + CREATE TABLE products_1 ( + product_no integer, + name text, + price numeric + ); + + CREATE TEMP TABLE products_2 ( + product_no integer, + name text, + price numeric + ); + + CREATE TEMPORARY TABLE products_3 ( + product_no integer, + name text, + price numeric + ); + alter table t_test1 add f_fk2 integer; alter table only t_test1 add constraint c_u1 unique (f_varchar); @@ -80,7 +98,7 @@ my $schema = $t->schema; isa_ok( $schema, 'SQL::Translator::Schema', 'Schema object' ); my @tables = $schema->get_tables; -is( scalar @tables, 2, 'Two tables' ); +is( scalar @tables, 5, 'Five tables' ); my $t1 = shift @tables; is( $t1->name, 't_test1', 'Table t_test1 exists' ); @@ -255,3 +273,8 @@ is( $t2_c2->type, PRIMARY_KEY, "Constraint is a PK" ); my $t2_c3 = shift @t2_constraints; is( $t2_c3->type, CHECK_C, "Constraint is a 'CHECK'" ); + +# test temporary tables +is( exists $schema->get_table('products_1')->extra()->{'temporary'}, "", "Table is NOT temporary"); +is( $schema->get_table('products_2')->extra('temporary'), 1,"Table is TEMP"); +is( $schema->get_table('products_3')->extra('temporary'), 1,"Table is TEMPORARY");