From c2d7b9aa3723db4cdad1c0316e7b85c136543b63 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Sun, 22 Jan 2012 19:25:24 +0000 Subject: [PATCH] AddTopoGeometryColumn: check child layer before incrementing sequence git-svn-id: http://svn.osgeo.org/postgis/trunk@8902 b70326c6-7e19-0410-871a-916f4a2858ee --- .../test/regress/addtopogeometrycolumn.sql | 1 + .../regress/addtopogeometrycolumn_expected | 1 + topology/topology.sql.in.c | 59 ++++++++----------- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/topology/test/regress/addtopogeometrycolumn.sql b/topology/test/regress/addtopogeometrycolumn.sql index c1606ace8..745f5d981 100644 --- a/topology/test/regress/addtopogeometrycolumn.sql +++ b/topology/test/regress/addtopogeometrycolumn.sql @@ -5,6 +5,7 @@ select createtopology('tt') > 0; select addtopogeometrycolumn('tt','public','feature','tg','POINT'); -- fail create table feature(id integer); select addtopogeometrycolumn('tt','public','feature','tg','BOGUS'); -- fail +select addtopogeometrycolumn('tt','public','feature','tg','POINT', 0); -- fail -- Expect first good call returning 1 select 'good', addtopogeometrycolumn('tt','public','feature','tg','POINT'); diff --git a/topology/test/regress/addtopogeometrycolumn_expected b/topology/test/regress/addtopogeometrycolumn_expected index 9f67abeee..e20690d11 100644 --- a/topology/test/regress/addtopogeometrycolumn_expected +++ b/topology/test/regress/addtopogeometrycolumn_expected @@ -1,6 +1,7 @@ t ERROR: relation "public.feature" does not exist ERROR: Layer type must be one of POINT,LINE,POLYGON,COLLECTION +ERROR: Child layer 0 does not exist in topology "tt" good|1 1|public|feature|tg|1|0| Topology 'tt' dropped diff --git a/topology/topology.sql.in.c b/topology/topology.sql.in.c index ea4256f91..aadb91509 100644 --- a/topology/topology.sql.in.c +++ b/topology/topology.sql.in.c @@ -581,6 +581,18 @@ BEGIN || ' topology.TopoGeometry;'; + -- + -- See if child id exists and extract its level + -- + IF child IS NOT NULL THEN + SELECT level + 1 FROM topology.layer + WHERE layer_id = child + INTO level; + IF level IS NULL THEN + RAISE EXCEPTION 'Child layer % does not exist in topology "%"', child, toponame; + END IF; + END IF; + -- -- Get new layer id from sequence -- @@ -589,41 +601,18 @@ BEGIN quote_ident(toponame) || '.layer_id_seq' ) || ')' INTO STRICT layer_id; - -- - -- See if child id exists and extract its level - -- - IF child IS NULL THEN - EXECUTE 'INSERT INTO ' - || 'topology.layer(topology_id, ' - || 'layer_id, schema_name, ' - || 'table_name, feature_column, feature_type) ' - || 'VALUES (' - || topoid || ',' - || layer_id || ',' - || quote_literal(schema) || ',' - || quote_literal(tbl) || ',' - || quote_literal(col) || ',' - || intltype || ');'; - ELSE - FOR rec IN EXECUTE 'SELECT level FROM topology.layer' - || ' WHERE layer_id = ' || child - LOOP - level = rec.level + 1; - END LOOP; - - EXECUTE 'INSERT INTO ' - || 'topology.layer(topology_id, ' - || 'layer_id, level, child_id, schema_name, ' - || 'table_name, feature_column, feature_type) ' - || 'VALUES (' - || topoid || ',' - || layer_id || ',' || level || ',' - || child || ',' - || quote_literal(schema) || ',' - || quote_literal(tbl) || ',' - || quote_literal(col) || ',' - || intltype || ');'; - END IF; + EXECUTE 'INSERT INTO ' + || 'topology.layer(topology_id, ' + || 'layer_id, level, child_id, schema_name, ' + || 'table_name, feature_column, feature_type) ' + || 'VALUES (' + || topoid || ',' + || layer_id || ',' || COALESCE(level, 0) || ',' + || COALESCE(child::text, 'NULL') || ',' + || quote_literal(schema) || ',' + || quote_literal(tbl) || ',' + || quote_literal(col) || ',' + || intltype || ');'; -- -- 2.50.1