module Database.Relational.Query.Internal.BaseSQL (
Duplication (..), showsDuplication,
Order (..), OrderColumn, OrderingTerm, composeOrderBy,
AssignColumn, AssignTerm, Assignment, composeSets, composeValues,
) 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, ColumnSQL, showsColumnSQL)
data Duplication = All | Distinct deriving Show
showsDuplication :: Duplication -> StringSQL
showsDuplication = dup where
dup All = ALL
dup Distinct = DISTINCT
data Order = Asc | Desc deriving Show
type OrderColumn = ColumnSQL
type OrderingTerm = (Order, OrderColumn)
composeOrderBy :: [OrderingTerm] -> StringSQL
composeOrderBy = d where
d [] = mempty
d ts@(_:_) = ORDER <> BY <> SQL.fold (|*|) (map showsOt ts)
showsOt (o, e) = showsColumnSQL e <> order o
order Asc = ASC
order Desc = DESC
type AssignColumn = ColumnSQL
type AssignTerm = ColumnSQL
type Assignment = (AssignColumn, AssignTerm)
composeSets :: [Assignment] -> StringSQL
composeSets as = assigns where
assignList = foldr (\ (col, term) r ->
(showsColumnSQL col .=. showsColumnSQL term) : r)
[] as
assigns | null assignList = error "Update assignment list is null!"
| otherwise = SET <> SQL.fold (|*|) assignList
composeValues :: [Assignment] -> StringSQL
composeValues as = rowConsStringSQL [ showsColumnSQL c | c <- cs ] <> VALUES <>
rowConsStringSQL [ showsColumnSQL c | c <- vs ] where
(cs, vs) = unzip as