module Database.Relational.SqlSyntax.Updates (
AssignColumn, AssignTerm, Assignment,
composeSets,
composeChunkValues,
composeChunkValuesWithColumns,
composeValuesListWithColumns,
) where
import Data.Monoid ((<>))
import Language.SQL.Keyword (Keyword(..), (|*|), (.=.))
import qualified Language.SQL.Keyword as SQL
import Database.Relational.Internal.String (StringSQL, rowConsStringSQL)
type AssignColumn = StringSQL
type AssignTerm = StringSQL
type Assignment = (AssignColumn, AssignTerm)
composeSets :: [Assignment] -> StringSQL
composeSets as = assigns where
assignList = foldr (\ (col, term) r ->
(col .=. term) : r)
[] as
assigns | null assignList = error "Update assignment list is null!"
| otherwise = SET <> SQL.fold (|*|) assignList
composeChunkValues :: Int
-> [AssignTerm]
-> Keyword
composeChunkValues n0 vs =
VALUES <> cvs
where
n | n0 >= 1 = n0
| otherwise = error $ "Invalid record count value: " ++ show n0
cvs = SQL.fold (|*|) . replicate n $ rowConsStringSQL vs
composeChunkValuesWithColumns :: Int
-> [Assignment]
-> StringSQL
composeChunkValuesWithColumns sz as =
rowConsStringSQL cs <> composeChunkValues sz vs
where
(cs, vs) = unzip as
composeValuesListWithColumns :: [[Assignment]]
-> StringSQL
composeValuesListWithColumns pss =
rowConsStringSQL cs <> VALUES <> SQL.fold (|*|) (map rowConsStringSQL vss)
where
cs = case pss of
[] -> error "insertValueList: no assignment chunks"
ps:_ -> fst $ unzip ps
vss = map (snd . unzip) pss