module Database.Relational.Query.Monad.Simple (
QuerySimple, SimpleQuery,
simple,
toSQL,
toSubQuery
) where
import Database.Relational.Query.Context (Flat)
import Database.Relational.Query.Projection (Projection)
import qualified Database.Relational.Query.Projection as Projection
import Database.Relational.Query.Monad.Class (MonadQualify(..))
import Database.Relational.Query.Monad.Trans.Join (join')
import Database.Relational.Query.Monad.Trans.Restricting (restrictings)
import Database.Relational.Query.Monad.Trans.Ordering
(Orderings, orderings, OrderedQuery, extractOrderingTerms)
import Database.Relational.Query.Monad.Type (ConfigureQuery, askConfig, QueryCore, extractCore)
import Database.Relational.Query.Component (Duplication, QueryRestriction, OrderingTerms)
import Database.Relational.Query.Sub (SubQuery, flatSubQuery, JoinProduct)
import qualified Database.Relational.Query.Sub as SubQuery
type QuerySimple = Orderings Flat QueryCore
type SimpleQuery r = OrderedQuery Flat QueryCore r
simple :: ConfigureQuery a -> QuerySimple a
simple = orderings . restrictings . join'
instance MonadQualify ConfigureQuery (Orderings Flat QueryCore) where
liftQualify = simple
extract :: SimpleQuery r
-> ConfigureQuery ((((Projection Flat r, OrderingTerms), QueryRestriction Flat),
JoinProduct), Duplication)
extract = extractCore . extractOrderingTerms
toSQL :: SimpleQuery r
-> ConfigureQuery String
toSQL = fmap SubQuery.toSQL . toSubQuery
toSubQuery :: SimpleQuery r
-> ConfigureQuery SubQuery
toSubQuery q = do
((((pj, ot), rs), pd), da) <- extract q
c <- askConfig
return $ flatSubQuery c (Projection.untype pj) da pd rs ot