WIP: converting DbStructure

This commit is contained in:
Joe Nelson
2016-01-24 18:09:18 -08:00
parent 7b92449343
commit 684b11badb
+29 -11
View File
@@ -10,9 +10,15 @@ module PostgREST.DbStructure (
, doesProcReturnJWT , doesProcReturnJWT
) where ) where
import qualified Hasql.Query as H
import qualified Hasql.Encoders as HE
import qualified Hasql.Decoders as HD
import Control.Applicative import Control.Applicative
import Control.Monad (join) import Control.Monad (join)
import Data.Functor.Contravariant (contramap)
import Data.Functor.Identity import Data.Functor.Identity
import Text.InterpolatedString.Perl6 (q)
import Data.List (elemIndex, find, subsequences, sort, transpose) import Data.List (elemIndex, find, subsequences, sort, transpose)
import Data.Maybe (fromMaybe, fromJust, isJust, mapMaybe, listToMaybe) import Data.Maybe (fromMaybe, fromJust, isJust, mapMaybe, listToMaybe)
import Data.Monoid import Data.Monoid
@@ -42,24 +48,36 @@ getDbStructure schema = do
, dbPrimaryKeys = keys' , dbPrimaryKeys = keys'
} }
doesProc :: forall c s. B.CxValue c Int => encodeQi :: HE.Params QualifiedIdentifier
(Text -> Text -> B.Stmt c) -> QualifiedIdentifier -> H.Tx c s Bool encodeQi =
doesProc stmt qi = do contramap qiSchema (HE.value HE.text) <>
row :: Maybe (Identity Int) <- H.maybeEx $ stmt (qiSchema qi) (qiName qi) contramap qiName (HE.value HE.text)
return $ isJust row
doesProcExist :: QualifiedIdentifier -> H.Session Bool decodeTable :: HD.Result Table
doesProcExist = doesProc [H.stmt| decodeTable =
HD.singleRow standardRow
where
standardRow = Table <$> HD.value HD.text <*> HD.value HD.text
<*> HD.value HD.bool
doesProcExist :: H.Query QualifiedIdentifier Bool
doesProcExist =
H.statement sql encodeQi (HD.singleRow (HD.value HD.bool)) True
where
sql = [q| SELECT EXISTS (
SELECT 1 SELECT 1
FROM pg_catalog.pg_namespace n FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_proc p JOIN pg_catalog.pg_proc p
ON pronamespace = n.oid ON pronamespace = n.oid
WHERE nspname = ? WHERE nspname = ?
AND proname = ? AND proname = ?
|] ) |]
doesProcReturnJWT :: QualifiedIdentifier -> H.Session Bool doesProcReturnJWT :: H.Query QualifiedIdentifier Bool
doesProcReturnJWT = doesProc [H.stmt| doesProcReturnJWT =
H.statement sql encodeQi (HD.singleRow (HD.value HD.bool)) True
where
sql = [q| SELECT EXISTS (
SELECT 1 SELECT 1
FROM pg_catalog.pg_namespace n FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_proc p JOIN pg_catalog.pg_proc p
@@ -67,7 +85,7 @@ doesProcReturnJWT = doesProc [H.stmt|
WHERE nspname = ? WHERE nspname = ?
AND proname = ? AND proname = ?
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 :: Schema -> H.Tx P.Postgres s [Table]
accessibleTables schema = do accessibleTables schema = do