move base SQL component functions to internal space.

This commit is contained in:
Kei Hibino 2017-02-14 22:43:40 +09:00
parent 5bafef9c98
commit 30d9428f25
2 changed files with 76 additions and 38 deletions

View File

@ -21,10 +21,8 @@ module Database.Relational.Query.Component
module Database.Relational.Query.Internal.Config,
-- * Duplication attribute
-- re-export
Duplication (..),
showsDuplication,
-- deprecated interfaces - import Duplication from internal module
Duplication (..), showsDuplication,
-- * Types for aggregation
-- re-export
@ -45,41 +43,39 @@ module Database.Relational.Query.Component
aggregateKeyProjection, aggregateKeyElement, unsafeAggregateKey,
-- * Types for ordering
-- re-export
Order (..), OrderColumn, OrderingTerm,
Order (..),
composeOrderBy,
-- deprecated interfaces
OrderColumn, OrderingTerm, composeOrderBy,
-- deprecated interfaces
OrderingTerms,
-- * Types for assignments
-- re-export
AssignColumn, AssignTerm, Assignment,
-- deprecated interfaces
AssignColumn, AssignTerm, Assignment, composeSets, composeValues,
-- deprecated interfaces
Assignments,
composeSets, composeValues,
-- * Compose window clause
composeOver,
) where
import Data.Monoid (Monoid (..), (<>))
import Language.SQL.Keyword (Keyword(..), (|*|), (.=.))
import Language.SQL.Keyword (Keyword(..), (|*|))
import qualified Language.SQL.Keyword as SQL
import Database.Relational.Query.Internal.Config
(NameConfig (..),
ProductUnitSupport (..), SchemaNameMode (..), IdentifierQuotation (..),
Config (..), defaultConfig,)
import Database.Relational.Query.Internal.SQL (StringSQL, rowConsStringSQL)
import Database.Relational.Query.Internal.SQL (StringSQL)
import qualified Database.Relational.Query.Internal.SQL as Internal
import Database.Relational.Query.Internal.BaseSQL
(Duplication (..),
Order (..), OrderColumn, OrderingTerm,
AssignColumn, AssignTerm, Assignment,)
(Duplication (..), Order (..),)
import qualified Database.Relational.Query.Internal.BaseSQL as BaseSQL
import Database.Relational.Query.Internal.GroupingSQL
(AggregateColumnRef,
AggregateBitKey (..), AggregateSet (..), AggregateElem (..), AggregateKey (..), )
@ -102,11 +98,10 @@ showsColumnSQL :: ColumnSQL -> StringSQL
showsColumnSQL = Internal.showsColumnSQL
{-# DEPRECATED showsDuplication "prepare to drop public interface. internally use Database.Relational.Query.Internal.BaseSQL.showsDuplication" #-}
-- | Compose duplication attribute string.
showsDuplication :: Duplication -> StringSQL
showsDuplication = dup where
dup All = ALL
dup Distinct = DISTINCT
showsDuplication = BaseSQL.showsDuplication
-- | Single term aggregation element.
@ -185,34 +180,39 @@ unsafeAggregateKey = AggregateKey
-- | Type for order-by terms
type OrderingTerms = [OrderingTerm]
{-# DEPRECATED OrderColumn, OrderingTerm, composeOrderBy "prepare to drop public interface. internally use Database.Relational.Query.Internal.BaseSQL.*" #-}
-- | Type for order-by column
type OrderColumn = BaseSQL.OrderColumn
-- | Type for order-by term
type OrderingTerm = BaseSQL.OrderingTerm
-- | Compose ORDER BY clause from OrderingTerms
composeOrderBy :: [OrderingTerm] -> StringSQL
composeOrderBy = d where
d [] = mempty
d ts@(_:_) = ORDER <> BY <> commaed (map showsOt ts)
showsOt (o, e) = showsColumnSQL e <> order o
order Asc = ASC
order Desc = DESC
composeOrderBy = BaseSQL.composeOrderBy
{-# DEPRECATED Assignments "use [Assignment]." #-}
-- | Assignment pair list.
type Assignments = [Assignment]
{-# DEPRECATED AssignColumn, AssignTerm, Assignment, composeSets, composeValues "prepare to drop public interface. internally use Database.Relational.Query.Internal.BaseSQL.*" #-}
-- | Column SQL String of assignment
type AssignColumn = BaseSQL.AssignColumn
-- | Value SQL String of assignment
type AssignTerm = BaseSQL.AssignTerm
-- | Assignment pair
type Assignment = BaseSQL.Assignment
-- | Compose SET clause from ['Assignment'].
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 <> commaed assignList
composeSets = BaseSQL.composeSets
-- | Compose VALUES clause from ['Assignment'].
composeValues :: [Assignment] -> StringSQL
composeValues as = rowConsStringSQL [ showsColumnSQL c | c <- cs ] <> VALUES <>
rowConsStringSQL [ showsColumnSQL c | c <- vs ] where
(cs, vs) = unzip as
composeValues = BaseSQL.composeValues
-- | Compose /OVER (PARTITION BY ... )/ clause.

View File

@ -9,17 +9,30 @@
--
-- This module provides base structure of SQL syntax tree.
module Database.Relational.Query.Internal.BaseSQL (
Duplication (..),
Order (..), OrderColumn, OrderingTerm,
AssignColumn, AssignTerm, Assignment,
Duplication (..), showsDuplication,
Order (..), OrderColumn, OrderingTerm, composeOrderBy,
AssignColumn, AssignTerm, Assignment, composeSets, composeValues,
) where
import Database.Relational.Query.Internal.SQL (ColumnSQL)
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)
-- | Result record duplication attribute
data Duplication = All | Distinct deriving Show
-- | Compose duplication attribute string.
showsDuplication :: Duplication -> StringSQL
showsDuplication = dup where
dup All = ALL
dup Distinct = DISTINCT
-- | Order direction. Ascendant or Descendant.
data Order = Asc | Desc deriving Show
@ -29,6 +42,16 @@ type OrderColumn = ColumnSQL
-- | Type for order-by term
type OrderingTerm = (Order, OrderColumn)
-- | Compose ORDER BY clause from OrderingTerms
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
-- | Column SQL String of assignment
type AssignColumn = ColumnSQL
@ -37,3 +60,18 @@ type AssignTerm = ColumnSQL
-- | Assignment pair
type Assignment = (AssignColumn, AssignTerm)
-- | Compose SET clause from ['Assignment'].
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
-- | Compose VALUES clause from ['Assignment'].
composeValues :: [Assignment] -> StringSQL
composeValues as = rowConsStringSQL [ showsColumnSQL c | c <- cs ] <> VALUES <>
rowConsStringSQL [ showsColumnSQL c | c <- vs ] where
(cs, vs) = unzip as