WIP: converting dbstructure

This commit is contained in:
Joe Nelson
2016-01-24 18:09:18 -08:00
parent 684b11badb
commit 2d5210464a
+17 -17
View File
@@ -53,11 +53,11 @@ encodeQi =
contramap qiSchema (HE.value HE.text) <> contramap qiSchema (HE.value HE.text) <>
contramap qiName (HE.value HE.text) contramap qiName (HE.value HE.text)
decodeTable :: HD.Result Table decodeTables :: HD.Result [Table]
decodeTable = decodeTables =
HD.singleRow standardRow HD.rowsList tblRow
where where
standardRow = Table <$> HD.value HD.text <*> HD.value HD.text tblRow = Table <$> HD.value HD.text <*> HD.value HD.text
<*> HD.value HD.bool <*> HD.value HD.bool
doesProcExist :: H.Query QualifiedIdentifier Bool doesProcExist :: H.Query QualifiedIdentifier Bool
@@ -87,10 +87,11 @@ doesProcReturnJWT =
AND pg_catalog.pg_get_function_result(p.oid) like '%jwt_claims' AND pg_catalog.pg_get_function_result(p.oid) like '%jwt_claims'
) |] ) |]
accessibleTables :: Schema -> H.Tx P.Postgres s [Table] accessibleTables :: H.Query Schema [Table]
accessibleTables schema = do accessibleTables =
rows <- H.listEx $ H.statement sql (HE.value HE.text) (HD.rowsList (HD.value HD.text)) True
[H.stmt| where
sql = [q|
select select
n.nspname as table_schema, n.nspname as table_schema,
relname as table_name, relname as table_name,
@@ -105,15 +106,14 @@ accessibleTables schema = do
join pg_namespace n on n.oid = c.relnamespace join pg_namespace n on n.oid = c.relnamespace
where where
c.relkind in ('v', 'r', 'm') c.relkind in ('v', 'r', 'm')
and n.nspname = ? and n.nspname = $1
and ( and (
pg_has_role(c.relowner, 'USAGE'::text) pg_has_role(c.relowner, 'USAGE'::text)
or has_table_privilege(c.oid, 'SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER'::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) or has_any_column_privilege(c.oid, 'SELECT, INSERT, UPDATE, REFERENCES'::text)
) )
order by relname order by relname
|] schema |]
return $ map tableFromRow rows
synonymousColumns :: [(Column,Column)] -> [Column] -> [[Column]] synonymousColumns :: [(Column,Column)] -> [Column] -> [[Column]]
synonymousColumns allSyns cols = synCols' synonymousColumns allSyns cols = synCols'
@@ -187,9 +187,11 @@ synonymousPrimaryKeys syns (key:keys) = key : newKeys ++ synonymousPrimaryKeys s
keySyns = filter ((\c -> colTable c == pkTable key && colName c == pkName key) . fst) syns keySyns = filter ((\c -> colTable c == pkTable key && colName c == pkName key) . fst) syns
newKeys = map ((\c -> PrimaryKey{pkTable=colTable c,pkName=colName c}) . snd) keySyns newKeys = map ((\c -> PrimaryKey{pkTable=colTable c,pkName=colName c}) . snd) keySyns
allTables :: H.Session [Table] allTables :: H.Query () [Table]
allTables = do allTables =
rows <- H.listEx $ [H.stmt| H.statement sql HE.unit decodeTables True
where
sql = [q|
SELECT SELECT
n.nspname AS table_schema, n.nspname AS table_schema,
c.relname AS table_name, c.relname AS table_name,
@@ -205,9 +207,7 @@ allTables = do
WHERE c.relkind IN ('v','r','m') WHERE c.relkind IN ('v','r','m')
AND n.nspname NOT IN ('pg_catalog', 'information_schema') AND n.nspname NOT IN ('pg_catalog', 'information_schema')
GROUP BY table_schema, table_name, insertable GROUP BY table_schema, table_name, insertable
ORDER BY table_schema, table_name ORDER BY table_schema, table_name |]
|]
return $ map tableFromRow rows
tableFromRow :: (Text, Text, Bool) -> Table tableFromRow :: (Text, Text, Bool) -> Table
tableFromRow (s, n, i) = Table s n i tableFromRow (s, n, i) = Table s n i