module Database.Persist.Sql.Types
    ( module Database.Persist.Sql.Types
    , SqlBackend, SqlReadBackend (..), SqlWriteBackend (..)
    , Statement (..), LogFunc, InsertSqlResult (..)
    , readToUnknown, readToWrite, writeToUnknown
    , SqlBackendCanRead, SqlBackendCanWrite, SqlReadT, SqlWriteT, IsSqlBackend
    , OverflowNatural(..)
    , ConnectionPoolConfig(..)
    ) where

import Control.Exception (Exception(..))
import Control.Monad.Logger (NoLoggingT)
import Control.Monad.Trans.Reader (ReaderT(..))
import Control.Monad.Trans.Resource (ResourceT)
import Data.Pool (Pool)
import Data.Text (Text)

import Data.Time (NominalDiffTime)
import Database.Persist.Sql.Types.Internal
import Database.Persist.Types

data Column = Column
    { Column -> FieldNameDB
cName      :: !FieldNameDB
    , Column -> Bool
cNull      :: !Bool
    , Column -> SqlType
cSqlType   :: !SqlType
    , Column -> Maybe Text
cDefault   :: !(Maybe Text)
    , Column -> Maybe Text
cGenerated :: !(Maybe Text)
    , Column -> Maybe ConstraintNameDB
cDefaultConstraintName   :: !(Maybe ConstraintNameDB)
    , Column -> Maybe Integer
cMaxLen    :: !(Maybe Integer)
    , Column -> Maybe ColumnReference
cReference :: !(Maybe ColumnReference)
    }
    deriving (Column -> Column -> Bool
(Column -> Column -> Bool)
-> (Column -> Column -> Bool) -> Eq Column
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Column -> Column -> Bool
== :: Column -> Column -> Bool
$c/= :: Column -> Column -> Bool
/= :: Column -> Column -> Bool
Eq, Eq Column
Eq Column =>
(Column -> Column -> Ordering)
-> (Column -> Column -> Bool)
-> (Column -> Column -> Bool)
-> (Column -> Column -> Bool)
-> (Column -> Column -> Bool)
-> (Column -> Column -> Column)
-> (Column -> Column -> Column)
-> Ord Column
Column -> Column -> Bool
Column -> Column -> Ordering
Column -> Column -> Column
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Column -> Column -> Ordering
compare :: Column -> Column -> Ordering
$c< :: Column -> Column -> Bool
< :: Column -> Column -> Bool
$c<= :: Column -> Column -> Bool
<= :: Column -> Column -> Bool
$c> :: Column -> Column -> Bool
> :: Column -> Column -> Bool
$c>= :: Column -> Column -> Bool
>= :: Column -> Column -> Bool
$cmax :: Column -> Column -> Column
max :: Column -> Column -> Column
$cmin :: Column -> Column -> Column
min :: Column -> Column -> Column
Ord, Int -> Column -> ShowS
[Column] -> ShowS
Column -> String
(Int -> Column -> ShowS)
-> (Column -> String) -> ([Column] -> ShowS) -> Show Column
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Column -> ShowS
showsPrec :: Int -> Column -> ShowS
$cshow :: Column -> String
show :: Column -> String
$cshowList :: [Column] -> ShowS
showList :: [Column] -> ShowS
Show)

-- | This value specifies how a field references another table.
--
-- @since 2.11.0.0
data ColumnReference = ColumnReference
    { ColumnReference -> EntityNameDB
crTableName :: !EntityNameDB
    -- ^ The table name that the
    --
    -- @since 2.11.0.0
    , ColumnReference -> ConstraintNameDB
crConstraintName :: !ConstraintNameDB
    -- ^ The name of the foreign key constraint.
    --
    -- @since 2.11.0.0
    , ColumnReference -> FieldCascade
crFieldCascade :: !FieldCascade
    -- ^ Whether or not updates/deletions to the referenced table cascade
    -- to this table.
    --
    -- @since 2.11.0.0
    }
    deriving (ColumnReference -> ColumnReference -> Bool
(ColumnReference -> ColumnReference -> Bool)
-> (ColumnReference -> ColumnReference -> Bool)
-> Eq ColumnReference
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ColumnReference -> ColumnReference -> Bool
== :: ColumnReference -> ColumnReference -> Bool
$c/= :: ColumnReference -> ColumnReference -> Bool
/= :: ColumnReference -> ColumnReference -> Bool
Eq, Eq ColumnReference
Eq ColumnReference =>
(ColumnReference -> ColumnReference -> Ordering)
-> (ColumnReference -> ColumnReference -> Bool)
-> (ColumnReference -> ColumnReference -> Bool)
-> (ColumnReference -> ColumnReference -> Bool)
-> (ColumnReference -> ColumnReference -> Bool)
-> (ColumnReference -> ColumnReference -> ColumnReference)
-> (ColumnReference -> ColumnReference -> ColumnReference)
-> Ord ColumnReference
ColumnReference -> ColumnReference -> Bool
ColumnReference -> ColumnReference -> Ordering
ColumnReference -> ColumnReference -> ColumnReference
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: ColumnReference -> ColumnReference -> Ordering
compare :: ColumnReference -> ColumnReference -> Ordering
$c< :: ColumnReference -> ColumnReference -> Bool
< :: ColumnReference -> ColumnReference -> Bool
$c<= :: ColumnReference -> ColumnReference -> Bool
<= :: ColumnReference -> ColumnReference -> Bool
$c> :: ColumnReference -> ColumnReference -> Bool
> :: ColumnReference -> ColumnReference -> Bool
$c>= :: ColumnReference -> ColumnReference -> Bool
>= :: ColumnReference -> ColumnReference -> Bool
$cmax :: ColumnReference -> ColumnReference -> ColumnReference
max :: ColumnReference -> ColumnReference -> ColumnReference
$cmin :: ColumnReference -> ColumnReference -> ColumnReference
min :: ColumnReference -> ColumnReference -> ColumnReference
Ord, Int -> ColumnReference -> ShowS
[ColumnReference] -> ShowS
ColumnReference -> String
(Int -> ColumnReference -> ShowS)
-> (ColumnReference -> String)
-> ([ColumnReference] -> ShowS)
-> Show ColumnReference
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ColumnReference -> ShowS
showsPrec :: Int -> ColumnReference -> ShowS
$cshow :: ColumnReference -> String
show :: ColumnReference -> String
$cshowList :: [ColumnReference] -> ShowS
showList :: [ColumnReference] -> ShowS
Show)

data PersistentSqlException = StatementAlreadyFinalized Text
                            | Couldn'tGetSQLConnection
    deriving Int -> PersistentSqlException -> ShowS
[PersistentSqlException] -> ShowS
PersistentSqlException -> String
(Int -> PersistentSqlException -> ShowS)
-> (PersistentSqlException -> String)
-> ([PersistentSqlException] -> ShowS)
-> Show PersistentSqlException
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PersistentSqlException -> ShowS
showsPrec :: Int -> PersistentSqlException -> ShowS
$cshow :: PersistentSqlException -> String
show :: PersistentSqlException -> String
$cshowList :: [PersistentSqlException] -> ShowS
showList :: [PersistentSqlException] -> ShowS
Show
instance Exception PersistentSqlException

type SqlPersistT = ReaderT SqlBackend

type SqlPersistM = SqlPersistT (NoLoggingT (ResourceT IO))

type ConnectionPool = Pool SqlBackend

-- | Values to configure a pool of database connections. See "Data.Pool" for details.
--
-- @since 2.11.0.0
data ConnectionPoolConfig = ConnectionPoolConfig
    { ConnectionPoolConfig -> Int
connectionPoolConfigStripes :: Int -- ^ How many stripes to divide the pool into. See "Data.Pool" for details. Default: 1.
    , ConnectionPoolConfig -> NominalDiffTime
connectionPoolConfigIdleTimeout :: NominalDiffTime -- ^ How long connections can remain idle before being disposed of, in seconds. Default: 600
    , ConnectionPoolConfig -> Int
connectionPoolConfigSize :: Int -- ^ How many connections should be held in the connection pool. Default: 10
    }
    deriving (Int -> ConnectionPoolConfig -> ShowS
[ConnectionPoolConfig] -> ShowS
ConnectionPoolConfig -> String
(Int -> ConnectionPoolConfig -> ShowS)
-> (ConnectionPoolConfig -> String)
-> ([ConnectionPoolConfig] -> ShowS)
-> Show ConnectionPoolConfig
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ConnectionPoolConfig -> ShowS
showsPrec :: Int -> ConnectionPoolConfig -> ShowS
$cshow :: ConnectionPoolConfig -> String
show :: ConnectionPoolConfig -> String
$cshowList :: [ConnectionPoolConfig] -> ShowS
showList :: [ConnectionPoolConfig] -> ShowS
Show)

-- TODO: Bad defaults for SQLite maybe?
-- | Initializes a ConnectionPoolConfig with default values. See the documentation of 'ConnectionPoolConfig' for each field's default value.
--
-- @since 2.11.0.0
defaultConnectionPoolConfig :: ConnectionPoolConfig
defaultConnectionPoolConfig :: ConnectionPoolConfig
defaultConnectionPoolConfig = Int -> NominalDiffTime -> Int -> ConnectionPoolConfig
ConnectionPoolConfig Int
1 NominalDiffTime
600 Int
10

-- $rawSql
--
-- Although it covers most of the useful cases, @persistent@'s
-- API may not be enough for some of your tasks.  May be you need
-- some complex @JOIN@ query, or a database-specific command
-- needs to be issued.
--
-- To issue raw SQL queries, use 'rawSql'. It does all the hard work of
-- automatically parsing the rows of the result.  It may return:
--
--   * An 'Entity', that which 'selectList' returns.
--     All of your entity's fields are
--     automatically parsed.
--
--   * A @'Single' a@, which is a single, raw column of type @a@.
--     You may use a Haskell type (such as in your entity
--     definitions), for example @Single Text@ or @Single Int@,
--     or you may get the raw column value with @Single
--     'PersistValue'@.
--
--   * A tuple combining any of these (including other tuples).
--     Using tuples allows you to return many entities in one
--     query.
--
-- The only difference between issuing SQL queries with 'rawSql'
-- and using other means is that we have an /entity selection/
-- /placeholder/, the double question mark @??@.  It /must/ be
-- used whenever you want to @SELECT@ an 'Entity' from your
-- query.  Here's a sample SQL query @sampleStmt@ that may be
-- issued:
--
-- @
-- SELECT ??, ??
-- FROM \"Person\", \"Likes\", \"Object\"
-- WHERE \"Person\".id = \"Likes\".\"personId\"
-- AND \"Object\".id = \"Likes\".\"objectId\"
-- AND \"Person\".name LIKE ?
-- @
--
-- To use that query, you could say
--
-- @
-- do results <- 'rawSql' sampleStmt [\"%Luke%\"]
--    forM_ results $
--      \\( Entity personKey person
--       , Entity objectKey object
--       ) -> do ...
-- @
--
-- Note that 'rawSql' knows how to replace the double question
-- marks @??@ because of the type of the @results@.


-- | A single column (see 'rawSql').  Any 'PersistField' may be
-- used here, including 'PersistValue' (which does not do any
-- processing).
newtype Single a = Single {forall a. Single a -> a
unSingle :: a}
    deriving (Single a -> Single a -> Bool
(Single a -> Single a -> Bool)
-> (Single a -> Single a -> Bool) -> Eq (Single a)
forall a. Eq a => Single a -> Single a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Single a -> Single a -> Bool
== :: Single a -> Single a -> Bool
$c/= :: forall a. Eq a => Single a -> Single a -> Bool
/= :: Single a -> Single a -> Bool
Eq, Eq (Single a)
Eq (Single a) =>
(Single a -> Single a -> Ordering)
-> (Single a -> Single a -> Bool)
-> (Single a -> Single a -> Bool)
-> (Single a -> Single a -> Bool)
-> (Single a -> Single a -> Bool)
-> (Single a -> Single a -> Single a)
-> (Single a -> Single a -> Single a)
-> Ord (Single a)
Single a -> Single a -> Bool
Single a -> Single a -> Ordering
Single a -> Single a -> Single a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (Single a)
forall a. Ord a => Single a -> Single a -> Bool
forall a. Ord a => Single a -> Single a -> Ordering
forall a. Ord a => Single a -> Single a -> Single a
$ccompare :: forall a. Ord a => Single a -> Single a -> Ordering
compare :: Single a -> Single a -> Ordering
$c< :: forall a. Ord a => Single a -> Single a -> Bool
< :: Single a -> Single a -> Bool
$c<= :: forall a. Ord a => Single a -> Single a -> Bool
<= :: Single a -> Single a -> Bool
$c> :: forall a. Ord a => Single a -> Single a -> Bool
> :: Single a -> Single a -> Bool
$c>= :: forall a. Ord a => Single a -> Single a -> Bool
>= :: Single a -> Single a -> Bool
$cmax :: forall a. Ord a => Single a -> Single a -> Single a
max :: Single a -> Single a -> Single a
$cmin :: forall a. Ord a => Single a -> Single a -> Single a
min :: Single a -> Single a -> Single a
Ord, Int -> Single a -> ShowS
[Single a] -> ShowS
Single a -> String
(Int -> Single a -> ShowS)
-> (Single a -> String) -> ([Single a] -> ShowS) -> Show (Single a)
forall a. Show a => Int -> Single a -> ShowS
forall a. Show a => [Single a] -> ShowS
forall a. Show a => Single a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Single a -> ShowS
showsPrec :: Int -> Single a -> ShowS
$cshow :: forall a. Show a => Single a -> String
show :: Single a -> String
$cshowList :: forall a. Show a => [Single a] -> ShowS
showList :: [Single a] -> ShowS
Show, ReadPrec [Single a]
ReadPrec (Single a)
Int -> ReadS (Single a)
ReadS [Single a]
(Int -> ReadS (Single a))
-> ReadS [Single a]
-> ReadPrec (Single a)
-> ReadPrec [Single a]
-> Read (Single a)
forall a. Read a => ReadPrec [Single a]
forall a. Read a => ReadPrec (Single a)
forall a. Read a => Int -> ReadS (Single a)
forall a. Read a => ReadS [Single a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Read a => Int -> ReadS (Single a)
readsPrec :: Int -> ReadS (Single a)
$creadList :: forall a. Read a => ReadS [Single a]
readList :: ReadS [Single a]
$creadPrec :: forall a. Read a => ReadPrec (Single a)
readPrec :: ReadPrec (Single a)
$creadListPrec :: forall a. Read a => ReadPrec [Single a]
readListPrec :: ReadPrec [Single a]
Read)