{-# OPTIONS_GHC -fno-warn-unused-binds -fno-warn-name-shadowing#-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE CPP #-}

-- | SQLite implementations of the Beam SQL syntax classes
--
-- The SQLite command syntax is implemented by 'SQLiteCommandSyntax'.
module Database.Beam.Sqlite.Syntax
  (  -- * SQLite syntaxes
    SqliteSyntax(..)

  , SqliteCommandSyntax(..)

  , SqliteSelectSyntax(..), SqliteInsertSyntax(..)
  , SqliteUpdateSyntax(..), SqliteDeleteSyntax(..)

  , SqliteOnConflictSyntax(..)
  , SqliteInsertValuesSyntax(..)
  , SqliteColumnSchemaSyntax(..)
  , SqliteExpressionSyntax(..), SqliteValueSyntax(..)
  , SqliteTableNameSyntax(..)
  , SqliteFieldNameSyntax(..)
  , SqliteAggregationSetQuantifierSyntax(..)

  , fromSqliteExpression

    -- * SQLite data type syntax
  , SqliteDataTypeSyntax(..)
  , sqliteTextType, sqliteBlobType
  , sqliteBigIntType, sqliteSerialType

    -- * Building and consuming 'SqliteSyntax'
  , fromSqliteCommand, formatSqliteInsert, formatSqliteInsertOnConflict

  , emit, emitValue, parens, commas, quotedIdentifier

  , sqliteEscape, withPlaceholders
  , sqliteRenderSyntaxScript
  ) where

import           Database.Beam.Backend.Internal.Compat
import           Database.Beam.Backend.SQL
import           Database.Beam.Backend.SQL.AST (ExtractField(..))
import           Database.Beam.Haskell.Syntax
import           Database.Beam.Migrate.Checks (HasDataTypeCreatedCheck(..))
import           Database.Beam.Migrate.SQL.Builder hiding (fromSqlConstraintAttributes)
import           Database.Beam.Migrate.SQL.SQL92
import           Database.Beam.Migrate.Serialization
import           Database.Beam.Query hiding (ExtractField(..))

import           Data.ByteString (ByteString)
import qualified Data.ByteString as B
import           Data.ByteString.Builder
import qualified Data.ByteString.Lazy.Char8 as BL
import           Data.Coerce
import qualified Data.DList as DL
import           Data.Hashable
import           Data.Int
import           Data.Maybe
import           Data.Scientific
import           Data.String
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import qualified Data.Text.Lazy as TL
import           Data.Time
import           Data.Word
#if !MIN_VERSION_base(4, 11, 0)
import           Data.Semigroup
#endif
import           GHC.TypeLits

import           Database.SQLite.Simple (SQLData(..))
import           Database.SQLite.Simple.ToField (toField)

import           GHC.Float
import           GHC.Generics

-- | The syntax for SQLite is stored as a 'Builder' along with a list of data
-- that hasn't been serialized yet.
--
-- The first argument is a function that receives a builder for 'SQLData' and
-- returns the concrete syntax to embed into the query. For queries sent to the
-- backend, this is simply a function that returns @"?"@. Thus, the syntax sent
-- to the backend includes proper placeholders. The list of data is sent to the
-- SQLite library for proper escaping.
--
-- When the syntax is being serialized for display (for use in beam migrate for
-- example), the data builder attempts to properly format and escape the data.
-- This returns syntax suitable for inclusion in scripts. In this case, the
-- value list is ignored.
data SqliteSyntax = SqliteSyntax ((SQLData -> Builder) -> Builder) (DL.DList SQLData)
newtype SqliteData = SqliteData SQLData -- newtype for Hashable
  deriving SqliteData -> SqliteData -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SqliteData -> SqliteData -> Bool
$c/= :: SqliteData -> SqliteData -> Bool
== :: SqliteData -> SqliteData -> Bool
$c== :: SqliteData -> SqliteData -> Bool
Eq

instance Show SqliteSyntax where
  show :: SqliteSyntax -> String
show (SqliteSyntax (SQLData -> Builder) -> Builder
s DList SQLData
d) =
    String
"SqliteSyntax (" forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show (Builder -> ByteString
toLazyByteString (((SQLData -> Builder) -> Builder) -> Builder
withPlaceholders (SQLData -> Builder) -> Builder
s)) forall a. Semigroup a => a -> a -> a
<> String
") " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show DList SQLData
d

instance Sql92DisplaySyntax SqliteSyntax where
  displaySyntax :: SqliteSyntax -> String
displaySyntax = ByteString -> String
BL.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteSyntax -> ByteString
sqliteRenderSyntaxScript

instance Semigroup SqliteSyntax where
  <> :: SqliteSyntax -> SqliteSyntax -> SqliteSyntax
(<>) = forall a. Monoid a => a -> a -> a
mappend

instance Monoid SqliteSyntax where
  mempty :: SqliteSyntax
mempty = ((SQLData -> Builder) -> Builder) -> DList SQLData -> SqliteSyntax
SqliteSyntax (\SQLData -> Builder
_ -> forall a. Monoid a => a
mempty) forall a. Monoid a => a
mempty
  mappend :: SqliteSyntax -> SqliteSyntax -> SqliteSyntax
mappend (SqliteSyntax (SQLData -> Builder) -> Builder
ab DList SQLData
av) (SqliteSyntax (SQLData -> Builder) -> Builder
bb DList SQLData
bv) =
    ((SQLData -> Builder) -> Builder) -> DList SQLData -> SqliteSyntax
SqliteSyntax (\SQLData -> Builder
v -> (SQLData -> Builder) -> Builder
ab SQLData -> Builder
v forall a. Semigroup a => a -> a -> a
<> (SQLData -> Builder) -> Builder
bb SQLData -> Builder
v) (DList SQLData
av forall a. Semigroup a => a -> a -> a
<> DList SQLData
bv)

instance Eq SqliteSyntax where
  SqliteSyntax (SQLData -> Builder) -> Builder
ab DList SQLData
av == :: SqliteSyntax -> SqliteSyntax -> Bool
== SqliteSyntax (SQLData -> Builder) -> Builder
bb DList SQLData
bv =
    Builder -> ByteString
toLazyByteString (((SQLData -> Builder) -> Builder) -> Builder
withPlaceholders (SQLData -> Builder) -> Builder
ab) forall a. Eq a => a -> a -> Bool
==
      Builder -> ByteString
toLazyByteString (((SQLData -> Builder) -> Builder) -> Builder
withPlaceholders (SQLData -> Builder) -> Builder
bb) Bool -> Bool -> Bool
&&
    DList SQLData
av forall a. Eq a => a -> a -> Bool
== DList SQLData
bv

instance Hashable SqliteSyntax where
  hashWithSalt :: Int -> SqliteSyntax -> Int
hashWithSalt Int
salt (SqliteSyntax (SQLData -> Builder) -> Builder
s DList SQLData
d) =
      forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt ( Builder -> ByteString
toLazyByteString (((SQLData -> Builder) -> Builder) -> Builder
withPlaceholders (SQLData -> Builder) -> Builder
s)
                        , forall a b. (a -> b) -> [a] -> [b]
map SQLData -> SqliteData
SqliteData (forall a. DList a -> [a]
DL.toList DList SQLData
d) )
instance Hashable SqliteData where
  hashWithSalt :: Int -> SqliteData -> Int
hashWithSalt Int
salt (SqliteData (SQLInteger Int64
i)) = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (Int
0 :: Int, Int64
i)
  hashWithSalt Int
salt (SqliteData (SQLFloat Double
d))   = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (Int
1 :: Int, Double
d)
  hashWithSalt Int
salt (SqliteData (SQLText Text
t))    = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (Int
2 :: Int, Text
t)
  hashWithSalt Int
salt (SqliteData (SQLBlob ByteString
b))    = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (Int
3 :: Int, ByteString
b)
  hashWithSalt Int
salt (SqliteData SQLData
SQLNull)        = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (Int
4 :: Int)

-- | Convert the first argument of 'SQLiteSyntax' to a 'ByteString' 'Builder',
-- where all the data has been replaced by @"?"@ placeholders.
withPlaceholders :: ((SQLData -> Builder) -> Builder) -> Builder
withPlaceholders :: ((SQLData -> Builder) -> Builder) -> Builder
withPlaceholders (SQLData -> Builder) -> Builder
build = (SQLData -> Builder) -> Builder
build (\SQLData
_ -> Builder
"?")

-- | Embed a 'ByteString' directly in the syntax
emit :: ByteString -> SqliteSyntax
emit :: ByteString -> SqliteSyntax
emit ByteString
b = ((SQLData -> Builder) -> Builder) -> DList SQLData -> SqliteSyntax
SqliteSyntax (\SQLData -> Builder
_ -> ByteString -> Builder
byteString ByteString
b) forall a. Monoid a => a
mempty

emit' :: Show a => a -> SqliteSyntax
emit' :: forall a. Show a => a -> SqliteSyntax
emit' a
x = ((SQLData -> Builder) -> Builder) -> DList SQLData -> SqliteSyntax
SqliteSyntax (\SQLData -> Builder
_ -> ByteString -> Builder
byteString (forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show a
x))) forall a. Monoid a => a
mempty

quotedIdentifier :: T.Text -> SqliteSyntax
quotedIdentifier :: Text -> SqliteSyntax
quotedIdentifier Text
txt = ByteString -> SqliteSyntax
emit ByteString
"\"" forall a. Semigroup a => a -> a -> a
<> ((SQLData -> Builder) -> Builder) -> DList SQLData -> SqliteSyntax
SqliteSyntax (\SQLData -> Builder
_ -> String -> Builder
stringUtf8 (Text -> String
T.unpack (Text -> Text
sqliteEscape Text
txt))) forall a. Monoid a => a
mempty forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
"\""

-- | A best effort attempt to implement the escaping rules of SQLite. This is
-- never used to escape data sent to the database; only for emitting scripts or
-- displaying syntax to the user.
sqliteEscape :: T.Text -> T.Text
sqliteEscape :: Text -> Text
sqliteEscape = (Char -> Text) -> Text -> Text
T.concatMap (\Char
c -> if Char
c forall a. Eq a => a -> a -> Bool
== Char
'"' then Text
"\"\"" else Char -> Text
T.singleton Char
c)

-- | Emit a properly escaped value into the syntax
--
-- This causes a literal @?@ 3
emitValue ::  SQLData -> SqliteSyntax
emitValue :: SQLData -> SqliteSyntax
emitValue SQLData
v = ((SQLData -> Builder) -> Builder) -> DList SQLData -> SqliteSyntax
SqliteSyntax (forall a b. (a -> b) -> a -> b
$ SQLData
v) (forall a. a -> DList a
DL.singleton SQLData
v)

-- | Render a 'SqliteSyntax' as a lazy 'BL.ByteString', for purposes of
-- displaying to a user. Embedded 'SQLData' is directly embedded into the
-- concrete syntax, with a best effort made to escape strings.
sqliteRenderSyntaxScript :: SqliteSyntax -> BL.ByteString
sqliteRenderSyntaxScript :: SqliteSyntax -> ByteString
sqliteRenderSyntaxScript (SqliteSyntax (SQLData -> Builder) -> Builder
s DList SQLData
_) =
    Builder -> ByteString
toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SQLData -> Builder) -> Builder
s forall a b. (a -> b) -> a -> b
$ \case
      SQLInteger Int64
i -> Int64 -> Builder
int64Dec Int64
i
      SQLFloat   Double
d -> Double -> Builder
doubleDec Double
d
      SQLText    Text
t -> Text -> Builder
TE.encodeUtf8Builder (Text -> Text
sqliteEscape Text
t)
      SQLBlob    ByteString
b -> Char -> Builder
char8 Char
'X' forall a. Semigroup a => a -> a -> a
<> Char -> Builder
char8 Char
'\'' forall a. Semigroup a => a -> a -> a
<>
                      forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Word8 -> Builder
word8Hex (ByteString -> [Word8]
B.unpack ByteString
b) forall a. Semigroup a => a -> a -> a
<>
                      Char -> Builder
char8 Char
'\''
      SQLData
SQLNull      -> Builder
"NULL"

-- * Syntax types

-- | A SQLite command. @INSERT@ is special cased to handle @AUTO INCREMENT@
-- columns. The 'fromSqliteCommand' function will take an 'SqliteCommandSyntax'
-- and convert it into the correct 'SqliteSyntax'.
data SqliteCommandSyntax
  = SqliteCommandSyntax SqliteSyntax
  | SqliteCommandInsert SqliteInsertSyntax

-- | Convert a 'SqliteCommandSyntax' into a renderable 'SqliteSyntax'
fromSqliteCommand :: SqliteCommandSyntax -> SqliteSyntax
fromSqliteCommand :: SqliteCommandSyntax -> SqliteSyntax
fromSqliteCommand (SqliteCommandSyntax SqliteSyntax
s) = SqliteSyntax
s
fromSqliteCommand (SqliteCommandInsert (SqliteInsertSyntax SqliteTableNameSyntax
tbl [Text]
fields SqliteInsertValuesSyntax
values Maybe SqliteOnConflictSyntax
onConflict)) =
    SqliteTableNameSyntax
-> [Text]
-> SqliteInsertValuesSyntax
-> Maybe SqliteOnConflictSyntax
-> SqliteSyntax
formatSqliteInsertOnConflict SqliteTableNameSyntax
tbl [Text]
fields SqliteInsertValuesSyntax
values Maybe SqliteOnConflictSyntax
onConflict

-- | SQLite @SELECT@ syntax
newtype SqliteSelectSyntax = SqliteSelectSyntax { SqliteSelectSyntax -> SqliteSyntax
fromSqliteSelect :: SqliteSyntax }

-- | SQLite @ON CONFLICT@ syntax
newtype SqliteOnConflictSyntax = SqliteOnConflictSyntax { SqliteOnConflictSyntax -> SqliteSyntax
fromSqliteOnConflict :: SqliteSyntax }

-- | SQLite @INSERT@ syntax. This doesn't directly wrap 'SqliteSyntax' because
-- we need to do some processing on @INSERT@ statements to deal with @AUTO
-- INCREMENT@ columns. Use 'formatSqliteInsert' to turn 'SqliteInsertSyntax'
-- into 'SqliteSyntax'.
data SqliteInsertSyntax
  = SqliteInsertSyntax
  { SqliteInsertSyntax -> SqliteTableNameSyntax
sqliteInsertTable      :: !SqliteTableNameSyntax
  , SqliteInsertSyntax -> [Text]
sqliteInsertFields     :: [ T.Text ]
  , SqliteInsertSyntax -> SqliteInsertValuesSyntax
sqliteInsertValues     :: !SqliteInsertValuesSyntax
  , SqliteInsertSyntax -> Maybe SqliteOnConflictSyntax
sqliteInsertOnConflict :: !(Maybe SqliteOnConflictSyntax)
  }

-- | SQLite @UPDATE@ syntax
newtype SqliteUpdateSyntax = SqliteUpdateSyntax { SqliteUpdateSyntax -> SqliteSyntax
fromSqliteUpdate :: SqliteSyntax }
-- | SQLite @DELETE@ syntax
newtype SqliteDeleteSyntax = SqliteDeleteSyntax { SqliteDeleteSyntax -> SqliteSyntax
fromSqliteDelete :: SqliteSyntax }

newtype SqliteSelectTableSyntax = SqliteSelectTableSyntax { SqliteSelectTableSyntax -> SqliteSyntax
fromSqliteSelectTable :: SqliteSyntax }

-- | Implements beam SQL expression syntaxes
data SqliteExpressionSyntax
  = SqliteExpressionSyntax SqliteSyntax
  | SqliteExpressionDefault
  deriving (Int -> SqliteExpressionSyntax -> ShowS
[SqliteExpressionSyntax] -> ShowS
SqliteExpressionSyntax -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SqliteExpressionSyntax] -> ShowS
$cshowList :: [SqliteExpressionSyntax] -> ShowS
show :: SqliteExpressionSyntax -> String
$cshow :: SqliteExpressionSyntax -> String
showsPrec :: Int -> SqliteExpressionSyntax -> ShowS
$cshowsPrec :: Int -> SqliteExpressionSyntax -> ShowS
Show, SqliteExpressionSyntax -> SqliteExpressionSyntax -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SqliteExpressionSyntax -> SqliteExpressionSyntax -> Bool
$c/= :: SqliteExpressionSyntax -> SqliteExpressionSyntax -> Bool
== :: SqliteExpressionSyntax -> SqliteExpressionSyntax -> Bool
$c== :: SqliteExpressionSyntax -> SqliteExpressionSyntax -> Bool
Eq, forall x. Rep SqliteExpressionSyntax x -> SqliteExpressionSyntax
forall x. SqliteExpressionSyntax -> Rep SqliteExpressionSyntax x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep SqliteExpressionSyntax x -> SqliteExpressionSyntax
$cfrom :: forall x. SqliteExpressionSyntax -> Rep SqliteExpressionSyntax x
Generic)
instance Hashable SqliteExpressionSyntax
newtype SqliteFromSyntax = SqliteFromSyntax { SqliteFromSyntax -> SqliteSyntax
fromSqliteFromSyntax :: SqliteSyntax }
newtype SqliteComparisonQuantifierSyntax = SqliteComparisonQuantifierSyntax { SqliteComparisonQuantifierSyntax -> SqliteSyntax
fromSqliteComparisonQuantifier :: SqliteSyntax }
newtype SqliteAggregationSetQuantifierSyntax = SqliteAggregationSetQuantifierSyntax { SqliteAggregationSetQuantifierSyntax -> SqliteSyntax
fromSqliteAggregationSetQuantifier :: SqliteSyntax }
newtype SqliteProjectionSyntax = SqliteProjectionSyntax { SqliteProjectionSyntax -> SqliteSyntax
fromSqliteProjection :: SqliteSyntax }
newtype SqliteGroupingSyntax = SqliteGroupingSyntax { SqliteGroupingSyntax -> SqliteSyntax
fromSqliteGrouping :: SqliteSyntax }
newtype SqliteOrderingSyntax = SqliteOrderingSyntax { SqliteOrderingSyntax -> SqliteSyntax
fromSqliteOrdering :: SqliteSyntax }
-- | SQLite syntax for values that can be embedded in 'SqliteSyntax'
newtype SqliteValueSyntax = SqliteValueSyntax { SqliteValueSyntax -> SqliteSyntax
fromSqliteValue :: SqliteSyntax }
newtype SqliteTableSourceSyntax = SqliteTableSourceSyntax { SqliteTableSourceSyntax -> SqliteSyntax
fromSqliteTableSource :: SqliteSyntax }
newtype SqliteFieldNameSyntax = SqliteFieldNameSyntax { SqliteFieldNameSyntax -> SqliteSyntax
fromSqliteFieldNameSyntax :: SqliteSyntax }

-- | SQLite @VALUES@ clause in @INSERT@. Expressions need to be handled
-- explicitly in order to deal with @DEFAULT@ values and @AUTO INCREMENT@
-- columns.
data SqliteInsertValuesSyntax
  = SqliteInsertExpressions [ [ SqliteExpressionSyntax ] ]
  | SqliteInsertFromSql SqliteSelectSyntax
newtype SqliteCreateTableSyntax = SqliteCreateTableSyntax { SqliteCreateTableSyntax -> SqliteSyntax
fromSqliteCreateTable :: SqliteSyntax }
data SqliteTableOptionsSyntax = SqliteTableOptionsSyntax SqliteSyntax SqliteSyntax

-- | SQLite syntax for column schemas in @CREATE TABLE@ or @ALTER COLUMN ... ADD
-- COLUMN@ statements
data SqliteColumnSchemaSyntax
  = SqliteColumnSchemaSyntax
  { SqliteColumnSchemaSyntax -> SqliteSyntax
fromSqliteColumnSchema :: SqliteSyntax
  , SqliteColumnSchemaSyntax -> Bool
sqliteIsSerialColumn   :: Bool }
  deriving (Int -> SqliteColumnSchemaSyntax -> ShowS
[SqliteColumnSchemaSyntax] -> ShowS
SqliteColumnSchemaSyntax -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SqliteColumnSchemaSyntax] -> ShowS
$cshowList :: [SqliteColumnSchemaSyntax] -> ShowS
show :: SqliteColumnSchemaSyntax -> String
$cshow :: SqliteColumnSchemaSyntax -> String
showsPrec :: Int -> SqliteColumnSchemaSyntax -> ShowS
$cshowsPrec :: Int -> SqliteColumnSchemaSyntax -> ShowS
Show, SqliteColumnSchemaSyntax -> SqliteColumnSchemaSyntax -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SqliteColumnSchemaSyntax -> SqliteColumnSchemaSyntax -> Bool
$c/= :: SqliteColumnSchemaSyntax -> SqliteColumnSchemaSyntax -> Bool
== :: SqliteColumnSchemaSyntax -> SqliteColumnSchemaSyntax -> Bool
$c== :: SqliteColumnSchemaSyntax -> SqliteColumnSchemaSyntax -> Bool
Eq, forall x.
Rep SqliteColumnSchemaSyntax x -> SqliteColumnSchemaSyntax
forall x.
SqliteColumnSchemaSyntax -> Rep SqliteColumnSchemaSyntax x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep SqliteColumnSchemaSyntax x -> SqliteColumnSchemaSyntax
$cfrom :: forall x.
SqliteColumnSchemaSyntax -> Rep SqliteColumnSchemaSyntax x
Generic)
instance Hashable SqliteColumnSchemaSyntax

instance Sql92DisplaySyntax SqliteColumnSchemaSyntax where
  displaySyntax :: SqliteColumnSchemaSyntax -> String
displaySyntax = forall syntax. Sql92DisplaySyntax syntax => syntax -> String
displaySyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteColumnSchemaSyntax -> SqliteSyntax
fromSqliteColumnSchema

-- | SQLite syntax that implements 'IsSql92DataTypeSyntax' and a good portion of
-- 'IsSql99DataTypeSyntax', except for array and row types.
data SqliteDataTypeSyntax
  = SqliteDataTypeSyntax
  { SqliteDataTypeSyntax -> SqliteSyntax
fromSqliteDataType :: SqliteSyntax
  , SqliteDataTypeSyntax -> HsDataType
sqliteDataTypeToHs :: HsDataType
  , SqliteDataTypeSyntax -> BeamSerializedDataType
sqliteDataTypeSerialized :: BeamSerializedDataType
  , SqliteDataTypeSyntax -> Bool
sqliteDataTypeSerial :: Bool
  } deriving (Int -> SqliteDataTypeSyntax -> ShowS
[SqliteDataTypeSyntax] -> ShowS
SqliteDataTypeSyntax -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SqliteDataTypeSyntax] -> ShowS
$cshowList :: [SqliteDataTypeSyntax] -> ShowS
show :: SqliteDataTypeSyntax -> String
$cshow :: SqliteDataTypeSyntax -> String
showsPrec :: Int -> SqliteDataTypeSyntax -> ShowS
$cshowsPrec :: Int -> SqliteDataTypeSyntax -> ShowS
Show, SqliteDataTypeSyntax -> SqliteDataTypeSyntax -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SqliteDataTypeSyntax -> SqliteDataTypeSyntax -> Bool
$c/= :: SqliteDataTypeSyntax -> SqliteDataTypeSyntax -> Bool
== :: SqliteDataTypeSyntax -> SqliteDataTypeSyntax -> Bool
$c== :: SqliteDataTypeSyntax -> SqliteDataTypeSyntax -> Bool
Eq, forall x. Rep SqliteDataTypeSyntax x -> SqliteDataTypeSyntax
forall x. SqliteDataTypeSyntax -> Rep SqliteDataTypeSyntax x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep SqliteDataTypeSyntax x -> SqliteDataTypeSyntax
$cfrom :: forall x. SqliteDataTypeSyntax -> Rep SqliteDataTypeSyntax x
Generic)
instance Hashable SqliteDataTypeSyntax where
  hashWithSalt :: Int -> SqliteDataTypeSyntax -> Int
hashWithSalt Int
salt (SqliteDataTypeSyntax SqliteSyntax
s HsDataType
_ BeamSerializedDataType
_ Bool
_) = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt SqliteSyntax
s
instance Sql92DisplaySyntax SqliteDataTypeSyntax where
  displaySyntax :: SqliteDataTypeSyntax -> String
displaySyntax = forall syntax. Sql92DisplaySyntax syntax => syntax -> String
displaySyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteDataTypeSyntax -> SqliteSyntax
fromSqliteDataType

data SqliteColumnConstraintDefinitionSyntax
  = SqliteColumnConstraintDefinitionSyntax
  { SqliteColumnConstraintDefinitionSyntax -> SqliteSyntax
fromSqliteColumnConstraintDefinition :: SqliteSyntax
  , SqliteColumnConstraintDefinitionSyntax
-> BeamSerializedConstraintDefinition
sqliteColumnConstraintDefinitionSerialized :: BeamSerializedConstraintDefinition
  } deriving (Int -> SqliteColumnConstraintDefinitionSyntax -> ShowS
[SqliteColumnConstraintDefinitionSyntax] -> ShowS
SqliteColumnConstraintDefinitionSyntax -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SqliteColumnConstraintDefinitionSyntax] -> ShowS
$cshowList :: [SqliteColumnConstraintDefinitionSyntax] -> ShowS
show :: SqliteColumnConstraintDefinitionSyntax -> String
$cshow :: SqliteColumnConstraintDefinitionSyntax -> String
showsPrec :: Int -> SqliteColumnConstraintDefinitionSyntax -> ShowS
$cshowsPrec :: Int -> SqliteColumnConstraintDefinitionSyntax -> ShowS
Show, SqliteColumnConstraintDefinitionSyntax
-> SqliteColumnConstraintDefinitionSyntax -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SqliteColumnConstraintDefinitionSyntax
-> SqliteColumnConstraintDefinitionSyntax -> Bool
$c/= :: SqliteColumnConstraintDefinitionSyntax
-> SqliteColumnConstraintDefinitionSyntax -> Bool
== :: SqliteColumnConstraintDefinitionSyntax
-> SqliteColumnConstraintDefinitionSyntax -> Bool
$c== :: SqliteColumnConstraintDefinitionSyntax
-> SqliteColumnConstraintDefinitionSyntax -> Bool
Eq)
instance Hashable SqliteColumnConstraintDefinitionSyntax where
  hashWithSalt :: Int -> SqliteColumnConstraintDefinitionSyntax -> Int
hashWithSalt Int
salt (SqliteColumnConstraintDefinitionSyntax SqliteSyntax
s BeamSerializedConstraintDefinition
_) = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt SqliteSyntax
s
instance Sql92DisplaySyntax SqliteColumnConstraintDefinitionSyntax where
  displaySyntax :: SqliteColumnConstraintDefinitionSyntax -> String
displaySyntax = forall syntax. Sql92DisplaySyntax syntax => syntax -> String
displaySyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteColumnConstraintDefinitionSyntax -> SqliteSyntax
fromSqliteColumnConstraintDefinition

data SqliteColumnConstraintSyntax
    = SqliteColumnConstraintSyntax
    { SqliteColumnConstraintSyntax
-> SqlConstraintAttributesBuilder -> SqliteSyntax
fromSqliteColumnConstraint :: SqlConstraintAttributesBuilder -> SqliteSyntax
    , SqliteColumnConstraintSyntax -> BeamSerializedConstraint
sqliteColumnConstraintSerialized :: BeamSerializedConstraint }
data SqliteTableConstraintSyntax
  = SqliteTableConstraintSyntax
  { SqliteTableConstraintSyntax -> SqliteSyntax
fromSqliteTableConstraint :: SqliteSyntax
  , SqliteTableConstraintSyntax -> Maybe [Text]
sqliteTableConstraintPrimaryKey :: Maybe [ T.Text ] }
data SqliteMatchTypeSyntax
    = SqliteMatchTypeSyntax
    { SqliteMatchTypeSyntax -> SqliteSyntax
fromSqliteMatchType :: SqliteSyntax
    , SqliteMatchTypeSyntax -> BeamSerializedMatchType
sqliteMatchTypeSerialized :: BeamSerializedMatchType }
data SqliteReferentialActionSyntax
    = SqliteReferentialActionSyntax
    { SqliteReferentialActionSyntax -> SqliteSyntax
fromSqliteReferentialAction :: SqliteSyntax
    , SqliteReferentialActionSyntax -> BeamSerializedReferentialAction
sqliteReferentialActionSerialized :: BeamSerializedReferentialAction }
newtype SqliteAlterTableSyntax = SqliteAlterTableSyntax { SqliteAlterTableSyntax -> SqliteSyntax
fromSqliteAlterTable :: SqliteSyntax }
newtype SqliteAlterTableActionSyntax = SqliteAlterTableActionSyntax { SqliteAlterTableActionSyntax -> Maybe SqliteSyntax
fromSqliteAlterTableAction :: Maybe SqliteSyntax }
newtype SqliteAlterColumnActionSyntax = SqliteAlterColumnActionSyntax { SqliteAlterColumnActionSyntax -> Maybe SqliteSyntax
fromSqliteAlterColumnAction :: Maybe SqliteSyntax }
newtype SqliteDropTableSyntax = SqliteDropTableSyntax { SqliteDropTableSyntax -> SqliteSyntax
fromSqliteDropTable :: SqliteSyntax }
newtype SqliteTableNameSyntax = SqliteTableNameSyntax { SqliteTableNameSyntax -> SqliteSyntax
fromSqliteTableName :: SqliteSyntax }

fromSqliteExpression :: SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression :: SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression (SqliteExpressionSyntax SqliteSyntax
s) = SqliteSyntax
s
fromSqliteExpression SqliteExpressionSyntax
SqliteExpressionDefault = ByteString -> SqliteSyntax
emit ByteString
"NULL /* DEFAULT */"

sqliteExpressionSerialized :: SqliteExpressionSyntax -> BeamSerializedExpression
sqliteExpressionSerialized :: SqliteExpressionSyntax -> BeamSerializedExpression
sqliteExpressionSerialized = Text -> BeamSerializedExpression
BeamSerializedExpression forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
TE.decodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
BL.toStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                             SqliteSyntax -> ByteString
sqliteRenderSyntaxScript forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression

-- | Format a SQLite @INSERT@ expression for the given table name, fields, and values.
formatSqliteInsert :: SqliteTableNameSyntax -> [ T.Text ] -> SqliteInsertValuesSyntax -> SqliteSyntax
formatSqliteInsert :: SqliteTableNameSyntax
-> [Text] -> SqliteInsertValuesSyntax -> SqliteSyntax
formatSqliteInsert SqliteTableNameSyntax
tblNm [Text]
fields SqliteInsertValuesSyntax
values =
  SqliteTableNameSyntax
-> [Text]
-> SqliteInsertValuesSyntax
-> Maybe SqliteOnConflictSyntax
-> SqliteSyntax
formatSqliteInsertOnConflict SqliteTableNameSyntax
tblNm [Text]
fields SqliteInsertValuesSyntax
values forall a. Maybe a
Nothing

-- | Format a SQLite @INSERT@ expression for the given table name, fields,
-- values, and optionally an @ON CONFLICT@ clause.
formatSqliteInsertOnConflict :: SqliteTableNameSyntax -> [ T.Text ] -> SqliteInsertValuesSyntax -> Maybe SqliteOnConflictSyntax -> SqliteSyntax
formatSqliteInsertOnConflict :: SqliteTableNameSyntax
-> [Text]
-> SqliteInsertValuesSyntax
-> Maybe SqliteOnConflictSyntax
-> SqliteSyntax
formatSqliteInsertOnConflict SqliteTableNameSyntax
tblNm [Text]
fields SqliteInsertValuesSyntax
values Maybe SqliteOnConflictSyntax
onConflict = forall a. Monoid a => [a] -> a
mconcat
  [ ByteString -> SqliteSyntax
emit ByteString
"INSERT INTO "
  , SqliteTableNameSyntax -> SqliteSyntax
fromSqliteTableName SqliteTableNameSyntax
tblNm
  , if forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
fields
      then forall a. Monoid a => a
mempty
      else SqliteSyntax -> SqliteSyntax
parens ([SqliteSyntax] -> SqliteSyntax
commas (forall a b. (a -> b) -> [a] -> [b]
map Text -> SqliteSyntax
quotedIdentifier [Text]
fields))
  , ByteString -> SqliteSyntax
emit ByteString
" "
  , case SqliteInsertValuesSyntax
values of
      SqliteInsertFromSql (SqliteSelectSyntax SqliteSyntax
select) -> SqliteSyntax
select
      -- Because SQLite doesn't support explicit DEFAULT values, if an insert
      -- batch contains any defaults, we split it into a series of single-row
      -- inserts specifying only the non-default columns (which could differ
      -- between rows in the batch). To insert all default values, there is
      -- special DEFAULT VALUES syntax, which only supports one row anyway.
      -- Unfortunately, SQLite doesn't currently support DEFAULT VALUES with ON
      -- CONFLICT. We don't specially catch that in hopes that SQLite will some
      -- day support it, since there is really no reason it shouldn't.
      SqliteInsertExpressions [[]] -> ByteString -> SqliteSyntax
emit ByteString
"DEFAULT VALUES"
      SqliteInsertExpressions [[SqliteExpressionSyntax]]
es ->
        ByteString -> SqliteSyntax
emit ByteString
"VALUES " forall a. Semigroup a => a -> a -> a
<> [SqliteSyntax] -> SqliteSyntax
commas (forall a b. (a -> b) -> [a] -> [b]
map (\[SqliteExpressionSyntax]
row -> SqliteSyntax -> SqliteSyntax
parens ([SqliteSyntax] -> SqliteSyntax
commas (forall a b. (a -> b) -> [a] -> [b]
map SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression [SqliteExpressionSyntax]
row)) ) [[SqliteExpressionSyntax]]
es)
  , forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty ((ByteString -> SqliteSyntax
emit ByteString
" " forall a. Semigroup a => a -> a -> a
<>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteOnConflictSyntax -> SqliteSyntax
fromSqliteOnConflict) Maybe SqliteOnConflictSyntax
onConflict
  ]

instance IsSql92Syntax SqliteCommandSyntax where
  type Sql92SelectSyntax SqliteCommandSyntax = SqliteSelectSyntax
  type Sql92InsertSyntax SqliteCommandSyntax = SqliteInsertSyntax
  type Sql92UpdateSyntax SqliteCommandSyntax = SqliteUpdateSyntax
  type Sql92DeleteSyntax SqliteCommandSyntax = SqliteDeleteSyntax

  selectCmd :: Sql92SelectSyntax SqliteCommandSyntax -> SqliteCommandSyntax
selectCmd = SqliteSyntax -> SqliteCommandSyntax
SqliteCommandSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteSelectSyntax -> SqliteSyntax
fromSqliteSelect
  insertCmd :: Sql92InsertSyntax SqliteCommandSyntax -> SqliteCommandSyntax
insertCmd = SqliteInsertSyntax -> SqliteCommandSyntax
SqliteCommandInsert
  updateCmd :: Sql92UpdateSyntax SqliteCommandSyntax -> SqliteCommandSyntax
updateCmd = SqliteSyntax -> SqliteCommandSyntax
SqliteCommandSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteUpdateSyntax -> SqliteSyntax
fromSqliteUpdate
  deleteCmd :: Sql92DeleteSyntax SqliteCommandSyntax -> SqliteCommandSyntax
deleteCmd = SqliteSyntax -> SqliteCommandSyntax
SqliteCommandSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteDeleteSyntax -> SqliteSyntax
fromSqliteDelete

instance IsSql92DdlCommandSyntax SqliteCommandSyntax where
  type Sql92DdlCommandCreateTableSyntax SqliteCommandSyntax = SqliteCreateTableSyntax
  type Sql92DdlCommandAlterTableSyntax SqliteCommandSyntax  = SqliteAlterTableSyntax
  type Sql92DdlCommandDropTableSyntax SqliteCommandSyntax = SqliteDropTableSyntax

  createTableCmd :: Sql92DdlCommandCreateTableSyntax SqliteCommandSyntax
-> SqliteCommandSyntax
createTableCmd = SqliteSyntax -> SqliteCommandSyntax
SqliteCommandSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteCreateTableSyntax -> SqliteSyntax
fromSqliteCreateTable
  alterTableCmd :: Sql92DdlCommandAlterTableSyntax SqliteCommandSyntax
-> SqliteCommandSyntax
alterTableCmd = SqliteSyntax -> SqliteCommandSyntax
SqliteCommandSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteAlterTableSyntax -> SqliteSyntax
fromSqliteAlterTable
  dropTableCmd :: Sql92DdlCommandDropTableSyntax SqliteCommandSyntax
-> SqliteCommandSyntax
dropTableCmd = SqliteSyntax -> SqliteCommandSyntax
SqliteCommandSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteDropTableSyntax -> SqliteSyntax
fromSqliteDropTable

instance IsSql92TableNameSyntax SqliteTableNameSyntax where
  -- SQLite doesn't have schemas proper, but it does have attached databases, which is what we use here
  tableName :: Maybe Text -> Text -> SqliteTableNameSyntax
tableName Maybe Text
Nothing Text
tbl = SqliteSyntax -> SqliteTableNameSyntax
SqliteTableNameSyntax (Text -> SqliteSyntax
quotedIdentifier Text
tbl)
  tableName (Just Text
sch) Text
tbl = SqliteSyntax -> SqliteTableNameSyntax
SqliteTableNameSyntax (Text -> SqliteSyntax
quotedIdentifier Text
sch forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
"." forall a. Semigroup a => a -> a -> a
<> Text -> SqliteSyntax
quotedIdentifier Text
tbl)

instance IsSql92DropTableSyntax SqliteDropTableSyntax where
  type Sql92DropTableTableNameSyntax SqliteDropTableSyntax = SqliteTableNameSyntax

  dropTableSyntax :: Sql92DropTableTableNameSyntax SqliteDropTableSyntax
-> SqliteDropTableSyntax
dropTableSyntax Sql92DropTableTableNameSyntax SqliteDropTableSyntax
nm = SqliteSyntax -> SqliteDropTableSyntax
SqliteDropTableSyntax (ByteString -> SqliteSyntax
emit ByteString
"DROP TABLE " forall a. Semigroup a => a -> a -> a
<> SqliteTableNameSyntax -> SqliteSyntax
fromSqliteTableName Sql92DropTableTableNameSyntax SqliteDropTableSyntax
nm)

instance IsSql92AlterTableSyntax SqliteAlterTableSyntax where
  type Sql92AlterTableAlterTableActionSyntax SqliteAlterTableSyntax = SqliteAlterTableActionSyntax
  type Sql92AlterTableTableNameSyntax SqliteAlterTableSyntax = SqliteTableNameSyntax

  alterTableSyntax :: Sql92AlterTableTableNameSyntax SqliteAlterTableSyntax
-> Sql92AlterTableAlterTableActionSyntax SqliteAlterTableSyntax
-> SqliteAlterTableSyntax
alterTableSyntax Sql92AlterTableTableNameSyntax SqliteAlterTableSyntax
nm Sql92AlterTableAlterTableActionSyntax SqliteAlterTableSyntax
action =
    SqliteSyntax -> SqliteAlterTableSyntax
SqliteAlterTableSyntax forall a b. (a -> b) -> a -> b
$
    case SqliteAlterTableActionSyntax -> Maybe SqliteSyntax
fromSqliteAlterTableAction Sql92AlterTableAlterTableActionSyntax SqliteAlterTableSyntax
action of
      Just SqliteSyntax
alterTable ->
        ByteString -> SqliteSyntax
emit ByteString
"ALTER TABLE " forall a. Semigroup a => a -> a -> a
<> SqliteTableNameSyntax -> SqliteSyntax
fromSqliteTableName Sql92AlterTableTableNameSyntax SqliteAlterTableSyntax
nm forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" " forall a. Semigroup a => a -> a -> a
<> SqliteSyntax
alterTable
      Maybe SqliteSyntax
Nothing ->
        ByteString -> SqliteSyntax
emit ByteString
"SELECT 1"

instance IsSql92AlterTableActionSyntax SqliteAlterTableActionSyntax where
  type Sql92AlterTableAlterColumnActionSyntax SqliteAlterTableActionSyntax = SqliteAlterColumnActionSyntax
  type Sql92AlterTableColumnSchemaSyntax SqliteAlterTableActionSyntax = SqliteColumnSchemaSyntax

  alterColumnSyntax :: Text
-> Sql92AlterTableAlterColumnActionSyntax
     SqliteAlterTableActionSyntax
-> SqliteAlterTableActionSyntax
alterColumnSyntax Text
columnNm Sql92AlterTableAlterColumnActionSyntax SqliteAlterTableActionSyntax
columnAction =
    Maybe SqliteSyntax -> SqliteAlterTableActionSyntax
SqliteAlterTableActionSyntax forall a b. (a -> b) -> a -> b
$
    case SqliteAlterColumnActionSyntax -> Maybe SqliteSyntax
fromSqliteAlterColumnAction Sql92AlterTableAlterColumnActionSyntax SqliteAlterTableActionSyntax
columnAction of
      Maybe SqliteSyntax
Nothing -> forall a. Maybe a
Nothing
      Just SqliteSyntax
columnAction ->
        forall a. a -> Maybe a
Just (ByteString -> SqliteSyntax
emit ByteString
"ALTER COLUMN " forall a. Semigroup a => a -> a -> a
<> Text -> SqliteSyntax
quotedIdentifier Text
columnNm forall a. Semigroup a => a -> a -> a
<> SqliteSyntax
columnAction)
  addColumnSyntax :: Text
-> Sql92AlterTableColumnSchemaSyntax SqliteAlterTableActionSyntax
-> SqliteAlterTableActionSyntax
addColumnSyntax Text
columnNm Sql92AlterTableColumnSchemaSyntax SqliteAlterTableActionSyntax
schema =
    Maybe SqliteSyntax -> SqliteAlterTableActionSyntax
SqliteAlterTableActionSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$
    ByteString -> SqliteSyntax
emit ByteString
"ADD COLUMN " forall a. Semigroup a => a -> a -> a
<> Text -> SqliteSyntax
quotedIdentifier Text
columnNm forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" " forall a. Semigroup a => a -> a -> a
<> SqliteColumnSchemaSyntax -> SqliteSyntax
fromSqliteColumnSchema Sql92AlterTableColumnSchemaSyntax SqliteAlterTableActionSyntax
schema
  dropColumnSyntax :: Text -> SqliteAlterTableActionSyntax
dropColumnSyntax Text
_ = Maybe SqliteSyntax -> SqliteAlterTableActionSyntax
SqliteAlterTableActionSyntax forall a. Maybe a
Nothing

  renameTableToSyntax :: Text -> SqliteAlterTableActionSyntax
renameTableToSyntax Text
newNm =
    Maybe SqliteSyntax -> SqliteAlterTableActionSyntax
SqliteAlterTableActionSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$
    ByteString -> SqliteSyntax
emit ByteString
"RENAME TO " forall a. Semigroup a => a -> a -> a
<> Text -> SqliteSyntax
quotedIdentifier Text
newNm

  renameColumnToSyntax :: Text -> Text -> SqliteAlterTableActionSyntax
renameColumnToSyntax Text
oldNm Text
newNm =
    Maybe SqliteSyntax -> SqliteAlterTableActionSyntax
SqliteAlterTableActionSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$
    ByteString -> SqliteSyntax
emit ByteString
"RENAME COLUMN " forall a. Semigroup a => a -> a -> a
<> Text -> SqliteSyntax
quotedIdentifier Text
oldNm forall a. Semigroup a => a -> a -> a
<>
    ByteString -> SqliteSyntax
emit ByteString
" TO "           forall a. Semigroup a => a -> a -> a
<> Text -> SqliteSyntax
quotedIdentifier Text
newNm

instance IsSql92AlterColumnActionSyntax SqliteAlterColumnActionSyntax where
  setNotNullSyntax :: SqliteAlterColumnActionSyntax
setNotNullSyntax = Maybe SqliteSyntax -> SqliteAlterColumnActionSyntax
SqliteAlterColumnActionSyntax forall a. Maybe a
Nothing
  setNullSyntax :: SqliteAlterColumnActionSyntax
setNullSyntax = Maybe SqliteSyntax -> SqliteAlterColumnActionSyntax
SqliteAlterColumnActionSyntax forall a. Maybe a
Nothing

instance IsSql92ColumnSchemaSyntax SqliteColumnSchemaSyntax where
  type Sql92ColumnSchemaColumnTypeSyntax SqliteColumnSchemaSyntax = SqliteDataTypeSyntax
  type Sql92ColumnSchemaExpressionSyntax SqliteColumnSchemaSyntax = SqliteExpressionSyntax
  type Sql92ColumnSchemaColumnConstraintDefinitionSyntax SqliteColumnSchemaSyntax = SqliteColumnConstraintDefinitionSyntax

  columnSchemaSyntax :: Sql92ColumnSchemaColumnTypeSyntax SqliteColumnSchemaSyntax
-> Maybe
     (Sql92ColumnSchemaExpressionSyntax SqliteColumnSchemaSyntax)
-> [Sql92ColumnSchemaColumnConstraintDefinitionSyntax
      SqliteColumnSchemaSyntax]
-> Maybe Text
-> SqliteColumnSchemaSyntax
columnSchemaSyntax  Sql92ColumnSchemaColumnTypeSyntax SqliteColumnSchemaSyntax
ty Maybe (Sql92ColumnSchemaExpressionSyntax SqliteColumnSchemaSyntax)
defVal [Sql92ColumnSchemaColumnConstraintDefinitionSyntax
   SqliteColumnSchemaSyntax]
constraints Maybe Text
collation =
    SqliteSyntax -> Bool -> SqliteColumnSchemaSyntax
SqliteColumnSchemaSyntax
      (SqliteDataTypeSyntax -> SqliteSyntax
fromSqliteDataType Sql92ColumnSchemaColumnTypeSyntax SqliteColumnSchemaSyntax
ty forall a. Semigroup a => a -> a -> a
<>
       forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqliteExpressionSyntax
defVal -> ByteString -> SqliteSyntax
emit ByteString
" DEFAULT " forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
defVal)) Maybe (Sql92ColumnSchemaExpressionSyntax SqliteColumnSchemaSyntax)
defVal forall a. Semigroup a => a -> a -> a
<>
       forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\SqliteColumnConstraintDefinitionSyntax
constraint -> ByteString -> SqliteSyntax
emit ByteString
" " forall a. Semigroup a => a -> a -> a
<> SqliteColumnConstraintDefinitionSyntax -> SqliteSyntax
fromSqliteColumnConstraintDefinition SqliteColumnConstraintDefinitionSyntax
constraint forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" ") [Sql92ColumnSchemaColumnConstraintDefinitionSyntax
   SqliteColumnSchemaSyntax]
constraints forall a. Semigroup a => a -> a -> a
<>
       forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\Text
c -> ByteString -> SqliteSyntax
emit ByteString
" COLLATE " forall a. Semigroup a => a -> a -> a
<> Text -> SqliteSyntax
quotedIdentifier Text
c) Maybe Text
collation)
      (if SqliteDataTypeSyntax -> Bool
sqliteDataTypeSerial Sql92ColumnSchemaColumnTypeSyntax SqliteColumnSchemaSyntax
ty then Bool
True else Bool
False)

instance IsSql92ColumnConstraintDefinitionSyntax SqliteColumnConstraintDefinitionSyntax where
  type Sql92ColumnConstraintDefinitionConstraintSyntax SqliteColumnConstraintDefinitionSyntax = SqliteColumnConstraintSyntax
  type Sql92ColumnConstraintDefinitionAttributesSyntax SqliteColumnConstraintDefinitionSyntax = SqlConstraintAttributesBuilder

  constraintDefinitionSyntax :: Maybe Text
-> Sql92ColumnConstraintDefinitionConstraintSyntax
     SqliteColumnConstraintDefinitionSyntax
-> Maybe
     (Sql92ColumnConstraintDefinitionAttributesSyntax
        SqliteColumnConstraintDefinitionSyntax)
-> SqliteColumnConstraintDefinitionSyntax
constraintDefinitionSyntax Maybe Text
nm Sql92ColumnConstraintDefinitionConstraintSyntax
  SqliteColumnConstraintDefinitionSyntax
def Maybe
  (Sql92ColumnConstraintDefinitionAttributesSyntax
     SqliteColumnConstraintDefinitionSyntax)
attrs =
    SqliteSyntax
-> BeamSerializedConstraintDefinition
-> SqliteColumnConstraintDefinitionSyntax
SqliteColumnConstraintDefinitionSyntax
      (forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\Text
nm' -> ByteString -> SqliteSyntax
emit ByteString
"CONSTRAINT " forall a. Semigroup a => a -> a -> a
<> Text -> SqliteSyntax
quotedIdentifier Text
nm') Maybe Text
nm forall a. Semigroup a => a -> a -> a
<>
       SqliteColumnConstraintSyntax
-> SqlConstraintAttributesBuilder -> SqliteSyntax
fromSqliteColumnConstraint Sql92ColumnConstraintDefinitionConstraintSyntax
  SqliteColumnConstraintDefinitionSyntax
def (forall a. a -> Maybe a -> a
fromMaybe forall a. Monoid a => a
mempty Maybe
  (Sql92ColumnConstraintDefinitionAttributesSyntax
     SqliteColumnConstraintDefinitionSyntax)
attrs))
      (forall constraint.
IsSql92ColumnConstraintDefinitionSyntax constraint =>
Maybe Text
-> Sql92ColumnConstraintDefinitionConstraintSyntax constraint
-> Maybe
     (Sql92ColumnConstraintDefinitionAttributesSyntax constraint)
-> constraint
constraintDefinitionSyntax Maybe Text
nm (SqliteColumnConstraintSyntax -> BeamSerializedConstraint
sqliteColumnConstraintSerialized Sql92ColumnConstraintDefinitionConstraintSyntax
  SqliteColumnConstraintDefinitionSyntax
def)
                                     (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SqlConstraintAttributesBuilder
-> BeamSerializedConstraintAttributes
sqlConstraintAttributesSerialized Maybe
  (Sql92ColumnConstraintDefinitionAttributesSyntax
     SqliteColumnConstraintDefinitionSyntax)
attrs))

instance Sql92SerializableConstraintDefinitionSyntax SqliteColumnConstraintDefinitionSyntax where
  serializeConstraint :: SqliteColumnConstraintDefinitionSyntax -> Value
serializeConstraint = BeamSerializedConstraintDefinition -> Value
fromBeamSerializedConstraintDefinition forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteColumnConstraintDefinitionSyntax
-> BeamSerializedConstraintDefinition
sqliteColumnConstraintDefinitionSerialized

instance IsSql92ColumnConstraintSyntax SqliteColumnConstraintSyntax where
  type Sql92ColumnConstraintMatchTypeSyntax SqliteColumnConstraintSyntax = SqliteMatchTypeSyntax
  type Sql92ColumnConstraintReferentialActionSyntax SqliteColumnConstraintSyntax = SqliteReferentialActionSyntax
  type Sql92ColumnConstraintExpressionSyntax SqliteColumnConstraintSyntax = SqliteExpressionSyntax

  notNullConstraintSyntax :: SqliteColumnConstraintSyntax
notNullConstraintSyntax = (SqlConstraintAttributesBuilder -> SqliteSyntax)
-> BeamSerializedConstraint -> SqliteColumnConstraintSyntax
SqliteColumnConstraintSyntax (\SqlConstraintAttributesBuilder
_ -> ByteString -> SqliteSyntax
emit ByteString
"NOT NULL") forall constraint.
IsSql92ColumnConstraintSyntax constraint =>
constraint
notNullConstraintSyntax
  uniqueColumnConstraintSyntax :: SqliteColumnConstraintSyntax
uniqueColumnConstraintSyntax = (SqlConstraintAttributesBuilder -> SqliteSyntax)
-> BeamSerializedConstraint -> SqliteColumnConstraintSyntax
SqliteColumnConstraintSyntax (\SqlConstraintAttributesBuilder
_ -> ByteString -> SqliteSyntax
emit ByteString
"UNIQUE") forall constraint.
IsSql92ColumnConstraintSyntax constraint =>
constraint
uniqueColumnConstraintSyntax
  primaryKeyColumnConstraintSyntax :: SqliteColumnConstraintSyntax
primaryKeyColumnConstraintSyntax = (SqlConstraintAttributesBuilder -> SqliteSyntax)
-> BeamSerializedConstraint -> SqliteColumnConstraintSyntax
SqliteColumnConstraintSyntax (\SqlConstraintAttributesBuilder
_ -> ByteString -> SqliteSyntax
emit ByteString
"PRIMARY KEY") forall constraint.
IsSql92ColumnConstraintSyntax constraint =>
constraint
primaryKeyColumnConstraintSyntax
  checkColumnConstraintSyntax :: Sql92ColumnConstraintExpressionSyntax SqliteColumnConstraintSyntax
-> SqliteColumnConstraintSyntax
checkColumnConstraintSyntax Sql92ColumnConstraintExpressionSyntax SqliteColumnConstraintSyntax
expr =
    (SqlConstraintAttributesBuilder -> SqliteSyntax)
-> BeamSerializedConstraint -> SqliteColumnConstraintSyntax
SqliteColumnConstraintSyntax (\SqlConstraintAttributesBuilder
_ -> ByteString -> SqliteSyntax
emit ByteString
"CHECK " forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression Sql92ColumnConstraintExpressionSyntax SqliteColumnConstraintSyntax
expr))
                                 (forall constraint.
IsSql92ColumnConstraintSyntax constraint =>
Sql92ColumnConstraintExpressionSyntax constraint -> constraint
checkColumnConstraintSyntax (SqliteExpressionSyntax -> BeamSerializedExpression
sqliteExpressionSerialized Sql92ColumnConstraintExpressionSyntax SqliteColumnConstraintSyntax
expr))
  referencesConstraintSyntax :: Text
-> [Text]
-> Maybe
     (Sql92ColumnConstraintMatchTypeSyntax SqliteColumnConstraintSyntax)
-> Maybe
     (Sql92ColumnConstraintReferentialActionSyntax
        SqliteColumnConstraintSyntax)
-> Maybe
     (Sql92ColumnConstraintReferentialActionSyntax
        SqliteColumnConstraintSyntax)
-> SqliteColumnConstraintSyntax
referencesConstraintSyntax Text
tbl [Text]
fields Maybe
  (Sql92ColumnConstraintMatchTypeSyntax SqliteColumnConstraintSyntax)
matchType Maybe
  (Sql92ColumnConstraintReferentialActionSyntax
     SqliteColumnConstraintSyntax)
onUpdate Maybe
  (Sql92ColumnConstraintReferentialActionSyntax
     SqliteColumnConstraintSyntax)
onDelete =
    (SqlConstraintAttributesBuilder -> SqliteSyntax)
-> BeamSerializedConstraint -> SqliteColumnConstraintSyntax
SqliteColumnConstraintSyntax SqlConstraintAttributesBuilder -> SqliteSyntax
sqliteConstraint
                                 (forall constraint.
IsSql92ColumnConstraintSyntax constraint =>
Text
-> [Text]
-> Maybe (Sql92ColumnConstraintMatchTypeSyntax constraint)
-> Maybe (Sql92ColumnConstraintReferentialActionSyntax constraint)
-> Maybe (Sql92ColumnConstraintReferentialActionSyntax constraint)
-> constraint
referencesConstraintSyntax Text
tbl [Text]
fields (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SqliteMatchTypeSyntax -> BeamSerializedMatchType
sqliteMatchTypeSerialized Maybe
  (Sql92ColumnConstraintMatchTypeSyntax SqliteColumnConstraintSyntax)
matchType)
                                                             (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SqliteReferentialActionSyntax -> BeamSerializedReferentialAction
sqliteReferentialActionSerialized Maybe
  (Sql92ColumnConstraintReferentialActionSyntax
     SqliteColumnConstraintSyntax)
onUpdate)
                                                             (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SqliteReferentialActionSyntax -> BeamSerializedReferentialAction
sqliteReferentialActionSerialized Maybe
  (Sql92ColumnConstraintReferentialActionSyntax
     SqliteColumnConstraintSyntax)
onDelete))
    where
      sqliteConstraint :: SqlConstraintAttributesBuilder -> SqliteSyntax
sqliteConstraint (SqlConstraintAttributesBuilder Maybe ConstraintAttributeTiming
atTime Maybe Bool
deferrable) =
        ByteString -> SqliteSyntax
emit ByteString
"REFERENCES " forall a. Semigroup a => a -> a -> a
<> Text -> SqliteSyntax
quotedIdentifier Text
tbl forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens ([SqliteSyntax] -> SqliteSyntax
commas (forall a b. (a -> b) -> [a] -> [b]
map Text -> SqliteSyntax
quotedIdentifier [Text]
fields)) forall a. Semigroup a => a -> a -> a
<>
        forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqliteMatchTypeSyntax
matchType' -> ByteString -> SqliteSyntax
emit ByteString
" MATCH " forall a. Semigroup a => a -> a -> a
<> SqliteMatchTypeSyntax -> SqliteSyntax
fromSqliteMatchType SqliteMatchTypeSyntax
matchType') Maybe
  (Sql92ColumnConstraintMatchTypeSyntax SqliteColumnConstraintSyntax)
matchType forall a. Semigroup a => a -> a -> a
<>
        forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqliteReferentialActionSyntax
onUpdate' -> ByteString -> SqliteSyntax
emit ByteString
" ON UPDATE " forall a. Semigroup a => a -> a -> a
<> SqliteReferentialActionSyntax -> SqliteSyntax
fromSqliteReferentialAction SqliteReferentialActionSyntax
onUpdate') Maybe
  (Sql92ColumnConstraintReferentialActionSyntax
     SqliteColumnConstraintSyntax)
onUpdate forall a. Semigroup a => a -> a -> a
<>
        forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqliteReferentialActionSyntax
onDelete' -> ByteString -> SqliteSyntax
emit ByteString
" ON DELETE " forall a. Semigroup a => a -> a -> a
<> SqliteReferentialActionSyntax -> SqliteSyntax
fromSqliteReferentialAction SqliteReferentialActionSyntax
onDelete') Maybe
  (Sql92ColumnConstraintReferentialActionSyntax
     SqliteColumnConstraintSyntax)
onDelete forall a. Semigroup a => a -> a -> a
<>
        case (Maybe Bool
deferrable, Maybe ConstraintAttributeTiming
atTime) of
          (Maybe Bool
_, Just ConstraintAttributeTiming
atTime) ->
            let deferrable' :: Bool
deferrable' = forall a. a -> Maybe a -> a
fromMaybe Bool
False Maybe Bool
deferrable
            in (if Bool
deferrable' then ByteString -> SqliteSyntax
emit ByteString
" DEFERRABLE " else ByteString -> SqliteSyntax
emit ByteString
" NOT DEFERRABLE ") forall a. Semigroup a => a -> a -> a
<>
               case ConstraintAttributeTiming
atTime of
                 ConstraintAttributeTiming
InitiallyDeferred -> ByteString -> SqliteSyntax
emit ByteString
"INITIALLY DEFERRED"
                 ConstraintAttributeTiming
InitiallyImmediate -> ByteString -> SqliteSyntax
emit ByteString
"INITIALLY IMMEDIATE"
          (Just Bool
deferrable', Maybe ConstraintAttributeTiming
_) ->
            if Bool
deferrable' then ByteString -> SqliteSyntax
emit ByteString
" DEFERRABLE" else ByteString -> SqliteSyntax
emit ByteString
" NOT DEFERRABLE"
          (Maybe Bool, Maybe ConstraintAttributeTiming)
_ -> forall a. Monoid a => a
mempty

instance IsSql92MatchTypeSyntax SqliteMatchTypeSyntax where
  fullMatchSyntax :: SqliteMatchTypeSyntax
fullMatchSyntax = SqliteSyntax -> BeamSerializedMatchType -> SqliteMatchTypeSyntax
SqliteMatchTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"FULL") forall match. IsSql92MatchTypeSyntax match => match
fullMatchSyntax
  partialMatchSyntax :: SqliteMatchTypeSyntax
partialMatchSyntax = SqliteSyntax -> BeamSerializedMatchType -> SqliteMatchTypeSyntax
SqliteMatchTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"PARTIAL") forall match. IsSql92MatchTypeSyntax match => match
partialMatchSyntax

instance IsSql92ReferentialActionSyntax SqliteReferentialActionSyntax where
  referentialActionCascadeSyntax :: SqliteReferentialActionSyntax
referentialActionCascadeSyntax = SqliteSyntax
-> BeamSerializedReferentialAction -> SqliteReferentialActionSyntax
SqliteReferentialActionSyntax (ByteString -> SqliteSyntax
emit ByteString
"CASCADE") forall refAction.
IsSql92ReferentialActionSyntax refAction =>
refAction
referentialActionCascadeSyntax
  referentialActionSetNullSyntax :: SqliteReferentialActionSyntax
referentialActionSetNullSyntax = SqliteSyntax
-> BeamSerializedReferentialAction -> SqliteReferentialActionSyntax
SqliteReferentialActionSyntax (ByteString -> SqliteSyntax
emit ByteString
"SET NULL") forall refAction.
IsSql92ReferentialActionSyntax refAction =>
refAction
referentialActionSetNullSyntax
  referentialActionSetDefaultSyntax :: SqliteReferentialActionSyntax
referentialActionSetDefaultSyntax = SqliteSyntax
-> BeamSerializedReferentialAction -> SqliteReferentialActionSyntax
SqliteReferentialActionSyntax (ByteString -> SqliteSyntax
emit ByteString
"SET DEFAULT") forall refAction.
IsSql92ReferentialActionSyntax refAction =>
refAction
referentialActionSetDefaultSyntax
  referentialActionNoActionSyntax :: SqliteReferentialActionSyntax
referentialActionNoActionSyntax = SqliteSyntax
-> BeamSerializedReferentialAction -> SqliteReferentialActionSyntax
SqliteReferentialActionSyntax (ByteString -> SqliteSyntax
emit ByteString
"NO ACTION") forall refAction.
IsSql92ReferentialActionSyntax refAction =>
refAction
referentialActionNoActionSyntax

instance IsSql92TableConstraintSyntax SqliteTableConstraintSyntax where
  primaryKeyConstraintSyntax :: [Text] -> SqliteTableConstraintSyntax
primaryKeyConstraintSyntax [Text]
fields =
    SqliteSyntax -> Maybe [Text] -> SqliteTableConstraintSyntax
SqliteTableConstraintSyntax
      (ByteString -> SqliteSyntax
emit ByteString
"PRIMARY KEY" forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens ([SqliteSyntax] -> SqliteSyntax
commas (forall a b. (a -> b) -> [a] -> [b]
map Text -> SqliteSyntax
quotedIdentifier [Text]
fields)))
      (forall a. a -> Maybe a
Just [Text]
fields)

instance IsSql92CreateTableSyntax SqliteCreateTableSyntax where
  type Sql92CreateTableColumnSchemaSyntax SqliteCreateTableSyntax = SqliteColumnSchemaSyntax
  type Sql92CreateTableTableConstraintSyntax SqliteCreateTableSyntax = SqliteTableConstraintSyntax
  type Sql92CreateTableOptionsSyntax SqliteCreateTableSyntax = SqliteTableOptionsSyntax
  type Sql92CreateTableTableNameSyntax SqliteCreateTableSyntax = SqliteTableNameSyntax

  createTableSyntax :: Maybe (Sql92CreateTableOptionsSyntax SqliteCreateTableSyntax)
-> Sql92CreateTableTableNameSyntax SqliteCreateTableSyntax
-> [(Text,
     Sql92CreateTableColumnSchemaSyntax SqliteCreateTableSyntax)]
-> [Sql92CreateTableTableConstraintSyntax SqliteCreateTableSyntax]
-> SqliteCreateTableSyntax
createTableSyntax Maybe (Sql92CreateTableOptionsSyntax SqliteCreateTableSyntax)
_ Sql92CreateTableTableNameSyntax SqliteCreateTableSyntax
nm [(Text,
  Sql92CreateTableColumnSchemaSyntax SqliteCreateTableSyntax)]
fields [Sql92CreateTableTableConstraintSyntax SqliteCreateTableSyntax]
constraints =
    let fieldDefs :: [SqliteSyntax]
fieldDefs = forall a b. (a -> b) -> [a] -> [b]
map (Text, SqliteColumnSchemaSyntax) -> SqliteSyntax
mkFieldDef [(Text,
  Sql92CreateTableColumnSchemaSyntax SqliteCreateTableSyntax)]
fields
        constraintDefs :: [SqliteSyntax]
constraintDefs = forall a b. (a -> b) -> [a] -> [b]
map SqliteTableConstraintSyntax -> SqliteSyntax
fromSqliteTableConstraint [Sql92CreateTableTableConstraintSyntax SqliteCreateTableSyntax]
constraints
        noPkConstraintDefs :: [SqliteSyntax]
noPkConstraintDefs = forall a b. (a -> b) -> [a] -> [b]
map SqliteTableConstraintSyntax -> SqliteSyntax
fromSqliteTableConstraint (forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Maybe a -> Bool
isNothing forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteTableConstraintSyntax -> Maybe [Text]
sqliteTableConstraintPrimaryKey) [Sql92CreateTableTableConstraintSyntax SqliteCreateTableSyntax]
constraints)

        constraintPks :: [[Text]]
constraintPks = forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe SqliteTableConstraintSyntax -> Maybe [Text]
sqliteTableConstraintPrimaryKey [Sql92CreateTableTableConstraintSyntax SqliteCreateTableSyntax]
constraints
        fieldPrimaryKey :: [Text]
fieldPrimaryKey = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall a. (a -> Bool) -> [a] -> [a]
filter (SqliteColumnSchemaSyntax -> Bool
sqliteIsSerialColumn forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) [(Text,
  Sql92CreateTableColumnSchemaSyntax SqliteCreateTableSyntax)]
fields)

        mkFieldDef :: (Text, SqliteColumnSchemaSyntax) -> SqliteSyntax
mkFieldDef (Text
fieldNm, SqliteColumnSchemaSyntax
fieldTy) =
          Text -> SqliteSyntax
quotedIdentifier Text
fieldNm forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" " forall a. Semigroup a => a -> a -> a
<>
          SqliteColumnSchemaSyntax -> SqliteSyntax
fromSqliteColumnSchema SqliteColumnSchemaSyntax
fieldTy

        createWithConstraints :: [SqliteSyntax] -> SqliteCreateTableSyntax
createWithConstraints [SqliteSyntax]
constraintDefs' =
          SqliteSyntax -> SqliteCreateTableSyntax
SqliteCreateTableSyntax forall a b. (a -> b) -> a -> b
$
          ByteString -> SqliteSyntax
emit ByteString
"CREATE TABLE " forall a. Semigroup a => a -> a -> a
<> SqliteTableNameSyntax -> SqliteSyntax
fromSqliteTableName Sql92CreateTableTableNameSyntax SqliteCreateTableSyntax
nm forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens ([SqliteSyntax] -> SqliteSyntax
commas ([SqliteSyntax]
fieldDefs forall a. Semigroup a => a -> a -> a
<> [SqliteSyntax]
constraintDefs'))
        normalCreateTable :: SqliteCreateTableSyntax
normalCreateTable = [SqliteSyntax] -> SqliteCreateTableSyntax
createWithConstraints [SqliteSyntax]
constraintDefs
        createTableNoPkConstraint :: SqliteCreateTableSyntax
createTableNoPkConstraint = [SqliteSyntax] -> SqliteCreateTableSyntax
createWithConstraints [SqliteSyntax]
noPkConstraintDefs

    in case [Text]
fieldPrimaryKey of
         []  -> SqliteCreateTableSyntax
normalCreateTable
         [Text
field] ->
           case [[Text]]
constraintPks of
             [] -> forall a. HasCallStack => String -> a
error String
"A column claims to have a primary key, but there is no key on this table"
             [[Text
fieldPk]]
               | Text
field forall a. Eq a => a -> a -> Bool
/= Text
fieldPk -> forall a. HasCallStack => String -> a
error String
"Two columns claim to be a primary key on this table"
               | Bool
otherwise -> SqliteCreateTableSyntax
createTableNoPkConstraint
             [[Text]]
_ -> forall a. HasCallStack => String -> a
error String
"There are multiple primary key constraints on this table"
         [Text]
_ -> forall a. HasCallStack => String -> a
error String
"More than one column claims to be a primary key on this table"

instance IsSql92DataTypeSyntax SqliteDataTypeSyntax where
  domainType :: Text -> SqliteDataTypeSyntax
domainType Text
nm = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (Text -> SqliteSyntax
quotedIdentifier Text
nm) (forall dataType. IsSql92DataTypeSyntax dataType => Text -> dataType
domainType Text
nm) (forall dataType. IsSql92DataTypeSyntax dataType => Text -> dataType
domainType Text
nm) Bool
False
  charType :: Maybe Word -> Maybe Text -> SqliteDataTypeSyntax
charType Maybe Word
prec Maybe Text
charSet = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"CHAR" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> SqliteSyntax
sqliteOptPrec Maybe Word
prec forall a. Semigroup a => a -> a -> a
<> Maybe Text -> SqliteSyntax
sqliteOptCharSet Maybe Text
charSet)
                                               (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> Maybe Text -> dataType
charType Maybe Word
prec Maybe Text
charSet)
                                               (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> Maybe Text -> dataType
charType Maybe Word
prec Maybe Text
charSet)
                                               Bool
False
  varCharType :: Maybe Word -> Maybe Text -> SqliteDataTypeSyntax
varCharType Maybe Word
prec Maybe Text
charSet = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"VARCHAR" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> SqliteSyntax
sqliteOptPrec Maybe Word
prec forall a. Semigroup a => a -> a -> a
<> Maybe Text -> SqliteSyntax
sqliteOptCharSet Maybe Text
charSet)
                                                  (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> Maybe Text -> dataType
varCharType Maybe Word
prec Maybe Text
charSet)
                                                  (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> Maybe Text -> dataType
varCharType Maybe Word
prec Maybe Text
charSet)
                                                  Bool
False
  nationalCharType :: Maybe Word -> SqliteDataTypeSyntax
nationalCharType Maybe Word
prec = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"NATIONAL CHAR" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> SqliteSyntax
sqliteOptPrec Maybe Word
prec)
                                               (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
nationalCharType Maybe Word
prec)
                                               (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
nationalCharType Maybe Word
prec)
                                               Bool
False
  nationalVarCharType :: Maybe Word -> SqliteDataTypeSyntax
nationalVarCharType Maybe Word
prec = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"NATIONAL CHARACTER VARYING" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> SqliteSyntax
sqliteOptPrec Maybe Word
prec)
                                                  (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
nationalVarCharType Maybe Word
prec)
                                                  (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
nationalVarCharType Maybe Word
prec)
                                                  Bool
False
  bitType :: Maybe Word -> SqliteDataTypeSyntax
bitType Maybe Word
prec = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"BIT" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> SqliteSyntax
sqliteOptPrec Maybe Word
prec) (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
bitType Maybe Word
prec) (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
bitType Maybe Word
prec) Bool
False
  varBitType :: Maybe Word -> SqliteDataTypeSyntax
varBitType Maybe Word
prec = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"BIT VARYING" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> SqliteSyntax
sqliteOptPrec Maybe Word
prec) (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
varBitType Maybe Word
prec) (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
varBitType Maybe Word
prec) Bool
False

  numericType :: Maybe (Word, Maybe Word) -> SqliteDataTypeSyntax
numericType Maybe (Word, Maybe Word)
prec = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"NUMERIC" forall a. Semigroup a => a -> a -> a
<> Maybe (Word, Maybe Word) -> SqliteSyntax
sqliteOptNumericPrec Maybe (Word, Maybe Word)
prec) (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe (Word, Maybe Word) -> dataType
numericType Maybe (Word, Maybe Word)
prec) (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe (Word, Maybe Word) -> dataType
numericType Maybe (Word, Maybe Word)
prec) Bool
False
  decimalType :: Maybe (Word, Maybe Word) -> SqliteDataTypeSyntax
decimalType Maybe (Word, Maybe Word)
prec = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"DECIMAL" forall a. Semigroup a => a -> a -> a
<> Maybe (Word, Maybe Word) -> SqliteSyntax
sqliteOptNumericPrec Maybe (Word, Maybe Word)
prec) (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe (Word, Maybe Word) -> dataType
decimalType Maybe (Word, Maybe Word)
prec) (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe (Word, Maybe Word) -> dataType
decimalType Maybe (Word, Maybe Word)
prec) Bool
False

  intType :: SqliteDataTypeSyntax
intType = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"INTEGER") forall dataType. IsSql92DataTypeSyntax dataType => dataType
intType forall dataType. IsSql92DataTypeSyntax dataType => dataType
intType Bool
False
  smallIntType :: SqliteDataTypeSyntax
smallIntType = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"SMALLINT") forall dataType. IsSql92DataTypeSyntax dataType => dataType
smallIntType forall dataType. IsSql92DataTypeSyntax dataType => dataType
smallIntType Bool
False

  floatType :: Maybe Word -> SqliteDataTypeSyntax
floatType Maybe Word
prec = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"FLOAT" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> SqliteSyntax
sqliteOptPrec Maybe Word
prec) (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
floatType Maybe Word
prec) (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
floatType Maybe Word
prec) Bool
False
  doubleType :: SqliteDataTypeSyntax
doubleType = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"DOUBLE PRECISION") forall dataType. IsSql92DataTypeSyntax dataType => dataType
doubleType forall dataType. IsSql92DataTypeSyntax dataType => dataType
doubleType Bool
False
  realType :: SqliteDataTypeSyntax
realType = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"REAL") forall dataType. IsSql92DataTypeSyntax dataType => dataType
realType forall dataType. IsSql92DataTypeSyntax dataType => dataType
realType Bool
False
  dateType :: SqliteDataTypeSyntax
dateType = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"DATE") forall dataType. IsSql92DataTypeSyntax dataType => dataType
dateType forall dataType. IsSql92DataTypeSyntax dataType => dataType
dateType Bool
False
  timeType :: Maybe Word -> Bool -> SqliteDataTypeSyntax
timeType Maybe Word
prec Bool
withTz = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"TIME" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> SqliteSyntax
sqliteOptPrec Maybe Word
prec forall a. Semigroup a => a -> a -> a
<> if Bool
withTz then ByteString -> SqliteSyntax
emit ByteString
" WITH TIME ZONE" else forall a. Monoid a => a
mempty)
                                              (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> Bool -> dataType
timeType Maybe Word
prec Bool
withTz) (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> Bool -> dataType
timeType Maybe Word
prec Bool
withTz) Bool
False
  timestampType :: Maybe Word -> Bool -> SqliteDataTypeSyntax
timestampType Maybe Word
prec Bool
withTz = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"TIMESTAMP" forall a. Semigroup a => a -> a -> a
<> Maybe Word -> SqliteSyntax
sqliteOptPrec Maybe Word
prec forall a. Semigroup a => a -> a -> a
<> if Bool
withTz then ByteString -> SqliteSyntax
emit ByteString
" WITH TIME ZONE" else forall a. Monoid a => a
mempty)
                                                   (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> Bool -> dataType
timestampType Maybe Word
prec Bool
withTz) (forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> Bool -> dataType
timestampType Maybe Word
prec Bool
withTz) Bool
False

instance IsSql99DataTypeSyntax SqliteDataTypeSyntax where
  characterLargeObjectType :: SqliteDataTypeSyntax
characterLargeObjectType = SqliteDataTypeSyntax
sqliteTextType
  binaryLargeObjectType :: SqliteDataTypeSyntax
binaryLargeObjectType = SqliteDataTypeSyntax
sqliteBlobType
  booleanType :: SqliteDataTypeSyntax
booleanType = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"BOOLEAN") forall dataType. IsSql99DataTypeSyntax dataType => dataType
booleanType forall dataType. IsSql99DataTypeSyntax dataType => dataType
booleanType Bool
False
  arrayType :: SqliteDataTypeSyntax -> Int -> SqliteDataTypeSyntax
arrayType SqliteDataTypeSyntax
_ Int
_ = forall a. HasCallStack => String -> a
error String
"SQLite does not support arrayType"
  rowType :: [(Text, SqliteDataTypeSyntax)] -> SqliteDataTypeSyntax
rowType [(Text, SqliteDataTypeSyntax)]
_ = forall a. HasCallStack => String -> a
error String
"SQLite does not support rowType"

instance IsSql2008BigIntDataTypeSyntax SqliteDataTypeSyntax where
  bigIntType :: SqliteDataTypeSyntax
bigIntType = SqliteDataTypeSyntax
sqliteBigIntType

sqliteTextType, sqliteBlobType, sqliteBigIntType :: SqliteDataTypeSyntax
sqliteTextType :: SqliteDataTypeSyntax
sqliteTextType = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"TEXT")
                                      (HsExpr -> HsType -> BeamSerializedDataType -> HsDataType
HsDataType (Text -> Text -> HsExpr
hsVarFrom Text
"sqliteText" Text
"Database.Beam.Sqlite")
                                                  (Type () -> HsImports -> HsType
HsType (String -> Type ()
tyConNamed String
"Text")
                                                          (Text -> [ImportSpec ()] -> HsImports
importSome Text
"Data.Text" [Text -> ImportSpec ()
importTyNamed Text
"Text"]))
                                                  forall dataType. IsSql99DataTypeSyntax dataType => dataType
characterLargeObjectType)
                                     forall dataType. IsSql99DataTypeSyntax dataType => dataType
characterLargeObjectType
                                     Bool
False
sqliteBlobType :: SqliteDataTypeSyntax
sqliteBlobType = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"BLOB")
                                      (HsExpr -> HsType -> BeamSerializedDataType -> HsDataType
HsDataType (Text -> Text -> HsExpr
hsVarFrom Text
"sqliteBlob" Text
"Database.Beam.Sqlite")
                                                  (Type () -> HsImports -> HsType
HsType (String -> Type ()
tyConNamed String
"ByteString")
                                                          (Text -> [ImportSpec ()] -> HsImports
importSome Text
"Data.ByteString" [Text -> ImportSpec ()
importTyNamed Text
"ByteString"]))
                                                  forall dataType. IsSql99DataTypeSyntax dataType => dataType
binaryLargeObjectType)
                                       forall dataType. IsSql99DataTypeSyntax dataType => dataType
binaryLargeObjectType
                                       Bool
False
sqliteBigIntType :: SqliteDataTypeSyntax
sqliteBigIntType = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"BIGINT")
                                        (HsExpr -> HsType -> BeamSerializedDataType -> HsDataType
HsDataType (Text -> Text -> HsExpr
hsVarFrom Text
"sqliteBigInt" Text
"Database.Beam.Sqlite")
                                                    (Type () -> HsImports -> HsType
HsType (String -> Type ()
tyConNamed String
"Int64")
                                                            (Text -> [ImportSpec ()] -> HsImports
importSome Text
"Data.Int" [Text -> ImportSpec ()
importTyNamed Text
"Int64"]))
                                                    forall dataType. IsSql2008BigIntDataTypeSyntax dataType => dataType
bigIntType)
                                        forall dataType. IsSql2008BigIntDataTypeSyntax dataType => dataType
bigIntType
                                        Bool
False

instance Sql92SerializableDataTypeSyntax SqliteDataTypeSyntax where
  serializeDataType :: SqliteDataTypeSyntax -> Value
serializeDataType = BeamSerializedDataType -> Value
fromBeamSerializedDataType forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteDataTypeSyntax -> BeamSerializedDataType
sqliteDataTypeSerialized

sqliteOptPrec :: Maybe Word -> SqliteSyntax
sqliteOptPrec :: Maybe Word -> SqliteSyntax
sqliteOptPrec Maybe Word
Nothing = forall a. Monoid a => a
mempty
sqliteOptPrec (Just Word
x) = SqliteSyntax -> SqliteSyntax
parens (ByteString -> SqliteSyntax
emit (forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Word
x)))

sqliteOptNumericPrec :: Maybe (Word, Maybe Word)  -> SqliteSyntax
sqliteOptNumericPrec :: Maybe (Word, Maybe Word) -> SqliteSyntax
sqliteOptNumericPrec Maybe (Word, Maybe Word)
Nothing = forall a. Monoid a => a
mempty
sqliteOptNumericPrec (Just (Word
prec, Maybe Word
Nothing)) = Maybe Word -> SqliteSyntax
sqliteOptPrec (forall a. a -> Maybe a
Just Word
prec)
sqliteOptNumericPrec (Just (Word
prec, Just Word
dec)) = SqliteSyntax -> SqliteSyntax
parens forall a b. (a -> b) -> a -> b
$ ByteString -> SqliteSyntax
emit (forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Word
prec)) forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
", " forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit (forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Word
dec))

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

instance IsSql92SelectSyntax SqliteSelectSyntax where
  type Sql92SelectSelectTableSyntax SqliteSelectSyntax = SqliteSelectTableSyntax
  type Sql92SelectOrderingSyntax SqliteSelectSyntax = SqliteOrderingSyntax

  selectStmt :: Sql92SelectSelectTableSyntax SqliteSelectSyntax
-> [Sql92SelectOrderingSyntax SqliteSelectSyntax]
-> Maybe Integer
-> Maybe Integer
-> SqliteSelectSyntax
selectStmt Sql92SelectSelectTableSyntax SqliteSelectSyntax
tbl [Sql92SelectOrderingSyntax SqliteSelectSyntax]
ordering Maybe Integer
limit Maybe Integer
offset =
    SqliteSyntax -> SqliteSelectSyntax
SqliteSelectSyntax forall a b. (a -> b) -> a -> b
$
    SqliteSelectTableSyntax -> SqliteSyntax
fromSqliteSelectTable Sql92SelectSelectTableSyntax SqliteSelectSyntax
tbl forall a. Semigroup a => a -> a -> a
<>
    (case [Sql92SelectOrderingSyntax SqliteSelectSyntax]
ordering of
       [] -> forall a. Monoid a => a
mempty
       [Sql92SelectOrderingSyntax SqliteSelectSyntax]
_ -> ByteString -> SqliteSyntax
emit ByteString
" ORDER BY " forall a. Semigroup a => a -> a -> a
<> [SqliteSyntax] -> SqliteSyntax
commas (coerce :: forall a b. Coercible a b => a -> b
coerce [Sql92SelectOrderingSyntax SqliteSelectSyntax]
ordering)) forall a. Semigroup a => a -> a -> a
<>
    case (Maybe Integer
limit, Maybe Integer
offset) of
      (Maybe Integer
Nothing, Maybe Integer
Nothing) -> forall a. Monoid a => a
mempty
      (Just Integer
limit, Maybe Integer
Nothing) -> ByteString -> SqliteSyntax
emit ByteString
" LIMIT " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> SqliteSyntax
emit' Integer
limit
      (Maybe Integer
Nothing, Just Integer
offset) -> ByteString -> SqliteSyntax
emit ByteString
" LIMIT -1 OFFSET " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> SqliteSyntax
emit' Integer
offset
      (Just Integer
limit, Just Integer
offset) -> ByteString -> SqliteSyntax
emit ByteString
" LIMIT " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> SqliteSyntax
emit' Integer
limit forall a. Semigroup a => a -> a -> a
<>
                                   ByteString -> SqliteSyntax
emit ByteString
" OFFSET " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> SqliteSyntax
emit' Integer
offset

instance IsSql92SelectTableSyntax SqliteSelectTableSyntax where
  type Sql92SelectTableSelectSyntax SqliteSelectTableSyntax = SqliteSelectSyntax
  type Sql92SelectTableExpressionSyntax SqliteSelectTableSyntax = SqliteExpressionSyntax
  type Sql92SelectTableProjectionSyntax SqliteSelectTableSyntax = SqliteProjectionSyntax
  type Sql92SelectTableFromSyntax SqliteSelectTableSyntax = SqliteFromSyntax
  type Sql92SelectTableGroupingSyntax SqliteSelectTableSyntax = SqliteGroupingSyntax
  type Sql92SelectTableSetQuantifierSyntax SqliteSelectTableSyntax = SqliteAggregationSetQuantifierSyntax

  selectTableStmt :: Maybe (Sql92SelectTableSetQuantifierSyntax SqliteSelectTableSyntax)
-> Sql92SelectTableProjectionSyntax SqliteSelectTableSyntax
-> Maybe (Sql92SelectTableFromSyntax SqliteSelectTableSyntax)
-> Maybe (Sql92SelectTableExpressionSyntax SqliteSelectTableSyntax)
-> Maybe (Sql92SelectTableGroupingSyntax SqliteSelectTableSyntax)
-> Maybe (Sql92SelectTableExpressionSyntax SqliteSelectTableSyntax)
-> SqliteSelectTableSyntax
selectTableStmt Maybe (Sql92SelectTableSetQuantifierSyntax SqliteSelectTableSyntax)
setQuantifier Sql92SelectTableProjectionSyntax SqliteSelectTableSyntax
proj Maybe (Sql92SelectTableFromSyntax SqliteSelectTableSyntax)
from Maybe (Sql92SelectTableExpressionSyntax SqliteSelectTableSyntax)
where_ Maybe (Sql92SelectTableGroupingSyntax SqliteSelectTableSyntax)
grouping Maybe (Sql92SelectTableExpressionSyntax SqliteSelectTableSyntax)
having =
    SqliteSyntax -> SqliteSelectTableSyntax
SqliteSelectTableSyntax forall a b. (a -> b) -> a -> b
$
    ByteString -> SqliteSyntax
emit 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 (forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" ") (SqliteAggregationSetQuantifierSyntax -> SqliteSyntax
fromSqliteAggregationSetQuantifier forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Sql92SelectTableSetQuantifierSyntax SqliteSelectTableSyntax)
setQuantifier) forall a. Semigroup a => a -> a -> a
<>
    SqliteProjectionSyntax -> SqliteSyntax
fromSqliteProjection Sql92SelectTableProjectionSyntax SqliteSelectTableSyntax
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 -> SqliteSyntax
emit ByteString
" FROM " forall a. Semigroup a => a -> a -> a
<>) (SqliteFromSyntax -> SqliteSyntax
fromSqliteFromSyntax forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Sql92SelectTableFromSyntax SqliteSelectTableSyntax)
from) forall a. Semigroup a => a -> a -> a
<>
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (ByteString -> SqliteSyntax
emit ByteString
" WHERE " forall a. Semigroup a => a -> a -> a
<>) (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Sql92SelectTableExpressionSyntax SqliteSelectTableSyntax)
where_) forall a. Semigroup a => a -> a -> a
<>
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (ByteString -> SqliteSyntax
emit ByteString
" GROUP BY " forall a. Semigroup a => a -> a -> a
<>) (SqliteGroupingSyntax -> SqliteSyntax
fromSqliteGrouping forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Sql92SelectTableGroupingSyntax SqliteSelectTableSyntax)
grouping) forall a. Semigroup a => a -> a -> a
<>
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (ByteString -> SqliteSyntax
emit ByteString
" HAVING " forall a. Semigroup a => a -> a -> a
<>) (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Sql92SelectTableExpressionSyntax SqliteSelectTableSyntax)
having)

  unionTables :: Bool
-> SqliteSelectTableSyntax
-> SqliteSelectTableSyntax
-> SqliteSelectTableSyntax
unionTables Bool
all = ByteString
-> SqliteSelectTableSyntax
-> SqliteSelectTableSyntax
-> SqliteSelectTableSyntax
tableOp (if Bool
all then ByteString
"UNION ALL" else ByteString
"UNION")
  intersectTables :: Bool
-> SqliteSelectTableSyntax
-> SqliteSelectTableSyntax
-> SqliteSelectTableSyntax
intersectTables Bool
all = ByteString
-> SqliteSelectTableSyntax
-> SqliteSelectTableSyntax
-> SqliteSelectTableSyntax
tableOp (if Bool
all then ByteString
"INTERSECT ALL" else ByteString
"INTERSECT")
  exceptTable :: Bool
-> SqliteSelectTableSyntax
-> SqliteSelectTableSyntax
-> SqliteSelectTableSyntax
exceptTable Bool
all = ByteString
-> SqliteSelectTableSyntax
-> SqliteSelectTableSyntax
-> SqliteSelectTableSyntax
tableOp (if Bool
all then ByteString
"EXCEPT ALL" else ByteString
"EXCEPT")

tableOp :: ByteString -> SqliteSelectTableSyntax -> SqliteSelectTableSyntax -> SqliteSelectTableSyntax
tableOp :: ByteString
-> SqliteSelectTableSyntax
-> SqliteSelectTableSyntax
-> SqliteSelectTableSyntax
tableOp ByteString
op SqliteSelectTableSyntax
a SqliteSelectTableSyntax
b =
  SqliteSyntax -> SqliteSelectTableSyntax
SqliteSelectTableSyntax forall a b. (a -> b) -> a -> b
$
  SqliteSelectTableSyntax -> SqliteSyntax
fromSqliteSelectTable SqliteSelectTableSyntax
a forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
spaces (ByteString -> SqliteSyntax
emit ByteString
op) forall a. Semigroup a => a -> a -> a
<> SqliteSelectTableSyntax -> SqliteSyntax
fromSqliteSelectTable SqliteSelectTableSyntax
b

instance IsSql92FromSyntax SqliteFromSyntax where
  type Sql92FromExpressionSyntax SqliteFromSyntax = SqliteExpressionSyntax
  type Sql92FromTableSourceSyntax SqliteFromSyntax = SqliteTableSourceSyntax

  fromTable :: Sql92FromTableSourceSyntax SqliteFromSyntax
-> Maybe (Text, Maybe [Text]) -> SqliteFromSyntax
fromTable Sql92FromTableSourceSyntax SqliteFromSyntax
tableSrc Maybe (Text, Maybe [Text])
Nothing = SqliteSyntax -> SqliteFromSyntax
SqliteFromSyntax (SqliteTableSourceSyntax -> SqliteSyntax
fromSqliteTableSource Sql92FromTableSourceSyntax SqliteFromSyntax
tableSrc)
  fromTable Sql92FromTableSourceSyntax SqliteFromSyntax
tableSrc (Just (Text
nm, Maybe [Text]
colNms)) =
    SqliteSyntax -> SqliteFromSyntax
SqliteFromSyntax (SqliteTableSourceSyntax -> SqliteSyntax
fromSqliteTableSource Sql92FromTableSourceSyntax SqliteFromSyntax
tableSrc forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" AS " forall a. Semigroup a => a -> a -> a
<> Text -> SqliteSyntax
quotedIdentifier 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' -> SqliteSyntax -> SqliteSyntax
parens ([SqliteSyntax] -> SqliteSyntax
commas (forall a b. (a -> b) -> [a] -> [b]
map Text -> SqliteSyntax
quotedIdentifier [Text]
colNms'))) Maybe [Text]
colNms)

  innerJoin :: SqliteFromSyntax
-> SqliteFromSyntax
-> Maybe (Sql92FromExpressionSyntax SqliteFromSyntax)
-> SqliteFromSyntax
innerJoin = ByteString
-> SqliteFromSyntax
-> SqliteFromSyntax
-> Maybe SqliteExpressionSyntax
-> SqliteFromSyntax
_join ByteString
"INNER JOIN"
  leftJoin :: SqliteFromSyntax
-> SqliteFromSyntax
-> Maybe (Sql92FromExpressionSyntax SqliteFromSyntax)
-> SqliteFromSyntax
leftJoin = ByteString
-> SqliteFromSyntax
-> SqliteFromSyntax
-> Maybe SqliteExpressionSyntax
-> SqliteFromSyntax
_join ByteString
"LEFT JOIN"
  rightJoin :: SqliteFromSyntax
-> SqliteFromSyntax
-> Maybe (Sql92FromExpressionSyntax SqliteFromSyntax)
-> SqliteFromSyntax
rightJoin = ByteString
-> SqliteFromSyntax
-> SqliteFromSyntax
-> Maybe SqliteExpressionSyntax
-> SqliteFromSyntax
_join ByteString
"RIGHT JOIN"

_join :: ByteString -> SqliteFromSyntax -> SqliteFromSyntax -> Maybe SqliteExpressionSyntax -> SqliteFromSyntax
_join :: ByteString
-> SqliteFromSyntax
-> SqliteFromSyntax
-> Maybe SqliteExpressionSyntax
-> SqliteFromSyntax
_join ByteString
joinType SqliteFromSyntax
a SqliteFromSyntax
b Maybe SqliteExpressionSyntax
Nothing =
  SqliteSyntax -> SqliteFromSyntax
SqliteFromSyntax (SqliteFromSyntax -> SqliteSyntax
fromSqliteFromSyntax SqliteFromSyntax
a forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
spaces (ByteString -> SqliteSyntax
emit ByteString
joinType) forall a. Semigroup a => a -> a -> a
<> SqliteFromSyntax -> SqliteSyntax
fromSqliteFromSyntax SqliteFromSyntax
b)
_join ByteString
joinType SqliteFromSyntax
a SqliteFromSyntax
b (Just SqliteExpressionSyntax
on) =
  SqliteSyntax -> SqliteFromSyntax
SqliteFromSyntax (SqliteFromSyntax -> SqliteSyntax
fromSqliteFromSyntax SqliteFromSyntax
a forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
spaces (ByteString -> SqliteSyntax
emit ByteString
joinType) forall a. Semigroup a => a -> a -> a
<> SqliteFromSyntax -> SqliteSyntax
fromSqliteFromSyntax SqliteFromSyntax
b forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" ON " forall a. Semigroup a => a -> a -> a
<> SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
on)

instance IsSql92ProjectionSyntax SqliteProjectionSyntax where
  type Sql92ProjectionExpressionSyntax SqliteProjectionSyntax = SqliteExpressionSyntax

  projExprs :: [(Sql92ProjectionExpressionSyntax SqliteProjectionSyntax,
  Maybe Text)]
-> SqliteProjectionSyntax
projExprs [(Sql92ProjectionExpressionSyntax SqliteProjectionSyntax,
  Maybe Text)]
exprs =
    SqliteSyntax -> SqliteProjectionSyntax
SqliteProjectionSyntax forall a b. (a -> b) -> a -> b
$
    [SqliteSyntax] -> SqliteSyntax
commas (forall a b. (a -> b) -> [a] -> [b]
map (\(SqliteExpressionSyntax
expr, Maybe Text
nm) -> SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
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 -> SqliteSyntax
emit ByteString
" AS " forall a. Semigroup a => a -> a -> a
<> Text -> SqliteSyntax
quotedIdentifier Text
nm) Maybe Text
nm) [(Sql92ProjectionExpressionSyntax SqliteProjectionSyntax,
  Maybe Text)]
exprs)

instance IsSql92FieldNameSyntax SqliteFieldNameSyntax where
  qualifiedField :: Text -> Text -> SqliteFieldNameSyntax
qualifiedField Text
a Text
b =
    SqliteSyntax -> SqliteFieldNameSyntax
SqliteFieldNameSyntax forall a b. (a -> b) -> a -> b
$
    Text -> SqliteSyntax
quotedIdentifier Text
a forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
"." forall a. Semigroup a => a -> a -> a
<> Text -> SqliteSyntax
quotedIdentifier Text
b
  unqualifiedField :: Text -> SqliteFieldNameSyntax
unqualifiedField Text
a =
    SqliteSyntax -> SqliteFieldNameSyntax
SqliteFieldNameSyntax forall a b. (a -> b) -> a -> b
$
    Text -> SqliteSyntax
quotedIdentifier Text
a

instance IsSql92TableSourceSyntax SqliteTableSourceSyntax where
  type Sql92TableSourceTableNameSyntax SqliteTableSourceSyntax = SqliteTableNameSyntax
  type Sql92TableSourceSelectSyntax SqliteTableSourceSyntax = SqliteSelectSyntax
  type Sql92TableSourceExpressionSyntax SqliteTableSourceSyntax = SqliteExpressionSyntax

  tableNamed :: Sql92TableSourceTableNameSyntax SqliteTableSourceSyntax
-> SqliteTableSourceSyntax
tableNamed = SqliteSyntax -> SqliteTableSourceSyntax
SqliteTableSourceSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteTableNameSyntax -> SqliteSyntax
fromSqliteTableName
  tableFromSubSelect :: Sql92TableSourceSelectSyntax SqliteTableSourceSyntax
-> SqliteTableSourceSyntax
tableFromSubSelect Sql92TableSourceSelectSyntax SqliteTableSourceSyntax
s =
    SqliteSyntax -> SqliteTableSourceSyntax
SqliteTableSourceSyntax (SqliteSyntax -> SqliteSyntax
parens (SqliteSelectSyntax -> SqliteSyntax
fromSqliteSelect Sql92TableSourceSelectSyntax SqliteTableSourceSyntax
s))
  tableFromValues :: [[Sql92TableSourceExpressionSyntax SqliteTableSourceSyntax]]
-> SqliteTableSourceSyntax
tableFromValues [[Sql92TableSourceExpressionSyntax SqliteTableSourceSyntax]]
vss = SqliteSyntax -> SqliteTableSourceSyntax
SqliteTableSourceSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteSyntax -> SqliteSyntax
parens forall a b. (a -> b) -> a -> b
$
                        ByteString -> SqliteSyntax
emit ByteString
"VALUES " forall a. Semigroup a => a -> a -> a
<>
                        [SqliteSyntax] -> SqliteSyntax
commas (forall a b. (a -> b) -> [a] -> [b]
map (\[SqliteExpressionSyntax]
vs -> SqliteSyntax -> SqliteSyntax
parens ([SqliteSyntax] -> SqliteSyntax
commas (forall a b. (a -> b) -> [a] -> [b]
map SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression [SqliteExpressionSyntax]
vs))) [[Sql92TableSourceExpressionSyntax SqliteTableSourceSyntax]]
vss)

instance IsSql92GroupingSyntax SqliteGroupingSyntax where
  type Sql92GroupingExpressionSyntax SqliteGroupingSyntax = SqliteExpressionSyntax

  groupByExpressions :: [Sql92GroupingExpressionSyntax SqliteGroupingSyntax]
-> SqliteGroupingSyntax
groupByExpressions [Sql92GroupingExpressionSyntax SqliteGroupingSyntax]
es =
    SqliteSyntax -> SqliteGroupingSyntax
SqliteGroupingSyntax forall a b. (a -> b) -> a -> b
$
    [SqliteSyntax] -> SqliteSyntax
commas (forall a b. (a -> b) -> [a] -> [b]
map SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression [Sql92GroupingExpressionSyntax SqliteGroupingSyntax]
es)

instance IsSql92OrderingSyntax SqliteOrderingSyntax where
  type Sql92OrderingExpressionSyntax SqliteOrderingSyntax = SqliteExpressionSyntax

  ascOrdering :: Sql92OrderingExpressionSyntax SqliteOrderingSyntax
-> SqliteOrderingSyntax
ascOrdering Sql92OrderingExpressionSyntax SqliteOrderingSyntax
e = SqliteSyntax -> SqliteOrderingSyntax
SqliteOrderingSyntax (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression Sql92OrderingExpressionSyntax SqliteOrderingSyntax
e forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" ASC")
  descOrdering :: Sql92OrderingExpressionSyntax SqliteOrderingSyntax
-> SqliteOrderingSyntax
descOrdering Sql92OrderingExpressionSyntax SqliteOrderingSyntax
e = SqliteSyntax -> SqliteOrderingSyntax
SqliteOrderingSyntax (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression Sql92OrderingExpressionSyntax SqliteOrderingSyntax
e forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" DESC")

instance HasSqlValueSyntax SqliteValueSyntax Int8 where
  sqlValueSyntax :: Int8 -> SqliteValueSyntax
sqlValueSyntax Int8
i = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Int64 -> SQLData
SQLInteger (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int8
i)))
instance HasSqlValueSyntax SqliteValueSyntax Int16 where
  sqlValueSyntax :: Int16 -> SqliteValueSyntax
sqlValueSyntax Int16
i = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Int64 -> SQLData
SQLInteger (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int16
i)))
instance HasSqlValueSyntax SqliteValueSyntax Int32 where
  sqlValueSyntax :: Int32 -> SqliteValueSyntax
sqlValueSyntax Int32
i = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Int64 -> SQLData
SQLInteger (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
i)))
instance HasSqlValueSyntax SqliteValueSyntax Int64 where
  sqlValueSyntax :: Int64 -> SqliteValueSyntax
sqlValueSyntax Int64
i = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Int64 -> SQLData
SQLInteger (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
i)))
instance HasSqlValueSyntax SqliteValueSyntax Word8 where
  sqlValueSyntax :: Word8 -> SqliteValueSyntax
sqlValueSyntax Word8
i = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Int64 -> SQLData
SQLInteger (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
i)))
instance HasSqlValueSyntax SqliteValueSyntax Word16 where
  sqlValueSyntax :: Word16 -> SqliteValueSyntax
sqlValueSyntax Word16
i = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Int64 -> SQLData
SQLInteger (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
i)))
instance HasSqlValueSyntax SqliteValueSyntax Word32 where
  sqlValueSyntax :: Word32 -> SqliteValueSyntax
sqlValueSyntax Word32
i = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Int64 -> SQLData
SQLInteger (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
i)))
instance HasSqlValueSyntax SqliteValueSyntax Word64 where
  sqlValueSyntax :: Word64 -> SqliteValueSyntax
sqlValueSyntax Word64
i = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Int64 -> SQLData
SQLInteger (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
i)))
instance HasSqlValueSyntax SqliteValueSyntax Scientific where
  sqlValueSyntax :: Scientific -> SqliteValueSyntax
sqlValueSyntax Scientific
s = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Text -> SQLData
SQLText (forall a. IsString a => String -> a
fromString (forall a. Show a => a -> String
show Scientific
s)))) -- Rely on sqlites duck typing
instance HasSqlValueSyntax SqliteValueSyntax Float where
  sqlValueSyntax :: Float -> SqliteValueSyntax
sqlValueSyntax Float
f = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Double -> SQLData
SQLFloat (Float -> Double
float2Double Float
f)))
instance HasSqlValueSyntax SqliteValueSyntax Double where
  sqlValueSyntax :: Double -> SqliteValueSyntax
sqlValueSyntax Double
f = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Double -> SQLData
SQLFloat Double
f))
instance HasSqlValueSyntax SqliteValueSyntax Bool where
  sqlValueSyntax :: Bool -> SqliteValueSyntax
sqlValueSyntax = forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\Bool
b -> if Bool
b then Int32
1 else Int32
0 :: Int32)
instance HasSqlValueSyntax SqliteValueSyntax SqlNull where
  sqlValueSyntax :: SqlNull -> SqliteValueSyntax
sqlValueSyntax SqlNull
_ = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (ByteString -> SqliteSyntax
emit ByteString
"NULL")
instance HasSqlValueSyntax SqliteValueSyntax String where
  sqlValueSyntax :: String -> SqliteValueSyntax
sqlValueSyntax String
s = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Text -> SQLData
SQLText (forall a. IsString a => String -> a
fromString String
s)))
instance HasSqlValueSyntax SqliteValueSyntax T.Text where
  sqlValueSyntax :: Text -> SqliteValueSyntax
sqlValueSyntax Text
s = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Text -> SQLData
SQLText Text
s))
instance HasSqlValueSyntax SqliteValueSyntax TL.Text where
  sqlValueSyntax :: Text -> SqliteValueSyntax
sqlValueSyntax Text
s = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Text -> SQLData
SQLText (Text -> Text
TL.toStrict Text
s)))
instance HasSqlValueSyntax SqliteValueSyntax x =>
  HasSqlValueSyntax SqliteValueSyntax (Maybe x) where
  sqlValueSyntax :: Maybe x -> SqliteValueSyntax
sqlValueSyntax (Just x
x) = forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax x
x
  sqlValueSyntax Maybe x
Nothing = forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax SqlNull
SqlNull

instance TypeError (PreferExplicitSize Int Int32) => HasSqlValueSyntax SqliteValueSyntax Int where
  sqlValueSyntax :: Int -> SqliteValueSyntax
sqlValueSyntax Int
i = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Int64 -> SQLData
SQLInteger (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i)))

instance TypeError (PreferExplicitSize Word Word32) => HasSqlValueSyntax SqliteValueSyntax Word where
  sqlValueSyntax :: Word -> SqliteValueSyntax
sqlValueSyntax Word
i = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Int64 -> SQLData
SQLInteger (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
i)))

instance IsCustomSqlSyntax SqliteExpressionSyntax where
  newtype CustomSqlSyntax SqliteExpressionSyntax =
    SqliteCustomExpressionSyntax { CustomSqlSyntax SqliteExpressionSyntax -> SqliteSyntax
fromSqliteCustomExpression :: SqliteSyntax }
    deriving (Semigroup (CustomSqlSyntax SqliteExpressionSyntax)
CustomSqlSyntax SqliteExpressionSyntax
[CustomSqlSyntax SqliteExpressionSyntax]
-> CustomSqlSyntax SqliteExpressionSyntax
CustomSqlSyntax SqliteExpressionSyntax
-> CustomSqlSyntax SqliteExpressionSyntax
-> CustomSqlSyntax SqliteExpressionSyntax
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
mconcat :: [CustomSqlSyntax SqliteExpressionSyntax]
-> CustomSqlSyntax SqliteExpressionSyntax
$cmconcat :: [CustomSqlSyntax SqliteExpressionSyntax]
-> CustomSqlSyntax SqliteExpressionSyntax
mappend :: CustomSqlSyntax SqliteExpressionSyntax
-> CustomSqlSyntax SqliteExpressionSyntax
-> CustomSqlSyntax SqliteExpressionSyntax
$cmappend :: CustomSqlSyntax SqliteExpressionSyntax
-> CustomSqlSyntax SqliteExpressionSyntax
-> CustomSqlSyntax SqliteExpressionSyntax
mempty :: CustomSqlSyntax SqliteExpressionSyntax
$cmempty :: CustomSqlSyntax SqliteExpressionSyntax
Monoid, NonEmpty (CustomSqlSyntax SqliteExpressionSyntax)
-> CustomSqlSyntax SqliteExpressionSyntax
CustomSqlSyntax SqliteExpressionSyntax
-> CustomSqlSyntax SqliteExpressionSyntax
-> CustomSqlSyntax SqliteExpressionSyntax
forall b.
Integral b =>
b
-> CustomSqlSyntax SqliteExpressionSyntax
-> CustomSqlSyntax SqliteExpressionSyntax
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
stimes :: forall b.
Integral b =>
b
-> CustomSqlSyntax SqliteExpressionSyntax
-> CustomSqlSyntax SqliteExpressionSyntax
$cstimes :: forall b.
Integral b =>
b
-> CustomSqlSyntax SqliteExpressionSyntax
-> CustomSqlSyntax SqliteExpressionSyntax
sconcat :: NonEmpty (CustomSqlSyntax SqliteExpressionSyntax)
-> CustomSqlSyntax SqliteExpressionSyntax
$csconcat :: NonEmpty (CustomSqlSyntax SqliteExpressionSyntax)
-> CustomSqlSyntax SqliteExpressionSyntax
<> :: CustomSqlSyntax SqliteExpressionSyntax
-> CustomSqlSyntax SqliteExpressionSyntax
-> CustomSqlSyntax SqliteExpressionSyntax
$c<> :: CustomSqlSyntax SqliteExpressionSyntax
-> CustomSqlSyntax SqliteExpressionSyntax
-> CustomSqlSyntax SqliteExpressionSyntax
Semigroup)

  customExprSyntax :: CustomSqlSyntax SqliteExpressionSyntax -> SqliteExpressionSyntax
customExprSyntax = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. CustomSqlSyntax SqliteExpressionSyntax -> SqliteSyntax
fromSqliteCustomExpression
  renderSyntax :: SqliteExpressionSyntax -> CustomSqlSyntax SqliteExpressionSyntax
renderSyntax = SqliteSyntax -> CustomSqlSyntax SqliteExpressionSyntax
SqliteCustomExpressionSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression
instance IsString (CustomSqlSyntax SqliteExpressionSyntax) where
  fromString :: String -> CustomSqlSyntax SqliteExpressionSyntax
fromString = SqliteSyntax -> CustomSqlSyntax SqliteExpressionSyntax
SqliteCustomExpressionSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> SqliteSyntax
emit forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IsString a => String -> a
fromString

instance IsSql92QuantifierSyntax SqliteComparisonQuantifierSyntax where
  quantifyOverAll :: SqliteComparisonQuantifierSyntax
quantifyOverAll = SqliteSyntax -> SqliteComparisonQuantifierSyntax
SqliteComparisonQuantifierSyntax (ByteString -> SqliteSyntax
emit ByteString
"ALL")
  quantifyOverAny :: SqliteComparisonQuantifierSyntax
quantifyOverAny = SqliteSyntax -> SqliteComparisonQuantifierSyntax
SqliteComparisonQuantifierSyntax (ByteString -> SqliteSyntax
emit ByteString
"ANY")

instance IsSql92ExpressionSyntax SqliteExpressionSyntax where
  type Sql92ExpressionValueSyntax SqliteExpressionSyntax = SqliteValueSyntax
  type Sql92ExpressionSelectSyntax SqliteExpressionSyntax = SqliteSelectSyntax
  type Sql92ExpressionFieldNameSyntax SqliteExpressionSyntax = SqliteFieldNameSyntax
  type Sql92ExpressionQuantifierSyntax SqliteExpressionSyntax = SqliteComparisonQuantifierSyntax
  type Sql92ExpressionCastTargetSyntax SqliteExpressionSyntax = SqliteDataTypeSyntax
  type Sql92ExpressionExtractFieldSyntax SqliteExpressionSyntax = ExtractField

  addE :: SqliteExpressionSyntax
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
addE = ByteString
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
binOp ByteString
"+"; subE :: SqliteExpressionSyntax
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
subE = ByteString
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
binOp ByteString
"-"; mulE :: SqliteExpressionSyntax
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
mulE = ByteString
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
binOp ByteString
"*"; divE :: SqliteExpressionSyntax
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
divE = ByteString
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
binOp ByteString
"/"
  modE :: SqliteExpressionSyntax
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
modE = ByteString
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
binOp ByteString
"%"; orE :: SqliteExpressionSyntax
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
orE = ByteString
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
binOp ByteString
"OR"; andE :: SqliteExpressionSyntax
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
andE = ByteString
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
binOp ByteString
"AND"; likeE :: SqliteExpressionSyntax
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
likeE = ByteString
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
binOp ByteString
"LIKE"
  overlapsE :: SqliteExpressionSyntax
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
overlapsE = ByteString
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
binOp ByteString
"OVERLAPS"

  eqE :: Maybe (Sql92ExpressionQuantifierSyntax SqliteExpressionSyntax)
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
eqE = ByteString
-> Maybe SqliteComparisonQuantifierSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
compOp ByteString
"="; neqE :: Maybe (Sql92ExpressionQuantifierSyntax SqliteExpressionSyntax)
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
neqE = ByteString
-> Maybe SqliteComparisonQuantifierSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
compOp ByteString
"<>"; ltE :: Maybe (Sql92ExpressionQuantifierSyntax SqliteExpressionSyntax)
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
ltE = ByteString
-> Maybe SqliteComparisonQuantifierSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
compOp ByteString
"<"; gtE :: Maybe (Sql92ExpressionQuantifierSyntax SqliteExpressionSyntax)
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
gtE = ByteString
-> Maybe SqliteComparisonQuantifierSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
compOp ByteString
">"
  leE :: Maybe (Sql92ExpressionQuantifierSyntax SqliteExpressionSyntax)
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
leE = ByteString
-> Maybe SqliteComparisonQuantifierSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
compOp ByteString
"<="; geE :: Maybe (Sql92ExpressionQuantifierSyntax SqliteExpressionSyntax)
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
geE = ByteString
-> Maybe SqliteComparisonQuantifierSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
compOp ByteString
">="

  negateE :: SqliteExpressionSyntax -> SqliteExpressionSyntax
negateE = ByteString -> SqliteExpressionSyntax -> SqliteExpressionSyntax
unOp ByteString
"-"; notE :: SqliteExpressionSyntax -> SqliteExpressionSyntax
notE = ByteString -> SqliteExpressionSyntax -> SqliteExpressionSyntax
unOp ByteString
"NOT"

  isNotNullE :: SqliteExpressionSyntax -> SqliteExpressionSyntax
isNotNullE = ByteString -> SqliteExpressionSyntax -> SqliteExpressionSyntax
postFix ByteString
"IS NOT NULL"; isNullE :: SqliteExpressionSyntax -> SqliteExpressionSyntax
isNullE = ByteString -> SqliteExpressionSyntax -> SqliteExpressionSyntax
postFix ByteString
"IS NULL"

  -- SQLite doesn't handle tri-state booleans properly
  isTrueE :: SqliteExpressionSyntax -> SqliteExpressionSyntax
isTrueE = ByteString -> SqliteExpressionSyntax -> SqliteExpressionSyntax
postFix ByteString
"IS 1"; isNotTrueE :: SqliteExpressionSyntax -> SqliteExpressionSyntax
isNotTrueE = ByteString -> SqliteExpressionSyntax -> SqliteExpressionSyntax
postFix ByteString
"IS NOT 1"
  isFalseE :: SqliteExpressionSyntax -> SqliteExpressionSyntax
isFalseE = ByteString -> SqliteExpressionSyntax -> SqliteExpressionSyntax
postFix ByteString
"IS 0"; isNotFalseE :: SqliteExpressionSyntax -> SqliteExpressionSyntax
isNotFalseE = ByteString -> SqliteExpressionSyntax -> SqliteExpressionSyntax
postFix ByteString
"IS NOT 0"
  isUnknownE :: SqliteExpressionSyntax -> SqliteExpressionSyntax
isUnknownE = ByteString -> SqliteExpressionSyntax -> SqliteExpressionSyntax
postFix ByteString
"IS NULL"; isNotUnknownE :: SqliteExpressionSyntax -> SqliteExpressionSyntax
isNotUnknownE = ByteString -> SqliteExpressionSyntax -> SqliteExpressionSyntax
postFix ByteString
"IS NOT NULL"

  existsE :: Sql92ExpressionSelectSyntax SqliteExpressionSyntax
-> SqliteExpressionSyntax
existsE Sql92ExpressionSelectSyntax SqliteExpressionSyntax
select = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (ByteString -> SqliteSyntax
emit ByteString
"EXISTS " forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteSelectSyntax -> SqliteSyntax
fromSqliteSelect Sql92ExpressionSelectSyntax SqliteExpressionSyntax
select))
  uniqueE :: Sql92ExpressionSelectSyntax SqliteExpressionSyntax
-> SqliteExpressionSyntax
uniqueE Sql92ExpressionSelectSyntax SqliteExpressionSyntax
select = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (ByteString -> SqliteSyntax
emit ByteString
"UNIQUE " forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteSelectSyntax -> SqliteSyntax
fromSqliteSelect Sql92ExpressionSelectSyntax SqliteExpressionSyntax
select))

  betweenE :: SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
betweenE SqliteExpressionSyntax
a SqliteExpressionSyntax
b SqliteExpressionSyntax
c = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
a) forall a. Semigroup a => a -> a -> a
<>
                                           ByteString -> SqliteSyntax
emit ByteString
" BETWEEN " forall a. Semigroup a => a -> a -> a
<>
                                           SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
b) forall a. Semigroup a => a -> a -> a
<>
                                           ByteString -> SqliteSyntax
emit ByteString
" AND " forall a. Semigroup a => a -> a -> a
<>
                                           SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
c))

  valueE :: Sql92ExpressionValueSyntax SqliteExpressionSyntax
-> SqliteExpressionSyntax
valueE = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteValueSyntax -> SqliteSyntax
fromSqliteValue

  rowE :: [SqliteExpressionSyntax] -> SqliteExpressionSyntax
rowE [SqliteExpressionSyntax]
vs = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (SqliteSyntax -> SqliteSyntax
parens ([SqliteSyntax] -> SqliteSyntax
commas (forall a b. (a -> b) -> [a] -> [b]
map SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression [SqliteExpressionSyntax]
vs)))
  fieldE :: Sql92ExpressionFieldNameSyntax SqliteExpressionSyntax
-> SqliteExpressionSyntax
fieldE = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteFieldNameSyntax -> SqliteSyntax
fromSqliteFieldNameSyntax

  subqueryE :: Sql92ExpressionSelectSyntax SqliteExpressionSyntax
-> SqliteExpressionSyntax
subqueryE = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteSyntax -> SqliteSyntax
parens forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqliteSelectSyntax -> SqliteSyntax
fromSqliteSelect

  positionE :: SqliteExpressionSyntax
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
positionE SqliteExpressionSyntax
needle SqliteExpressionSyntax
haystack =
    SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax forall a b. (a -> b) -> a -> b
$
    ByteString -> SqliteSyntax
emit ByteString
"POSITION" forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
needle) forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" IN " forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
haystack))
  nullIfE :: SqliteExpressionSyntax
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
nullIfE SqliteExpressionSyntax
a SqliteExpressionSyntax
b =
    SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax forall a b. (a -> b) -> a -> b
$
    ByteString -> SqliteSyntax
emit ByteString
"NULLIF" forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
a forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
", " forall a. Semigroup a => a -> a -> a
<> SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
b)
  absE :: SqliteExpressionSyntax -> SqliteExpressionSyntax
absE SqliteExpressionSyntax
x = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (ByteString -> SqliteSyntax
emit ByteString
"ABS" forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
x))
  bitLengthE :: SqliteExpressionSyntax -> SqliteExpressionSyntax
bitLengthE SqliteExpressionSyntax
x = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (ByteString -> SqliteSyntax
emit ByteString
"8 * LENGTH" forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (ByteString -> SqliteSyntax
emit ByteString
"CAST" forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
x) forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" AS BLOB")))
  charLengthE :: SqliteExpressionSyntax -> SqliteExpressionSyntax
charLengthE SqliteExpressionSyntax
x = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (ByteString -> SqliteSyntax
emit ByteString
"LENGTH" forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
x))
  octetLengthE :: SqliteExpressionSyntax -> SqliteExpressionSyntax
octetLengthE SqliteExpressionSyntax
x = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (ByteString -> SqliteSyntax
emit ByteString
"LENGTH" forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (ByteString -> SqliteSyntax
emit ByteString
"CAST" forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
x) forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" AS BLOB")))
  lowerE :: SqliteExpressionSyntax -> SqliteExpressionSyntax
lowerE SqliteExpressionSyntax
x = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (ByteString -> SqliteSyntax
emit ByteString
"LOWER" forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
x))
  upperE :: SqliteExpressionSyntax -> SqliteExpressionSyntax
upperE SqliteExpressionSyntax
x = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (ByteString -> SqliteSyntax
emit ByteString
"UPPER" forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
x))
  trimE :: SqliteExpressionSyntax -> SqliteExpressionSyntax
trimE SqliteExpressionSyntax
x = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (ByteString -> SqliteSyntax
emit ByteString
"TRIM" forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
x))
  coalesceE :: [SqliteExpressionSyntax] -> SqliteExpressionSyntax
coalesceE [SqliteExpressionSyntax]
es = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (ByteString -> SqliteSyntax
emit ByteString
"COALESCE" forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens ([SqliteSyntax] -> SqliteSyntax
commas (forall a b. (a -> b) -> [a] -> [b]
map SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression [SqliteExpressionSyntax]
es)))
  extractE :: Sql92ExpressionExtractFieldSyntax SqliteExpressionSyntax
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
extractE = ExtractField -> SqliteExpressionSyntax -> SqliteExpressionSyntax
sqliteExtract
  castE :: SqliteExpressionSyntax
-> Sql92ExpressionCastTargetSyntax SqliteExpressionSyntax
-> SqliteExpressionSyntax
castE SqliteExpressionSyntax
e Sql92ExpressionCastTargetSyntax SqliteExpressionSyntax
t = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (ByteString -> SqliteSyntax
emit ByteString
"CAST" forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
e) forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" AS " forall a. Semigroup a => a -> a -> a
<> SqliteDataTypeSyntax -> SqliteSyntax
fromSqliteDataType Sql92ExpressionCastTargetSyntax SqliteExpressionSyntax
t))
  caseE :: [(SqliteExpressionSyntax, SqliteExpressionSyntax)]
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
caseE [(SqliteExpressionSyntax, SqliteExpressionSyntax)]
cases SqliteExpressionSyntax
else_ =
    SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax forall a b. (a -> b) -> a -> b
$
    ByteString -> SqliteSyntax
emit ByteString
"CASE " forall a. Semigroup a => a -> a -> a
<>
    forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\(SqliteExpressionSyntax
cond, SqliteExpressionSyntax
res) -> ByteString -> SqliteSyntax
emit ByteString
"WHEN " forall a. Semigroup a => a -> a -> a
<> SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
cond forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" THEN " forall a. Semigroup a => a -> a -> a
<> SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
res forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" ") [(SqliteExpressionSyntax, SqliteExpressionSyntax)]
cases forall a. Semigroup a => a -> a -> a
<>
    ByteString -> SqliteSyntax
emit ByteString
"ELSE " forall a. Semigroup a => a -> a -> a
<> SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
else_ forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" END"

  currentTimestampE :: SqliteExpressionSyntax
currentTimestampE = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (ByteString -> SqliteSyntax
emit ByteString
"CURRENT_TIMESTAMP")

  defaultE :: SqliteExpressionSyntax
defaultE = SqliteExpressionSyntax
SqliteExpressionDefault
  inE :: SqliteExpressionSyntax
-> [SqliteExpressionSyntax] -> SqliteExpressionSyntax
inE SqliteExpressionSyntax
e [SqliteExpressionSyntax]
es = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
e) forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" IN " forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens ([SqliteSyntax] -> SqliteSyntax
commas (forall a b. (a -> b) -> [a] -> [b]
map SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression [SqliteExpressionSyntax]
es)))
  inSelectE :: SqliteExpressionSyntax
-> Sql92ExpressionSelectSyntax SqliteExpressionSyntax
-> SqliteExpressionSyntax
inSelectE SqliteExpressionSyntax
e Sql92ExpressionSelectSyntax SqliteExpressionSyntax
sel =
      SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
e) forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" IN " forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteSelectSyntax -> SqliteSyntax
fromSqliteSelect Sql92ExpressionSelectSyntax SqliteExpressionSyntax
sel))

instance IsSql99ConcatExpressionSyntax SqliteExpressionSyntax where
  concatE :: [SqliteExpressionSyntax] -> SqliteExpressionSyntax
concatE [] = forall expr.
IsSql92ExpressionSyntax expr =>
Sql92ExpressionValueSyntax expr -> expr
valueE (forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax (Text
"" :: T.Text))
  concatE (SqliteExpressionSyntax
x:[SqliteExpressionSyntax]
xs) =
    SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax forall a b. (a -> b) -> a -> b
$ SqliteSyntax -> SqliteSyntax
parens forall a b. (a -> b) -> a -> b
$
    forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\SqliteSyntax
a SqliteExpressionSyntax
b -> SqliteSyntax
a forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" || " forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
b)) (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
x) [SqliteExpressionSyntax]
xs

instance IsSql99FunctionExpressionSyntax SqliteExpressionSyntax where
  functionCallE :: SqliteExpressionSyntax
-> [SqliteExpressionSyntax] -> SqliteExpressionSyntax
functionCallE SqliteExpressionSyntax
fn [SqliteExpressionSyntax]
args =
    SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax forall a b. (a -> b) -> a -> b
$
    SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
fn forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens ([SqliteSyntax] -> SqliteSyntax
commas (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression [SqliteExpressionSyntax]
args))
  functionNameE :: Text -> SqliteExpressionSyntax
functionNameE Text
nm = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (ByteString -> SqliteSyntax
emit (Text -> ByteString
TE.encodeUtf8 Text
nm))

binOp :: ByteString -> SqliteExpressionSyntax -> SqliteExpressionSyntax -> SqliteExpressionSyntax
binOp :: ByteString
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
binOp ByteString
op SqliteExpressionSyntax
a SqliteExpressionSyntax
b =
  SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax forall a b. (a -> b) -> a -> b
$
  SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
a) forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" " forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
op forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" " forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
b)

compOp :: ByteString -> Maybe SqliteComparisonQuantifierSyntax
       -> SqliteExpressionSyntax -> SqliteExpressionSyntax
       -> SqliteExpressionSyntax
compOp :: ByteString
-> Maybe SqliteComparisonQuantifierSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
compOp ByteString
op Maybe SqliteComparisonQuantifierSyntax
quantifier SqliteExpressionSyntax
a SqliteExpressionSyntax
b =
  SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax forall a b. (a -> b) -> a -> b
$
  SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
a) forall a. Semigroup a => a -> a -> a
<>
  ByteString -> SqliteSyntax
emit 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 (\SqliteComparisonQuantifierSyntax
q -> ByteString -> SqliteSyntax
emit ByteString
" " forall a. Semigroup a => a -> a -> a
<> SqliteComparisonQuantifierSyntax -> SqliteSyntax
fromSqliteComparisonQuantifier SqliteComparisonQuantifierSyntax
q forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" ") Maybe SqliteComparisonQuantifierSyntax
quantifier forall a. Semigroup a => a -> a -> a
<>
  SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
b)

unOp, postFix :: ByteString -> SqliteExpressionSyntax -> SqliteExpressionSyntax
unOp :: ByteString -> SqliteExpressionSyntax -> SqliteExpressionSyntax
unOp ByteString
op SqliteExpressionSyntax
a =
  SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (ByteString -> SqliteSyntax
emit ByteString
op forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
a))
postFix :: ByteString -> SqliteExpressionSyntax -> SqliteExpressionSyntax
postFix ByteString
op SqliteExpressionSyntax
a =
  SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (SqliteSyntax -> SqliteSyntax
parens (SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
a) forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" " forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
op)

instance IsSql92AggregationExpressionSyntax SqliteExpressionSyntax where
  type Sql92AggregationSetQuantifierSyntax SqliteExpressionSyntax = SqliteAggregationSetQuantifierSyntax

  countAllE :: SqliteExpressionSyntax
countAllE = SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (ByteString -> SqliteSyntax
emit ByteString
"COUNT(*)")
  countE :: Maybe (Sql92AggregationSetQuantifierSyntax SqliteExpressionSyntax)
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
countE = ByteString
-> Maybe SqliteAggregationSetQuantifierSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
unAgg ByteString
"COUNT"
  sumE :: Maybe (Sql92AggregationSetQuantifierSyntax SqliteExpressionSyntax)
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
sumE = ByteString
-> Maybe SqliteAggregationSetQuantifierSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
unAgg ByteString
"SUM"
  avgE :: Maybe (Sql92AggregationSetQuantifierSyntax SqliteExpressionSyntax)
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
avgE = ByteString
-> Maybe SqliteAggregationSetQuantifierSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
unAgg ByteString
"AVG"
  minE :: Maybe (Sql92AggregationSetQuantifierSyntax SqliteExpressionSyntax)
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
minE = ByteString
-> Maybe SqliteAggregationSetQuantifierSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
unAgg ByteString
"MIN"
  maxE :: Maybe (Sql92AggregationSetQuantifierSyntax SqliteExpressionSyntax)
-> SqliteExpressionSyntax -> SqliteExpressionSyntax
maxE = ByteString
-> Maybe SqliteAggregationSetQuantifierSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
unAgg ByteString
"MAX"

unAgg :: ByteString -> Maybe SqliteAggregationSetQuantifierSyntax -> SqliteExpressionSyntax
      -> SqliteExpressionSyntax
unAgg :: ByteString
-> Maybe SqliteAggregationSetQuantifierSyntax
-> SqliteExpressionSyntax
-> SqliteExpressionSyntax
unAgg ByteString
fn Maybe SqliteAggregationSetQuantifierSyntax
q SqliteExpressionSyntax
e =
  SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax forall a b. (a -> b) -> a -> b
$
  ByteString -> SqliteSyntax
emit ByteString
fn forall a. Semigroup a => a -> a -> a
<> SqliteSyntax -> SqliteSyntax
parens (forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqliteAggregationSetQuantifierSyntax
q -> SqliteAggregationSetQuantifierSyntax -> SqliteSyntax
fromSqliteAggregationSetQuantifier SqliteAggregationSetQuantifierSyntax
q forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" ") Maybe SqliteAggregationSetQuantifierSyntax
q forall a. Semigroup a => a -> a -> a
<>
                     SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
e)

instance IsSql92AggregationSetQuantifierSyntax SqliteAggregationSetQuantifierSyntax where
  setQuantifierDistinct :: SqliteAggregationSetQuantifierSyntax
setQuantifierDistinct = SqliteSyntax -> SqliteAggregationSetQuantifierSyntax
SqliteAggregationSetQuantifierSyntax (ByteString -> SqliteSyntax
emit ByteString
"DISTINCT")
  setQuantifierAll :: SqliteAggregationSetQuantifierSyntax
setQuantifierAll = SqliteSyntax -> SqliteAggregationSetQuantifierSyntax
SqliteAggregationSetQuantifierSyntax (ByteString -> SqliteSyntax
emit ByteString
"ALL")

instance IsSql92InsertSyntax SqliteInsertSyntax where
  type Sql92InsertTableNameSyntax SqliteInsertSyntax = SqliteTableNameSyntax
  type Sql92InsertValuesSyntax SqliteInsertSyntax = SqliteInsertValuesSyntax

  insertStmt :: Sql92InsertTableNameSyntax SqliteInsertSyntax
-> [Text]
-> Sql92InsertValuesSyntax SqliteInsertSyntax
-> SqliteInsertSyntax
insertStmt Sql92InsertTableNameSyntax SqliteInsertSyntax
table [Text]
fields Sql92InsertValuesSyntax SqliteInsertSyntax
values = SqliteTableNameSyntax
-> [Text]
-> SqliteInsertValuesSyntax
-> Maybe SqliteOnConflictSyntax
-> SqliteInsertSyntax
SqliteInsertSyntax Sql92InsertTableNameSyntax SqliteInsertSyntax
table [Text]
fields Sql92InsertValuesSyntax SqliteInsertSyntax
values forall a. Maybe a
Nothing

instance IsSql92InsertValuesSyntax SqliteInsertValuesSyntax where
  type Sql92InsertValuesExpressionSyntax SqliteInsertValuesSyntax = SqliteExpressionSyntax
  type Sql92InsertValuesSelectSyntax SqliteInsertValuesSyntax = SqliteSelectSyntax

  insertSqlExpressions :: [[Sql92InsertValuesExpressionSyntax SqliteInsertValuesSyntax]]
-> SqliteInsertValuesSyntax
insertSqlExpressions = [[SqliteExpressionSyntax]] -> SqliteInsertValuesSyntax
SqliteInsertExpressions
  insertFromSql :: Sql92InsertValuesSelectSyntax SqliteInsertValuesSyntax
-> SqliteInsertValuesSyntax
insertFromSql = SqliteSelectSyntax -> SqliteInsertValuesSyntax
SqliteInsertFromSql

instance IsSql92UpdateSyntax SqliteUpdateSyntax where
  type Sql92UpdateTableNameSyntax SqliteUpdateSyntax = SqliteTableNameSyntax
  type Sql92UpdateFieldNameSyntax SqliteUpdateSyntax = SqliteFieldNameSyntax
  type Sql92UpdateExpressionSyntax SqliteUpdateSyntax = SqliteExpressionSyntax

  updateStmt :: Sql92UpdateTableNameSyntax SqliteUpdateSyntax
-> [(Sql92UpdateFieldNameSyntax SqliteUpdateSyntax,
     Sql92UpdateExpressionSyntax SqliteUpdateSyntax)]
-> Maybe (Sql92UpdateExpressionSyntax SqliteUpdateSyntax)
-> SqliteUpdateSyntax
updateStmt Sql92UpdateTableNameSyntax SqliteUpdateSyntax
tbl [(Sql92UpdateFieldNameSyntax SqliteUpdateSyntax,
  Sql92UpdateExpressionSyntax SqliteUpdateSyntax)]
fields Maybe (Sql92UpdateExpressionSyntax SqliteUpdateSyntax)
where_ =
    SqliteSyntax -> SqliteUpdateSyntax
SqliteUpdateSyntax forall a b. (a -> b) -> a -> b
$
    ByteString -> SqliteSyntax
emit ByteString
"UPDATE " forall a. Semigroup a => a -> a -> a
<> SqliteTableNameSyntax -> SqliteSyntax
fromSqliteTableName Sql92UpdateTableNameSyntax SqliteUpdateSyntax
tbl forall a. Semigroup a => a -> a -> a
<>
    (case [(Sql92UpdateFieldNameSyntax SqliteUpdateSyntax,
  Sql92UpdateExpressionSyntax SqliteUpdateSyntax)]
fields of
       [] -> forall a. Monoid a => a
mempty
       [(Sql92UpdateFieldNameSyntax SqliteUpdateSyntax,
  Sql92UpdateExpressionSyntax SqliteUpdateSyntax)]
_ -> ByteString -> SqliteSyntax
emit ByteString
" SET " forall a. Semigroup a => a -> a -> a
<>
            [SqliteSyntax] -> SqliteSyntax
commas (forall a b. (a -> b) -> [a] -> [b]
map (\(SqliteFieldNameSyntax
field, SqliteExpressionSyntax
val) -> SqliteFieldNameSyntax -> SqliteSyntax
fromSqliteFieldNameSyntax SqliteFieldNameSyntax
field forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
"=" forall a. Semigroup a => a -> a -> a
<> SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
val) [(Sql92UpdateFieldNameSyntax SqliteUpdateSyntax,
  Sql92UpdateExpressionSyntax SqliteUpdateSyntax)]
fields)) forall a. Semigroup a => a -> a -> a
<>
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqliteExpressionSyntax
where_ -> ByteString -> SqliteSyntax
emit ByteString
" WHERE " forall a. Semigroup a => a -> a -> a
<> SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
where_) Maybe (Sql92UpdateExpressionSyntax SqliteUpdateSyntax)
where_

instance IsSql92DeleteSyntax SqliteDeleteSyntax where
  type Sql92DeleteTableNameSyntax SqliteDeleteSyntax = SqliteTableNameSyntax
  type Sql92DeleteExpressionSyntax SqliteDeleteSyntax = SqliteExpressionSyntax

  deleteStmt :: Sql92DeleteTableNameSyntax SqliteDeleteSyntax
-> Maybe Text
-> Maybe (Sql92DeleteExpressionSyntax SqliteDeleteSyntax)
-> SqliteDeleteSyntax
deleteStmt Sql92DeleteTableNameSyntax SqliteDeleteSyntax
tbl Maybe Text
Nothing Maybe (Sql92DeleteExpressionSyntax SqliteDeleteSyntax)
where_ =
    SqliteSyntax -> SqliteDeleteSyntax
SqliteDeleteSyntax forall a b. (a -> b) -> a -> b
$
    ByteString -> SqliteSyntax
emit ByteString
"DELETE FROM " forall a. Semigroup a => a -> a -> a
<> SqliteTableNameSyntax -> SqliteSyntax
fromSqliteTableName Sql92DeleteTableNameSyntax SqliteDeleteSyntax
tbl forall a. Semigroup a => a -> a -> a
<>
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\SqliteExpressionSyntax
where_ -> ByteString -> SqliteSyntax
emit ByteString
" WHERE " forall a. Semigroup a => a -> a -> a
<> SqliteExpressionSyntax -> SqliteSyntax
fromSqliteExpression SqliteExpressionSyntax
where_) Maybe (Sql92DeleteExpressionSyntax SqliteDeleteSyntax)
where_
  deleteStmt Sql92DeleteTableNameSyntax SqliteDeleteSyntax
_ (Just Text
_) Maybe (Sql92DeleteExpressionSyntax SqliteDeleteSyntax)
_ =
      forall a. HasCallStack => String -> a
error String
"beam-sqlite: invariant failed: DELETE must not have a table alias"

spaces, parens :: SqliteSyntax -> SqliteSyntax
spaces :: SqliteSyntax -> SqliteSyntax
spaces SqliteSyntax
a = ByteString -> SqliteSyntax
emit ByteString
" " forall a. Semigroup a => a -> a -> a
<> SqliteSyntax
a forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
" "
parens :: SqliteSyntax -> SqliteSyntax
parens SqliteSyntax
a = ByteString -> SqliteSyntax
emit ByteString
"(" forall a. Semigroup a => a -> a -> a
<> SqliteSyntax
a forall a. Semigroup a => a -> a -> a
<> ByteString -> SqliteSyntax
emit ByteString
")"

commas :: [SqliteSyntax] -> SqliteSyntax
commas :: [SqliteSyntax] -> SqliteSyntax
commas [] = forall a. Monoid a => a
mempty
commas [SqliteSyntax
x] = SqliteSyntax
x
commas (SqliteSyntax
x:[SqliteSyntax]
xs) = SqliteSyntax
x forall a. Semigroup a => a -> a -> a
<> forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (ByteString -> SqliteSyntax
emit ByteString
", " forall a. Semigroup a => a -> a -> a
<>) [SqliteSyntax]
xs

strftimeSyntax :: SqliteExpressionSyntax -> SqliteExpressionSyntax -> [ SqliteExpressionSyntax ] -> SqliteExpressionSyntax
strftimeSyntax :: SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> [SqliteExpressionSyntax]
-> SqliteExpressionSyntax
strftimeSyntax SqliteExpressionSyntax
fmt SqliteExpressionSyntax
ts [SqliteExpressionSyntax]
mods =
    forall expr.
IsSql99FunctionExpressionSyntax expr =>
expr -> [expr] -> expr
functionCallE (SqliteSyntax -> SqliteExpressionSyntax
SqliteExpressionSyntax (ByteString -> SqliteSyntax
emit ByteString
"strftime"))
                  (SqliteExpressionSyntax
fmtforall a. a -> [a] -> [a]
:SqliteExpressionSyntax
tsforall a. a -> [a] -> [a]
:[SqliteExpressionSyntax]
mods)

-- | SQLite does not support @EXTRACT@ directly, but we can emulate
-- the behavior if we know which field we want.
sqliteExtract :: ExtractField -> SqliteExpressionSyntax -> SqliteExpressionSyntax
sqliteExtract :: ExtractField -> SqliteExpressionSyntax -> SqliteExpressionSyntax
sqliteExtract ExtractField
field SqliteExpressionSyntax
from =
    case ExtractField
field of
      ExtractField
ExtractFieldTimeZoneHour   -> forall a. HasCallStack => String -> a
error String
"sqliteExtract: TODO ExtractFieldTimeZoneHour"
      ExtractField
ExtractFieldTimeZoneMinute -> forall a. HasCallStack => String -> a
error String
"sqliteExtract: TODO ExtractFieldTimeZoneMinute"

      ExtractField
ExtractFieldDateTimeYear   -> String -> SqliteExpressionSyntax
extractStrftime String
"%Y"
      ExtractField
ExtractFieldDateTimeMonth  -> String -> SqliteExpressionSyntax
extractStrftime String
"%m"
      ExtractField
ExtractFieldDateTimeDay    -> String -> SqliteExpressionSyntax
extractStrftime String
"%d"
      ExtractField
ExtractFieldDateTimeHour   -> String -> SqliteExpressionSyntax
extractStrftime String
"%H"
      ExtractField
ExtractFieldDateTimeMinute -> String -> SqliteExpressionSyntax
extractStrftime String
"%M"
      ExtractField
ExtractFieldDateTimeSecond -> String -> SqliteExpressionSyntax
extractStrftime String
"%S"

    where
      extractStrftime :: String -> SqliteExpressionSyntax
      extractStrftime :: String -> SqliteExpressionSyntax
extractStrftime String
fmt = SqliteExpressionSyntax
-> SqliteExpressionSyntax
-> [SqliteExpressionSyntax]
-> SqliteExpressionSyntax
strftimeSyntax (forall expr.
IsSql92ExpressionSyntax expr =>
Sql92ExpressionValueSyntax expr -> expr
valueE (forall expr ty. HasSqlValueSyntax expr ty => ty -> expr
sqlValueSyntax String
fmt)) SqliteExpressionSyntax
from []

sqliteSerialType :: SqliteDataTypeSyntax
sqliteSerialType :: SqliteDataTypeSyntax
sqliteSerialType = SqliteSyntax
-> HsDataType
-> BeamSerializedDataType
-> Bool
-> SqliteDataTypeSyntax
SqliteDataTypeSyntax (ByteString -> SqliteSyntax
emit ByteString
"INTEGER PRIMARY KEY AUTOINCREMENT")
                                        forall dataType. IsSql92DataTypeSyntax dataType => dataType
intType
                                        (Value -> BeamSerializedDataType
BeamSerializedDataType (Text -> Value -> Value
beamSerializeJSON Text
"sqlite" Value
"serial"))
                                        Bool
True

instance HasSqlValueSyntax SqliteValueSyntax ByteString where
  sqlValueSyntax :: ByteString -> SqliteValueSyntax
sqlValueSyntax ByteString
bs = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (ByteString -> SQLData
SQLBlob ByteString
bs))

instance HasSqlValueSyntax SqliteValueSyntax UTCTime where
  sqlValueSyntax :: UTCTime -> SqliteValueSyntax
sqlValueSyntax UTCTime
tm = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (forall a. ToField a => a -> SQLData
toField UTCTime
tm))

instance HasSqlValueSyntax SqliteValueSyntax LocalTime where
  sqlValueSyntax :: LocalTime -> SqliteValueSyntax
sqlValueSyntax LocalTime
tm = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Text -> SQLData
SQLText (forall a. IsString a => String -> a
fromString String
tmStr)))
    where tmStr :: String
tmStr = forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale (Maybe String -> String
iso8601DateFormat (forall a. a -> Maybe a
Just String
"%H:%M:%S%Q")) LocalTime
tm

instance HasSqlValueSyntax SqliteValueSyntax Day where
  sqlValueSyntax :: Day -> SqliteValueSyntax
sqlValueSyntax Day
tm = SqliteSyntax -> SqliteValueSyntax
SqliteValueSyntax (SQLData -> SqliteSyntax
emitValue (Text -> SQLData
SQLText (forall a. IsString a => String -> a
fromString String
tmStr)))
    where tmStr :: String
tmStr = forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale (Maybe String -> String
iso8601DateFormat forall a. Maybe a
Nothing) Day
tm

instance HasDataTypeCreatedCheck SqliteDataTypeSyntax where
  dataTypeHasBeenCreated :: SqliteDataTypeSyntax
-> (forall preCondition. Typeable preCondition => [preCondition])
-> Bool
dataTypeHasBeenCreated SqliteDataTypeSyntax
_ forall preCondition. Typeable preCondition => [preCondition]
_ = Bool
True