X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FParser%2FPostgreSQL.pm;h=6a83b8882a5ce9f1644463c234600f9b2475070a;hb=d4f84dd192edc7a64a0b1a9923f1bafc0bc5ef9d;hp=09a881577cbf8588a512a44c1425da9f81b43d77;hpb=a20abbda86ddc2117f815ac62867bfa3de6a0ab4;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Parser/PostgreSQL.pm b/lib/SQL/Translator/Parser/PostgreSQL.pm index 09a8815..6a83b88 100644 --- a/lib/SQL/Translator/Parser/PostgreSQL.pm +++ b/lib/SQL/Translator/Parser/PostgreSQL.pm @@ -1,9 +1,9 @@ package SQL::Translator::Parser::PostgreSQL; # ------------------------------------------------------------------- -# $Id: PostgreSQL.pm,v 1.50 2007-11-13 19:25:04 mwz444 Exp $ +# $Id: PostgreSQL.pm 1440 2009-01-17 16:31:57Z jawnsy $ # ------------------------------------------------------------------- -# Copyright (C) 2002-4 SQLFairy Authors +# Copyright (C) 2002-2009 SQLFairy Authors # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -107,8 +107,7 @@ View table: =cut use strict; -use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.50 $ =~ /(\d+)\.(\d+)/; +use vars qw[ $DEBUG $GRAMMAR @EXPORT_OK ]; $DEBUG = 0 unless defined $DEBUG; use Data::Dumper; @@ -154,6 +153,9 @@ statement : create | connect | update | set + | select + | copy + | readin_symbol | connect : /^\s*\\\connect.*\n/ @@ -172,6 +174,9 @@ revoke : /revoke/i WORD(s /,/) /on/i TABLE(?) table_name /from/i name_with_opt_q } } +revoke : /revoke/i WORD(s /,/) /on/i SCHEMA(?) schema_name /from/i name_with_opt_quotes(s /,/) ';' + { 1 } + grant : /grant/i WORD(s /,/) /on/i TABLE(?) table_name /to/i name_with_opt_quotes(s /,/) ';' { my $table_info = $item{'table_name'}; @@ -184,6 +189,9 @@ grant : /grant/i WORD(s /,/) /on/i TABLE(?) table_name /to/i name_with_opt_quote } } +grant : /grant/i WORD(s /,/) /on/i SCHEMA(?) schema_name /to/i name_with_opt_quotes(s /,/) ';' + { 1 } + drop : /drop/i /[^;]*/ ';' string : @@ -200,7 +208,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'}; @@ -209,13 +217,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 } = @@ -235,7 +245,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; } @@ -462,6 +472,8 @@ table_name : schema_qualification(?) name_with_opt_quotes { schema_qualification : name_with_opt_quotes '.' +schema_name : name_with_opt_quotes + field_name : name_with_opt_quotes name_with_opt_quotes : double_quote(?) NAME double_quote(?) { $item[2] } @@ -594,7 +606,8 @@ pg_data_type : parens_value_list : '(' VALUE(s /,/) ')' { $item[2] } -parens_word_list : '(' WORD(s /,/) ')' + +parens_word_list : '(' name_with_opt_quotes(s /,/) ')' { $item[2] } field_size : '(' num_range ')' { $item{'num_range'} } @@ -765,8 +778,18 @@ alter : alter_table table_name DROP /constraint/i NAME restrict_or_cascade ';' alter : alter_table table_name /owner/i /to/i NAME ';' { 1 } +alter : alter_sequence NAME /owned/i /by/i column_name ';' + { 1 } + 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'} } @@ -832,6 +855,8 @@ add_column : ADD COLUMN(?) alter_table : ALTER TABLE ONLY(?) +alter_sequence : ALTER SEQUENCE + drop_column : DROP COLUMN(?) alter_column : ALTER COLUMN(?) @@ -841,6 +866,24 @@ rename_column : /rename/i COLUMN(?) restrict_or_cascade : /restrict/i | /cascade/i +# Handle functions that can be called +select : SELECT select_function ';' + { 1 } + +# Read the setval function but don't do anything with it because this parser +# isn't handling sequences +select_function : schema_qualification(?) /setval/i '(' VALUE /,/ VALUE /,/ /(true|false)/i ')' + { 1 } + +# Skipping all COPY commands +copy : COPY WORD /[^;]+/ ';' { 1 } + { 1 } + +# The "\." allows reading in from STDIN but this isn't needed for schema +# creation, so it is skipped. +readin_symbol : '\.' + {1} + # # End basically useless stuff. - ky # @@ -849,7 +892,7 @@ create_table : CREATE TABLE create_index : CREATE /index/i -default_val : DEFAULT /(\d+|'[^']*'|\w+\(.*?\))|\w+/ +default_val : DEFAULT /(\d+|'[^']*'|\w+\(.*\))|\w+/ { my $val = defined $item[2] ? $item[2] : ''; $val =~ s/^'|'$//g; @@ -901,8 +944,16 @@ COLUMN : /column/i TABLE : /table/i +SCHEMA : /schema/i + SEMICOLON : /\s*;\n?/ +SEQUENCE : /sequence/i + +SELECT : /select/i + +COPY : /copy/i + INTEGER : /\d+/ WORD : /\w+/ @@ -958,6 +1009,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 { @@ -993,7 +1046,7 @@ sub parse { name => $idata->{'name'}, type => uc $idata->{'type'}, fields => $idata->{'fields'}, - ) or die $table->error; + ) or die $table->error . ' ' . $table->name; } for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {