]> granicus.if.org Git - postgis/commitdiff
Check typmod on geometry input. Fixes before triggers (#1320)
authorSandro Santilli <strk@keybit.net>
Thu, 15 Dec 2011 23:26:28 +0000 (23:26 +0000)
committerSandro Santilli <strk@keybit.net>
Thu, 15 Dec 2011 23:26:28 +0000 (23:26 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@8431 b70326c6-7e19-0410-871a-916f4a2858ee

postgis/lwgeom_inout.c
regress/tickets.sql
regress/tickets_expected

index 7a232ec321ace785a8332dc41b1f5ec54bb04695..8dae97fcc0d20967068c2cacd4654beac50b3c12 100644 (file)
@@ -46,12 +46,17 @@ PG_FUNCTION_INFO_V1(LWGEOM_in);
 Datum LWGEOM_in(PG_FUNCTION_ARGS)
 {
        char *input = PG_GETARG_CSTRING(0);
+       int32 geom_typmod = -1;
        char *str = input;
        LWGEOM_PARSER_RESULT lwg_parser_result;
        LWGEOM *lwgeom;
        GSERIALIZED *ret;
        int srid = 0;
 
+       if ( (PG_NARGS()>2) && (!PG_ARGISNULL(2)) ) {
+               geom_typmod = PG_GETARG_INT32(2);
+       }
+
        lwgeom_parser_result_init(&lwg_parser_result);
 
        /* Empty string. */
@@ -108,6 +113,17 @@ Datum LWGEOM_in(PG_FUNCTION_ARGS)
                ret = geometry_serialize(lwgeom);
                lwgeom_parser_result_free(&lwg_parser_result);
        }
+
+       if ( geom_typmod >= 0 )
+       {
+               postgis_valid_typmod(lwgeom, geom_typmod);
+               POSTGIS_DEBUG(3, "typmod and geometry were consistent");
+       }
+       else
+       {
+               POSTGIS_DEBUG(3, "typmod was -1");
+       }
+
        
        PG_RETURN_POINTER(ret);
 
index dacaee4aa30e3b30b6293286576dcfbce5f9eced..d74a8c700bef87b4497f7a39387aa676e3ef0817 100644 (file)
@@ -420,6 +420,41 @@ analyze t;
 select '#877.4', st_estimated_extent('t','g');
 drop table t;
 
+-- #1320
+SELECT '<#1320>';
+CREATE TABLE A ( geom geometry(MultiPolygon, 4326),
+                 geog geography(MultiPolygon, 4326) );
+-- Valid inserts
+INSERT INTO a(geog) VALUES('SRID=4326;MULTIPOLYGON (((0 0, 10 0, 10 10, 0 0)))'::geography);
+INSERT INTO a(geom) VALUES('SRID=4326;MULTIPOLYGON (((0 0, 10 0, 10 10, 0 0)))'::geometry);
+SELECT '#1320.geog.1', geometrytype(geog::geometry), st_srid(geog::geometry) FROM a where geog is not null;
+SELECT '#1320.geom.1', geometrytype(geom), st_srid(geom) FROM a where geom is not null;
+-- Type mismatches is not allowed
+INSERT INTO a(geog) VALUES('SRID=4326;POLYGON ((0 0, 10 0, 10 10, 0 0))'::geography);
+INSERT INTO a(geom) VALUES('SRID=4326;POLYGON ((0 0, 10 0, 10 10, 0 0))'::geometry);
+SELECT '#1320.geog.2', geometrytype(geog::geometry), st_srid(geog::geometry) FROM a where geog is not null;
+SELECT '#1320.geom.2', geometrytype(geom), st_srid(geom) FROM a where geom is not null;
+-- Even if it's a trigger changing the type
+CREATE OR REPLACE FUNCTION triga() RETURNS trigger AS
+$$ BEGIN
+       NEW.geom = ST_GeometryN(New.geom,1);
+       NEW.geog = ST_GeometryN(New.geog::geometry,1)::geography;
+       RETURN NEW;
+END; $$ language plpgsql VOLATILE;
+CREATE TRIGGER triga_before
+  BEFORE INSERT ON a FOR EACH ROW
+  EXECUTE PROCEDURE triga();
+INSERT INTO a(geog) VALUES('SRID=4326;MULTIPOLYGON (((0 0, 10 0, 10 10, 0 0)))'::geography);
+INSERT INTO a(geom) VALUES('SRID=4326;MULTIPOLYGON (((0 0, 10 0, 10 10, 0 0)))'::geometry);
+SELECT '#1320.geog.3', geometrytype(geog::geometry), st_srid(geog::geometry) FROM a where geog is not null;
+SELECT '#1320.geom.3', geometrytype(geom), st_srid(geom) FROM a where geom is not null;
+DROP TABLE A;
+DROP FUNCTION triga();
+SELECT '</#1320>';
+
+-- st_AsText POLYGON((0 0,10 0,10 10,0 0))
+
+
 -- Clean up
 DELETE FROM spatial_ref_sys;
 
index f653e5879b4906902568947fe1112aebb7ae9c46..30c8d5d16ea5c7608001459c62c7f382841a27e4 100644 (file)
@@ -128,3 +128,15 @@ WARNING:  No stats for "public"."t"."g" (empty or not analyzed)
 WARNING:  No stats for "<current>"."t"."g" (empty or not analyzed)
 #877.3|
 #877.4|BOX(-10 -50,20 30)
+<#1320>
+#1320.geog.1|MULTIPOLYGON|4326
+#1320.geom.1|MULTIPOLYGON|4326
+ERROR:  Geometry type (Polygon) does not match column type (MultiPolygon)
+ERROR:  Geometry type (Polygon) does not match column type (MultiPolygon)
+#1320.geog.2|MULTIPOLYGON|4326
+#1320.geom.2|MULTIPOLYGON|4326
+ERROR:  Geometry type (Polygon) does not match column type (MultiPolygon)
+ERROR:  Geometry type (Polygon) does not match column type (MultiPolygon)
+#1320.geog.3|MULTIPOLYGON|4326
+#1320.geom.3|MULTIPOLYGON|4326
+</#1320>