-- Copyright   :  Daan Leijen (c) 1999, daan@cs.uu.nl
--                HWT Group (c) 2003, haskelldb-users@lists.sourceforge.net
-- License     :  BSD-style

module Opaleye.Internal.HaskellDB.Sql where


import qualified Data.List.NonEmpty as NEL

-----------------------------------------------------------
-- * SQL data type
-----------------------------------------------------------

data SqlTable = SqlTable
  { SqlTable -> Maybe String
sqlTableSchemaName :: Maybe String
  , SqlTable -> String
sqlTableName       :: String
  } deriving Int -> SqlTable -> ShowS
[SqlTable] -> ShowS
SqlTable -> String
(Int -> SqlTable -> ShowS)
-> (SqlTable -> String) -> ([SqlTable] -> ShowS) -> Show SqlTable
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SqlTable] -> ShowS
$cshowList :: [SqlTable] -> ShowS
show :: SqlTable -> String
$cshow :: SqlTable -> String
showsPrec :: Int -> SqlTable -> ShowS
$cshowsPrec :: Int -> SqlTable -> ShowS
Show

newtype SqlColumn = SqlColumn String deriving Int -> SqlColumn -> ShowS
[SqlColumn] -> ShowS
SqlColumn -> String
(Int -> SqlColumn -> ShowS)
-> (SqlColumn -> String)
-> ([SqlColumn] -> ShowS)
-> Show SqlColumn
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SqlColumn] -> ShowS
$cshowList :: [SqlColumn] -> ShowS
show :: SqlColumn -> String
$cshow :: SqlColumn -> String
showsPrec :: Int -> SqlColumn -> ShowS
$cshowsPrec :: Int -> SqlColumn -> ShowS
Show

-- | A valid SQL name for a parameter.
type SqlName = String

data SqlOrderNulls = SqlNullsFirst | SqlNullsLast
                   deriving Int -> SqlOrderNulls -> ShowS
[SqlOrderNulls] -> ShowS
SqlOrderNulls -> String
(Int -> SqlOrderNulls -> ShowS)
-> (SqlOrderNulls -> String)
-> ([SqlOrderNulls] -> ShowS)
-> Show SqlOrderNulls
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SqlOrderNulls] -> ShowS
$cshowList :: [SqlOrderNulls] -> ShowS
show :: SqlOrderNulls -> String
$cshow :: SqlOrderNulls -> String
showsPrec :: Int -> SqlOrderNulls -> ShowS
$cshowsPrec :: Int -> SqlOrderNulls -> ShowS
Show

data SqlOrderDirection = SqlAsc | SqlDesc
                       deriving Int -> SqlOrderDirection -> ShowS
[SqlOrderDirection] -> ShowS
SqlOrderDirection -> String
(Int -> SqlOrderDirection -> ShowS)
-> (SqlOrderDirection -> String)
-> ([SqlOrderDirection] -> ShowS)
-> Show SqlOrderDirection
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SqlOrderDirection] -> ShowS
$cshowList :: [SqlOrderDirection] -> ShowS
show :: SqlOrderDirection -> String
$cshow :: SqlOrderDirection -> String
showsPrec :: Int -> SqlOrderDirection -> ShowS
$cshowsPrec :: Int -> SqlOrderDirection -> ShowS
Show

data SqlOrder = SqlOrder { SqlOrder -> SqlOrderDirection
sqlOrderDirection :: SqlOrderDirection
                         , SqlOrder -> SqlOrderNulls
sqlOrderNulls     :: SqlOrderNulls }
  deriving Int -> SqlOrder -> ShowS
[SqlOrder] -> ShowS
SqlOrder -> String
(Int -> SqlOrder -> ShowS)
-> (SqlOrder -> String) -> ([SqlOrder] -> ShowS) -> Show SqlOrder
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SqlOrder] -> ShowS
$cshowList :: [SqlOrder] -> ShowS
show :: SqlOrder -> String
$cshow :: SqlOrder -> String
showsPrec :: Int -> SqlOrder -> ShowS
$cshowsPrec :: Int -> SqlOrder -> ShowS
Show

data SqlRangeBound = Inclusive SqlExpr | Exclusive SqlExpr | PosInfinity | NegInfinity
                   deriving Int -> SqlRangeBound -> ShowS
[SqlRangeBound] -> ShowS
SqlRangeBound -> String
(Int -> SqlRangeBound -> ShowS)
-> (SqlRangeBound -> String)
-> ([SqlRangeBound] -> ShowS)
-> Show SqlRangeBound
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SqlRangeBound] -> ShowS
$cshowList :: [SqlRangeBound] -> ShowS
show :: SqlRangeBound -> String
$cshow :: SqlRangeBound -> String
showsPrec :: Int -> SqlRangeBound -> ShowS
$cshowsPrec :: Int -> SqlRangeBound -> ShowS
Show

data SqlDistinct = SqlDistinct | SqlNotDistinct
                 deriving Int -> SqlDistinct -> ShowS
[SqlDistinct] -> ShowS
SqlDistinct -> String
(Int -> SqlDistinct -> ShowS)
-> (SqlDistinct -> String)
-> ([SqlDistinct] -> ShowS)
-> Show SqlDistinct
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SqlDistinct] -> ShowS
$cshowList :: [SqlDistinct] -> ShowS
show :: SqlDistinct -> String
$cshow :: SqlDistinct -> String
showsPrec :: Int -> SqlDistinct -> ShowS
$cshowsPrec :: Int -> SqlDistinct -> ShowS
Show

-- | Expressions in SQL statements.
data SqlExpr = ColumnSqlExpr  SqlColumn
             | CompositeSqlExpr SqlExpr String
             | BinSqlExpr     String SqlExpr SqlExpr
             | SubscriptSqlExpr SqlExpr SqlExpr
             | PrefixSqlExpr  String SqlExpr
             | PostfixSqlExpr String SqlExpr
             | FunSqlExpr     String [SqlExpr]
             | AggrFunSqlExpr String [SqlExpr] [(SqlExpr, SqlOrder)] SqlDistinct -- ^ Aggregate functions separate from normal functions.
             | ConstSqlExpr   String
             | CaseSqlExpr    (NEL.NonEmpty (SqlExpr,SqlExpr)) SqlExpr
             | ListSqlExpr    (NEL.NonEmpty SqlExpr)
             | ParamSqlExpr (Maybe SqlName) SqlExpr
             | PlaceHolderSqlExpr
             | ParensSqlExpr SqlExpr
             | CastSqlExpr String SqlExpr
             | DefaultSqlExpr
             | ArraySqlExpr [SqlExpr]
             | RangeSqlExpr String SqlRangeBound SqlRangeBound
  deriving Int -> SqlExpr -> ShowS
[SqlExpr] -> ShowS
SqlExpr -> String
(Int -> SqlExpr -> ShowS)
-> (SqlExpr -> String) -> ([SqlExpr] -> ShowS) -> Show SqlExpr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SqlExpr] -> ShowS
$cshowList :: [SqlExpr] -> ShowS
show :: SqlExpr -> String
$cshow :: SqlExpr -> String
showsPrec :: Int -> SqlExpr -> ShowS
$cshowsPrec :: Int -> SqlExpr -> ShowS
Show

-- | Data type for SQL UPDATE statements.
data SqlUpdate  = SqlUpdate SqlTable [(SqlColumn,SqlExpr)] [SqlExpr]

-- | Data type for SQL DELETE statements.
data SqlDelete  = SqlDelete SqlTable [SqlExpr]

data OnConflict = DoNothing
                -- ^ @ON CONFLICT DO NOTHING@

--- | Data type for SQL INSERT statements.
data SqlInsert  = SqlInsert SqlTable [SqlColumn] (NEL.NonEmpty [SqlExpr]) (Maybe OnConflict)