Add tables back to DbStructure

This commit is contained in:
calebmer
2015-11-11 10:32:19 -05:00
parent 16af8fc61e
commit 0374d4e651
3 changed files with 26 additions and 3 deletions
+2 -1
View File
@@ -147,7 +147,7 @@ app db conf reqBody req =
-- select * from public.proc(a := "foo"::undefined) where whereT limit limitT
([], _) -> do
body <- encode <$> tables (cs schema)
body <- encode <$> accessibleTables (filter ((== cs schema) . tableSchema) allTabs)
return $ responseLBS status200 [jsonH] $ cs body
([table], "OPTIONS") -> do
@@ -160,6 +160,7 @@ app db conf reqBody req =
return $ responseLBS status404 [] ""
where
allTabs = tables db
allRels = relations db
allCols = columns db
allPrKeys = primaryKeys db
+22 -1
View File
@@ -5,6 +5,7 @@
{-# LANGUAGE TypeSynonymInstances #-}
module PostgREST.DbStructure (
createDbStructure
, accessibleTables
, doesProcExist
, doesProcReturnJWT
) where
@@ -71,6 +72,26 @@ doesProcReturnJWT = doesProc [H.stmt|
AND pg_catalog.pg_get_function_result(p.oid) like '%jwt_claims'
|]
accessibleTables :: [Table] -> H.Tx P.Postgres s [Table]
accessibleTables allTabs = do
accessible <- H.listEx $ [H.stmt|
SELECT
n.nspname AS table_schema,
c.relname AS table_name
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE
c.relkind IN ('v','r','m') AND
n.nspname NOT IN ('pg_catalog', 'information_schema') AND (
pg_has_role(c.relowner, 'USAGE'::text) OR
has_table_privilege(c.oid, 'SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER'::text) OR
has_any_column_privilege(c.oid, 'SELECT, INSERT, UPDATE, REFERENCES'::text)
)
ORDER BY table_schema, table_name
|]
let isAccessible table = isJust $ find (\(s,n) -> tableSchema table == s && tableName table == n) accessible
return $ filter isAccessible allTabs
synonymousColumns :: [(Column,Column)] -> [Column] -> [[Column]]
synonymousColumns allSyns cols = synCols'
where
@@ -159,7 +180,7 @@ allTables = do
WHERE c.relkind IN ('v','r','m')
AND n.nspname NOT IN ('pg_catalog', 'information_schema')
GROUP BY table_schema, table_name, insertable
ORDER BY table_schema, table_name;
ORDER BY table_schema, table_name
|]
return $ map tableFromRow rows
+2 -1
View File
@@ -6,7 +6,8 @@ import Data.Aeson
import Data.Map
data DbStructure = DbStructure {
columns :: [Column]
tables :: [Table]
, columns :: [Column]
, relations :: [Relation]
, primaryKeys :: [PrimaryKey]
} deriving (Show, Eq)