-- |
-- Module      : Database.Relational.Monad.BaseType
-- Copyright   : 2015-2017 Kei Hibino
-- License     : BSD3
--
-- Maintainer  : ex8k.hibino@gmail.com
-- Stability   : experimental
-- Portability : unknown
--
-- This module defines base monad type to build queries.
module Database.Relational.Monad.BaseType
       ( -- * Base monad type to build queries
         ConfigureQuery, configureQuery,
         qualifyQuery, askConfig,

         -- * Relation type
         Relation, unsafeTypeRelation, untypeRelation, relationWidth,

         dump,
         sqlFromRelationWith, sqlFromRelation,

         rightPh, leftPh,
       ) where

import Data.Functor.Identity (Identity, runIdentity)
import Control.Applicative ((<$>))

import Database.Record.Persistable (PersistableRecordWidth, unsafePersistableRecordWidth)

import Database.Relational.Internal.String (StringSQL, showStringSQL)
import Database.Relational.Internal.Config (Config, defaultConfig)
import Database.Relational.SqlSyntax (Qualified, SubQuery, showSQL, width)

import qualified Database.Relational.Monad.Trans.Qualify as Qualify
import Database.Relational.Monad.Trans.Qualify (Qualify, qualify, evalQualifyPrime)
import Database.Relational.Monad.Trans.Config (QueryConfig, runQueryConfig, askQueryConfig)

-- | Thin monad type for untyped structure.
type ConfigureQuery = Qualify (QueryConfig Identity)

-- | Run 'ConfigureQuery' monad with initial state to get only result.
configureQuery :: ConfigureQuery q -> Config -> q
configureQuery :: forall q. ConfigureQuery q -> Config -> q
configureQuery ConfigureQuery q
cq Config
c = forall a. Identity a -> a
runIdentity forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. QueryConfig m a -> Config -> m a
runQueryConfig (forall (m :: * -> *) a. Monad m => Qualify m a -> m a
evalQualifyPrime ConfigureQuery q
cq) Config
c

-- | Get qualifyed table form query.
qualifyQuery :: a -> ConfigureQuery (Qualified a)
qualifyQuery :: forall a. a -> ConfigureQuery (Qualified a)
qualifyQuery =  forall (m :: * -> *) query.
Monad m =>
query -> Qualify m (Qualified query)
Qualify.qualifyQuery

-- | Read configuration.
askConfig :: ConfigureQuery Config
askConfig :: ConfigureQuery Config
askConfig =  forall (m :: * -> *) a. Monad m => m a -> Qualify m a
qualify forall (m :: * -> *). Monad m => QueryConfig m Config
askQueryConfig


-- | Relation type with place-holder parameter 'p' and query result type 'r'.
newtype Relation p r = SubQuery (ConfigureQuery SubQuery)

-- | Unsafely type qualified subquery into record typed relation type.
unsafeTypeRelation :: ConfigureQuery SubQuery -> Relation p r
unsafeTypeRelation :: forall p r. ConfigureQuery SubQuery -> Relation p r
unsafeTypeRelation = forall p r. ConfigureQuery SubQuery -> Relation p r
SubQuery

-- | Sub-query Qualify monad from relation.
untypeRelation :: Relation p r -> ConfigureQuery SubQuery
untypeRelation :: forall p r. Relation p r -> ConfigureQuery SubQuery
untypeRelation (SubQuery ConfigureQuery SubQuery
qsub) = ConfigureQuery SubQuery
qsub

-- | 'PersistableRecordWidth' of 'Relation' type.
relationWidth :: Relation p r ->  PersistableRecordWidth r
relationWidth :: forall p r. Relation p r -> PersistableRecordWidth r
relationWidth Relation p r
rel =
  forall a. Int -> PersistableRecordWidth a
unsafePersistableRecordWidth forall b c a. (b -> c) -> (a -> b) -> a -> c
. SubQuery -> Int
width forall a b. (a -> b) -> a -> b
$ forall q. ConfigureQuery q -> Config -> q
configureQuery (forall p r. Relation p r -> ConfigureQuery SubQuery
untypeRelation Relation p r
rel) Config
defaultConfig
  ---                               Assume that width is independent from Config structure

unsafeCastPlaceHolder :: Relation a r -> Relation b r
unsafeCastPlaceHolder :: forall a r b. Relation a r -> Relation b r
unsafeCastPlaceHolder (SubQuery ConfigureQuery SubQuery
qsub) = forall p r. ConfigureQuery SubQuery -> Relation p r
SubQuery ConfigureQuery SubQuery
qsub

-- | Simplify placeholder type applying left identity element.
rightPh :: Relation ((), p) r -> Relation p r
rightPh :: forall p r. Relation ((), p) r -> Relation p r
rightPh =  forall a r b. Relation a r -> Relation b r
unsafeCastPlaceHolder

-- | Simplify placeholder type applying right identity element.
leftPh :: Relation (p, ()) r -> Relation p r
leftPh :: forall p r. Relation (p, ()) r -> Relation p r
leftPh =  forall a r b. Relation a r -> Relation b r
unsafeCastPlaceHolder

-- | Generate SQL string from 'Relation' with configuration.
sqlFromRelationWith :: Relation p r -> Config -> StringSQL
sqlFromRelationWith :: forall p r. Relation p r -> Config -> StringSQL
sqlFromRelationWith =  forall q. ConfigureQuery q -> Config -> q
configureQuery forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SubQuery -> StringSQL
showSQL forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall p r. Relation p r -> ConfigureQuery SubQuery
untypeRelation

-- | SQL string from 'Relation'.
sqlFromRelation :: Relation p r -> StringSQL
sqlFromRelation :: forall p r. Relation p r -> StringSQL
sqlFromRelation =  (forall p r. Relation p r -> Config -> StringSQL
`sqlFromRelationWith` Config
defaultConfig)

-- | Dump internal structure tree.
dump :: Relation p r -> String
dump :: forall p r. Relation p r -> String
dump =  forall a. Show a => a -> String
show forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall q. ConfigureQuery q -> Config -> q
`configureQuery` Config
defaultConfig) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall p r. Relation p r -> ConfigureQuery SubQuery
untypeRelation

instance Show (Relation p r) where
  show :: Relation p r -> String
show = StringSQL -> String
showStringSQL forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall p r. Relation p r -> StringSQL
sqlFromRelation