+
* Correct postgis geography type insertion and linebreak fix for multiple geometry/geography columns
+* made PostgreSQL producer consistent with other producers in terms of
+ quoting and allowing functions in constraints and indices
# ----------------------------------------------------------
# 0.11007 2010-11-30
# my $max_id_length = 62;
my %used_identifiers = ();
my %global_names;
-my %unreserve;
my %truncated;
=pod
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 wantarray
}
# -------------------------------------------------------------------
-sub unreserve {
- my $name = shift || '';
- my $schema_obj_name = shift || '';
-
- my ( $suffix ) = ( $name =~ s/(\W.*)$// ) ? $1 : '';
-
- # also trap fields that don't begin with a letter
- return $name if (!$reserved{ uc $name }) && $name =~ /^[a-z]/i;
-
- if ( $schema_obj_name ) {
- ++$unreserve{"$schema_obj_name.$name"};
- }
- else {
- ++$unreserve{"$name (table name)"};
- }
-
- my $unreserve = sprintf '%s_', $name;
- return $unreserve.$suffix;
-}
-
-# -------------------------------------------------------------------
sub next_unused_name {
my $orig_name = shift or return;
my $name = $orig_name;
my $table_name = $table->name or next;
my ( $fql_tbl_name ) = ( $table_name =~ s/\W(.*)$// ) ? $1 : q{};
my $table_name_ur = $qt ? $table_name
- : $fql_tbl_name ? join('.', $table_name, unreserve($fql_tbl_name))
- : unreserve($table_name);
+ : $fql_tbl_name ? join('.', $table_name, $fql_tbl_name)
+ : $table_name;
$table->name($table_name_ur);
# print STDERR "$table_name table_name\n";
$field_name_scope{$table_name} ||= {};
my $field_name = $field->name;
- my $field_name_ur = $qf ? $field_name : unreserve($field_name, $table_name );
- $field->name($field_name_ur);
my $field_comments = $field->comments
? "-- " . $field->comments . "\n "
: '';
- my $field_def = $field_comments.qq[$qf$field_name_ur$qf];
+ my $field_def = $field_comments.qq[$qf$field_name$qf];
#
# Datatype
my $qt = $options->{quote_table_names} ||'';
my $qf = $options->{quote_field_names} ||'';
my $table_name = $index->table->name;
-# my $table_name_ur = $qt ? unreserve($table_name) : $table_name;
my ($index_def, @constraint_defs);
);
my $type = $index->type || NORMAL;
- my @fields =
- map { $_ =~ s/\(.+\)//; $_ }
- map { $qt ? $_ : unreserve($_, $table_name ) }
- $index->fields;
+ my @fields = $index->fields;
next unless @fields;
- my $def_start = qq[CONSTRAINT "$name" ];
+ my $def_start = qq[CONSTRAINT ${qf}$name${qf} ];
+ my $field_names = '(' . join(", ", (map { $_ =~ /\(.*\)/ ? $_ : ($qf . $_ . $qf ) } @fields)) . ')';
if ( $type eq PRIMARY_KEY ) {
- push @constraint_defs, "${def_start}PRIMARY KEY ".
- '(' .$qf . join( $qf. ', '.$qf, @fields ) . $qf . ')';
+ push @constraint_defs, "${def_start}PRIMARY KEY ".$field_names;
}
elsif ( $type eq UNIQUE ) {
- push @constraint_defs, "${def_start}UNIQUE " .
- '(' . $qf . join( $qf. ', '.$qf, @fields ) . $qf.')';
+ push @constraint_defs, "${def_start}UNIQUE " .$field_names;
}
elsif ( $type eq NORMAL ) {
$index_def =
- "CREATE INDEX ${qf}${name}${qf} on ${qt}${table_name}${qt} (".
- join( ', ', map { qq[$qf$_$qf] } @fields ).
- ')'
+ "CREATE INDEX ${qf}${name}${qf} on ${qt}${table_name}${qt} ".$field_names
;
}
else {
$name = next_unused_name($name);
}
- my @fields =
- map { $_ =~ s/\(.+\)//; $_ }
- map { $qt ? $_ : unreserve( $_, $table_name )}
- $c->fields;
+ my @fields = grep { defined } $c->fields;
- my @rfields =
- map { $_ =~ s/\(.+\)//; $_ }
- map { $qt ? $_ : unreserve( $_, $table_name )}
- $c->reference_fields;
+ my @rfields = grep { defined } $c->reference_fields;
next if !@fields && $c->type ne CHECK_C;
- my $def_start = $name ? qq[CONSTRAINT "$name" ] : '';
+ my $def_start = $name ? qq[CONSTRAINT ${qf}$name${qf} ] : '';
+ my $field_names = '(' . join(", ", (map { $_ =~ /\(.*\)/ ? $_ : ($qf . $_ . $qf ) } @fields)) . ')';
if ( $c->type eq PRIMARY_KEY ) {
- push @constraint_defs, "${def_start}PRIMARY KEY ".
- '('.$qf . join( $qf.', '.$qf, @fields ) . $qf.')';
+ push @constraint_defs, "${def_start}PRIMARY KEY ".$field_names;
}
elsif ( $c->type eq UNIQUE ) {
- $name = next_unused_name($name);
- push @constraint_defs, "${def_start}UNIQUE " .
- '('.$qf . join( $qf.', '.$qf, @fields ) . $qf.')';
+ push @constraint_defs, "${def_start}UNIQUE " .$field_names;
}
elsif ( $c->type eq CHECK_C ) {
my $expression = $c->expression;
push @constraint_defs, "${def_start}CHECK ($expression)";
}
elsif ( $c->type eq FOREIGN_KEY ) {
- my $def .= "ALTER TABLE ${qt}${table_name}${qt} ADD FOREIGN KEY (" .
- join( ', ', map { qq[$qf$_$qf] } @fields ) . ')' .
+ my $def .= "ALTER TABLE ${qt}${table_name}${qt} ADD FOREIGN KEY " . $field_names .
"\n REFERENCES " . $qt . $c->reference_table . $qt;
if ( @rfields ) {
ALTER TABLE person RENAME COLUMN description TO physical_description;
-ALTER TABLE person ADD CONSTRAINT "unique_name" UNIQUE (name);
+ALTER TABLE person ADD CONSTRAINT unique_name UNIQUE (name);
ALTER TABLE employee ADD FOREIGN KEY (employee_id)
REFERENCES person (person_id) DEFERRABLE;
-ALTER TABLE person ADD CONSTRAINT "UC_person_id" UNIQUE (person_id);
+ALTER TABLE person ADD CONSTRAINT UC_person_id UNIQUE (person_id);
-ALTER TABLE person ADD CONSTRAINT "UC_age_name" UNIQUE (age, name);
+ALTER TABLE person ADD CONSTRAINT UC_age_name UNIQUE (age, name);
DROP TABLE deleted CASCADE;
ALTER TABLE person RENAME COLUMN description TO physical_description;
-ALTER TABLE person ADD CONSTRAINT "UC_person_id" UNIQUE (person_id);
+ALTER TABLE person ADD CONSTRAINT UC_person_id UNIQUE (person_id);
-ALTER TABLE person ADD CONSTRAINT "UC_age_name" UNIQUE (age, name);
+ALTER TABLE person ADD CONSTRAINT UC_age_name UNIQUE (age, name);
DROP TABLE deleted CASCADE;
use Test::More;
use Test::Exception;
use Test::SQL::Translator qw(maybe_plan);
-
+use SQL::Translator::Schema::Constants;
use Data::Dumper;
use FindBin qw/$Bin/;
#=============================================================================
BEGIN {
- maybe_plan(26,
+ maybe_plan(38,
'SQL::Translator::Producer::PostgreSQL',
'Test::Differences',
)
SELECT id, name FROM thing
WITH CASCADED CHECK OPTION";
is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2');
+
+{
+ my $table = SQL::Translator::Schema::Table->new( name => 'foobar', fields => [qw( foo bar )] );
+ my $quote = { quote_table_names => '"', quote_field_names => '"' };
+
+ {
+ my $index = $table->add_index(name => 'myindex', fields => ['foo']);
+ my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
+ is($def, "CREATE INDEX myindex on foobar (foo)", 'index created');
+ ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
+ is($def, 'CREATE INDEX "myindex" on "foobar" ("foo")', 'index created w/ quotes');
+ }
+
+ {
+ my $index = $table->add_index(name => 'myindex', fields => ['lower(foo)']);
+ my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
+ is($def, "CREATE INDEX myindex on foobar (lower(foo))", 'index created');
+ ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
+ is($def, 'CREATE INDEX "myindex" on "foobar" (lower(foo))', 'index created w/ quotes');
+ }
+
+ {
+ my $index = $table->add_index(name => 'myindex', fields => ['bar', 'lower(foo)']);
+ my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
+ is($def, "CREATE INDEX myindex on foobar (bar, lower(foo))", 'index created');
+ ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
+ is($def, 'CREATE INDEX "myindex" on "foobar" ("bar", lower(foo))', 'index created w/ quotes');
+ }
+
+ {
+ my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['foo']);
+ my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
+ is($def->[0], 'CONSTRAINT constr UNIQUE (foo)', 'constraint created');
+ ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
+ is($def->[0], 'CONSTRAINT "constr" UNIQUE ("foo")', 'constraint created w/ quotes');
+ }
+
+ {
+ my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['lower(foo)']);
+ my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
+ is($def->[0], 'CONSTRAINT constr UNIQUE (lower(foo))', 'constraint created');
+ ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
+ is($def->[0], 'CONSTRAINT "constr" UNIQUE (lower(foo))', 'constraint created w/ quotes');
+ }
+
+ {
+ my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['bar', 'lower(foo)']);
+ my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
+ is($def->[0], 'CONSTRAINT constr UNIQUE (bar, lower(foo))', 'constraint created');
+ ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
+ is($def->[0], 'CONSTRAINT "constr" UNIQUE ("bar", lower(foo))', 'constraint created w/ quotes');
+ }
+}
my $field1_geocon = SQL::Translator::Producer::PostgreSQL::add_geometry_constraints($field1);
-is($field1_geocon, qq[ALTER TABLE mytable ADD CONSTRAINT "enforce_dims_myfield" CHECK ((st_ndims(myfield) = 2))
-ALTER TABLE mytable ADD CONSTRAINT "enforce_srid_myfield" CHECK ((st_srid(myfield) = -1))
-ALTER TABLE mytable ADD CONSTRAINT "enforce_geotype_myfield" CHECK ((geometrytype(myfield) = 'POINT'::text OR myfield IS NULL))],
+is($field1_geocon, qq[ALTER TABLE mytable ADD CONSTRAINT enforce_dims_myfield CHECK ((st_ndims(myfield) = 2))
+ALTER TABLE mytable ADD CONSTRAINT enforce_srid_myfield CHECK ((st_srid(myfield) = -1))
+ALTER TABLE mytable ADD CONSTRAINT enforce_geotype_myfield CHECK ((geometrytype(myfield) = 'POINT'::text OR myfield IS NULL))],
'Add geometry constraints works');
my $field2 = SQL::Translator::Schema::Field->new( name => 'myfield',
is($alter_field2, qq[ALTER TABLE mytable ALTER COLUMN myfield DROP NOT NULL
ALTER TABLE mytable ALTER COLUMN myfield TYPE geometry
INSERT INTO geometry_columns VALUES ('','myschema','mytable','myfield','2','-1','POINT')
-ALTER TABLE mytable ADD CONSTRAINT "enforce_dims_myfield" CHECK ((st_ndims(myfield) = 2))
-ALTER TABLE mytable ADD CONSTRAINT "enforce_srid_myfield" CHECK ((st_srid(myfield) = -1))
-ALTER TABLE mytable ADD CONSTRAINT "enforce_geotype_myfield" CHECK ((geometrytype(myfield) = 'POINT'::text OR myfield IS NULL))],
+ALTER TABLE mytable ADD CONSTRAINT enforce_dims_myfield CHECK ((st_ndims(myfield) = 2))
+ALTER TABLE mytable ADD CONSTRAINT enforce_srid_myfield CHECK ((st_srid(myfield) = -1))
+ALTER TABLE mytable ADD CONSTRAINT enforce_geotype_myfield CHECK ((geometrytype(myfield) = 'POINT'::text OR myfield IS NULL))],
'Alter field non geometry to geometry works');
$field1->name('field3');
is($add_field, qq[ALTER TABLE mytable ADD COLUMN field3 geometry
INSERT INTO geometry_columns VALUES ('','myschema','mytable','field3','2','-1','POINT')
-ALTER TABLE mytable ADD CONSTRAINT "enforce_dims_field3" CHECK ((st_ndims(field3) = 2))
-ALTER TABLE mytable ADD CONSTRAINT "enforce_srid_field3" CHECK ((st_srid(field3) = -1))
-ALTER TABLE mytable ADD CONSTRAINT "enforce_geotype_field3" CHECK ((geometrytype(field3) = 'POINT'::text OR field3 IS NULL))],
+ALTER TABLE mytable ADD CONSTRAINT enforce_dims_field3 CHECK ((st_ndims(field3) = 2))
+ALTER TABLE mytable ADD CONSTRAINT enforce_srid_field3 CHECK ((st_srid(field3) = -1))
+ALTER TABLE mytable ADD CONSTRAINT enforce_geotype_field3 CHECK ((geometrytype(field3) = 'POINT'::text OR field3 IS NULL))],
'Add geometry field works');
my $drop_field = SQL::Translator::Producer::PostgreSQL::drop_field($field1);
CREATE TABLE mytable (
field3 geometry,
field4 geography(POINT,-1),
- CONSTRAINT "enforce_dims_field3" CHECK ((st_ndims(field3) = 2)),
- CONSTRAINT "enforce_srid_field3" CHECK ((st_srid(field3) = -1)),
- CONSTRAINT "enforce_geotype_field3" CHECK ((geometrytype(field3) = 'POINT'::text OR field3 IS NULL))
+ CONSTRAINT enforce_dims_field3 CHECK ((st_ndims(field3) = 2)),
+ CONSTRAINT enforce_srid_field3 CHECK ((st_srid(field3) = -1)),
+ CONSTRAINT enforce_geotype_field3 CHECK ((geometrytype(field3) = 'POINT'::text OR field3 IS NULL))
);
INSERT INTO geometry_columns VALUES ('','myschema','mytable','field3','2','-1','POINT')],'Create table with geometry works.');