Add definitions to specify duplication attribute.

This commit is contained in:
Kei Hibino 2013-12-25 01:11:33 +09:00
parent 79873fbb53
commit c65dc76373
8 changed files with 34 additions and 22 deletions

View File

@ -197,7 +197,8 @@ userGroupStr :: Relation () (Maybe String)
userGroupStr = userGroupStr =
relation $ relation $
[ u ?!? User.name' ?||? just (value " - ") ?||? g ?!? Group.name' [ u ?!? User.name' ?||? just (value " - ") ?||? g ?!? Group.name'
| ug <- query userGroup2 | () <- distinct
, ug <- query userGroup2
, let u = ug ! fst' , let u = ug ! fst'
g = ug ! snd' g = ug ! snd'
] ]

View File

@ -53,7 +53,7 @@ import Database.Relational.Query.Projection (Projection, list)
import Database.Relational.Query.Projectable import Database.Relational.Query.Projectable
import Database.Relational.Query.ProjectableExtended import Database.Relational.Query.ProjectableExtended
import Database.Relational.Query.Monad.Class import Database.Relational.Query.Monad.Class
(on, wheres, groupBy, having, onE, wheresE, havingE) (distinct, all', on, wheres, groupBy, having, onE, wheresE, havingE)
import Database.Relational.Query.Monad.Trans.Aggregating import Database.Relational.Query.Monad.Trans.Aggregating
(groupBy', key, key', set, bkey, rollup, cube, groupingSets) (groupBy', key, key', set, bkey, rollup, cube, groupingSets)
import Database.Relational.Query.Monad.Trans.Ordering (orderBy, asc, desc) import Database.Relational.Query.Monad.Trans.Ordering (orderBy, asc, desc)

View File

@ -16,6 +16,7 @@ module Database.Relational.Query.Monad.Class (
MonadQualify (..), MonadRestrict (..), MonadQualify (..), MonadRestrict (..),
MonadQuery (..), MonadAggregate (..), MonadPartition (..), MonadQuery (..), MonadAggregate (..), MonadPartition (..),
distinct, all',
onE, on, wheresE, wheres, onE, on, wheresE, wheres,
groupBy, groupBy,
havingE, having havingE, having
@ -23,7 +24,8 @@ module Database.Relational.Query.Monad.Class (
import Database.Relational.Query.Context (Flat, Aggregated) import Database.Relational.Query.Context (Flat, Aggregated)
import Database.Relational.Query.Expr (Expr) import Database.Relational.Query.Expr (Expr)
import Database.Relational.Query.Component (AggregateElem, AggregateColumnRef, aggregateColumnRef) import Database.Relational.Query.Component
(Duplication (Distinct, All), AggregateElem, AggregateColumnRef, aggregateColumnRef)
import Database.Relational.Query.Projection (Projection) import Database.Relational.Query.Projection (Projection)
import qualified Database.Relational.Query.Projection as Projection import qualified Database.Relational.Query.Projection as Projection
import Database.Relational.Query.Projectable (expr) import Database.Relational.Query.Projectable (expr)
@ -39,6 +41,8 @@ class (Functor m, Monad m) => MonadRestrict c m where
-- | Query building interface. -- | Query building interface.
class (Functor m, Monad m) => MonadQuery m where class (Functor m, Monad m) => MonadQuery m where
-- | Specify duplication attribute.
specifyDuplication :: Duplication -> m ()
-- | Add restriction to last join. -- | Add restriction to last join.
restrictJoin :: Expr Flat (Maybe Bool) -- ^ 'Expr' 'Projection' which represent restriction restrictJoin :: Expr Flat (Maybe Bool) -- ^ 'Expr' 'Projection' which represent restriction
-> m () -- ^ Restricted query context -> m () -- ^ Restricted query context
@ -64,6 +68,14 @@ class Monad m => MonadPartition m where
unsafeAddPartitionKey :: AggregateColumnRef -- ^ Partitioning key to add into partition by clause unsafeAddPartitionKey :: AggregateColumnRef -- ^ Partitioning key to add into partition by clause
-> m () -- ^ Result context -> m () -- ^ Result context
-- | Specify distinct attribute to query context.
distinct :: MonadQuery m => m ()
distinct = specifyDuplication Distinct
-- | Specify all attribute to query context.
all' :: MonadQuery m => m ()
all' = specifyDuplication All
-- | Add restriction to last join. -- | Add restriction to last join.
onE :: MonadQuery m => Expr Flat (Maybe Bool) -> m () onE :: MonadQuery m => Expr Flat (Maybe Bool) -> m ()
onE = restrictJoin onE = restrictJoin

View File

@ -90,8 +90,9 @@ instance MonadRestrict c m => MonadRestrict c (AggregatingSetT m) where
-- | Aggregated 'MonadQuery'. -- | Aggregated 'MonadQuery'.
instance MonadQuery m => MonadQuery (AggregatingSetT m) where instance MonadQuery m => MonadQuery (AggregatingSetT m) where
restrictJoin = aggregatings . restrictJoin specifyDuplication = aggregatings . specifyDuplication
unsafeSubQuery na = aggregatings . unsafeSubQuery na restrictJoin = aggregatings . restrictJoin
unsafeSubQuery na = aggregatings . unsafeSubQuery na
-- | Unsafely update aggregating context. -- | Unsafely update aggregating context.
updateAggregatingContext :: Monad m => (TermsContext at -> TermsContext at) -> Aggregatings ac at m () updateAggregatingContext :: Monad m => (TermsContext at -> TermsContext at) -> Aggregatings ac at m ()

View File

@ -26,7 +26,7 @@ import Control.Arrow (second, (&&&))
import Database.Relational.Query.Context (Flat) import Database.Relational.Query.Context (Flat)
import Database.Relational.Query.Monad.Trans.JoinState import Database.Relational.Query.Monad.Trans.JoinState
(JoinContext, primeJoinContext, updateProduct, joinProduct, duplication) (JoinContext, primeJoinContext, updateProduct, joinProduct, setDuplication, duplication)
import Database.Relational.Query.Internal.Product (NodeAttr, restrictProduct, growProduct) import Database.Relational.Query.Internal.Product (NodeAttr, restrictProduct, growProduct)
import Database.Relational.Query.Projection (Projection) import Database.Relational.Query.Projection (Projection)
import qualified Database.Relational.Query.Projection as Projection import qualified Database.Relational.Query.Projection as Projection
@ -69,8 +69,9 @@ updateJoinRestriction e = updateContext (updateProduct d) where
-- | Joinable query instance. -- | Joinable query instance.
instance (Monad q, Functor q) => MonadQuery (QueryJoin q) where instance (Monad q, Functor q) => MonadQuery (QueryJoin q) where
restrictJoin = updateJoinRestriction specifyDuplication = updateContext . setDuplication
unsafeSubQuery = unsafeSubQueryWithAttr restrictJoin = updateJoinRestriction
unsafeSubQuery = unsafeSubQueryWithAttr
-- | Unsafely join subquery with this query. -- | Unsafely join subquery with this query.
unsafeSubQueryWithAttr :: Monad q unsafeSubQueryWithAttr :: Monad q

View File

@ -13,12 +13,12 @@ module Database.Relational.Query.Monad.Trans.JoinState (
-- * Join context -- * Join context
JoinContext, primeJoinContext, updateProduct, joinProduct, JoinContext, primeJoinContext, updateProduct, joinProduct,
setDistinct, setAll, duplication setDuplication, duplication
) where ) where
import Prelude hiding (product) import Prelude hiding (product)
import Database.Relational.Query.Component (Duplication (Distinct, All)) import Database.Relational.Query.Component (Duplication (All))
import qualified Database.Relational.Query.Internal.Product as Product import qualified Database.Relational.Query.Internal.Product as Product
import Database.Relational.Query.Sub (QueryProductNode, JoinProduct) import Database.Relational.Query.Sub (QueryProductNode, JoinProduct)
@ -46,13 +46,9 @@ updateProduct uf = updateProduct' (Just . uf)
joinProduct :: JoinContext -> JoinProduct joinProduct :: JoinContext -> JoinProduct
joinProduct = fmap Product.nodeTree . product joinProduct = fmap Product.nodeTree . product
-- | Set duplication attribute to Distinct. -- | Set duplication attribute.
setDistinct :: JoinContext -> JoinContext setDuplication :: Duplication -> JoinContext -> JoinContext
setDistinct ctx = ctx { duplicationAttribute = Distinct } setDuplication da ctx = ctx { duplicationAttribute = da }
-- | Set duplication attribute to All.
setAll :: JoinContext -> JoinContext
setAll ctx = ctx { duplicationAttribute = All }
-- | Take duplication attribute. -- | Take duplication attribute.
duplication :: JoinContext -> Duplication duplication :: JoinContext -> Duplication

View File

@ -70,9 +70,9 @@ instance MonadRestrict rc m => MonadRestrict rc (Orderings c m) where
-- | 'MonadQuery' with ordering. -- | 'MonadQuery' with ordering.
instance MonadQuery m => MonadQuery (Orderings c m) where instance MonadQuery m => MonadQuery (Orderings c m) where
restrictJoin = orderings . restrictJoin specifyDuplication = orderings . specifyDuplication
unsafeSubQuery na = orderings . unsafeSubQuery na restrictJoin = orderings . restrictJoin
-- unsafeMergeAnotherQuery = unsafeMergeAnotherOrderBys unsafeSubQuery na = orderings . unsafeSubQuery na
-- | 'MonadAggregate' with ordering. -- | 'MonadAggregate' with ordering.
instance MonadAggregate m => MonadAggregate (Orderings c m) where instance MonadAggregate m => MonadAggregate (Orderings c m) where

View File

@ -67,8 +67,9 @@ instance (Monad q, Functor q) => MonadRestrict c (Restrictings c q) where
-- | Restricted 'MonadQuery' instance. -- | Restricted 'MonadQuery' instance.
instance MonadQuery q => MonadQuery (Restrictings c q) where instance MonadQuery q => MonadQuery (Restrictings c q) where
restrictJoin = restrictings . restrictJoin specifyDuplication = restrictings . specifyDuplication
unsafeSubQuery a = restrictings . unsafeSubQuery a restrictJoin = restrictings . restrictJoin
unsafeSubQuery a = restrictings . unsafeSubQuery a
-- | Resticted 'MonadAggregate' instance. -- | Resticted 'MonadAggregate' instance.
instance MonadAggregate m => MonadAggregate (Restrictings c m) where instance MonadAggregate m => MonadAggregate (Restrictings c m) where