Merge pull request #88 from CartoDB/86-CDB_QueryTables-fix-long-names
Add a new function CDB_QueryTablesText #86
This commit is contained in:
@@ -2,12 +2,12 @@
|
|||||||
--
|
--
|
||||||
-- Requires PostgreSQL 9.x+
|
-- Requires PostgreSQL 9.x+
|
||||||
--
|
--
|
||||||
CREATE OR REPLACE FUNCTION CDB_QueryTables(query text)
|
CREATE OR REPLACE FUNCTION CDB_QueryTablesText(query text)
|
||||||
RETURNS name[]
|
RETURNS text[]
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
exp XML;
|
exp XML;
|
||||||
tables NAME[];
|
tables text[];
|
||||||
rec RECORD;
|
rec RECORD;
|
||||||
rec2 RECORD;
|
rec2 RECORD;
|
||||||
BEGIN
|
BEGIN
|
||||||
@@ -41,11 +41,11 @@ BEGIN
|
|||||||
xpath('//x:Relation-Name/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as x,
|
xpath('//x:Relation-Name/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as x,
|
||||||
xpath('//x:Relation-Name/../x:Schema/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as s
|
xpath('//x:Relation-Name/../x:Schema/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as s
|
||||||
)
|
)
|
||||||
SELECT unnest(x)::name as p, unnest(s)::name as sc from inp
|
SELECT unnest(x) as p, unnest(s) as sc from inp
|
||||||
LOOP
|
LOOP
|
||||||
-- RAISE DEBUG 'tab: %', rec2.p;
|
-- RAISE DEBUG 'tab: %', rec2.p;
|
||||||
-- RAISE DEBUG 'sc: %', rec2.sc;
|
-- RAISE DEBUG 'sc: %', rec2.sc;
|
||||||
tables := array_append(tables, (rec2.sc || '.' || rec2.p)::name);
|
tables := array_append(tables, (rec2.sc || '.' || rec2.p));
|
||||||
END LOOP;
|
END LOOP;
|
||||||
|
|
||||||
-- RAISE DEBUG 'Tables: %', tables;
|
-- RAISE DEBUG 'Tables: %', tables;
|
||||||
@@ -65,3 +65,14 @@ BEGIN
|
|||||||
return tables;
|
return tables;
|
||||||
END
|
END
|
||||||
$$ LANGUAGE 'plpgsql' VOLATILE STRICT;
|
$$ LANGUAGE 'plpgsql' VOLATILE STRICT;
|
||||||
|
|
||||||
|
|
||||||
|
-- Keep CDB_QueryTables with same signature for backwards compatibility.
|
||||||
|
-- It should probably be removed in the future.
|
||||||
|
CREATE OR REPLACE FUNCTION CDB_QueryTables(query text)
|
||||||
|
RETURNS name[]
|
||||||
|
AS $$
|
||||||
|
BEGIN
|
||||||
|
RETURN CDB_QueryTablesText(query)::name[];
|
||||||
|
END
|
||||||
|
$$ LANGUAGE 'plpgsql' VOLATILE STRICT;
|
||||||
|
|||||||
@@ -4,10 +4,14 @@ CREATE table "my'tab;le" as select 1|{}
|
|||||||
SELECT a.oid, b.oid FROM pg_class a, pg_class b|{pg_catalog.pg_class}
|
SELECT a.oid, b.oid FROM pg_class a, pg_class b|{pg_catalog.pg_class}
|
||||||
SELECT 1 as col1; select 2 as col2|{}
|
SELECT 1 as col1; select 2 as col2|{}
|
||||||
WARNING: CDB_QueryTables cannot explain query: select 1 from nonexistant (42P01: relation "nonexistant" does not exist)
|
WARNING: CDB_QueryTables cannot explain query: select 1 from nonexistant (42P01: relation "nonexistant" does not exist)
|
||||||
|
CONTEXT: PL/pgSQL function cdb_querytables(text) line 3 at RETURN
|
||||||
ERROR: relation "nonexistant" does not exist
|
ERROR: relation "nonexistant" does not exist
|
||||||
|
CONTEXT: PL/pgSQL function cdb_querytables(text) line 3 at RETURN
|
||||||
begin; select * from pg_class; commit;|{pg_catalog.pg_class}
|
begin; select * from pg_class; commit;|{pg_catalog.pg_class}
|
||||||
WARNING: CDB_QueryTables cannot explain query: select * from test (42P01: relation "test" does not exist)
|
WARNING: CDB_QueryTables cannot explain query: select * from test (42P01: relation "test" does not exist)
|
||||||
|
CONTEXT: PL/pgSQL function cdb_querytables(text) line 3 at RETURN
|
||||||
ERROR: relation "test" does not exist
|
ERROR: relation "test" does not exist
|
||||||
|
CONTEXT: PL/pgSQL function cdb_querytables(text) line 3 at RETURN
|
||||||
WITH a AS (select * from pg_class) select * from a|{pg_catalog.pg_class}
|
WITH a AS (select * from pg_class) select * from a|{pg_catalog.pg_class}
|
||||||
CREATE SCHEMA
|
CREATE SCHEMA
|
||||||
CREATE TABLE
|
CREATE TABLE
|
||||||
|
|||||||
Reference in New Issue
Block a user