From: Ken Youens-Clark Date: Wed, 4 Dec 2002 01:53:51 +0000 (+0000) Subject: Fixed a bug in Oracle producer that allowed for identifiers longer than the X-Git-Tag: v0.01~32 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=da8e499e6ef984a6248483ea280664dc3dbddc1d;p=dbsrgits%2FSQL-Translator.git Fixed a bug in Oracle producer that allowed for identifiers longer than the maximum allowed length, also there was a problem with not using the unreserved table name. Added decent amount of functionality to PG producer. --- diff --git a/lib/SQL/Translator/Producer/Oracle.pm b/lib/SQL/Translator/Producer/Oracle.pm index e01d4ca..aea2153 100644 --- a/lib/SQL/Translator/Producer/Oracle.pm +++ b/lib/SQL/Translator/Producer/Oracle.pm @@ -1,7 +1,7 @@ package SQL::Translator::Producer::Oracle; # ------------------------------------------------------------------- -# $Id: Oracle.pm,v 1.6 2002-11-26 03:59:58 kycl4rk Exp $ +# $Id: Oracle.pm,v 1.7 2002-12-04 01:53:51 kycl4rk Exp $ # ------------------------------------------------------------------- # Copyright (C) 2002 Ken Y. Clark , # darren chamberlain @@ -23,7 +23,7 @@ package SQL::Translator::Producer::Oracle; use strict; use vars qw[ $VERSION $DEBUG $WARN ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/; $DEBUG = 0 unless defined $DEBUG; my %translate = ( @@ -211,7 +211,7 @@ sub produce { # # Default value # - if ( $field->{'default'} ) { + if ( defined $field->{'default'} ) { $field_str .= sprintf( ' DEFAULT %s', $field->{'default'} =~ m/null/i ? 'NULL' : @@ -297,8 +297,10 @@ sub produce { $index_name = mk_name( $table_name, $index_name || ++$idx_name_default ); - push @index_decs, "CREATE INDEX $index_name on $table_name (". - join( ', ', @fields ). ");"; + push @index_decs, + "CREATE INDEX $index_name on $table_name_ur (". + join( ', ', @fields ). + ");"; } else { warn "Unknown index type ($index_type) on table $table_name.\n" diff --git a/lib/SQL/Translator/Producer/PostgreSQL.pm b/lib/SQL/Translator/Producer/PostgreSQL.pm index 5fc1ffa..5cb3ab5 100644 --- a/lib/SQL/Translator/Producer/PostgreSQL.pm +++ b/lib/SQL/Translator/Producer/PostgreSQL.pm @@ -1,7 +1,7 @@ package SQL::Translator::Producer::PostgreSQL; # ------------------------------------------------------------------- -# $Id: PostgreSQL.pm,v 1.3 2002-11-26 03:59:58 kycl4rk Exp $ +# $Id: PostgreSQL.pm,v 1.4 2002-12-04 01:53:51 kycl4rk Exp $ # ------------------------------------------------------------------- # Copyright (C) 2002 Ken Y. Clark , # darren chamberlain @@ -29,7 +29,7 @@ SQL::Translator::Producer::PostgreSQL - PostgreSQL producer for SQL::Translator use strict; use vars qw[ $DEBUG $WARN $VERSION ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/; $DEBUG = 1 unless defined $DEBUG; use Data::Dumper; @@ -47,7 +47,7 @@ my %translate = ( smallint => 'smallint', tinyint => 'smallint', char => 'char', - varchar => 'varchar', + varchar => 'character varying', longtext => 'text', mediumtext => 'text', text => 'text', @@ -56,8 +56,8 @@ my %translate = ( blob => 'bytea', mediumblob => 'bytea', longblob => 'bytea', - enum => 'varchar', - set => 'varchar', + enum => 'character varying', + set => 'character varying', date => 'date', datetime => 'timestamp', time => 'date', @@ -69,7 +69,7 @@ my %translate = ( # number => 'integer', char => 'char', - varchar2 => 'varchar', + varchar2 => 'character varying', long => 'text', CLOB => 'bytea', date => 'date', @@ -79,7 +79,7 @@ my %translate = ( # int => 'integer', money => 'money', - varchar => 'varchar', + varchar => 'character varying', datetime => 'timestamp', text => 'text', real => 'double precision', @@ -143,6 +143,15 @@ and table_constraint is: [ MATCH FULL | MATCH PARTIAL ] [ ON DELETE action ] [ ON UPDATE action ] } [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] +=head1 Create Index Syntax + + CREATE [ UNIQUE ] INDEX index_name ON table + [ USING acc_method ] ( column [ ops_name ] [, ...] ) + [ WHERE predicate ] + CREATE [ UNIQUE ] INDEX index_name ON table + [ USING acc_method ] ( func_name( column [, ... ]) [ ops_name ] ) + [ WHERE predicate ] + =cut # ------------------------------------------------------------------- @@ -153,9 +162,9 @@ sub produce { my $no_comments = $translator->no_comments; my $add_drop_table = $translator->add_drop_table; - my $create; + my $output; unless ( $no_comments ) { - $create .= sprintf + $output .= sprintf "--\n-- Created by %s\n-- Created on %s\n--\n\n", __PACKAGE__, scalar localtime; } @@ -166,86 +175,172 @@ sub produce { map { [ $_->{'order'}, $_ ] } values %$data ) { - my $table_name = $table->{'table_name'}; - my @fields = - map { $_->[1] } - sort { $a->[0] <=> $b->[0] } - map { [ $_->{'order'}, $_ ] } - values %{ $table->{'fields'} }; + my $table_name = $table->{'table_name'}; + $table_name = mk_name( $table_name, '', undef, 1 ); + my $table_name_ur = unreserve($table_name); + + my ( @comments, @field_decs, @sequence_decs, @constraints ); - $create .= "--\n-- Table: $table_name\n--\n" unless $no_comments; - $create = "DROP TABLE $table_name;\n" if $add_drop_table; - $create .= "CREATE TABLE $table_name (\n"; + push @comments, "--\n-- Table: $table_name_ur\n--" unless $no_comments; # # Fields # my %field_name_scope; - my @field_statements; - for my $field ( @fields ) { - my @fdata = ("", $field); - + for my $field ( + map { $_->[1] } + sort { $a->[0] <=> $b->[0] } + map { [ $_->{'order'}, $_ ] } + values %{ $table->{'fields'} } + ) { my $field_name = mk_name( $field->{'name'}, '', \%field_name_scope, 1 ); my $field_name_ur = unreserve( $field_name, $table_name ); - my $field_str = $field_name_ur; - - # data type and size - push @fdata, sprintf "%s%s", - $field->{'data_type'}, - ( defined $field->{'size'} ) - ? "($field->{'size'})" : ''; - - # Null? - push @fdata, "NOT NULL" unless $field->{'null'}; - - # Default? XXX Need better quoting! - my $default = $field->{'default'}; - if ( defined $default ) { - push @fdata, "DEFAULT '$default'"; -# if (int $default eq "$default") { -# push @fdata, "DEFAULT $default"; -# } else { -# push @fdata, "DEFAULT '$default'"; -# } + my $field_str = qq["$field_name_ur"]; + + # + # Datatype + # + my $data_type = lc $field->{'data_type'}; + my $list = $field->{'list'} || []; + my $commalist = join ",", @$list; + my $seq_name; + + if ( $data_type eq 'enum' ) { + my $len = 0; + $len = ($len < length($_)) ? length($_) : $len for (@$list); + my $check_name = mk_name( $table_name.'_'.$field_name, 'chk' ); + push @constraints, + "CONSTRAINT $check_name CHECK ($field_name IN ($commalist))"; + $field_str .= " character varying($len)"; + } + elsif ( $data_type eq 'set' ) { + # XXX add a CHECK constraint maybe + # (trickier and slower, than enum :) + my $len = length $commalist; + $field_str .= " character varying($len) /* set $commalist */"; + } + elsif ( $field->{'is_auto_inc'} ) { + $field_str .= ' serial'; + $seq_name = mk_name( $table_name.'_'.$field_name, 'sq' ); + push @sequence_decs, qq[DROP SEQUENCE "$seq_name";]; + push @sequence_decs, qq[CREATE SEQUENCE "$seq_name";]; + } + else { + $data_type = defined $translate{ $data_type } ? + $translate{ $data_type } : + die "Unknown datatype: $data_type\n"; + $field_str .= ' '.$data_type; + if ( $data_type =~ /(char|varbit|numeric|decimal)/i ) { + $field_str .= '('.join(',', @{ $field->{'size'} }).')' + if @{ $field->{'size'} || [] }; + } } - # auto_increment? - push @fdata, "auto_increment" if $field->{'is_auto_inc'}; + # + # Default value + # + if ( defined $field->{'default'} ) { + $field_str .= sprintf( ' DEFAULT %s', + ( $field->{'is_auto_inc'} && $seq_name ) + ? qq[nextval('"$seq_name"'::text)] : + ( $field->{'default'} =~ m/null/i ) + ? 'NULL' : + "'".$field->{'default'}."'" + ); + } - # primary key? - push @fdata, "PRIMARY KEY" if $field->{'is_primary_key'}; + # + # Not null constraint + # + unless ( $field->{'null'} ) { + my $constraint_name = mk_name($field_name_ur, 'nn'); +# $field_str .= ' CONSTRAINT '.$constraint_name.' NOT NULL'; + $field_str .= ' NOT NULL'; + } - push @field_statements, join( " ", @fdata ); + # + # Primary key + # +# if ( $field->{'is_primary_key'} ) { +# my $constraint_name = mk_name($field_name_ur, 'pk'); +# $field_str .= ' CONSTRAINT '.$constraint_name.' PRIMARY KEY'; +# } + push @field_decs, $field_str; } - $create .= join( ",\n", @field_statements ); # - # Other keys + # Index Declarations # - my @indices = @{ $table->{'indices'} || [] }; - for ( my $i = 0; $i <= $#indices; $i++ ) { - $create .= ",\n"; - my $key = $indices[$i]; - my ( $name, $type, $fields ) = @{ $key }{ qw( name type fields ) }; - if ( $type eq 'primary_key' ) { - $create .= " PRIMARY KEY (@{$fields})" - } + my @index_decs = (); + my $idx_name_default; + for my $index ( @{ $table->{'indices'} } ) { + my $index_name = $index->{'name'} || ''; + my $index_type = $index->{'type'} || 'normal'; + my @fields = map { unreserve( $_, $table_name ) } + @{ $index->{'fields'} }; + next unless @fields; + + if ( $index_type eq 'primary_key' ) { + $index_name = mk_name( $table_name, 'pk' ); + push @constraints, 'CONSTRAINT '.$index_name.' PRIMARY KEY '. + '(' . join( ', ', @fields ) . ')'; + } + elsif ( $index_type eq 'unique' ) { + $index_name = mk_name( + $table_name, $index_name || ++$idx_name_default + ); + push @constraints, 'CONSTRAINT ' . $index_name . ' UNIQUE ' . + '(' . join( ', ', @fields ) . ')'; + } + elsif ( $index_type eq 'normal' ) { + $index_name = mk_name( + $table_name, $index_name || ++$idx_name_default + ); + push @index_decs, + qq[CREATE INDEX "$index_name" on $table_name_ur (]. + join( ', ', @fields ). + ');'; + } else { - local $" = ", "; - $create .= " KEY $name (@{$fields})" + warn "Unknown index type ($index_type) on table $table_name.\n" + if $WARN; } } - # - # Footer - # - $create .= "\n);\n\n"; + my $create_statement; + $create_statement = qq[DROP TABLE "$table_name_ur";\n] + if $add_drop_table; + $create_statement .= qq[CREATE TABLE "$table_name_ur" (\n]. + join( ",\n", map { " $_" } @field_decs, @constraints ). + "\n);" + ; + + $output .= join( "\n\n", + @comments, + @sequence_decs, + $create_statement, + @index_decs, + '' + ); + } + + if ( $WARN ) { + if ( %truncated ) { + warn "Truncated " . keys( %truncated ) . " names:\n"; + warn "\t" . join( "\n\t", sort keys %truncated ) . "\n"; + } + + if ( %unreserve ) { + warn "Encounted " . keys( %unreserve ) . + " unsafe names in schema (reserved or invalid):\n"; + warn "\t" . join( "\n\t", sort keys %unreserve ) . "\n"; + } } - return $create; + return $output; } # -------------------------------------------------------------------