From 7582f2cbc59d01bd975a37bd220d5030327759a4 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Wed, 2 Sep 2015 12:06:04 +0200 Subject: [PATCH] CDB_ColumnType uses schema and table from regclass Fixes #130 --- scripts-available/CDB_ColumnType.sql | 13 +++++++------ test/extension/test.sh | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/scripts-available/CDB_ColumnType.sql b/scripts-available/CDB_ColumnType.sql index f887771..7793c0b 100644 --- a/scripts-available/CDB_ColumnType.sql +++ b/scripts-available/CDB_ColumnType.sql @@ -3,12 +3,13 @@ CREATE OR REPLACE FUNCTION CDB_ColumnType(REGCLASS, TEXT) RETURNS information_schema.character_data AS $$ - SELECT data_type - FROM information_schema.columns - WHERE - table_name IN (SELECT CDB_UserTables()) - AND table_name = '' || $1 || '' - AND column_name = '' || quote_ident($2) || ''; + SELECT c.data_type + FROM information_schema.columns c, pg_class _tn, pg_namespace _sn + WHERE table_name = _tn.relname + AND table_schema = _sn.nspname + AND column_name = $2 + 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 f381232..f9ed2ec 100755 --- a/test/extension/test.sh +++ b/test/extension/test.sh @@ -351,6 +351,20 @@ function test_cdb_column_names() { sql cdb_testmember_2 'DROP TABLE cdb_testmember_2.table_cnames' } +function test_cdb_column_type() { + sql cdb_testmember_1 'CREATE TABLE cdb_testmember_1.table_ctype(c int, a int, r int, t int, o int);' + sql cdb_testmember_2 'CREATE TABLE cdb_testmember_2.table_ctype(c text, a text, r text, t text, o text);' + + sql cdb_testmember_1 "SELECT cartodb.CDB_ColumnType('table_ctype', 'c')" should "integer" + sql cdb_testmember_2 "SELECT cartodb.CDB_ColumnType('table_ctype', 'c')" should "text" + + sql postgres "SELECT cartodb.CDB_ColumnType('cdb_testmember_1.table_ctype', 'c')" should "integer" + sql postgres "SELECT cartodb.CDB_ColumnType('cdb_testmember_2.table_ctype', 'c')" should "text" + + sql cdb_testmember_1 'DROP TABLE cdb_testmember_1.table_ctype' + sql cdb_testmember_2 'DROP TABLE cdb_testmember_2.table_ctype' +} + #################################################### TESTS END HERE #################################################### run_tests $@