{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE UndecidableInstances #-}

-- | Provides a syntax 'SqlSyntaxBuilder' that uses a
--   'Data.ByteString.Builder.Builder' to construct SQL expressions as strings.
--   Mainly serves as documentation for how to write a syntax for backends. Note
--   that, although you can use this to turn most 'Q' and 'QGenExpr's into
--   'ByteString' queries, it is /very unwise/ to ship these to the database.
--   This module does not take into account server-specific quoting. Some
--   backends are very particular to quoting, and shipping arbitrary
--   'ByteString's as queries can expose you to SQL injection vulnerabilities.
--   Always use the provided backends to submit queries and data manipulation
--   commands to the database.
module Database.Beam.Backend.SQL.Builder
  ( SqlSyntaxBuilder(..) --, SqlSyntaxBackend
  , buildSepBy
  , quoteSql
  , renderSql ) where

import           Database.Beam.Backend.Internal.Compat
import           Database.Beam.Backend.SQL

import           Control.Monad.IO.Class

import           Data.ByteString (ByteString)
import           Data.ByteString.Builder
import qualified Data.ByteString.Lazy.Char8 as BL
import qualified Data.Text.Encoding as TE
import qualified Data.Text as T
import           Data.Text (Text)

import           Data.Coerce
import           Data.Hashable
import           Data.Int
import           Data.String
import qualified Control.Monad.Fail as Fail
import           GHC.TypeLits

-- | The main syntax. A wrapper over 'Builder'
newtype SqlSyntaxBuilder
  = SqlSyntaxBuilder { SqlSyntaxBuilder -> Builder
buildSql :: Builder }

instance Hashable SqlSyntaxBuilder where
  hashWithSalt :: Int -> SqlSyntaxBuilder -> Int
hashWithSalt Int
salt (SqlSyntaxBuilder Builder
b) = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (Builder -> ByteString
toLazyByteString Builder
b)

instance Show SqlSyntaxBuilder where
  showsPrec :: Int -> SqlSyntaxBuilder -> ShowS
showsPrec Int
prec (SqlSyntaxBuilder Builder
s) =
    Bool -> ShowS -> ShowS
showParen (Int
prec forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$
    String -> ShowS
showString String
"SqlSyntaxBuilder (" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    forall a. Show a => a -> ShowS
shows (Builder -> ByteString
toLazyByteString Builder
s) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    String -> ShowS
showString String
")"

instance Eq SqlSyntaxBuilder where
  SqlSyntaxBuilder
a == :: SqlSyntaxBuilder -> SqlSyntaxBuilder -> Bool
== SqlSyntaxBuilder
b = Builder -> ByteString
toLazyByteString (SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
a) forall a. Eq a => a -> a -> Bool
== Builder -> ByteString
toLazyByteString (SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
b)

instance Semigroup SqlSyntaxBuilder where
  SqlSyntaxBuilder Builder
a <> :: SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
<> SqlSyntaxBuilder Builder
b =  Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Builder
a forall a. Semigroup a => a -> a -> a
<> Builder
b)

instance Monoid SqlSyntaxBuilder where
  mempty :: SqlSyntaxBuilder
mempty = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a. Monoid a => a
mempty
  mappend :: SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
mappend = forall a. Semigroup a => a -> a -> a
(<>)

instance IsSql92Syntax SqlSyntaxBuilder where
  type Sql92SelectSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92InsertSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92DeleteSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92UpdateSyntax SqlSyntaxBuilder = SqlSyntaxBuilder

  selectCmd :: Sql92SelectSyntax SqlSyntaxBuilder -> SqlSyntaxBuilder
selectCmd = forall a. a -> a
id
  insertCmd :: Sql92InsertSyntax SqlSyntaxBuilder -> SqlSyntaxBuilder
insertCmd = forall a. a -> a
id
  updateCmd :: Sql92UpdateSyntax SqlSyntaxBuilder -> SqlSyntaxBuilder
updateCmd = forall a. a -> a
id
  deleteCmd :: Sql92DeleteSyntax SqlSyntaxBuilder -> SqlSyntaxBuilder
deleteCmd = forall a. a -> a
id

instance IsSql92SelectSyntax SqlSyntaxBuilder where
  type Sql92SelectSelectTableSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92SelectOrderingSyntax SqlSyntaxBuilder = SqlSyntaxBuilder

  selectStmt :: Sql92SelectSelectTableSyntax SqlSyntaxBuilder
-> [Sql92SelectOrderingSyntax SqlSyntaxBuilder]
-> Maybe Integer
-> Maybe Integer
-> SqlSyntaxBuilder
selectStmt Sql92SelectSelectTableSyntax SqlSyntaxBuilder
tableSrc [Sql92SelectOrderingSyntax SqlSyntaxBuilder]
ordering Maybe Integer
limit Maybe Integer
offset =
      Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
      SqlSyntaxBuilder -> Builder
buildSql Sql92SelectSelectTableSyntax SqlSyntaxBuilder
tableSrc forall a. Semigroup a => a -> a -> a
<>
      ( case [Sql92SelectOrderingSyntax SqlSyntaxBuilder]
ordering of
          [] -> forall a. Monoid a => a
mempty
          [Sql92SelectOrderingSyntax SqlSyntaxBuilder]
_ -> ByteString -> Builder
byteString ByteString
" ORDER BY " forall a. Semigroup a => a -> a -> a
<>
               Builder -> [Builder] -> Builder
buildSepBy (ByteString -> Builder
byteString ByteString
", ") (forall a b. (a -> b) -> [a] -> [b]
map SqlSyntaxBuilder -> Builder
buildSql [Sql92SelectOrderingSyntax SqlSyntaxBuilder]
ordering) ) forall a. Semigroup a => a -> a -> a
<>
      forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\Integer
l -> ByteString -> Builder
byteString ByteString
" LIMIT " forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString (forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Integer
l))) Maybe Integer
limit forall a. Semigroup a => a -> a -> a
<>
      forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\Integer
o -> ByteString -> Builder
byteString ByteString
" OFFSET " forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString (forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Integer
o))) Maybe Integer
offset

instance IsSql92GroupingSyntax SqlSyntaxBuilder where
  type Sql92GroupingExpressionSyntax SqlSyntaxBuilder = SqlSyntaxBuilder

  groupByExpressions :: [Sql92GroupingExpressionSyntax SqlSyntaxBuilder]
-> SqlSyntaxBuilder
groupByExpressions [Sql92GroupingExpressionSyntax SqlSyntaxBuilder]
es =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    Builder -> [Builder] -> Builder
buildSepBy (ByteString -> Builder
byteString ByteString
", ") (forall a b. (a -> b) -> [a] -> [b]
map SqlSyntaxBuilder -> Builder
buildSql [Sql92GroupingExpressionSyntax SqlSyntaxBuilder]
es)

instance IsSql92SelectTableSyntax SqlSyntaxBuilder where
  type Sql92SelectTableSelectSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92SelectTableExpressionSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92SelectTableProjectionSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92SelectTableFromSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92SelectTableGroupingSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92SelectTableSetQuantifierSyntax SqlSyntaxBuilder = SqlSyntaxBuilder

  selectTableStmt :: Maybe (Sql92SelectTableSetQuantifierSyntax SqlSyntaxBuilder)
-> Sql92SelectTableProjectionSyntax SqlSyntaxBuilder
-> Maybe (Sql92SelectTableFromSyntax SqlSyntaxBuilder)
-> Maybe (Sql92SelectTableExpressionSyntax SqlSyntaxBuilder)
-> Maybe (Sql92SelectTableGroupingSyntax SqlSyntaxBuilder)
-> Maybe (Sql92SelectTableExpressionSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder
selectTableStmt Maybe (Sql92SelectTableSetQuantifierSyntax SqlSyntaxBuilder)
setQuantifier Sql92SelectTableProjectionSyntax SqlSyntaxBuilder
proj Maybe (Sql92SelectTableFromSyntax SqlSyntaxBuilder)
from Maybe (Sql92SelectTableExpressionSyntax SqlSyntaxBuilder)
where_ Maybe (Sql92SelectTableGroupingSyntax SqlSyntaxBuilder)
grouping Maybe (Sql92SelectTableExpressionSyntax SqlSyntaxBuilder)
having =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString ByteString
"SELECT " forall a. Semigroup a => a -> a -> a
<>
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqlSyntaxBuilder
setQuantifier' -> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
setQuantifier' forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
" ") Maybe (Sql92SelectTableSetQuantifierSyntax SqlSyntaxBuilder)
setQuantifier forall a. Semigroup a => a -> a -> a
<>
    SqlSyntaxBuilder -> Builder
buildSql Sql92SelectTableProjectionSyntax SqlSyntaxBuilder
proj forall a. Semigroup a => a -> a -> a
<>
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty ((ByteString -> Builder
byteString ByteString
" FROM " forall a. Semigroup a => a -> a -> a
<>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqlSyntaxBuilder -> Builder
buildSql) Maybe (Sql92SelectTableFromSyntax SqlSyntaxBuilder)
from forall a. Semigroup a => a -> a -> a
<>
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqlSyntaxBuilder
w -> ByteString -> Builder
byteString ByteString
" WHERE " forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
w) Maybe (Sql92SelectTableExpressionSyntax SqlSyntaxBuilder)
where_ forall a. Semigroup a => a -> a -> a
<>
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqlSyntaxBuilder
g -> ByteString -> Builder
byteString ByteString
" GROUP BY " forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
g) Maybe (Sql92SelectTableGroupingSyntax SqlSyntaxBuilder)
grouping forall a. Semigroup a => a -> a -> a
<>
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqlSyntaxBuilder
e -> ByteString -> Builder
byteString ByteString
" HAVING " forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
e) Maybe (Sql92SelectTableExpressionSyntax SqlSyntaxBuilder)
having

  unionTables :: Bool -> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
unionTables = ByteString
-> Bool -> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
tableOp ByteString
"UNION"
  intersectTables :: Bool -> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
intersectTables  = ByteString
-> Bool -> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
tableOp ByteString
"INTERSECT"
  exceptTable :: Bool -> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
exceptTable = ByteString
-> Bool -> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
tableOp ByteString
"EXCEPT"

tableOp :: ByteString -> Bool -> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
tableOp :: ByteString
-> Bool -> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
tableOp ByteString
op Bool
all SqlSyntaxBuilder
a SqlSyntaxBuilder
b =
  Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
  ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
a forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
") " forall a. Semigroup a => a -> a -> a
<>
  ByteString -> Builder
byteString ByteString
op forall a. Semigroup a => a -> a -> a
<> if Bool
all then ByteString -> Builder
byteString ByteString
" ALL " else forall a. Monoid a => a
mempty forall a. Semigroup a => a -> a -> a
<>
  ByteString -> Builder
byteString ByteString
" (" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
b forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")"

instance IsSql92InsertSyntax SqlSyntaxBuilder where
  type Sql92InsertValuesSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92InsertTableNameSyntax SqlSyntaxBuilder = SqlSyntaxBuilder

  insertStmt :: Sql92InsertTableNameSyntax SqlSyntaxBuilder
-> [Text]
-> Sql92InsertValuesSyntax SqlSyntaxBuilder
-> SqlSyntaxBuilder
insertStmt Sql92InsertTableNameSyntax SqlSyntaxBuilder
tblNm [Text]
fields Sql92InsertValuesSyntax SqlSyntaxBuilder
values =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString ByteString
"INSERT INTO " forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql Sql92InsertTableNameSyntax SqlSyntaxBuilder
tblNm forall a. Semigroup a => a -> a -> a
<>
    ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<> Builder -> [Builder] -> Builder
buildSepBy (ByteString -> Builder
byteString ByteString
", ") (forall a b. (a -> b) -> [a] -> [b]
map Text -> Builder
quoteSql [Text]
fields) forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
") " forall a. Semigroup a => a -> a -> a
<>
    SqlSyntaxBuilder -> Builder
buildSql Sql92InsertValuesSyntax SqlSyntaxBuilder
values

instance IsSql92InsertValuesSyntax SqlSyntaxBuilder where
  type Sql92InsertValuesExpressionSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92InsertValuesSelectSyntax SqlSyntaxBuilder = SqlSyntaxBuilder

  insertSqlExpressions :: [[Sql92InsertValuesExpressionSyntax SqlSyntaxBuilder]]
-> SqlSyntaxBuilder
insertSqlExpressions [[Sql92InsertValuesExpressionSyntax SqlSyntaxBuilder]]
values =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString ByteString
"VALUES " forall a. Semigroup a => a -> a -> a
<>
    Builder -> [Builder] -> Builder
buildSepBy (ByteString -> Builder
byteString ByteString
", ") (forall a b. (a -> b) -> [a] -> [b]
map [SqlSyntaxBuilder] -> Builder
mkValues [[Sql92InsertValuesExpressionSyntax SqlSyntaxBuilder]]
values)
    where mkValues :: [SqlSyntaxBuilder] -> Builder
mkValues [SqlSyntaxBuilder]
values' =
            ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<>
            Builder -> [Builder] -> Builder
buildSepBy (ByteString -> Builder
byteString ByteString
", ") (forall a b. (a -> b) -> [a] -> [b]
map SqlSyntaxBuilder -> Builder
buildSql [SqlSyntaxBuilder]
values') forall a. Semigroup a => a -> a -> a
<>
            ByteString -> Builder
byteString ByteString
")"

  insertFromSql :: Sql92InsertValuesSelectSyntax SqlSyntaxBuilder -> SqlSyntaxBuilder
insertFromSql Sql92InsertValuesSelectSyntax SqlSyntaxBuilder
select = Sql92InsertValuesSelectSyntax SqlSyntaxBuilder
select

instance IsSql92UpdateSyntax SqlSyntaxBuilder where
  type Sql92UpdateFieldNameSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92UpdateExpressionSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92UpdateTableNameSyntax SqlSyntaxBuilder = SqlSyntaxBuilder

  updateStmt :: Sql92UpdateTableNameSyntax SqlSyntaxBuilder
-> [(Sql92UpdateFieldNameSyntax SqlSyntaxBuilder,
     Sql92UpdateExpressionSyntax SqlSyntaxBuilder)]
-> Maybe (Sql92UpdateExpressionSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder
updateStmt Sql92UpdateTableNameSyntax SqlSyntaxBuilder
tblNm [(Sql92UpdateFieldNameSyntax SqlSyntaxBuilder,
  Sql92UpdateExpressionSyntax SqlSyntaxBuilder)]
set Maybe (Sql92UpdateExpressionSyntax SqlSyntaxBuilder)
where_ =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString ByteString
"UPDATE " forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql Sql92UpdateTableNameSyntax SqlSyntaxBuilder
tblNm forall a. Semigroup a => a -> a -> a
<>
    (case [(Sql92UpdateFieldNameSyntax SqlSyntaxBuilder,
  Sql92UpdateExpressionSyntax SqlSyntaxBuilder)]
set of
       [] -> forall a. Monoid a => a
mempty
       [(Sql92UpdateFieldNameSyntax SqlSyntaxBuilder,
  Sql92UpdateExpressionSyntax SqlSyntaxBuilder)]
es -> ByteString -> Builder
byteString ByteString
" SET " forall a. Semigroup a => a -> a -> a
<> Builder -> [Builder] -> Builder
buildSepBy (ByteString -> Builder
byteString ByteString
", ") (forall a b. (a -> b) -> [a] -> [b]
map (\(SqlSyntaxBuilder
field, SqlSyntaxBuilder
expr) -> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
field forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
"=" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
expr) [(Sql92UpdateFieldNameSyntax SqlSyntaxBuilder,
  Sql92UpdateExpressionSyntax SqlSyntaxBuilder)]
es)) forall a. Semigroup a => a -> a -> a
<>
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqlSyntaxBuilder
where_ -> ByteString -> Builder
byteString ByteString
" WHERE " forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
where_) Maybe (Sql92UpdateExpressionSyntax SqlSyntaxBuilder)
where_

instance IsSql92DeleteSyntax SqlSyntaxBuilder where
  type Sql92DeleteExpressionSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92DeleteTableNameSyntax SqlSyntaxBuilder = SqlSyntaxBuilder

  deleteStmt :: Sql92DeleteTableNameSyntax SqlSyntaxBuilder
-> Maybe Text
-> Maybe (Sql92DeleteExpressionSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder
deleteStmt Sql92DeleteTableNameSyntax SqlSyntaxBuilder
tblNm Maybe Text
alias Maybe (Sql92DeleteExpressionSyntax SqlSyntaxBuilder)
where_ =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString ByteString
"DELETE FROM " forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql Sql92DeleteTableNameSyntax SqlSyntaxBuilder
tblNm forall a. Semigroup a => a -> a -> a
<>
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\Text
alias_ -> ByteString -> Builder
byteString ByteString
" AS " forall a. Semigroup a => a -> a -> a
<> Text -> Builder
quoteSql Text
alias_) Maybe Text
alias forall a. Semigroup a => a -> a -> a
<>
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqlSyntaxBuilder
where_ -> ByteString -> Builder
byteString ByteString
" WHERE " forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
where_) Maybe (Sql92DeleteExpressionSyntax SqlSyntaxBuilder)
where_

  deleteSupportsAlias :: Proxy SqlSyntaxBuilder -> Bool
deleteSupportsAlias Proxy SqlSyntaxBuilder
_ = Bool
True

instance IsSql92FieldNameSyntax SqlSyntaxBuilder where
  qualifiedField :: Text -> Text -> SqlSyntaxBuilder
qualifiedField Text
a Text
b =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString ByteString
"`" forall a. Semigroup a => a -> a -> a
<> String -> Builder
stringUtf8 (Text -> String
T.unpack Text
a) forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
"`.`" forall a. Semigroup a => a -> a -> a
<>
    String -> Builder
stringUtf8 (Text -> String
T.unpack Text
b) forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
"`"
  unqualifiedField :: Text -> SqlSyntaxBuilder
unqualifiedField Text
a =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString ByteString
"`" forall a. Semigroup a => a -> a -> a
<> String -> Builder
stringUtf8 (Text -> String
T.unpack Text
a) forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
"`"

instance IsSql92QuantifierSyntax SqlSyntaxBuilder where
  quantifyOverAll :: SqlSyntaxBuilder
quantifyOverAll = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder Builder
"ALL"
  quantifyOverAny :: SqlSyntaxBuilder
quantifyOverAny = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder Builder
"ANY"

instance IsSql92ExtractFieldSyntax SqlSyntaxBuilder where
  secondsField :: SqlSyntaxBuilder
secondsField = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"SECOND")
  minutesField :: SqlSyntaxBuilder
minutesField = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"MINUTE")
  hourField :: SqlSyntaxBuilder
hourField    = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"HOUR")
  dayField :: SqlSyntaxBuilder
dayField     = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"DAY")
  monthField :: SqlSyntaxBuilder
monthField   = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"MONTH")
  yearField :: SqlSyntaxBuilder
yearField    = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"YEAR")

instance IsSql92ExpressionSyntax SqlSyntaxBuilder where
  type Sql92ExpressionValueSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92ExpressionSelectSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92ExpressionFieldNameSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92ExpressionQuantifierSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92ExpressionCastTargetSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92ExpressionExtractFieldSyntax SqlSyntaxBuilder = SqlSyntaxBuilder

  rowE :: [SqlSyntaxBuilder] -> SqlSyntaxBuilder
rowE [SqlSyntaxBuilder]
vs = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
            ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<>
            Builder -> [Builder] -> Builder
buildSepBy (ByteString -> Builder
byteString ByteString
", ") (forall a b. (a -> b) -> [a] -> [b]
map SqlSyntaxBuilder -> Builder
buildSql (coerce :: forall a b. Coercible a b => a -> b
coerce [SqlSyntaxBuilder]
vs)) forall a. Semigroup a => a -> a -> a
<>
            ByteString -> Builder
byteString ByteString
")"
  isNotNullE :: SqlSyntaxBuilder -> SqlSyntaxBuilder
isNotNullE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlPostFixOp ByteString
"IS NOT NULL"
  isNullE :: SqlSyntaxBuilder -> SqlSyntaxBuilder
isNullE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlPostFixOp ByteString
"IS NULL"
  isTrueE :: SqlSyntaxBuilder -> SqlSyntaxBuilder
isTrueE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlPostFixOp ByteString
"IS TRUE"
  isFalseE :: SqlSyntaxBuilder -> SqlSyntaxBuilder
isFalseE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlPostFixOp ByteString
"IS FALSE"
  isUnknownE :: SqlSyntaxBuilder -> SqlSyntaxBuilder
isUnknownE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlPostFixOp ByteString
"IS UNKNOWN"
  isNotTrueE :: SqlSyntaxBuilder -> SqlSyntaxBuilder
isNotTrueE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlPostFixOp ByteString
"IS NOT TRUE"
  isNotFalseE :: SqlSyntaxBuilder -> SqlSyntaxBuilder
isNotFalseE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlPostFixOp ByteString
"IS NOT FALSE"
  isNotUnknownE :: SqlSyntaxBuilder -> SqlSyntaxBuilder
isNotUnknownE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlPostFixOp ByteString
"IS NOT UNKNOWN"
  caseE :: [(SqlSyntaxBuilder, SqlSyntaxBuilder)]
-> SqlSyntaxBuilder -> SqlSyntaxBuilder
caseE [(SqlSyntaxBuilder, SqlSyntaxBuilder)]
cases SqlSyntaxBuilder
else_ =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString ByteString
"CASE " forall a. Semigroup a => a -> a -> a
<>
    forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\(SqlSyntaxBuilder
cond, SqlSyntaxBuilder
res) -> ByteString -> Builder
byteString ByteString
"WHEN " forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
cond forall a. Semigroup a => a -> a -> a
<>
                             ByteString -> Builder
byteString ByteString
" THEN " forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
res forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
" ") [(SqlSyntaxBuilder, SqlSyntaxBuilder)]
cases forall a. Semigroup a => a -> a -> a
<>
    ByteString -> Builder
byteString ByteString
"ELSE " forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
else_ forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
" END"
  fieldE :: Sql92ExpressionFieldNameSyntax SqlSyntaxBuilder -> SqlSyntaxBuilder
fieldE = forall a. a -> a
id

  nullIfE :: SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
nullIfE SqlSyntaxBuilder
a SqlSyntaxBuilder
b = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlFuncOp ByteString
"NULLIF" forall a b. (a -> b) -> a -> b
$
                Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
                ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
a forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
"), (" forall a. Semigroup a => a -> a -> a
<>
                SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
b forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")"
  positionE :: SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
positionE SqlSyntaxBuilder
needle SqlSyntaxBuilder
haystack =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$ ByteString -> Builder
byteString ByteString
"POSITION(" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
needle forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
") IN (" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
haystack forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")"
  extractE :: Sql92ExpressionExtractFieldSyntax SqlSyntaxBuilder
-> SqlSyntaxBuilder -> SqlSyntaxBuilder
extractE Sql92ExpressionExtractFieldSyntax SqlSyntaxBuilder
what SqlSyntaxBuilder
from =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$ ByteString -> Builder
byteString ByteString
"EXTRACT(" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql Sql92ExpressionExtractFieldSyntax SqlSyntaxBuilder
what forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
" FROM (" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
from forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
"))"
  absE :: SqlSyntaxBuilder -> SqlSyntaxBuilder
absE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlFuncOp ByteString
"ABS"
  charLengthE :: SqlSyntaxBuilder -> SqlSyntaxBuilder
charLengthE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlFuncOp ByteString
"CHAR_LENGTH"
  bitLengthE :: SqlSyntaxBuilder -> SqlSyntaxBuilder
bitLengthE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlFuncOp ByteString
"BIT_LENGTH"
  octetLengthE :: SqlSyntaxBuilder -> SqlSyntaxBuilder
octetLengthE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlFuncOp ByteString
"OCTET_LENGTH"
  lowerE :: SqlSyntaxBuilder -> SqlSyntaxBuilder
lowerE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlFuncOp ByteString
"LOWER"
  upperE :: SqlSyntaxBuilder -> SqlSyntaxBuilder
upperE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlFuncOp ByteString
"UPPER"
  trimE :: SqlSyntaxBuilder -> SqlSyntaxBuilder
trimE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlFuncOp ByteString
"TRIM"

  addE :: SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
addE = ByteString
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlBinOp ByteString
"+"
  likeE :: SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
likeE = ByteString
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlBinOp ByteString
"LIKE"
  subE :: SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
subE = ByteString
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlBinOp ByteString
"-"
  mulE :: SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
mulE = ByteString
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlBinOp ByteString
"*"
  divE :: SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
divE = ByteString
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlBinOp ByteString
"/"
  modE :: SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
modE = ByteString
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlBinOp ByteString
"%"
  overlapsE :: SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
overlapsE = ByteString
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlBinOp ByteString
"OVERLAPS"
  andE :: SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
andE = ByteString
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlBinOp ByteString
"AND"
  orE :: SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
orE  = ByteString
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlBinOp ByteString
"OR"
  castE :: SqlSyntaxBuilder
-> Sql92ExpressionCastTargetSyntax SqlSyntaxBuilder
-> SqlSyntaxBuilder
castE SqlSyntaxBuilder
a Sql92ExpressionCastTargetSyntax SqlSyntaxBuilder
ty = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString ByteString
"CAST((" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
a forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
") AS " forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql Sql92ExpressionCastTargetSyntax SqlSyntaxBuilder
ty forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")"
  coalesceE :: [SqlSyntaxBuilder] -> SqlSyntaxBuilder
coalesceE [SqlSyntaxBuilder]
es = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString ByteString
"COALESCE(" forall a. Semigroup a => a -> a -> a
<>
    Builder -> [Builder] -> Builder
buildSepBy (ByteString -> Builder
byteString ByteString
", ") (forall a b. (a -> b) -> [a] -> [b]
map (\SqlSyntaxBuilder
e -> ByteString -> Builder
byteStringByteString
"(" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
e forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")") [SqlSyntaxBuilder]
es) forall a. Semigroup a => a -> a -> a
<>
    ByteString -> Builder
byteString ByteString
")"
  betweenE :: SqlSyntaxBuilder
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
betweenE SqlSyntaxBuilder
a SqlSyntaxBuilder
b SqlSyntaxBuilder
c = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
a forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
") BETWEEN (" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
b forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
") AND (" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
c forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")"
  eqE :: Maybe (Sql92ExpressionQuantifierSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
eqE  = ByteString
-> Maybe SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
sqlCompOp ByteString
"="
  neqE :: Maybe (Sql92ExpressionQuantifierSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
neqE = ByteString
-> Maybe SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
sqlCompOp ByteString
"<>"
  ltE :: Maybe (Sql92ExpressionQuantifierSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
ltE  = ByteString
-> Maybe SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
sqlCompOp ByteString
"<"
  gtE :: Maybe (Sql92ExpressionQuantifierSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
gtE  = ByteString
-> Maybe SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
sqlCompOp ByteString
">"
  leE :: Maybe (Sql92ExpressionQuantifierSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
leE  = ByteString
-> Maybe SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
sqlCompOp ByteString
"<="
  geE :: Maybe (Sql92ExpressionQuantifierSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
geE  = ByteString
-> Maybe SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
sqlCompOp ByteString
">="
  negateE :: SqlSyntaxBuilder -> SqlSyntaxBuilder
negateE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlUnOp ByteString
"-"
  notE :: SqlSyntaxBuilder -> SqlSyntaxBuilder
notE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlUnOp ByteString
"NOT"
  existsE :: Sql92ExpressionSelectSyntax SqlSyntaxBuilder -> SqlSyntaxBuilder
existsE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlUnOp ByteString
"EXISTS"
  uniqueE :: Sql92ExpressionSelectSyntax SqlSyntaxBuilder -> SqlSyntaxBuilder
uniqueE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlUnOp ByteString
"UNIQUE"
  subqueryE :: Sql92ExpressionSelectSyntax SqlSyntaxBuilder -> SqlSyntaxBuilder
subqueryE Sql92ExpressionSelectSyntax SqlSyntaxBuilder
a = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$ ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql Sql92ExpressionSelectSyntax SqlSyntaxBuilder
a forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")"
  valueE :: Sql92ExpressionValueSyntax SqlSyntaxBuilder -> SqlSyntaxBuilder
valueE = forall a. a -> a
id

  currentTimestampE :: SqlSyntaxBuilder
currentTimestampE = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"CURRENT_TIMESTAMP")

  defaultE :: SqlSyntaxBuilder
defaultE = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"DEFAULT")
  inE :: SqlSyntaxBuilder -> [SqlSyntaxBuilder] -> SqlSyntaxBuilder
inE SqlSyntaxBuilder
a [SqlSyntaxBuilder]
es = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
a forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
") IN (" forall a. Semigroup a => a -> a -> a
<>
                               Builder -> [Builder] -> Builder
buildSepBy (ByteString -> Builder
byteString ByteString
", ") (forall a b. (a -> b) -> [a] -> [b]
map SqlSyntaxBuilder -> Builder
buildSql [SqlSyntaxBuilder]
es) forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")")
  inSelectE :: SqlSyntaxBuilder
-> Sql92ExpressionSelectSyntax SqlSyntaxBuilder -> SqlSyntaxBuilder
inSelectE SqlSyntaxBuilder
a Sql92ExpressionSelectSyntax SqlSyntaxBuilder
sel = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
a forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
") IN (" forall a. Semigroup a => a -> a -> a
<>
                                      SqlSyntaxBuilder -> Builder
buildSql Sql92ExpressionSelectSyntax SqlSyntaxBuilder
sel forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")")

instance IsSql99FunctionExpressionSyntax SqlSyntaxBuilder where
  functionNameE :: Text -> SqlSyntaxBuilder
functionNameE Text
fn = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString (Text -> ByteString
TE.encodeUtf8 Text
fn))
  functionCallE :: SqlSyntaxBuilder -> [SqlSyntaxBuilder] -> SqlSyntaxBuilder
functionCallE SqlSyntaxBuilder
function [SqlSyntaxBuilder]
args =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
function forall a. Semigroup a => a -> a -> a
<>
    ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<>
    Builder -> [Builder] -> Builder
buildSepBy (ByteString -> Builder
byteString ByteString
", ") (forall a b. (a -> b) -> [a] -> [b]
map SqlSyntaxBuilder -> Builder
buildSql [SqlSyntaxBuilder]
args) forall a. Semigroup a => a -> a -> a
<>
    ByteString -> Builder
byteString ByteString
")"

instance IsSql99ExpressionSyntax SqlSyntaxBuilder where
  distinctE :: Sql92ExpressionSelectSyntax SqlSyntaxBuilder -> SqlSyntaxBuilder
distinctE = ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlUnOp ByteString
"DISTINCT"
  similarToE :: SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
similarToE = ByteString
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlBinOp ByteString
"SIMILAR TO"

  instanceFieldE :: SqlSyntaxBuilder -> Text -> SqlSyntaxBuilder
instanceFieldE SqlSyntaxBuilder
e Text
fieldNm =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
e forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")." forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString (Text -> ByteString
TE.encodeUtf8 Text
fieldNm)
  refFieldE :: SqlSyntaxBuilder -> Text -> SqlSyntaxBuilder
refFieldE SqlSyntaxBuilder
e Text
fieldNm =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
e forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")->" forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString (Text -> ByteString
TE.encodeUtf8 Text
fieldNm)

instance IsSql2003ExpressionSyntax SqlSyntaxBuilder where
  type Sql2003ExpressionWindowFrameSyntax SqlSyntaxBuilder = SqlSyntaxBuilder

  overE :: SqlSyntaxBuilder
-> Sql2003ExpressionWindowFrameSyntax SqlSyntaxBuilder
-> SqlSyntaxBuilder
overE SqlSyntaxBuilder
expr Sql2003ExpressionWindowFrameSyntax SqlSyntaxBuilder
frame =
      Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
      SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
expr forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql Sql2003ExpressionWindowFrameSyntax SqlSyntaxBuilder
frame
  rowNumberE :: SqlSyntaxBuilder
rowNumberE = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"ROW_NUMBER()")

instance IsSql2003WindowFrameSyntax SqlSyntaxBuilder where
  type Sql2003WindowFrameExpressionSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql2003WindowFrameOrderingSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql2003WindowFrameBoundsSyntax SqlSyntaxBuilder = SqlSyntaxBuilder

  frameSyntax :: Maybe [Sql2003WindowFrameExpressionSyntax SqlSyntaxBuilder]
-> Maybe [Sql2003WindowFrameOrderingSyntax SqlSyntaxBuilder]
-> Maybe (Sql2003WindowFrameBoundsSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder
frameSyntax Maybe [Sql2003WindowFrameExpressionSyntax SqlSyntaxBuilder]
partition_ Maybe [Sql2003WindowFrameOrderingSyntax SqlSyntaxBuilder]
ordering_ Maybe (Sql2003WindowFrameBoundsSyntax SqlSyntaxBuilder)
bounds_ =
      Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
      ByteString -> Builder
byteString ByteString
" OVER (" forall a. Semigroup a => a -> a -> a
<>
      forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\[SqlSyntaxBuilder]
p -> ByteString -> Builder
byteString ByteString
"PARTITION BY " forall a. Semigroup a => a -> a -> a
<> Builder -> [Builder] -> Builder
buildSepBy (ByteString -> Builder
byteString ByteString
", ") (forall a b. (a -> b) -> [a] -> [b]
map SqlSyntaxBuilder -> Builder
buildSql [SqlSyntaxBuilder]
p)) Maybe [Sql2003WindowFrameExpressionSyntax SqlSyntaxBuilder]
partition_ forall a. Semigroup a => a -> a -> a
<>
      forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\[SqlSyntaxBuilder]
o -> ByteString -> Builder
byteString ByteString
" ORDER BY " forall a. Semigroup a => a -> a -> a
<> Builder -> [Builder] -> Builder
buildSepBy (ByteString -> Builder
byteString ByteString
", ") (forall a b. (a -> b) -> [a] -> [b]
map SqlSyntaxBuilder -> Builder
buildSql [SqlSyntaxBuilder]
o)) Maybe [Sql2003WindowFrameOrderingSyntax SqlSyntaxBuilder]
ordering_ forall a. Semigroup a => a -> a -> a
<>
      forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqlSyntaxBuilder
b -> ByteString -> Builder
byteString ByteString
" ROWS " forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
b) Maybe (Sql2003WindowFrameBoundsSyntax SqlSyntaxBuilder)
bounds_ forall a. Semigroup a => a -> a -> a
<>
      ByteString -> Builder
byteString ByteString
")"

instance IsSql2003ExpressionElementaryOLAPOperationsSyntax SqlSyntaxBuilder where
  filterAggE :: SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
filterAggE SqlSyntaxBuilder
agg_ SqlSyntaxBuilder
filter_ =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
agg_ forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
" FILTER (WHERE " forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
filter_ forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")"
  rankAggE :: SqlSyntaxBuilder
rankAggE = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder Builder
"RANK()"

instance IsSql2003ExpressionAdvancedOLAPOperationsSyntax SqlSyntaxBuilder where
  denseRankAggE :: SqlSyntaxBuilder
denseRankAggE = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder Builder
"DENSE_RANK()"
  percentRankAggE :: SqlSyntaxBuilder
percentRankAggE = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder Builder
"PERCENT_RANK()"
  cumeDistAggE :: SqlSyntaxBuilder
cumeDistAggE = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder Builder
"CUME_DIST()"

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

instance IsSql2003WindowFrameBoundsSyntax SqlSyntaxBuilder where
  type Sql2003WindowFrameBoundsBoundSyntax SqlSyntaxBuilder = SqlWindowFrameBound

  fromToBoundSyntax :: Sql2003WindowFrameBoundsBoundSyntax SqlSyntaxBuilder
-> Maybe (Sql2003WindowFrameBoundsBoundSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder
fromToBoundSyntax Sql2003WindowFrameBoundsBoundSyntax SqlSyntaxBuilder
SqlWindowFrameBound
SqlWindowFrameUnbounded Maybe (Sql2003WindowFrameBoundsBoundSyntax SqlSyntaxBuilder)
Nothing = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder Builder
"UNBOUNDED PRECEDING"
  fromToBoundSyntax (SqlWindowFrameBounded Int
0) Maybe (Sql2003WindowFrameBoundsBoundSyntax SqlSyntaxBuilder)
Nothing = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder Builder
"CURRENT ROW"
  fromToBoundSyntax (SqlWindowFrameBounded Int
n) Maybe (Sql2003WindowFrameBoundsBoundSyntax SqlSyntaxBuilder)
Nothing = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Int
n) forall a. Semigroup a => a -> a -> a
<> Builder
" PRECEDING")
  fromToBoundSyntax Sql2003WindowFrameBoundsBoundSyntax SqlSyntaxBuilder
SqlWindowFrameBound
SqlWindowFrameUnbounded (Just Sql2003WindowFrameBoundsBoundSyntax SqlSyntaxBuilder
SqlWindowFrameBound
SqlWindowFrameUnbounded) =
      Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder Builder
"BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING"
  fromToBoundSyntax Sql2003WindowFrameBoundsBoundSyntax SqlSyntaxBuilder
SqlWindowFrameBound
SqlWindowFrameUnbounded (Just (SqlWindowFrameBounded Int
0)) =
      Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder Builder
"BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW"
  fromToBoundSyntax Sql2003WindowFrameBoundsBoundSyntax SqlSyntaxBuilder
SqlWindowFrameBound
SqlWindowFrameUnbounded (Just (SqlWindowFrameBounded Int
n)) =
      Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Builder
"BETWEEN UNBOUNDED PRECEDING AND " forall a. Semigroup a => a -> a -> a
<> forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Int
n) forall a. Semigroup a => a -> a -> a
<> Builder
" FOLLOWING")
  fromToBoundSyntax (SqlWindowFrameBounded Int
0) (Just Sql2003WindowFrameBoundsBoundSyntax SqlSyntaxBuilder
SqlWindowFrameBound
SqlWindowFrameUnbounded) =
      Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder Builder
"BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING"
  fromToBoundSyntax (SqlWindowFrameBounded Int
0) (Just (SqlWindowFrameBounded Int
0)) =
      Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder Builder
"BETWEEN CURRENT ROW AND CURRENT ROW"
  fromToBoundSyntax (SqlWindowFrameBounded Int
0) (Just (SqlWindowFrameBounded Int
n)) =
      Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Builder
"BETWEEN CURRENT ROW AND " forall a. Semigroup a => a -> a -> a
<> forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Int
n) forall a. Semigroup a => a -> a -> a
<> Builder
" FOLLOWING")
  fromToBoundSyntax (SqlWindowFrameBounded Int
n) (Just Sql2003WindowFrameBoundsBoundSyntax SqlSyntaxBuilder
SqlWindowFrameBound
SqlWindowFrameUnbounded) =
      Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Builder
"BETWEEN " forall a. Semigroup a => a -> a -> a
<> forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Int
n) forall a. Semigroup a => a -> a -> a
<> Builder
" PRECEDING AND UNBOUNDED FOLLOWING")
  fromToBoundSyntax (SqlWindowFrameBounded Int
n) (Just (SqlWindowFrameBounded Int
0)) =
      Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Builder
"BETWEEN " forall a. Semigroup a => a -> a -> a
<> forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Int
n) forall a. Semigroup a => a -> a -> a
<> Builder
" PRECEDING AND CURRENT ROW")
  fromToBoundSyntax (SqlWindowFrameBounded Int
n1) (Just (SqlWindowFrameBounded Int
n2)) =
      Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Builder
"BETWEEN " forall a. Semigroup a => a -> a -> a
<> forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Int
n1) forall a. Semigroup a => a -> a -> a
<> Builder
" PRECEDING AND " forall a. Semigroup a => a -> a -> a
<> forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Int
n2) forall a. Semigroup a => a -> a -> a
<> Builder
" FOLLOWING")

instance IsSql2003WindowFrameBoundSyntax SqlWindowFrameBound where
  unboundedSyntax :: SqlWindowFrameBound
unboundedSyntax = SqlWindowFrameBound
SqlWindowFrameUnbounded
  nrowsBoundSyntax :: Int -> SqlWindowFrameBound
nrowsBoundSyntax = Int -> SqlWindowFrameBound
SqlWindowFrameBounded

instance IsSql92AggregationExpressionSyntax SqlSyntaxBuilder where
  type Sql92AggregationSetQuantifierSyntax SqlSyntaxBuilder = SqlSyntaxBuilder

  countAllE :: SqlSyntaxBuilder
countAllE = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"COUNT(*)")
  countE :: Maybe (Sql92AggregationSetQuantifierSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder -> SqlSyntaxBuilder
countE Maybe (Sql92AggregationSetQuantifierSyntax SqlSyntaxBuilder)
q SqlSyntaxBuilder
x = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"COUNT(" forall a. Semigroup a => a -> a -> a
<> forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqlSyntaxBuilder
q' -> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
q' forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
" ") Maybe (Sql92AggregationSetQuantifierSyntax SqlSyntaxBuilder)
q forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
x forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")")
  avgE :: Maybe (Sql92AggregationSetQuantifierSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder -> SqlSyntaxBuilder
avgE Maybe (Sql92AggregationSetQuantifierSyntax SqlSyntaxBuilder)
q SqlSyntaxBuilder
x = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"AVG(" forall a. Semigroup a => a -> a -> a
<>  forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqlSyntaxBuilder
q' -> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
q' forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
" ") Maybe (Sql92AggregationSetQuantifierSyntax SqlSyntaxBuilder)
q forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
x forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")")
  minE :: Maybe (Sql92AggregationSetQuantifierSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder -> SqlSyntaxBuilder
minE Maybe (Sql92AggregationSetQuantifierSyntax SqlSyntaxBuilder)
q SqlSyntaxBuilder
x = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"MIN(" forall a. Semigroup a => a -> a -> a
<> forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqlSyntaxBuilder
q' -> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
q' forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
" ") Maybe (Sql92AggregationSetQuantifierSyntax SqlSyntaxBuilder)
q forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
x forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")")
  maxE :: Maybe (Sql92AggregationSetQuantifierSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder -> SqlSyntaxBuilder
maxE Maybe (Sql92AggregationSetQuantifierSyntax SqlSyntaxBuilder)
q SqlSyntaxBuilder
x = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"MAX(" forall a. Semigroup a => a -> a -> a
<> forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqlSyntaxBuilder
q' -> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
q' forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
" ") Maybe (Sql92AggregationSetQuantifierSyntax SqlSyntaxBuilder)
q forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
x forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")")
  sumE :: Maybe (Sql92AggregationSetQuantifierSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder -> SqlSyntaxBuilder
sumE Maybe (Sql92AggregationSetQuantifierSyntax SqlSyntaxBuilder)
q SqlSyntaxBuilder
x = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"SUM(" forall a. Semigroup a => a -> a -> a
<> forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqlSyntaxBuilder
q' -> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
q' forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
" ") Maybe (Sql92AggregationSetQuantifierSyntax SqlSyntaxBuilder)
q forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
x forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")")

instance IsSql92AggregationSetQuantifierSyntax SqlSyntaxBuilder where
  setQuantifierAll :: SqlSyntaxBuilder
setQuantifierAll = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"ALL")
  setQuantifierDistinct :: SqlSyntaxBuilder
setQuantifierDistinct = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"DISTINCT")

instance IsSql92ProjectionSyntax SqlSyntaxBuilder where
  type Sql92ProjectionExpressionSyntax SqlSyntaxBuilder = SqlSyntaxBuilder

  projExprs :: [(Sql92ProjectionExpressionSyntax SqlSyntaxBuilder, Maybe Text)]
-> SqlSyntaxBuilder
projExprs [(Sql92ProjectionExpressionSyntax SqlSyntaxBuilder, Maybe Text)]
exprs =
      Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
      Builder -> [Builder] -> Builder
buildSepBy (ByteString -> Builder
byteString ByteString
", ")
                 (forall a b. (a -> b) -> [a] -> [b]
map (\(SqlSyntaxBuilder
expr, Maybe Text
nm) -> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
expr forall a. Semigroup a => a -> a -> a
<>
                                      forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\Text
nm -> ByteString -> Builder
byteString ByteString
" AS " forall a. Semigroup a => a -> a -> a
<> Text -> Builder
quoteSql Text
nm) Maybe Text
nm) [(Sql92ProjectionExpressionSyntax SqlSyntaxBuilder, Maybe Text)]
exprs)

instance IsSql92OrderingSyntax SqlSyntaxBuilder where
  type Sql92OrderingExpressionSyntax SqlSyntaxBuilder = SqlSyntaxBuilder

  ascOrdering :: Sql92OrderingExpressionSyntax SqlSyntaxBuilder -> SqlSyntaxBuilder
ascOrdering Sql92OrderingExpressionSyntax SqlSyntaxBuilder
expr = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (SqlSyntaxBuilder -> Builder
buildSql Sql92OrderingExpressionSyntax SqlSyntaxBuilder
expr forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
" ASC")
  descOrdering :: Sql92OrderingExpressionSyntax SqlSyntaxBuilder -> SqlSyntaxBuilder
descOrdering Sql92OrderingExpressionSyntax SqlSyntaxBuilder
expr = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (SqlSyntaxBuilder -> Builder
buildSql Sql92OrderingExpressionSyntax SqlSyntaxBuilder
expr forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
" DESC")

instance IsSql92TableSourceSyntax SqlSyntaxBuilder where
  type Sql92TableSourceTableNameSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92TableSourceSelectSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
  type Sql92TableSourceExpressionSyntax SqlSyntaxBuilder = SqlSyntaxBuilder

  tableNamed :: Sql92TableSourceTableNameSyntax SqlSyntaxBuilder
-> SqlSyntaxBuilder
tableNamed = forall a. a -> a
id
  tableFromSubSelect :: Sql92TableSourceSelectSyntax SqlSyntaxBuilder -> SqlSyntaxBuilder
tableFromSubSelect Sql92TableSourceSelectSyntax SqlSyntaxBuilder
query = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql Sql92TableSourceSelectSyntax SqlSyntaxBuilder
query forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")")
  tableFromValues :: [[Sql92TableSourceExpressionSyntax SqlSyntaxBuilder]]
-> SqlSyntaxBuilder
tableFromValues [[Sql92TableSourceExpressionSyntax SqlSyntaxBuilder]]
vss =
      Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
      ByteString -> Builder
byteString ByteString
"VALUES " forall a. Semigroup a => a -> a -> a
<>
      Builder -> [Builder] -> Builder
buildSepBy (ByteString -> Builder
byteString ByteString
", ")
       (forall a b. (a -> b) -> [a] -> [b]
map (\[SqlSyntaxBuilder]
vs -> ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<>
                    Builder -> [Builder] -> Builder
buildSepBy (ByteString -> Builder
byteString ByteString
", ") (forall a b. (a -> b) -> [a] -> [b]
map SqlSyntaxBuilder -> Builder
buildSql [SqlSyntaxBuilder]
vs) forall a. Semigroup a => a -> a -> a
<>
                    ByteString -> Builder
byteString ByteString
")") [[Sql92TableSourceExpressionSyntax SqlSyntaxBuilder]]
vss)

instance IsSql92TableNameSyntax SqlSyntaxBuilder where
  tableName :: Maybe Text -> Text -> SqlSyntaxBuilder
tableName Maybe Text
Nothing Text
t  = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$ Text -> Builder
quoteSql Text
t
  tableName (Just Text
s) Text
t = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$ Text -> Builder
quoteSql Text
s forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
"." forall a. Semigroup a => a -> a -> a
<> Text -> Builder
quoteSql Text
t

instance IsSql92FromSyntax SqlSyntaxBuilder where
    type Sql92FromTableSourceSyntax SqlSyntaxBuilder = SqlSyntaxBuilder
    type Sql92FromExpressionSyntax SqlSyntaxBuilder = SqlSyntaxBuilder

    fromTable :: Sql92FromTableSourceSyntax SqlSyntaxBuilder
-> Maybe (Text, Maybe [Text]) -> SqlSyntaxBuilder
fromTable Sql92FromTableSourceSyntax SqlSyntaxBuilder
t Maybe (Text, Maybe [Text])
Nothing = Sql92FromTableSourceSyntax SqlSyntaxBuilder
t
    fromTable Sql92FromTableSourceSyntax SqlSyntaxBuilder
t (Just (Text
nm, Maybe [Text]
colNms)) =
        Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (SqlSyntaxBuilder -> Builder
buildSql Sql92FromTableSourceSyntax SqlSyntaxBuilder
t forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
" AS " forall a. Semigroup a => a -> a -> a
<> Text -> Builder
quoteSql Text
nm forall a. Semigroup a => a -> a -> a
<>
                          forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\[Text]
colNms' -> ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<> Builder -> [Builder] -> Builder
buildSepBy (ByteString -> Builder
byteString ByteString
", ") (forall a b. (a -> b) -> [a] -> [b]
map Text -> Builder
quoteSql [Text]
colNms') forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")") Maybe [Text]
colNms)

    innerJoin :: SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> Maybe (Sql92FromExpressionSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder
innerJoin = ByteString
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> Maybe SqlSyntaxBuilder
-> SqlSyntaxBuilder
join ByteString
"INNER JOIN"
    leftJoin :: SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> Maybe (Sql92FromExpressionSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder
leftJoin = ByteString
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> Maybe SqlSyntaxBuilder
-> SqlSyntaxBuilder
join ByteString
"LEFT JOIN"
    rightJoin :: SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> Maybe (Sql92FromExpressionSyntax SqlSyntaxBuilder)
-> SqlSyntaxBuilder
rightJoin = ByteString
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> Maybe SqlSyntaxBuilder
-> SqlSyntaxBuilder
join ByteString
"RIGHT JOIN"

instance IsSql92DataTypeSyntax SqlSyntaxBuilder where
    domainType :: Text -> SqlSyntaxBuilder
domainType Text
nm = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Text -> Builder
quoteSql Text
nm)
    charType :: Maybe Word -> Maybe Text -> SqlSyntaxBuilder
charType Maybe Word
prec Maybe Text
charSet = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Builder
"CHAR" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> Builder
sqlOptPrec Maybe Word
prec forall a. Semigroup a => a -> a -> a
<> Maybe Text -> Builder
sqlOptCharSet Maybe Text
charSet)
    varCharType :: Maybe Word -> Maybe Text -> SqlSyntaxBuilder
varCharType Maybe Word
prec Maybe Text
charSet = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Builder
"VARCHAR" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> Builder
sqlOptPrec Maybe Word
prec forall a. Semigroup a => a -> a -> a
<> Maybe Text -> Builder
sqlOptCharSet Maybe Text
charSet)
    nationalCharType :: Maybe Word -> SqlSyntaxBuilder
nationalCharType Maybe Word
prec = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Builder
"NATIONAL CHAR" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> Builder
sqlOptPrec Maybe Word
prec)
    nationalVarCharType :: Maybe Word -> SqlSyntaxBuilder
nationalVarCharType Maybe Word
prec = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Builder
"NATIONAL CHARACTER VARYING" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> Builder
sqlOptPrec Maybe Word
prec)

    bitType :: Maybe Word -> SqlSyntaxBuilder
bitType Maybe Word
prec = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Builder
"BIT" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> Builder
sqlOptPrec Maybe Word
prec)
    varBitType :: Maybe Word -> SqlSyntaxBuilder
varBitType Maybe Word
prec = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Builder
"BIT VARYING" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> Builder
sqlOptPrec Maybe Word
prec)

    numericType :: Maybe (Word, Maybe Word) -> SqlSyntaxBuilder
numericType Maybe (Word, Maybe Word)
prec = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Builder
"NUMERIC" forall a. Semigroup a => a -> a -> a
<> Maybe (Word, Maybe Word) -> Builder
sqlOptNumericPrec Maybe (Word, Maybe Word)
prec)
    decimalType :: Maybe (Word, Maybe Word) -> SqlSyntaxBuilder
decimalType Maybe (Word, Maybe Word)
prec = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Builder
"DECIMAL" forall a. Semigroup a => a -> a -> a
<> Maybe (Word, Maybe Word) -> Builder
sqlOptNumericPrec Maybe (Word, Maybe Word)
prec)

    intType :: SqlSyntaxBuilder
intType = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder Builder
"INT"
    smallIntType :: SqlSyntaxBuilder
smallIntType = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder Builder
"SMALLINT"

    floatType :: Maybe Word -> SqlSyntaxBuilder
floatType Maybe Word
prec = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Builder
"FLOAT" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> Builder
sqlOptPrec Maybe Word
prec)
    doubleType :: SqlSyntaxBuilder
doubleType = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder Builder
"DOUBLE PRECISION"
    realType :: SqlSyntaxBuilder
realType = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder Builder
"REAL"
    dateType :: SqlSyntaxBuilder
dateType = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder Builder
"DATE"
    timeType :: Maybe Word -> Bool -> SqlSyntaxBuilder
timeType Maybe Word
prec Bool
withTz =
        Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Builder
"TIME" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> Builder
sqlOptPrec Maybe Word
prec forall a. Semigroup a => a -> a -> a
<> if Bool
withTz then Builder
" WITH TIME ZONE" else forall a. Monoid a => a
mempty)
    timestampType :: Maybe Word -> Bool -> SqlSyntaxBuilder
timestampType Maybe Word
prec Bool
withTz =
        Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (Builder
"TIMESTAMP" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> Builder
sqlOptPrec Maybe Word
prec forall a. Semigroup a => a -> a -> a
<> if Bool
withTz then Builder
" WITH TIME ZONE" else forall a. Monoid a => a
mempty)

sqlOptPrec :: Maybe Word -> Builder
sqlOptPrec :: Maybe Word -> Builder
sqlOptPrec Maybe Word
Nothing = forall a. Monoid a => a
mempty
sqlOptPrec (Just Word
x) = Builder
"(" forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString (forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Word
x)) forall a. Semigroup a => a -> a -> a
<> Builder
")"

sqlOptCharSet :: Maybe T.Text -> Builder
sqlOptCharSet :: Maybe Text -> Builder
sqlOptCharSet Maybe Text
Nothing = forall a. Monoid a => a
mempty
sqlOptCharSet (Just Text
cs) = Builder
" CHARACTER SET " forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString (Text -> ByteString
TE.encodeUtf8 Text
cs)

sqlOptNumericPrec :: Maybe (Word, Maybe Word) -> Builder
sqlOptNumericPrec :: Maybe (Word, Maybe Word) -> Builder
sqlOptNumericPrec Maybe (Word, Maybe Word)
Nothing = forall a. Monoid a => a
mempty
sqlOptNumericPrec (Just (Word
prec, Maybe Word
Nothing)) = Maybe Word -> Builder
sqlOptPrec (forall a. a -> Maybe a
Just Word
prec)
sqlOptNumericPrec (Just (Word
prec, Just Word
dec)) = Builder
"(" forall a. Semigroup a => a -> a -> a
<> forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Word
prec) forall a. Semigroup a => a -> a -> a
<> Builder
", " forall a. Semigroup a => a -> a -> a
<> forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Word
dec) forall a. Semigroup a => a -> a -> a
<> Builder
")"

-- TODO These instances are wrong (Text doesn't handle quoting for example)
instance HasSqlValueSyntax SqlSyntaxBuilder Int32 where
  sqlValueSyntax :: Int32 -> SqlSyntaxBuilder
sqlValueSyntax Int32
x = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString (forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Int32
x))
instance HasSqlValueSyntax SqlSyntaxBuilder Bool where
  sqlValueSyntax :: Bool -> SqlSyntaxBuilder
sqlValueSyntax Bool
True = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"TRUE")
  sqlValueSyntax Bool
False = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"FALSE")
instance HasSqlValueSyntax SqlSyntaxBuilder Text where
  sqlValueSyntax :: Text -> SqlSyntaxBuilder
sqlValueSyntax Text
x = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString (forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Text
x))
instance HasSqlValueSyntax SqlSyntaxBuilder SqlNull where
  sqlValueSyntax :: SqlNull -> SqlSyntaxBuilder
sqlValueSyntax SqlNull
_ = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder (ByteString -> Builder
byteString ByteString
"NULL")

instance TypeError (PreferExplicitSize Int Int32) => HasSqlValueSyntax SqlSyntaxBuilder Int where
  sqlValueSyntax :: Int -> SqlSyntaxBuilder
sqlValueSyntax Int
x = Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString (forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Int
x))

renderSql :: SqlSyntaxBuilder -> String
renderSql :: SqlSyntaxBuilder -> String
renderSql (SqlSyntaxBuilder Builder
b) = ByteString -> String
BL.unpack (Builder -> ByteString
toLazyByteString Builder
b)

buildSepBy :: Builder -> [Builder] -> Builder
buildSepBy :: Builder -> [Builder] -> Builder
buildSepBy Builder
_   [] = forall a. Monoid a => a
mempty
buildSepBy Builder
_   [Builder
x] = Builder
x
buildSepBy Builder
sep (Builder
x:[Builder]
xs) = Builder
x forall a. Semigroup a => a -> a -> a
<> Builder
sep forall a. Semigroup a => a -> a -> a
<> Builder -> [Builder] -> Builder
buildSepBy Builder
sep [Builder]
xs

-- TODO actual quoting
quoteSql :: Text -> Builder
quoteSql :: Text -> Builder
quoteSql Text
table =
    ByteString -> Builder
byteString ByteString
"\"" forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString (Text -> ByteString
TE.encodeUtf8 Text
table) forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
"\""

join :: ByteString
     -> SqlSyntaxBuilder -> SqlSyntaxBuilder
     -> Maybe SqlSyntaxBuilder -> SqlSyntaxBuilder
join :: ByteString
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> Maybe SqlSyntaxBuilder
-> SqlSyntaxBuilder
join ByteString
type_ SqlSyntaxBuilder
a SqlSyntaxBuilder
b Maybe SqlSyntaxBuilder
on =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
a forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
" " forall a. Semigroup a => a -> a -> a
<>  ByteString -> Builder
byteString ByteString
type_ forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
" " forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
b forall a. Semigroup a => a -> a -> a
<>
    case Maybe SqlSyntaxBuilder
on of
      Maybe SqlSyntaxBuilder
Nothing -> forall a. Monoid a => a
mempty
      Just SqlSyntaxBuilder
on -> ByteString -> Builder
byteString ByteString
" ON (" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
on forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")"
sqlPostFixOp, sqlUnOp :: ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlUnOp :: ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlUnOp ByteString
op SqlSyntaxBuilder
a =
  Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
  ByteString -> Builder
byteString ByteString
op forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
" (" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
a forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")"
sqlPostFixOp :: ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlPostFixOp ByteString
op SqlSyntaxBuilder
a =
  Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
  ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
a forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
") " forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
op
sqlCompOp :: ByteString -> Maybe SqlSyntaxBuilder
          -> SqlSyntaxBuilder -> SqlSyntaxBuilder
          -> SqlSyntaxBuilder
sqlCompOp :: ByteString
-> Maybe SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
-> SqlSyntaxBuilder
sqlCompOp ByteString
op Maybe SqlSyntaxBuilder
quant SqlSyntaxBuilder
a SqlSyntaxBuilder
b =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
a forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
") " forall a. Semigroup a => a -> a -> a
<>
    ByteString -> Builder
byteString ByteString
op forall a. Semigroup a => a -> a -> a
<>
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqlSyntaxBuilder
quant -> ByteString -> Builder
byteString ByteString
" " forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
quant) Maybe SqlSyntaxBuilder
quant forall a. Semigroup a => a -> a -> a
<>
    ByteString -> Builder
byteString ByteString
" (" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
b forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")"
sqlBinOp :: ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
         -> SqlSyntaxBuilder
sqlBinOp :: ByteString
-> SqlSyntaxBuilder -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlBinOp ByteString
op SqlSyntaxBuilder
a SqlSyntaxBuilder
b =
    Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
    ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
a forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
") " forall a. Semigroup a => a -> a -> a
<>
    ByteString -> Builder
byteString ByteString
op forall a. Semigroup a => a -> a -> a
<>
    ByteString -> Builder
byteString ByteString
" (" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
b forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
")"
sqlFuncOp :: ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlFuncOp :: ByteString -> SqlSyntaxBuilder -> SqlSyntaxBuilder
sqlFuncOp ByteString
fun SqlSyntaxBuilder
a =
  Builder -> SqlSyntaxBuilder
SqlSyntaxBuilder forall a b. (a -> b) -> a -> b
$
  ByteString -> Builder
byteString ByteString
fun forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
"(" forall a. Semigroup a => a -> a -> a
<> SqlSyntaxBuilder -> Builder
buildSql SqlSyntaxBuilder
a forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteStringByteString
")"


-- * Fake 'MonadBeam' instance (for using 'SqlSyntaxBuilder' with migrations mainly)

-- data SqlSyntaxBackend

-- class Trivial a
-- instance Trivial a

newtype SqlSyntaxM a = SqlSyntaxM (IO a)
  deriving (Functor SqlSyntaxM
forall a. a -> SqlSyntaxM a
forall a b. SqlSyntaxM a -> SqlSyntaxM b -> SqlSyntaxM a
forall a b. SqlSyntaxM a -> SqlSyntaxM b -> SqlSyntaxM b
forall a b. SqlSyntaxM (a -> b) -> SqlSyntaxM a -> SqlSyntaxM b
forall a b c.
(a -> b -> c) -> SqlSyntaxM a -> SqlSyntaxM b -> SqlSyntaxM c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. SqlSyntaxM a -> SqlSyntaxM b -> SqlSyntaxM a
$c<* :: forall a b. SqlSyntaxM a -> SqlSyntaxM b -> SqlSyntaxM a
*> :: forall a b. SqlSyntaxM a -> SqlSyntaxM b -> SqlSyntaxM b
$c*> :: forall a b. SqlSyntaxM a -> SqlSyntaxM b -> SqlSyntaxM b
liftA2 :: forall a b c.
(a -> b -> c) -> SqlSyntaxM a -> SqlSyntaxM b -> SqlSyntaxM c
$cliftA2 :: forall a b c.
(a -> b -> c) -> SqlSyntaxM a -> SqlSyntaxM b -> SqlSyntaxM c
<*> :: forall a b. SqlSyntaxM (a -> b) -> SqlSyntaxM a -> SqlSyntaxM b
$c<*> :: forall a b. SqlSyntaxM (a -> b) -> SqlSyntaxM a -> SqlSyntaxM b
pure :: forall a. a -> SqlSyntaxM a
$cpure :: forall a. a -> SqlSyntaxM a
Applicative, forall a b. a -> SqlSyntaxM b -> SqlSyntaxM a
forall a b. (a -> b) -> SqlSyntaxM a -> SqlSyntaxM b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> SqlSyntaxM b -> SqlSyntaxM a
$c<$ :: forall a b. a -> SqlSyntaxM b -> SqlSyntaxM a
fmap :: forall a b. (a -> b) -> SqlSyntaxM a -> SqlSyntaxM b
$cfmap :: forall a b. (a -> b) -> SqlSyntaxM a -> SqlSyntaxM b
Functor, Applicative SqlSyntaxM
forall a. a -> SqlSyntaxM a
forall a b. SqlSyntaxM a -> SqlSyntaxM b -> SqlSyntaxM b
forall a b. SqlSyntaxM a -> (a -> SqlSyntaxM b) -> SqlSyntaxM b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> SqlSyntaxM a
$creturn :: forall a. a -> SqlSyntaxM a
>> :: forall a b. SqlSyntaxM a -> SqlSyntaxM b -> SqlSyntaxM b
$c>> :: forall a b. SqlSyntaxM a -> SqlSyntaxM b -> SqlSyntaxM b
>>= :: forall a b. SqlSyntaxM a -> (a -> SqlSyntaxM b) -> SqlSyntaxM b
$c>>= :: forall a b. SqlSyntaxM a -> (a -> SqlSyntaxM b) -> SqlSyntaxM b
Monad, Monad SqlSyntaxM
forall a. IO a -> SqlSyntaxM a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
liftIO :: forall a. IO a -> SqlSyntaxM a
$cliftIO :: forall a. IO a -> SqlSyntaxM a
MonadIO, Monad SqlSyntaxM
forall a. String -> SqlSyntaxM a
forall (m :: * -> *).
Monad m -> (forall a. String -> m a) -> MonadFail m
fail :: forall a. String -> SqlSyntaxM a
$cfail :: forall a. String -> SqlSyntaxM a
Fail.MonadFail)