module Database.Relational.Query.Internal.BaseSQL (
Duplication (..), showsDuplication,
Order (..), Nulls (..), OrderColumn, OrderingTerm, composeOrderBy,
AssignColumn, AssignTerm, Assignment, composeSets,
composeChunkValues, composeChunkValuesWithColumns,
) where
import Data.Monoid (Monoid (..), (<>))
import Language.SQL.Keyword (Keyword(..), (|*|), (.=.))
import qualified Language.SQL.Keyword as SQL
import Database.Relational.Query.Internal.SQL
(StringSQL, rowConsStringSQL)
data Duplication = All | Distinct deriving Show
showsDuplication :: Duplication -> StringSQL
showsDuplication = dup where
dup All = ALL
dup Distinct = DISTINCT
data Order = Asc | Desc deriving Show
data Nulls = NullsFirst | NullsLast deriving Show
type OrderColumn = StringSQL
type OrderingTerm = ((Order, Maybe Nulls), OrderColumn)
composeOrderBy :: [OrderingTerm] -> StringSQL
composeOrderBy = d where
d [] = mempty
d ts@(_:_) = ORDER <> BY <> SQL.fold (|*|) (map showsOt ts)
showsOt ((o, mn), e) = e <> order o <> maybe mempty ((NULLS <>) . nulls) mn
order Asc = ASC
order Desc = DESC
nulls NullsFirst = FIRST
nulls NullsLast = LAST
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
-> [StringSQL]
-> 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