-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- -- -- PostGIS - Spatial Types for PostgreSQL -- http://postgis.net -- -- Copyright (C) 2022 Sandro Santilli -- -- This is free software; you can redistribute and/or modify it under -- the terms of the GNU General Public Licence. See the COPYING file. -- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --{ -- AddTopoGeometryColumn(toponame, schema, table, column, type, [child]) -- -- Add a TopoGeometry column to a table, making it a topology layer. -- Returns created layer id. -- -- Availability: 3.6.0 -- -- Changed: 3.6.0 allows user defined layerid and simplifies schema.tablename to regclass CREATE OR REPLACE FUNCTION topology.AddTopoGeometryColumn(toponame name, tab regclass, col name, layerid integer, layertype varchar, child integer DEFAULT NULL) RETURNS integer AS $$ DECLARE intlayertype integer; newlevel integer; topoid integer; rec RECORD; newlayer_id integer; query text; cnt integer; schema varchar; tbl varchar; sql TEXT; BEGIN IF layerid IS NOT NULL and layerid <= 0 THEN RAISE EXCEPTION 'Invalid Layer ID % (must be > 0)', layerid; END IF; SELECT n.nspname::text, c.relname::text INTO schema, tbl FROM pg_class c JOIN pg_namespace n ON c.relnamespace = n.oid WHERE c.oid = tab; --RAISE NOTICE 'Creating % %.%', tab, schema, tbl; -- Get topology id SELECT id INTO topoid FROM topology.topology WHERE name = toponame; IF NOT FOUND THEN RAISE EXCEPTION 'Topology % does not exist', quote_literal(toponame); END IF; IF layertype ILIKE '%POINT%' OR layertype ILIKE 'PUNTAL' THEN intlayertype = 1; ELSIF layertype ILIKE '%LINE%' OR layertype ILIKE 'LINEAL' THEN intlayertype = 2; ELSIF layertype ILIKE '%POLYGON%' OR layertype ILIKE 'AREAL' THEN intlayertype = 3; ELSIF layertype ILIKE '%COLLECTION%' OR layertype ILIKE 'GEOMETRY' THEN intlayertype = 4; ELSE RAISE EXCEPTION 'Layer type must be one of POINT,LINE,POLYGON,COLLECTION'; END IF; -- -- Add new TopoGeometry column in tab -- EXECUTE 'ALTER TABLE ' || tab::text || ' ADD COLUMN ' || quote_ident(col) || ' 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 AND topology_id = topoid INTO newlevel; IF newlevel IS NULL THEN RAISE EXCEPTION 'Child layer % does not exist in topology "%"', child, toponame; END IF; END IF; -- Get new layer id from sequence -- EXECUTE 'SELECT nextval(' || quote_literal( quote_ident(toponame) || '.layer_id_seq' ) || ')' INTO STRICT newlayer_id; IF layerid IS NOT NULL THEN -- Check if the id is already used SELECT count(*) INTO cnt FROM topology.layer WHERE layer_id = layerid; IF cnt > 0 THEN RAISE EXCEPTION 'Layer ID % is already in use', layerid; END IF; IF layerid > newlayer_id THEN -- set sequence to match layer id EXECUTE 'SELECT setval(' || quote_literal( quote_ident(toponame) || '.layer_id_seq') || ', ' || layerid || ')' INTO STRICT newlayer_id; END IF; newlayer_id := layerid; END IF; sql := 'INSERT INTO ' 'topology.layer(topology_id, ' 'layer_id, level, child_id, schema_name, ' 'table_name, feature_column, feature_type) ' 'VALUES (' || topoid || ',' || newlayer_id || ',' || COALESCE(newlevel, 0) || ',' || COALESCE(child::text, 'NULL') || ',' || quote_literal(schema) || ',' || quote_literal(tbl) || ',' || quote_literal(col) || ',' || intlayertype || ');'; EXECUTE sql; -- -- Create a sequence for TopoGeometries in this new layer -- EXECUTE 'CREATE SEQUENCE ' || quote_ident(toponame) || '.topogeo_s_' || newlayer_id; -- -- Add constraints on TopoGeom column -- EXECUTE 'ALTER TABLE ' || tab::text || ' ADD CONSTRAINT "check_topogeom_' || col || '" CHECK (' 'topology_id(' || quote_ident(col) || ') = ' || topoid || ' AND ' 'layer_id(' || quote_ident(col) || ') = ' || newlayer_id || ' AND ' 'type(' || quote_ident(col) || ') = ' || intlayertype || ');'; -- -- Add dependency of the feature column on the topology schema -- query = 'INSERT INTO pg_catalog.pg_depend SELECT ' 'fcat.oid, fobj.oid, fsub.attnum, tcat.oid, ' 'tobj.oid, 0, ''n'' ' 'FROM pg_class fcat, pg_namespace fnsp, ' ' pg_class fobj, pg_attribute fsub, ' ' pg_class tcat, pg_namespace tobj ' ' WHERE fcat.relname = ''pg_class'' ' ' AND fnsp.nspname = ' || quote_literal(schema) || ' AND fobj.relnamespace = fnsp.oid ' ' AND fobj.relname = ' || quote_literal(tbl) || ' AND fsub.attrelid = fobj.oid ' ' AND fsub.attname = ' || quote_literal(col) || ' AND tcat.relname = ''pg_namespace'' ' ' AND tobj.nspname = ' || quote_literal(toponame); -- -- The only reason to add this dependency is to avoid -- simple drop of a feature column. Still, drop cascade -- will remove both the feature column and the sequence -- corrupting the topology anyway ... -- #if 0 -- -- Add dependency of the topogeom sequence on the feature column -- This is a dirty hack ... -- query = 'INSERT INTO pg_catalog.pg_depend SELECT ' 'scat.oid, sobj.oid, 0, fcat.oid, ' 'fobj.oid, fsub.attnum, ''n'' ' 'FROM pg_class fcat, pg_namespace fnsp, ' ' pg_class fobj, pg_attribute fsub, ' ' pg_class scat, pg_class sobj, ' ' pg_namespace snsp ' ' WHERE fcat.relname = ''pg_class'' ' ' AND fnsp.nspname = ' || quote_literal(schema) || ' AND fobj.relnamespace = fnsp.oid ' ' AND fobj.relname = ' || quote_literal(tbl) || ' AND fsub.attrelid = fobj.oid ' ' AND fsub.attname = ' || quote_literal(col) || ' AND scat.relname = ''pg_class'' ' ' AND snsp.nspname = ' || quote_literal(toponame) || ' AND sobj.relnamespace = snsp.oid ' ' AND sobj.relname = ' ' ''topogeo_s_' || newlayer_id || ''' '; RAISE NOTICE '%', query; EXECUTE query; #endif RETURN newlayer_id; END; $$ LANGUAGE 'plpgsql' VOLATILE; --}{ AddTopoGeometryColumn -- Changed: 3.6.0 allows user defined layer id -- Replaces addtopogeometrycolumn(varchar, varchar, varchar, varchar, varchar, integer) deprecated in 3.6.0 CREATE OR REPLACE FUNCTION topology.AddTopoGeometryColumn(toponame name, schema name, tbl name, col name, ltype varchar, child integer default null) RETURNS integer AS $$ SELECT topology.AddTopoGeometryColumn($1, format('%I.%I', $2, $3)::regclass, $4, null, $5, $6); $$ LANGUAGE 'sql' VOLATILE;