getting foreign key data with a column.
This commit is contained in:
+9
-9
@@ -1,8 +1,6 @@
|
|||||||
-- {{{ Imports
|
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-}
|
||||||
|
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
|
|
||||||
module PgStructure where
|
module PgStructure where
|
||||||
|
|
||||||
import Data.Functor ( (<$>) )
|
import Data.Functor ( (<$>) )
|
||||||
@@ -19,8 +17,6 @@ import Database.HDBC.PostgreSQL
|
|||||||
|
|
||||||
import Data.Aeson ((.=))
|
import Data.Aeson ((.=))
|
||||||
|
|
||||||
-- }}}
|
|
||||||
|
|
||||||
data Table = Table {
|
data Table = Table {
|
||||||
tableSchema :: String
|
tableSchema :: String
|
||||||
, tableName :: String
|
, tableName :: String
|
||||||
@@ -69,6 +65,7 @@ data Column = Column {
|
|||||||
, colMaxLen :: Maybe Int
|
, colMaxLen :: Maybe Int
|
||||||
, colPrecision :: Maybe Int
|
, colPrecision :: Maybe Int
|
||||||
, colDefault :: Maybe String
|
, colDefault :: Maybe String
|
||||||
|
, colFK :: Maybe ForeignKey
|
||||||
} deriving (Show)
|
} deriving (Show)
|
||||||
|
|
||||||
instance JSON.ToJSON Column where
|
instance JSON.ToJSON Column where
|
||||||
@@ -120,11 +117,15 @@ columns s t conn = do
|
|||||||
\ from information_schema.columns\
|
\ from information_schema.columns\
|
||||||
\ where table_schema = ?\
|
\ where table_schema = ?\
|
||||||
\ and table_name = ?" [toSql s, toSql t]
|
\ and table_name = ?" [toSql s, toSql t]
|
||||||
return $ mapMaybe mkColumn r
|
fks <- foreignKeys s t conn
|
||||||
|
let lookupFK (_:_:name:_) = Map.lookup (fromSql name) fks
|
||||||
|
lookupFK _ = Nothing
|
||||||
|
let cols = zipWith ($) (map mkColumn r) (map lookupFK r)
|
||||||
|
return cols
|
||||||
|
|
||||||
where
|
where
|
||||||
mkColumn [schema, table, name, pos, nullable, colT, updatable, maxlen, precision, defVal] =
|
--TODO: handle failed pattern match with an appropriate exception
|
||||||
Just $ Column (fromSql schema)
|
mkColumn [schema, table, name, pos, nullable, colT, updatable, maxlen, precision, defVal] = Column (fromSql schema)
|
||||||
(fromSql table)
|
(fromSql table)
|
||||||
(fromSql name)
|
(fromSql name)
|
||||||
(fromSql pos)
|
(fromSql pos)
|
||||||
@@ -134,7 +135,6 @@ columns s t conn = do
|
|||||||
(fromSql maxlen)
|
(fromSql maxlen)
|
||||||
(fromSql precision)
|
(fromSql precision)
|
||||||
(fromSql defVal)
|
(fromSql defVal)
|
||||||
mkColumn _ = Nothing
|
|
||||||
|
|
||||||
printTables :: String -> Connection -> IO BL.ByteString
|
printTables :: String -> Connection -> IO BL.ByteString
|
||||||
printTables schema conn = JSON.encode <$> tables schema conn
|
printTables schema conn = JSON.encode <$> tables schema conn
|
||||||
|
|||||||
@@ -16,12 +16,18 @@ spec = around dbWithSchema $ beforeWith setRole $ do
|
|||||||
map tableName ts `shouldBe` ["auto_incrementing_pk","compound_pk",
|
map tableName ts `shouldBe` ["auto_incrementing_pk","compound_pk",
|
||||||
"has_fk","items","menagerie","no_pk", "simple_pk"]
|
"has_fk","items","menagerie","no_pk", "simple_pk"]
|
||||||
|
|
||||||
describe "columns" $
|
describe "columns" $ do
|
||||||
it "responds with each column for the table" $ \conn -> do
|
it "responds with each column for the table" $ \conn -> do
|
||||||
cs <- columns "1" "auto_incrementing_pk" conn
|
cs <- columns "1" "auto_incrementing_pk" conn
|
||||||
map colName cs `shouldBe` ["id","nullable_string","non_nullable_string",
|
map colName cs `shouldBe` ["id","nullable_string","non_nullable_string",
|
||||||
"inserted_at"]
|
"inserted_at"]
|
||||||
|
|
||||||
|
it "includes foreign key data" $ \conn -> do
|
||||||
|
cs <- columns "1" "has_fk" conn
|
||||||
|
map colFK cs `shouldBe` [Nothing,
|
||||||
|
Just $ ForeignKey "auto_incrementing_pk" "id",
|
||||||
|
Just $ ForeignKey "simple_pk" "k"]
|
||||||
|
|
||||||
describe "foreignKeys" $
|
describe "foreignKeys" $
|
||||||
it "has a description of the foreign key columns" $ \conn ->
|
it "has a description of the foreign key columns" $ \conn ->
|
||||||
foreignKeys "1" "has_fk" conn `shouldReturn` M.fromList [
|
foreignKeys "1" "has_fk" conn `shouldReturn` M.fromList [
|
||||||
|
|||||||
Reference in New Issue
Block a user