Fix Rambo's test case, of a single geometry-only table

with no SRID in the metadata (thanks mate).
master
Paul Ramsey 10 years ago
parent f3c20ac2fb
commit 14414c4bf3

@ -990,39 +990,25 @@ BEGIN
-- Start building the SQL! -- Start building the SQL!
sql := 'CREATE TABLE ' || copyname || ' AS SELECT '; sql := 'CREATE TABLE ' || copyname || ' AS SELECT ';
-- Add cartodb ID! -- Add cartodb ID!
IF has_usable_primary_key THEN IF has_usable_primary_key THEN
sql := sql || primary_key_name; sql := sql || primary_key_name;
ELSE ELSE
sql := sql || 'nextval(''' || destseq || ''') AS ' || primary_key_name; sql := sql || 'nextval(''' || destseq || ''') AS ' || primary_key_name;
END IF; END IF;
-- Add the geometry columns! -- Add the geometry columns!
IF has_usable_geoms THEN IF has_usable_geoms THEN
sql := sql || ',' || geom_name || ',' || mercgeom_name; sql := sql || ',' || geom_name || ',' || mercgeom_name;
ELSE ELSE
-- The geometry columns weren't in the right projection, -- This gets complicated: we have to make sure the
-- so we need to find the first decent geometry column -- geometry column we are using can be transformed into
-- in the table and wrap it in two transforms, one to 4326 -- geographics, which means it needs to have a valid
-- and another to 3857. Then remember its name so we can -- SRID. And the geometry objects can have an
-- ignore it when we build the list of other columns to SELECT a.attname
-- add to the output table INTO rec
SELECT ',ST_Transform('
|| a.attname
|| ',4326)::Geometry('
|| postgis_typmod_type(a.atttypmod)
|| ', 4326) AS '
|| geom_name
|| ', ST_Transform('
|| a.attname
|| ',3857)::Geometry('
|| postgis_typmod_type(a.atttypmod)
|| ', 3857) AS '
|| mercgeom_name,
a.attname
INTO geom_transform_sql, geom_column_source
FROM pg_class c FROM pg_class c
JOIN pg_attribute a ON a.attrelid = c.oid JOIN pg_attribute a ON a.attrelid = c.oid
JOIN pg_type t ON a.atttypid = t.oid JOIN pg_type t ON a.atttypid = t.oid
@ -1030,28 +1016,68 @@ BEGIN
AND t.typname = 'geometry' AND t.typname = 'geometry'
AND a.attnum > 0 AND a.attnum > 0
AND NOT a.attisdropped AND NOT a.attisdropped
AND postgis_typmod_srid(a.atttypmod) > 0
ORDER BY a.attnum ORDER BY a.attnum
LIMIT 1; LIMIT 1;
-- If there is no geometry column, we continue making a IF NOT FOUND THEN
-- non-spatial table. This is important for folks who want -- If there is no geometry column, we continue making a
-- their tables to invalidate the SQL API -- non-spatial table. This is important for folks who want
-- cache on update/insert/delete. -- their tables to invalidate the SQL API
IF FOUND THEN -- cache on update/insert/delete.
sql := sql || geom_transform_sql;
ELSE
geom_column_source := ''; geom_column_source := '';
ELSE
EXECUTE Format('SELECT ST_SRID(%s) AS srid FROM %s LIMIT 1', rec.attname, reloid::text)
INTO rec;
-- The geometry columns weren't in the right projection,
-- so we need to find the first decent geometry column
-- in the table and wrap it in two transforms, one to 4326
-- and another to 3857. Then remember its name so we can
-- ignore it when we build the list of other columns to
-- add to the output table
SELECT ',ST_Transform('
|| a.attname
|| ',4326)::Geometry('
|| postgis_typmod_type(a.atttypmod)
|| ', 4326) AS '
|| geom_name
|| ', ST_Transform('
|| a.attname
|| ',3857)::Geometry('
|| postgis_typmod_type(a.atttypmod)
|| ', 3857) AS '
|| mercgeom_name,
a.attname
INTO geom_transform_sql, geom_column_source
FROM pg_class c
JOIN pg_attribute a ON a.attrelid = c.oid
JOIN pg_type t ON a.atttypid = t.oid,
( SELECT rec.srid AS srid ) AS srid
WHERE c.oid = reloid
AND t.typname = 'geometry'
AND a.attnum > 0
AND NOT a.attisdropped
AND (postgis_typmod_srid(a.atttypmod) > 0 OR srid.srid > 0)
ORDER BY a.attnum
LIMIT 1;
IF FOUND THEN
sql := sql || geom_transform_sql;
END IF;
END IF; END IF;
END IF; END IF;
-- Add now add all the rest of the columns -- Add now add all the rest of the columns
-- by selecting their names into an array and -- by selecting their names into an array and
-- joining the array with a comma -- joining the array with a comma
SELECT SELECT
',' || array_to_string(array_agg(a.attname),',') ',' || array_to_string(array_agg(a.attname),',') AS column_name_sql,
INTO column_name_sql Count(*) AS count
INTO rec
FROM pg_class c FROM pg_class c
JOIN pg_attribute a ON a.attrelid = c.oid JOIN pg_attribute a ON a.attrelid = c.oid
JOIN pg_type t ON a.atttypid = t.oid JOIN pg_type t ON a.atttypid = t.oid
@ -1060,14 +1086,17 @@ BEGIN
AND a.attname NOT IN (geom_name, mercgeom_name, primary_key_name, geom_column_source) AND a.attname NOT IN (geom_name, mercgeom_name, primary_key_name, geom_column_source)
AND NOT a.attisdropped; AND NOT a.attisdropped;
-- No non-cartodb columns? Possible, I guess. -- No non-cartodb columns? Possible, I guess.
IF NOT FOUND THEN IF rec.count = 0 THEN
column_name_sql := ''; column_name_sql := '';
ELSE
column_name_sql := rec.column_name_sql;
END IF; END IF;
-- Add the source table to the SQL -- Add the source table to the SQL
sql := sql || column_name_sql || ' FROM ' || reloid::text; sql := sql || column_name_sql || ' FROM ' || reloid::text;
RAISE DEBUG '_CDB_Rewrite_Table: %', sql; RAISE DEBUG '_CDB_Rewrite_Table generated SQL: %', sql;
-- Run it! -- Run it!
EXECUTE sql; EXECUTE sql;

Loading…
Cancel
Save