From 4b5c5dd275ed36b912adb275ed632eb62e747777 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Wed, 2 Sep 2015 12:04:52 +0200 Subject: [PATCH] CDB_ColumnNames uses schema and table name from regclass Fixes #122 --- scripts-available/CDB_ColumnNames.sql | 11 ++++++----- test/extension/test.sh | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/scripts-available/CDB_ColumnNames.sql b/scripts-available/CDB_ColumnNames.sql index 693ba3d..51e0244 100644 --- a/scripts-available/CDB_ColumnNames.sql +++ b/scripts-available/CDB_ColumnNames.sql @@ -3,11 +3,12 @@ CREATE OR REPLACE FUNCTION CDB_ColumnNames(REGCLASS) RETURNS SETOF information_schema.sql_identifier AS $$ - SELECT column_name - FROM information_schema.columns - WHERE - table_name IN (SELECT CDB_UserTables()) - AND table_name = '' || $1 || ''; + SELECT c.column_name + FROM information_schema.columns c, pg_class _tn, pg_namespace _sn + WHERE table_name = _tn.relname + AND table_schema = _sn.nspname + AND _tn.oid = $1::regclass::oid + AND _sn.oid = _tn.relnamespace; $$ LANGUAGE SQL; diff --git a/test/extension/test.sh b/test/extension/test.sh index ff9de57..f381232 100755 --- a/test/extension/test.sh +++ b/test/extension/test.sh @@ -337,6 +337,20 @@ function test_cdb_tablemetadatatouch_fails_from_user_without_permission() { sql postgres "REVOKE ALL ON CDB_TableMetadata FROM cdb_testmember_1;" } +function test_cdb_column_names() { + sql cdb_testmember_1 'CREATE TABLE cdb_testmember_1.table_cnames(c int, a int, r int, t int, o int);' + sql cdb_testmember_2 'CREATE TABLE cdb_testmember_2.table_cnames(d int, b int);' + + sql cdb_testmember_1 "SELECT string_agg(c,'') from (SELECT cartodb.CDB_ColumnNames('table_cnames') c) as s" should "carto" + sql cdb_testmember_2 "SELECT string_agg(c,'') from (SELECT cartodb.CDB_ColumnNames('table_cnames') c) as s" should "db" + + sql postgres "SELECT string_agg(c,'') from (SELECT cartodb.CDB_ColumnNames('cdb_testmember_1.table_cnames'::regclass) c) as s" should "carto" + sql postgres "SELECT string_agg(c,'') from (SELECT cartodb.CDB_ColumnNames('cdb_testmember_2.table_cnames') c) as s" should "db" + + sql cdb_testmember_1 'DROP TABLE cdb_testmember_1.table_cnames' + sql cdb_testmember_2 'DROP TABLE cdb_testmember_2.table_cnames' +} + #################################################### TESTS END HERE #################################################### run_tests $@