diff --git a/README.md b/README.md index ed38bff..5ed529d 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ See https://github.com/CartoDB/cartodb/wiki/CartoDB-PostgreSQL-extension Dependencies ------------ - * PostgreSQL 9.3+ (with plpythonu extension) + * PostgreSQL 9.3+ (with plpythonu extension and xml support) * [PostGIS extension](http://postgis.net) * [Schema triggers extension] (https://bitbucket.org/malloclabs/pg_schema_triggers) diff --git a/scripts-available/CDB_UserTables.sql b/scripts-available/CDB_UserTables.sql index 40e2f51..aa82f37 100644 --- a/scripts-available/CDB_UserTables.sql +++ b/scripts-available/CDB_UserTables.sql @@ -6,34 +6,23 @@ -- Currently accepted permissions are: 'public', 'private' or 'all' -- CREATE OR REPLACE FUNCTION CDB_UserTables(perm text DEFAULT 'all') -RETURNS SETOF information_schema.sql_identifier +RETURNS SETOF name AS $$ - WITH usertables AS ( - -- TODO: query CDB_TableMetadata for this ? - -- See http://github.com/CartoDB/cartodb/issues/254#issuecomment-26044777 - SELECT table_name as t - FROM information_schema.tables - WHERE - table_type='BASE TABLE' - AND table_schema='public' - AND table_name NOT IN ( - 'cdb_tablemetadata', - 'spatial_ref_sys' - ) - ), perms AS ( - SELECT t, has_table_privilege('public', 'public'||'.'||t, 'SELECT') as p - FROM usertables - ) - SELECT t FROM perms - WHERE ( - p = CASE WHEN $1 = 'private' THEN false - WHEN $1 = 'public' THEN true - ELSE not p -- none - END - OR $1 = 'all' - ) - AND has_table_privilege('public'||'.'||t, 'SELECT') - ; + +SELECT c.relname +FROM pg_class c +JOIN pg_roles r ON r.oid = c.relowner +JOIN pg_namespace n ON n.oid = c.relnamespace +WHERE r.rolname = current_user +AND c.relkind = 'r' +AND c.relname NOT IN ('cdb_tablemetadata', 'spatial_ref_sys') +AND n.nspname NOT IN ('pg_catalog', 'information_schema') +AND CASE WHEN perm = 'public' THEN has_table_privilege('public', c.oid, 'SELECT') + WHEN perm = 'private' THEN has_table_privilege(c.oid, 'SELECT') AND NOT + has_table_privilege('public', c.oid, 'SELECT') + WHEN perm = 'all' THEN has_table_privilege(c.oid, 'SELECT') + ELSE false END; + $$ LANGUAGE 'sql'; -- This is to migrate from pre-0.2.0 version diff --git a/test/CDB_QueryTablesTest.sql b/test/CDB_QueryTablesTest.sql index b87315c..695bf70 100644 --- a/test/CDB_QueryTablesTest.sql +++ b/test/CDB_QueryTablesTest.sql @@ -31,6 +31,8 @@ create table sc.test (a int); insert into sc.test values (1); WITH inp AS ( select 'select * from sc.test'::text as q ) SELECT q, CDB_QueryTables(q) from inp; +DROP TABLE sc.test; +DROP SCHEMA sc; WITH inp AS ( select 'SELECT * FROM geometry_columns'::text as q ) diff --git a/test/CDB_QueryTablesTest_expect b/test/CDB_QueryTablesTest_expect index 9959982..94d4138 100644 --- a/test/CDB_QueryTablesTest_expect +++ b/test/CDB_QueryTablesTest_expect @@ -17,5 +17,7 @@ CREATE SCHEMA CREATE TABLE INSERT 0 1 select * from sc.test|{sc.test} +DROP TABLE +DROP SCHEMA SELECT * FROM geometry_columns|{pg_catalog.pg_attribute,pg_catalog.pg_class,pg_catalog.pg_namespace,pg_catalog.pg_type}