Correct postgis geography type insertion and linebreak fix for multiple geometry...
Peter Rabbitson [Wed, 15 Dec 2010 15:53:41 +0000 (15:53 +0000)]
Changes
lib/SQL/Translator/Producer/PostgreSQL.pm
t/63-spacial-pgsql.t

diff --git a/Changes b/Changes
index 271cb9c..62509d0 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,3 +1,5 @@
+* Correct postgis geography type insertion and linebreak fix for multiple geometry/geography columns
+
 # ----------------------------------------------------------
 # 0.11007 2010-11-30
 # ----------------------------------------------------------
index 1526d79..4029c5d 100644 (file)
@@ -326,7 +326,13 @@ sub next_unused_name {
 sub is_geometry
 {
        my $field = shift;
-       return 1 if $field->data_type eq 'geometry' || $field->data_type eq 'geography';
+       return 1 if $field->data_type eq 'geometry';
+}
+
+sub is_geography
+{
+    my $field = shift;
+    return 1 if $field->data_type eq 'geography';
 }
 
 sub create_table 
@@ -432,8 +438,10 @@ sub create_table
        #
        if(grep { is_geometry($_) } $table->get_fields){
         $create_statement .= ";";
-               $create_statement .= "\n".join("\n", map { drop_geometry_column($_) if is_geometry($_); } $table->get_fields) if $options->{add_drop_table};
-               $create_statement .= "\n".join("\n", map { add_geometry_column($_) if is_geometry($_);} $table->get_fields);
+        my @geometry_columns;
+        foreach my $col ($table->get_fields) { push(@geometry_columns,$col) if is_geometry($col); }
+               $create_statement .= "\n".join("\n", map{ drop_geometry_column($_) } @geometry_columns) if $options->{add_drop_table};
+               $create_statement .= "\n".join("\n", map{ add_geometry_column($_) } @geometry_columns);
        }
 
     return $create_statement, \@fks;
@@ -779,6 +787,13 @@ sub convert_datatype
         $data_type .= '(' . join( ',', @size ) . ')';
     }
 
+    #
+    # Geography
+    #
+    if($data_type eq 'geography'){
+        $data_type .= '('.$field->{extra}{geography_type}.','. $field->{extra}{srid} .')'
+    }
+
     return $data_type;
 }
 
index 3ff3de3..64fb313 100644 (file)
@@ -102,12 +102,28 @@ is($drop_field, qq[ALTER TABLE mytable DROP COLUMN field3
 DELETE FROM geometry_columns WHERE f_table_schema = 'myschema' AND f_table_name = 'mytable' AND f_geometry_column = 'field3'], 'Drop geometry field works');
 
 $table->add_field($field1);
+
+my $field4 = SQL::Translator::Schema::Field->new( name      => 'field4',
+                                                  table     => $table,
+                                                  data_type => 'geography',
+                                                  extra     => {
+                                                                                                     geography_type => 'POINT',
+                                                                                                     srid          => -1
+                                                                                                       },
+                                                  default_value     => undef,
+                                                  is_auto_increment => 0,
+                                                  is_nullable       => 1,
+                                                  is_foreign_key    => 0,
+                                                  is_unique         => 0 );
+$table->add_field($field4);
+
 my ($create_table,$fks) = SQL::Translator::Producer::PostgreSQL::create_table($table);
 is($create_table,qq[--
 -- Table: mytable
 --
 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))
@@ -123,3 +139,4 @@ $table->name("table2");
 my $drop_table = SQL::Translator::Producer::PostgreSQL::drop_table($table);
 is($drop_table, qq[DROP TABLE table2 CASCADE
 DELETE FROM geometry_columns WHERE f_table_schema = 'myschema' AND f_table_name = 'table2' AND f_geometry_column = 'field3'], 'Drop table with geometry works.');
+