DbStructure compiles

This commit is contained in:
Joe Nelson
2016-01-24 18:09:18 -08:00
parent 4b515c5df4
commit abc30d5170
2 changed files with 15 additions and 16 deletions
+11 -13
View File
@@ -15,9 +15,8 @@ import qualified Hasql.Encoders as HE
import qualified Hasql.Decoders as HD import qualified Hasql.Decoders as HD
import Control.Applicative import Control.Applicative
import Control.Monad (join) import Control.Monad (join, replicateM)
import Data.Functor.Contravariant (contramap) import Data.Functor.Contravariant (contramap)
import Data.Functor.Identity
import Text.InterpolatedString.Perl6 (q) 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)
@@ -30,13 +29,13 @@ import GHC.Exts (groupWith)
import GHC.Int (Int32) import GHC.Int (Int32)
import Prelude import Prelude
getDbStructure :: Schema -> H.Query () DbStructure getDbStructure :: Schema -> H.Session DbStructure
getDbStructure schema = do getDbStructure schema = do
tabs <- allTables tabs <- H.query () $ allTables
cols <- allColumns tabs cols <- H.query () $ allColumns tabs
syns <- allSynonyms cols syns <- H.query () $ allSynonyms cols
rels <- allRelations tabs cols rels <- H.query () $ allRelations tabs cols
keys <- allPrimaryKeys tabs keys <- H.query () $ allPrimaryKeys tabs
let rels' = (addManyToManyRelations . raiseRelations schema syns . addParentRelations . addSynonymousRelations syns) rels let rels' = (addManyToManyRelations . raiseRelations schema syns . addParentRelations . addSynonymousRelations syns) rels
cols' = addForeignKeys rels' cols cols' = addForeignKeys rels' cols
@@ -83,10 +82,10 @@ decodeRelations tables cols =
relRow = (,,,,,) relRow = (,,,,,)
<$> HD.value HD.text <$> HD.value HD.text
<*> HD.value HD.text <*> HD.value HD.text
<*> HD.value (HD.array $ HD.arrayValue HD.text) <*> HD.value (HD.array (HD.arrayDimension replicateM (HD.arrayValue HD.text)))
<*> HD.value HD.text <*> HD.value HD.text
<*> HD.value HD.text <*> HD.value HD.text
<*> HD.value (HD.array $ HD.arrayValue HD.text) <*> HD.value (HD.array (HD.arrayDimension replicateM (HD.arrayValue HD.text)))
decodePks :: [Table] -> HD.Result [PrimaryKey] decodePks :: [Table] -> HD.Result [PrimaryKey]
decodePks tables = decodePks tables =
@@ -132,7 +131,7 @@ doesProcReturnJWT =
accessibleTables :: H.Query Schema [Table] accessibleTables :: H.Query Schema [Table]
accessibleTables = accessibleTables =
H.statement sql (HE.value HE.text) (HD.rowsList (HD.value HD.text)) True H.statement sql (HE.value HE.text) decodeTables True
where where
sql = [q| sql = [q|
select select
@@ -155,8 +154,7 @@ accessibleTables =
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 |]
|]
synonymousColumns :: [(Column,Column)] -> [Column] -> [[Column]] synonymousColumns :: [(Column,Column)] -> [Column] -> [[Column]]
synonymousColumns allSyns cols = synCols' synonymousColumns allSyns cols = synCols'
+4 -3
View File
@@ -5,6 +5,7 @@ import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString as BS import qualified Data.ByteString as BS
import qualified Data.Vector as V import qualified Data.Vector as V
import Data.Aeson import Data.Aeson
import GHC.Int (Int32)
data DbStructure = DbStructure { data DbStructure = DbStructure {
dbTables :: [Table] dbTables :: [Table]
@@ -31,12 +32,12 @@ data Column =
Column { Column {
colTable :: Table colTable :: Table
, colName :: Text , colName :: Text
, colPosition :: Int , colPosition :: Int32
, colNullable :: Bool , colNullable :: Bool
, colType :: Text , colType :: Text
, colUpdatable :: Bool , colUpdatable :: Bool
, colMaxLen :: Maybe Int , colMaxLen :: Maybe Int32
, colPrecision :: Maybe Int , colPrecision :: Maybe Int32
, colDefault :: Maybe Text , colDefault :: Maybe Text
, colEnum :: [Text] , colEnum :: [Text]
, colFK :: Maybe ForeignKey , colFK :: Maybe ForeignKey