From 581835d4ff2fa0e612a6e903e9f48b7b53ccb52b Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Mon, 14 Sep 2015 11:50:10 +0200 Subject: [PATCH] Extract query into _cdb_geom_candidate_columns #141 --- scripts-available/CDB_CartodbfyTable.sql | 45 ++++++++++++++++-------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/scripts-available/CDB_CartodbfyTable.sql b/scripts-available/CDB_CartodbfyTable.sql index ea3dc1e..71c7cad 100644 --- a/scripts-available/CDB_CartodbfyTable.sql +++ b/scripts-available/CDB_CartodbfyTable.sql @@ -683,8 +683,36 @@ BEGIN END; $$ LANGUAGE 'plpgsql'; +-- Return a set of columns that can be candidates to be the_geom +-- with some extra information to filter them out. +CREATE OR REPLACE FUNCTION _cdb_geom_candidate_columns(reloid REGCLASS) +RETURNS TABLE (attname name, srid integer, typname name, desired_attname text, desired_srid integer) +AS $$ +DECLARE + const RECORD; +BEGIN + + const := _CDB_Columns(); + + RETURN QUERY + SELECT + a.attname, + CASE WHEN t.typname = 'geometry' THEN postgis_typmod_srid(a.atttypmod) ELSE NULL END AS srid, + t.typname, + f.desired_attname, f.desired_srid + FROM pg_class c + JOIN pg_attribute a ON a.attrelid = c.oid + JOIN pg_type t ON a.atttypid = t.oid, + (VALUES (const.geomcol, 4326), (const.mercgeomcol, 3857) ) as f(desired_attname, desired_srid) + WHERE c.oid = reloid + AND a.attnum > 0 + AND NOT a.attisdropped + AND postgis_typmod_srid(a.atttypmod) IN (4326, 3857, 0) + ORDER BY t.oid ASC; +END; +$$ LANGUAGE 'plpgsql'; + -DROP FUNCTION IF EXISTS _CDB_Has_Usable_Geom(regclass); CREATE OR REPLACE FUNCTION _CDB_Has_Usable_Geom(reloid REGCLASS) RETURNS RECORD AS $$ @@ -718,20 +746,7 @@ BEGIN -- Do we have a column we can use? FOR r1 IN - SELECT - a.attname, - CASE WHEN t.typname = 'geometry' THEN postgis_typmod_srid(a.atttypmod) ELSE NULL END AS srid, - t.typname, - f.desired_attname, f.desired_srid - FROM pg_class c - JOIN pg_attribute a ON a.attrelid = c.oid - JOIN pg_type t ON a.atttypid = t.oid, - (VALUES (const.geomcol, 4326), (const.mercgeomcol, 3857) ) as f(desired_attname, desired_srid) - WHERE c.oid = reloid - AND a.attnum > 0 - AND NOT a.attisdropped - AND postgis_typmod_srid(a.atttypmod) IN (4326, 3857, 0) - ORDER BY t.oid ASC + SELECT * FROM _cdb_geom_candidate_columns(reloid) LOOP RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %', Format('checking column ''%s''', r1.attname);