{-# LANGUAGE OverloadedStrings #-}
module Quokka.Functions (
build
, build1
, buildWith1Rel
, build1With1Rel
, buildWithManyRels
, build1WithManyRels
, delete
, deleteStatement
, id'
, insertStatement
, insertStatementWith1Rel
, insertStatementWithManyRels
, mapFromIdToResult
) where
import Data.Int (Int64)
import Data.Text (intercalate)
import Data.Text.Encoding (encodeUtf8)
import Database.PostgreSQL.Simple (Connection, ToRow, execute_, returning, query)
import Database.PostgreSQL.Simple.Types (Query (Query))
import Quokka.Types (ChildTable (ChildTable)
, Id (getId)
, ParentTable (ParentTable)
, Table (Table)
, Result (SingleResult))
import Quokka.Text.Countable (singularize)
build
:: (ToRow q)
=> Connection
-> ParentTable
-> [q]
-> IO [Id]
build conn tbl =
let
qry = insertStatement tbl
in
returning conn qry
build1
:: (ToRow q)
=> Connection
-> ParentTable
-> q
-> IO (Maybe Id)
build1 conn tbl =
let
qry = insertStatement tbl
in
fmap build1Helper . query conn qry
buildWith1Rel
:: (ToRow q)
=> Connection
-> ParentTable
-> ChildTable
-> [q]
-> IO [Id]
buildWith1Rel conn parent child =
let
qry = insertStatementWith1Rel parent child
in
returning conn qry
build1With1Rel
:: (ToRow q)
=> Connection
-> ParentTable
-> ChildTable
-> q
-> IO (Maybe Id)
build1With1Rel conn parent child =
let
qry = insertStatementWith1Rel parent child
in
fmap build1Helper . query conn qry
buildWithManyRels
:: (ToRow q)
=> Connection
-> [ParentTable]
-> ChildTable
-> [q]
-> IO [Id]
buildWithManyRels conn parents child =
let
qry = insertStatementWithManyRels parents child
in
returning conn qry
build1WithManyRels
:: (ToRow q)
=> Connection
-> [ParentTable]
-> ChildTable
-> q
-> IO (Maybe Id)
build1WithManyRels conn parents child =
let
qry = insertStatementWithManyRels parents child
in
fmap build1Helper . query conn qry
delete
:: Connection
-> Table
-> IO Int64
delete conn tbl = do
let
alter = alterSequenceStatement tbl
qry = deleteStatement tbl
_ <- execute_ conn alter
execute_ conn qry
insertStatement
:: ParentTable
-> Query
insertStatement (ParentTable name columns) =
let
columnsAsText = intercalate "," columns
valuesAsText = intercalate "," (map (const "?") columns)
baseInsert = "insert into " <> name <> " (" <> columnsAsText <> ")"
in
Query (encodeUtf8 $ baseInsert <> " values (" <> valuesAsText <> ") returning id;")
insertStatementWith1Rel
:: ParentTable
-> ChildTable
-> Query
insertStatementWith1Rel parent =
insertStatementWithManyRels [parent]
insertStatementWithManyRels
:: [ParentTable]
-> ChildTable
-> Query
insertStatementWithManyRels parents (ChildTable name columns) =
let
updatedColumns = columns ++ map (\(ParentTable parentName _) -> singularize parentName <> "_id") parents
columnsAsText = intercalate "," updatedColumns
valuesAsText = intercalate "," (map (const "?") updatedColumns)
baseInsert = "insert into " <> name <> " (" <> columnsAsText <> ")"
in
Query (encodeUtf8 $ baseInsert <> " values (" <> valuesAsText <> ") returning id;")
alterSequenceStatement
:: Table
-> Query
alterSequenceStatement (Table name) =
Query (encodeUtf8 $ "alter sequence " <> name <> "_id_seq restart;")
deleteStatement
:: Table
-> Query
deleteStatement (Table name) =
Query (encodeUtf8 $ "truncate table " <> name <> " cascade;")
id' :: [Id] -> Int
id' (x:_) =
getId x
id' [] =
-1
build1Helper
:: [Id]
-> Maybe Id
build1Helper (x:_) =
Just x
build1Helper [] =
Nothing
mapFromIdToResult
:: ParentTable
-> [Id]
-> [Result]
mapFromIdToResult tbl =
map (SingleResult tbl)