2014-05-05 23:13:06 +08:00
|
|
|
-- Function returning list of cartodb user tables
|
|
|
|
--
|
|
|
|
-- The optional argument restricts the result to tables
|
|
|
|
-- of the specified access type.
|
|
|
|
--
|
|
|
|
-- Currently accepted permissions are: 'public', 'private' or 'all'
|
|
|
|
--
|
|
|
|
CREATE OR REPLACE FUNCTION CDB_UserTables(perm text DEFAULT 'all')
|
|
|
|
RETURNS SETOF information_schema.sql_identifier
|
|
|
|
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 (
|
2014-05-08 22:42:38 +08:00
|
|
|
SELECT t, has_table_privilege('public', 'public'||'.'||t, 'SELECT') as p
|
2014-05-05 23:13:06 +08:00
|
|
|
FROM usertables
|
|
|
|
)
|
|
|
|
SELECT t FROM perms
|
2014-06-06 17:02:51 +08:00
|
|
|
WHERE (
|
|
|
|
p = CASE WHEN $1 = 'private' THEN false
|
2014-05-05 23:13:06 +08:00
|
|
|
WHEN $1 = 'public' THEN true
|
|
|
|
ELSE not p -- none
|
|
|
|
END
|
|
|
|
OR $1 = 'all'
|
2014-06-06 17:02:51 +08:00
|
|
|
)
|
|
|
|
AND has_table_privilege('public'||'.'||t, 'SELECT')
|
2014-05-05 23:13:06 +08:00
|
|
|
;
|
|
|
|
$$ LANGUAGE 'sql';
|
|
|
|
|
2014-06-06 17:33:34 +08:00
|
|
|
-- This is to migrate from pre-0.2.0 version
|
|
|
|
-- See http://github.com/CartoDB/cartodb-postgresql/issues/36
|
|
|
|
GRANT EXECUTE ON FUNCTION CDB_UserTables(text) TO public;
|