{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE CPP #-}

-- | Serialization and deserialization helpers for beam data types.
--
-- Used to read and write machine-readable schema descriptions.

module Database.Beam.Migrate.Serialization
       ( -- * Serialization helpers
         -- $serialization
         BeamSerializedDataType(..)
       , BeamSerializedConstraintDefinition(..)
       , BeamSerializedConstraintAttributes(..)
       , BeamSerializedConstraint(..)
       , BeamSerializedMatchType(..)
       , BeamSerializedReferentialAction(..)
       , BeamSerializedExpression(..)

       , beamSerializeJSON, serializePrecAndDecimal

       -- * Deserialization helpers
       -- $deserialization

       , BeamDeserializers(..)

       , beamDeserialize, beamDeserializeMaybe
       , beamDeserializer, sql92Deserializers
       , sql99DataTypeDeserializers
       , sql2003BinaryAndVarBinaryDataTypeDeserializers
       , sql2008BigIntDataTypeDeserializers
       ) where

import           Database.Beam.Backend.SQL
import           Database.Beam.Migrate.SQL.SQL92
import           Database.Beam.Migrate.SQL.Types

import           Control.Applicative
import           Control.Monad

import           Data.Aeson
#if MIN_VERSION_aeson(2,0,0)
import qualified Data.Aeson.Key as DAK
#endif
import           Data.Aeson.Types (Parser)
import qualified Data.Dependent.Map as D
import qualified Data.GADT.Compare as D
import           Data.Text (Text, unpack)
import           Data.Typeable (Typeable, (:~:)( Refl ), eqT, typeRep, typeOf)
import qualified Data.Vector as V
#if !MIN_VERSION_base(4, 11, 0)
import           Data.Semigroup
#endif

-- * Serialization helpers

-- | An 'IsSql92DataTypeSyntax' for JSON. Supports all superclasses of
-- `IsSql92DataTypeSyntax` declared in @beam-core@.
newtype BeamSerializedDataType
  = BeamSerializedDataType { BeamSerializedDataType -> Value
fromBeamSerializedDataType :: Value }
  deriving (Int -> BeamSerializedDataType -> ShowS
[BeamSerializedDataType] -> ShowS
BeamSerializedDataType -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BeamSerializedDataType] -> ShowS
$cshowList :: [BeamSerializedDataType] -> ShowS
show :: BeamSerializedDataType -> String
$cshow :: BeamSerializedDataType -> String
showsPrec :: Int -> BeamSerializedDataType -> ShowS
$cshowsPrec :: Int -> BeamSerializedDataType -> ShowS
Show, BeamSerializedDataType -> BeamSerializedDataType -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BeamSerializedDataType -> BeamSerializedDataType -> Bool
$c/= :: BeamSerializedDataType -> BeamSerializedDataType -> Bool
== :: BeamSerializedDataType -> BeamSerializedDataType -> Bool
$c== :: BeamSerializedDataType -> BeamSerializedDataType -> Bool
Eq)

instance IsSql92DataTypeSyntax BeamSerializedDataType where
  domainType :: Text -> BeamSerializedDataType
domainType Text
nm = Value -> BeamSerializedDataType
BeamSerializedDataType ([Pair] -> Value
object [ Key
"domain" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
nm])
  charType :: Maybe Word -> Maybe Text -> BeamSerializedDataType
charType Maybe Word
prec Maybe Text
collation =
    Value -> BeamSerializedDataType
BeamSerializedDataType ([Pair] -> Value
object [ Key
"char" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
object [ Key
"prec" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Word
prec
                                                      , Key
"collation" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Text
collation ]])
  varCharType :: Maybe Word -> Maybe Text -> BeamSerializedDataType
varCharType Maybe Word
prec Maybe Text
collation =
    Value -> BeamSerializedDataType
BeamSerializedDataType ([Pair] -> Value
object [ Key
"varchar" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
object [ Key
"prec" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Word
prec
                                                         , Key
"collation" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Text
collation ]])
  nationalCharType :: Maybe Word -> BeamSerializedDataType
nationalCharType Maybe Word
prec =
    Value -> BeamSerializedDataType
BeamSerializedDataType ([Pair] -> Value
object [ Key
"national-char" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
object [ Key
"prec" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Word
prec ]])
  nationalVarCharType :: Maybe Word -> BeamSerializedDataType
nationalVarCharType Maybe Word
prec =
    Value -> BeamSerializedDataType
BeamSerializedDataType ([Pair] -> Value
object [ Key
"national-varchar" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
object [ Key
"prec" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Word
prec ]])

  bitType :: Maybe Word -> BeamSerializedDataType
bitType Maybe Word
prec =
    Value -> BeamSerializedDataType
BeamSerializedDataType ([Pair] -> Value
object [ Key
"bit" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
object [ Key
"prec" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Word
prec ]])
  varBitType :: Maybe Word -> BeamSerializedDataType
varBitType Maybe Word
prec =
    Value -> BeamSerializedDataType
BeamSerializedDataType ([Pair] -> Value
object [ Key
"varbit" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
object [ Key
"prec" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Word
prec ]])

  numericType :: Maybe (Word, Maybe Word) -> BeamSerializedDataType
numericType Maybe (Word, Maybe Word)
precAndDecimal =
    Value -> BeamSerializedDataType
BeamSerializedDataType ([Pair] -> Value
object [ Key
"numeric" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe (Word, Maybe Word) -> Value
serializePrecAndDecimal Maybe (Word, Maybe Word)
precAndDecimal ])
  decimalType :: Maybe (Word, Maybe Word) -> BeamSerializedDataType
decimalType Maybe (Word, Maybe Word)
precAndDecimal =
    Value -> BeamSerializedDataType
BeamSerializedDataType ([Pair] -> Value
object [ Key
"decimal" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe (Word, Maybe Word) -> Value
serializePrecAndDecimal Maybe (Word, Maybe Word)
precAndDecimal ])

  intType :: BeamSerializedDataType
intType = Value -> BeamSerializedDataType
BeamSerializedDataType Value
"int"
  smallIntType :: BeamSerializedDataType
smallIntType = Value -> BeamSerializedDataType
BeamSerializedDataType Value
"smallint"
  floatType :: Maybe Word -> BeamSerializedDataType
floatType Maybe Word
prec =
    Value -> BeamSerializedDataType
BeamSerializedDataType ([Pair] -> Value
object [ Key
"float" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
object [ Key
"prec" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Word
prec ] ])
  doubleType :: BeamSerializedDataType
doubleType = Value -> BeamSerializedDataType
BeamSerializedDataType Value
"double"
  realType :: BeamSerializedDataType
realType = Value -> BeamSerializedDataType
BeamSerializedDataType Value
"real"

  dateType :: BeamSerializedDataType
dateType = Value -> BeamSerializedDataType
BeamSerializedDataType Value
"date"
  timeType :: Maybe Word -> Bool -> BeamSerializedDataType
timeType Maybe Word
prec Bool
withTz =
    Value -> BeamSerializedDataType
BeamSerializedDataType ([Pair] -> Value
object [ Key
"time" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
object [ Key
"prec" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Word
prec
                                                      , Key
"timezone" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Bool
withTz ]])
  timestampType :: Maybe Word -> Bool -> BeamSerializedDataType
timestampType Maybe Word
prec Bool
withTz =
    Value -> BeamSerializedDataType
BeamSerializedDataType ([Pair] -> Value
object [ Key
"timestamp" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
object [ Key
"prec" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Word
prec
                                                           , Key
"timezone" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Bool
withTz ]])

instance IsSql99DataTypeSyntax BeamSerializedDataType where
  characterLargeObjectType :: BeamSerializedDataType
characterLargeObjectType = Value -> BeamSerializedDataType
BeamSerializedDataType Value
"clob"
  binaryLargeObjectType :: BeamSerializedDataType
binaryLargeObjectType = Value -> BeamSerializedDataType
BeamSerializedDataType Value
"blob"
  booleanType :: BeamSerializedDataType
booleanType = Value -> BeamSerializedDataType
BeamSerializedDataType Value
"boolean"
  arrayType :: BeamSerializedDataType -> Int -> BeamSerializedDataType
arrayType BeamSerializedDataType
ty Int
count = Value -> BeamSerializedDataType
BeamSerializedDataType ([Pair] -> Value
object [ Key
"array" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
object [ Key
"of" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= BeamSerializedDataType
ty
                                                                          , Key
"count" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Int
count ]])
  rowType :: [(Text, BeamSerializedDataType)] -> BeamSerializedDataType
rowType [(Text, BeamSerializedDataType)]
tys = Value -> BeamSerializedDataType
BeamSerializedDataType ([Pair] -> Value
object [ Key
"row" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [(Text, BeamSerializedDataType)]
tys ])

instance IsSql2003BinaryAndVarBinaryDataTypeSyntax BeamSerializedDataType where
  binaryType :: Maybe Word -> BeamSerializedDataType
binaryType Maybe Word
sz = Value -> BeamSerializedDataType
BeamSerializedDataType ([Pair] -> Value
object [ Key
"binary" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Word
sz ])
  varBinaryType :: Maybe Word -> BeamSerializedDataType
varBinaryType Maybe Word
sz = Value -> BeamSerializedDataType
BeamSerializedDataType ([Pair] -> Value
object [ Key
"varbinary" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Word
sz ])

instance IsSql2008BigIntDataTypeSyntax BeamSerializedDataType where
  bigIntType :: BeamSerializedDataType
bigIntType = Value -> BeamSerializedDataType
BeamSerializedDataType Value
"bigint"

instance ToJSON BeamSerializedDataType where
  toJSON :: BeamSerializedDataType -> Value
toJSON = BeamSerializedDataType -> Value
fromBeamSerializedDataType

-- | 'IsSql92ColumnConstraintDefinitionSyntax' type for JSON
newtype BeamSerializedConstraintDefinition
  = BeamSerializedConstraintDefinition
  { BeamSerializedConstraintDefinition -> Value
fromBeamSerializedConstraintDefinition :: Value
  } deriving (Int -> BeamSerializedConstraintDefinition -> ShowS
[BeamSerializedConstraintDefinition] -> ShowS
BeamSerializedConstraintDefinition -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BeamSerializedConstraintDefinition] -> ShowS
$cshowList :: [BeamSerializedConstraintDefinition] -> ShowS
show :: BeamSerializedConstraintDefinition -> String
$cshow :: BeamSerializedConstraintDefinition -> String
showsPrec :: Int -> BeamSerializedConstraintDefinition -> ShowS
$cshowsPrec :: Int -> BeamSerializedConstraintDefinition -> ShowS
Show, BeamSerializedConstraintDefinition
-> BeamSerializedConstraintDefinition -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BeamSerializedConstraintDefinition
-> BeamSerializedConstraintDefinition -> Bool
$c/= :: BeamSerializedConstraintDefinition
-> BeamSerializedConstraintDefinition -> Bool
== :: BeamSerializedConstraintDefinition
-> BeamSerializedConstraintDefinition -> Bool
$c== :: BeamSerializedConstraintDefinition
-> BeamSerializedConstraintDefinition -> Bool
Eq)

-- | 'IsSql92ConstraintAttributesSyntax' type for JSON
newtype BeamSerializedConstraintAttributes
  = BeamSerializedConstraintAttributes
  { BeamSerializedConstraintAttributes -> [Value]
fromBeamSerializedConstraintAttributes :: [ Value ]
  } deriving (Int -> BeamSerializedConstraintAttributes -> ShowS
[BeamSerializedConstraintAttributes] -> ShowS
BeamSerializedConstraintAttributes -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BeamSerializedConstraintAttributes] -> ShowS
$cshowList :: [BeamSerializedConstraintAttributes] -> ShowS
show :: BeamSerializedConstraintAttributes -> String
$cshow :: BeamSerializedConstraintAttributes -> String
showsPrec :: Int -> BeamSerializedConstraintAttributes -> ShowS
$cshowsPrec :: Int -> BeamSerializedConstraintAttributes -> ShowS
Show, BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes -> Bool
$c/= :: BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes -> Bool
== :: BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes -> Bool
$c== :: BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes -> Bool
Eq, Semigroup BeamSerializedConstraintAttributes
BeamSerializedConstraintAttributes
[BeamSerializedConstraintAttributes]
-> BeamSerializedConstraintAttributes
BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
mconcat :: [BeamSerializedConstraintAttributes]
-> BeamSerializedConstraintAttributes
$cmconcat :: [BeamSerializedConstraintAttributes]
-> BeamSerializedConstraintAttributes
mappend :: BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
$cmappend :: BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
mempty :: BeamSerializedConstraintAttributes
$cmempty :: BeamSerializedConstraintAttributes
Monoid, NonEmpty BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
forall b.
Integral b =>
b
-> BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
stimes :: forall b.
Integral b =>
b
-> BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
$cstimes :: forall b.
Integral b =>
b
-> BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
sconcat :: NonEmpty BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
$csconcat :: NonEmpty BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
<> :: BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
$c<> :: BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
-> BeamSerializedConstraintAttributes
Semigroup)

-- | 'IsSql92ColumnConstraintSyntax' type for JSON
newtype BeamSerializedConstraint
  = BeamSerializedConstraint
  { BeamSerializedConstraint -> Value
fromBeamSerializedConstraint :: Value
  } deriving (Int -> BeamSerializedConstraint -> ShowS
[BeamSerializedConstraint] -> ShowS
BeamSerializedConstraint -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BeamSerializedConstraint] -> ShowS
$cshowList :: [BeamSerializedConstraint] -> ShowS
show :: BeamSerializedConstraint -> String
$cshow :: BeamSerializedConstraint -> String
showsPrec :: Int -> BeamSerializedConstraint -> ShowS
$cshowsPrec :: Int -> BeamSerializedConstraint -> ShowS
Show, BeamSerializedConstraint -> BeamSerializedConstraint -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BeamSerializedConstraint -> BeamSerializedConstraint -> Bool
$c/= :: BeamSerializedConstraint -> BeamSerializedConstraint -> Bool
== :: BeamSerializedConstraint -> BeamSerializedConstraint -> Bool
$c== :: BeamSerializedConstraint -> BeamSerializedConstraint -> Bool
Eq)

-- | 'IsSql92MatchTypeSyntax' type for JSON
newtype BeamSerializedMatchType
  = BeamSerializedMatchType
  { BeamSerializedMatchType -> Value
fromBeamSerializedMatchType :: Value
  } deriving (Int -> BeamSerializedMatchType -> ShowS
[BeamSerializedMatchType] -> ShowS
BeamSerializedMatchType -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BeamSerializedMatchType] -> ShowS
$cshowList :: [BeamSerializedMatchType] -> ShowS
show :: BeamSerializedMatchType -> String
$cshow :: BeamSerializedMatchType -> String
showsPrec :: Int -> BeamSerializedMatchType -> ShowS
$cshowsPrec :: Int -> BeamSerializedMatchType -> ShowS
Show, BeamSerializedMatchType -> BeamSerializedMatchType -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BeamSerializedMatchType -> BeamSerializedMatchType -> Bool
$c/= :: BeamSerializedMatchType -> BeamSerializedMatchType -> Bool
== :: BeamSerializedMatchType -> BeamSerializedMatchType -> Bool
$c== :: BeamSerializedMatchType -> BeamSerializedMatchType -> Bool
Eq)

-- | 'IsSql92ReferentialActionSyntax' type for JSON
newtype BeamSerializedReferentialAction
  = BeamSerializedReferentialAction
  { BeamSerializedReferentialAction -> Value
fromBeamSerializedReferentialAction :: Value
  } deriving (Int -> BeamSerializedReferentialAction -> ShowS
[BeamSerializedReferentialAction] -> ShowS
BeamSerializedReferentialAction -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BeamSerializedReferentialAction] -> ShowS
$cshowList :: [BeamSerializedReferentialAction] -> ShowS
show :: BeamSerializedReferentialAction -> String
$cshow :: BeamSerializedReferentialAction -> String
showsPrec :: Int -> BeamSerializedReferentialAction -> ShowS
$cshowsPrec :: Int -> BeamSerializedReferentialAction -> ShowS
Show, BeamSerializedReferentialAction
-> BeamSerializedReferentialAction -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BeamSerializedReferentialAction
-> BeamSerializedReferentialAction -> Bool
$c/= :: BeamSerializedReferentialAction
-> BeamSerializedReferentialAction -> Bool
== :: BeamSerializedReferentialAction
-> BeamSerializedReferentialAction -> Bool
$c== :: BeamSerializedReferentialAction
-> BeamSerializedReferentialAction -> Bool
Eq)

-- | 'IsSql92ExpressionSyntax' is too complex for us to store in JSON.
-- Additionally, many backends provide substantial amounts of extensions to the
-- syntax that would make storing this highly unfeasible. Expressions are
-- therefore represented as their full text rendering.
--
-- This means that expressions only match as equal if they match /exactly/.
-- While this may seem overly pedantic, it's not much of a concern if your
-- migrations are generated solely by @beam-migrate@. If you've modified the
-- schema yourself, you may have to use 'IsCustomSqlSyntax' to provide an exact
-- expression.
newtype BeamSerializedExpression
  = BeamSerializedExpression
  { BeamSerializedExpression -> Text
fromBeamSerializedExpression :: Text
  } deriving (Int -> BeamSerializedExpression -> ShowS
[BeamSerializedExpression] -> ShowS
BeamSerializedExpression -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BeamSerializedExpression] -> ShowS
$cshowList :: [BeamSerializedExpression] -> ShowS
show :: BeamSerializedExpression -> String
$cshow :: BeamSerializedExpression -> String
showsPrec :: Int -> BeamSerializedExpression -> ShowS
$cshowsPrec :: Int -> BeamSerializedExpression -> ShowS
Show, BeamSerializedExpression -> BeamSerializedExpression -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BeamSerializedExpression -> BeamSerializedExpression -> Bool
$c/= :: BeamSerializedExpression -> BeamSerializedExpression -> Bool
== :: BeamSerializedExpression -> BeamSerializedExpression -> Bool
$c== :: BeamSerializedExpression -> BeamSerializedExpression -> Bool
Eq)

instance IsSql92ColumnConstraintDefinitionSyntax BeamSerializedConstraintDefinition where
  type Sql92ColumnConstraintDefinitionAttributesSyntax BeamSerializedConstraintDefinition =
    BeamSerializedConstraintAttributes
  type Sql92ColumnConstraintDefinitionConstraintSyntax BeamSerializedConstraintDefinition =
    BeamSerializedConstraint

  constraintDefinitionSyntax :: Maybe Text
-> Sql92ColumnConstraintDefinitionConstraintSyntax
     BeamSerializedConstraintDefinition
-> Maybe
     (Sql92ColumnConstraintDefinitionAttributesSyntax
        BeamSerializedConstraintDefinition)
-> BeamSerializedConstraintDefinition
constraintDefinitionSyntax Maybe Text
nm Sql92ColumnConstraintDefinitionConstraintSyntax
  BeamSerializedConstraintDefinition
constraint Maybe
  (Sql92ColumnConstraintDefinitionAttributesSyntax
     BeamSerializedConstraintDefinition)
attrs =
    Value -> BeamSerializedConstraintDefinition
BeamSerializedConstraintDefinition forall a b. (a -> b) -> a -> b
$
    [Pair] -> Value
object [ Key
"name" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Text
nm
           , Key
"attributes" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap BeamSerializedConstraintAttributes -> [Value]
fromBeamSerializedConstraintAttributes Maybe
  (Sql92ColumnConstraintDefinitionAttributesSyntax
     BeamSerializedConstraintDefinition)
attrs
           , Key
"constraint" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= BeamSerializedConstraint -> Value
fromBeamSerializedConstraint Sql92ColumnConstraintDefinitionConstraintSyntax
  BeamSerializedConstraintDefinition
constraint ]

instance IsSql92ColumnConstraintSyntax BeamSerializedConstraint where
  type Sql92ColumnConstraintMatchTypeSyntax BeamSerializedConstraint =
    BeamSerializedMatchType
  type Sql92ColumnConstraintReferentialActionSyntax BeamSerializedConstraint =
    BeamSerializedReferentialAction
  type Sql92ColumnConstraintExpressionSyntax BeamSerializedConstraint =
    BeamSerializedExpression

  notNullConstraintSyntax :: BeamSerializedConstraint
notNullConstraintSyntax = Value -> BeamSerializedConstraint
BeamSerializedConstraint Value
"not-null"
  uniqueColumnConstraintSyntax :: BeamSerializedConstraint
uniqueColumnConstraintSyntax = Value -> BeamSerializedConstraint
BeamSerializedConstraint Value
"unique"
  primaryKeyColumnConstraintSyntax :: BeamSerializedConstraint
primaryKeyColumnConstraintSyntax = Value -> BeamSerializedConstraint
BeamSerializedConstraint Value
"primary-key"
  checkColumnConstraintSyntax :: Sql92ColumnConstraintExpressionSyntax BeamSerializedConstraint
-> BeamSerializedConstraint
checkColumnConstraintSyntax Sql92ColumnConstraintExpressionSyntax BeamSerializedConstraint
e = Value -> BeamSerializedConstraint
BeamSerializedConstraint ([Pair] -> Value
object [ Key
"check-column" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= BeamSerializedExpression -> Text
fromBeamSerializedExpression Sql92ColumnConstraintExpressionSyntax BeamSerializedConstraint
e])
  referencesConstraintSyntax :: Text
-> [Text]
-> Maybe
     (Sql92ColumnConstraintMatchTypeSyntax BeamSerializedConstraint)
-> Maybe
     (Sql92ColumnConstraintReferentialActionSyntax
        BeamSerializedConstraint)
-> Maybe
     (Sql92ColumnConstraintReferentialActionSyntax
        BeamSerializedConstraint)
-> BeamSerializedConstraint
referencesConstraintSyntax Text
tbl [Text]
fields Maybe
  (Sql92ColumnConstraintMatchTypeSyntax BeamSerializedConstraint)
matchType Maybe
  (Sql92ColumnConstraintReferentialActionSyntax
     BeamSerializedConstraint)
onUpdate Maybe
  (Sql92ColumnConstraintReferentialActionSyntax
     BeamSerializedConstraint)
onDelete =
    Value -> BeamSerializedConstraint
BeamSerializedConstraint ([Pair] -> Value
object [ Key
"references" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.=
                                         [Pair] -> Value
object [ Key
"table" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
tbl, Key
"fields" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Text]
fields
                                                , Key
"match-type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap BeamSerializedMatchType -> Value
fromBeamSerializedMatchType Maybe
  (Sql92ColumnConstraintMatchTypeSyntax BeamSerializedConstraint)
matchType
                                                , Key
"on-update"  forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap BeamSerializedReferentialAction -> Value
fromBeamSerializedReferentialAction Maybe
  (Sql92ColumnConstraintReferentialActionSyntax
     BeamSerializedConstraint)
onUpdate
                                                , Key
"on-delete"  forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap BeamSerializedReferentialAction -> Value
fromBeamSerializedReferentialAction Maybe
  (Sql92ColumnConstraintReferentialActionSyntax
     BeamSerializedConstraint)
onDelete ] ])

instance IsSql92MatchTypeSyntax BeamSerializedMatchType where
  fullMatchSyntax :: BeamSerializedMatchType
fullMatchSyntax = Value -> BeamSerializedMatchType
BeamSerializedMatchType Value
"full"
  partialMatchSyntax :: BeamSerializedMatchType
partialMatchSyntax = Value -> BeamSerializedMatchType
BeamSerializedMatchType Value
"partial"

instance IsSql92ReferentialActionSyntax BeamSerializedReferentialAction where
  referentialActionCascadeSyntax :: BeamSerializedReferentialAction
referentialActionCascadeSyntax = Value -> BeamSerializedReferentialAction
BeamSerializedReferentialAction Value
"cascade"
  referentialActionSetNullSyntax :: BeamSerializedReferentialAction
referentialActionSetNullSyntax = Value -> BeamSerializedReferentialAction
BeamSerializedReferentialAction Value
"set-null"
  referentialActionSetDefaultSyntax :: BeamSerializedReferentialAction
referentialActionSetDefaultSyntax = Value -> BeamSerializedReferentialAction
BeamSerializedReferentialAction Value
"set-default"
  referentialActionNoActionSyntax :: BeamSerializedReferentialAction
referentialActionNoActionSyntax = Value -> BeamSerializedReferentialAction
BeamSerializedReferentialAction Value
"nothing"

instance IsSql92ConstraintAttributesSyntax BeamSerializedConstraintAttributes where
  initiallyDeferredAttributeSyntax :: BeamSerializedConstraintAttributes
initiallyDeferredAttributeSyntax = [Value] -> BeamSerializedConstraintAttributes
BeamSerializedConstraintAttributes [ Value
"initially-deferred" ]
  initiallyImmediateAttributeSyntax :: BeamSerializedConstraintAttributes
initiallyImmediateAttributeSyntax = [Value] -> BeamSerializedConstraintAttributes
BeamSerializedConstraintAttributes [ Value
"initially-immediate" ]
  notDeferrableAttributeSyntax :: BeamSerializedConstraintAttributes
notDeferrableAttributeSyntax = [Value] -> BeamSerializedConstraintAttributes
BeamSerializedConstraintAttributes [ Value
"not-deferrable" ]
  deferrableAttributeSyntax :: BeamSerializedConstraintAttributes
deferrableAttributeSyntax = [Value] -> BeamSerializedConstraintAttributes
BeamSerializedConstraintAttributes [ Value
"deferrable" ]

-- | Some backends serialize data that can only be read by that backend. If so,
-- they should wrap these data in 'beamSerializeJSON', which provides a standard
-- syntax for specifying backend specific data, as well as which backend the
-- data are valid for.
--
-- The first argument is a string that is unique to a given backend
beamSerializeJSON :: Text -> Value -> Value
beamSerializeJSON :: Text -> Value -> Value
beamSerializeJSON Text
backend Value
v =
  [Pair] -> Value
object [ Key
"be-specific" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
backend
         , Key
"be-data" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Value
v ]

-- | Helper for serializing the precision and decimal count parameters to
-- 'decimalType', etc.
serializePrecAndDecimal :: Maybe (Word, Maybe Word) -> Value
serializePrecAndDecimal :: Maybe (Word, Maybe Word) -> Value
serializePrecAndDecimal Maybe (Word, Maybe Word)
Nothing =
  [Pair] -> Value
object []
serializePrecAndDecimal (Just (Word
prec, Maybe Word
Nothing)) =
  [Pair] -> Value
object [ Key
"prec" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Word
prec ]
serializePrecAndDecimal (Just (Word
prec, Just Word
decimal)) =
  [Pair] -> Value
object [ Key
"prec" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Word
prec
         , Key
"decimal" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Word
decimal ]

-- * Deserialization helpers

-- ** Data types

newtype BeamDeserializer syntax
  = BeamDeserializer (forall be. BeamDeserializers be -> Value -> Parser syntax)

-- | Provides a collection of deserializers from aeson 'Value's for arbitrary
-- types. The @cmd@ type parameter is a phantom type parameter. Notionally, all
-- deserializers within this 'BeamDeserializers' relate to the @cmd@ syntax.
newtype BeamDeserializers be
  = BeamDeserializers
  { forall be.
BeamDeserializers be -> DMap BeamDeserializerLabel BeamDeserializer
beamArbitraryDeserializers :: D.DMap BeamDeserializerLabel BeamDeserializer
  }

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

instance Monoid (BeamDeserializer be) where
  mempty :: BeamDeserializer be
mempty = forall syntax.
(forall be. BeamDeserializers be -> Value -> Parser syntax)
-> BeamDeserializer syntax
BeamDeserializer (forall a b. a -> b -> a
const (forall a b. a -> b -> a
const forall (m :: * -> *) a. MonadPlus m => m a
mzero))
  mappend :: BeamDeserializer be -> BeamDeserializer be -> BeamDeserializer be
mappend (BeamDeserializer forall be. BeamDeserializers be -> Value -> Parser be
a) (BeamDeserializer forall be. BeamDeserializers be -> Value -> Parser be
b) =
    forall syntax.
(forall be. BeamDeserializers be -> Value -> Parser syntax)
-> BeamDeserializer syntax
BeamDeserializer forall a b. (a -> b) -> a -> b
$ \BeamDeserializers be
d Value
o ->
    forall be. BeamDeserializers be -> Value -> Parser be
a BeamDeserializers be
d Value
o forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall be. BeamDeserializers be -> Value -> Parser be
b BeamDeserializers be
d Value
o

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

instance Monoid (BeamDeserializers be) where
  mempty :: BeamDeserializers be
mempty = forall be.
DMap BeamDeserializerLabel BeamDeserializer -> BeamDeserializers be
BeamDeserializers forall a. Monoid a => a
mempty
  mappend :: BeamDeserializers be
-> BeamDeserializers be -> BeamDeserializers be
mappend (BeamDeserializers DMap BeamDeserializerLabel BeamDeserializer
a) (BeamDeserializers DMap BeamDeserializerLabel BeamDeserializer
b) =
    forall be.
DMap BeamDeserializerLabel BeamDeserializer -> BeamDeserializers be
BeamDeserializers (forall {k1} (k2 :: k1 -> *) (f :: k1 -> *).
GCompare k2 =>
(forall (v :: k1). k2 v -> f v -> f v -> f v)
-> DMap k2 f -> DMap k2 f -> DMap k2 f
D.unionWithKey (forall a b. a -> b -> a
const forall a. Monoid a => a -> a -> a
mappend) DMap BeamDeserializerLabel BeamDeserializer
a DMap BeamDeserializerLabel BeamDeserializer
b)

-- | Helper function to deserialize data from a 'Maybe' 'Value'.
--
-- @
-- beamDeserializeMaybe _ Nothing = pure Nothing
-- beamDeserializeMaybe d (Just v) = Just <$> beamDeserialize d v
-- @
--
beamDeserializeMaybe :: Typeable a
                     => BeamDeserializers be
                     -> Maybe Value
                     -> Parser (Maybe a)
beamDeserializeMaybe :: forall a be.
Typeable a =>
BeamDeserializers be -> Maybe Value -> Parser (Maybe a)
beamDeserializeMaybe BeamDeserializers be
_ Maybe Value
Nothing = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
beamDeserializeMaybe BeamDeserializers be
d (Just Value
v) =
  forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a be.
Typeable a =>
BeamDeserializers be -> Value -> Parser a
beamDeserialize BeamDeserializers be
d Value
v

-- | Deserialize the requested type from the given deserializers and aeson 'Value'.
beamDeserialize :: forall a be. Typeable a
                => BeamDeserializers be -> Value
                -> Parser a
beamDeserialize :: forall a be.
Typeable a =>
BeamDeserializers be -> Value -> Parser a
beamDeserialize allD :: BeamDeserializers be
allD@(BeamDeserializers DMap BeamDeserializerLabel BeamDeserializer
d) Value
v =
  case forall {k1} (k2 :: k1 -> *) (f :: k1 -> *) (v :: k1).
GCompare k2 =>
k2 v -> DMap k2 f -> Maybe (f v)
D.lookup (forall ty. Typeable ty => BeamDeserializerLabel ty
BeamDeserializerLabel :: BeamDeserializerLabel a) DMap BeamDeserializerLabel BeamDeserializer
d of
    Maybe (BeamDeserializer a)
Nothing -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"beamDeserialize: No deserializer for " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall a. Typeable a => a -> TypeRep
typeOf (forall a. HasCallStack => a
undefined :: a)))
    Just (BeamDeserializer forall be. BeamDeserializers be -> Value -> Parser a
doParse) ->
      forall be. BeamDeserializers be -> Value -> Parser a
doParse BeamDeserializers be
allD Value
v

data BeamDeserializerLabel ty where
  BeamDeserializerLabel :: Typeable ty
                        => BeamDeserializerLabel ty
instance D.GEq BeamDeserializerLabel where
  geq :: forall a b.
BeamDeserializerLabel a
-> BeamDeserializerLabel b -> Maybe (a :~: b)
geq BeamDeserializerLabel a
a BeamDeserializerLabel b
b =
    case forall k (f :: k -> *) (a :: k) (b :: k).
GCompare f =>
f a -> f b -> GOrdering a b
D.gcompare BeamDeserializerLabel a
a BeamDeserializerLabel b
b of
      GOrdering a b
D.GEQ -> forall a. a -> Maybe a
Just forall {k} (a :: k). a :~: a
Refl
      GOrdering a b
_ -> forall a. Maybe a
Nothing
instance D.GCompare BeamDeserializerLabel where
  gcompare :: forall a b.
BeamDeserializerLabel a -> BeamDeserializerLabel b -> GOrdering a b
gcompare a :: BeamDeserializerLabel a
a@(BeamDeserializerLabel a
BeamDeserializerLabel :: BeamDeserializerLabel a)
           b :: BeamDeserializerLabel b
b@(BeamDeserializerLabel b
BeamDeserializerLabel :: BeamDeserializerLabel b) =
    case forall {k} (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT of
      Just (a :~: b
Refl :: a :~: b)-> forall {k} (a :: k). GOrdering a a
D.GEQ
      Maybe (a :~: b)
Nothing ->
        case forall a. Ord a => a -> a -> Ordering
compare (forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep BeamDeserializerLabel a
a) (forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep BeamDeserializerLabel b
b) of
          Ordering
LT -> forall {k} (a :: k) (b :: k). GOrdering a b
D.GLT
          Ordering
GT -> forall {k} (a :: k) (b :: k). GOrdering a b
D.GGT
          Ordering
EQ -> forall a. HasCallStack => String -> a
error String
"Impossible"

beamDeserializer :: Typeable ty
                 => (forall be'. BeamDeserializers be' -> Value -> Parser ty)
                 -> BeamDeserializers be
beamDeserializer :: forall ty be.
Typeable ty =>
(forall be'. BeamDeserializers be' -> Value -> Parser ty)
-> BeamDeserializers be
beamDeserializer forall be'. BeamDeserializers be' -> Value -> Parser ty
parse =
  forall be.
DMap BeamDeserializerLabel BeamDeserializer -> BeamDeserializers be
BeamDeserializers (forall {k1} (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f
D.singleton forall ty. Typeable ty => BeamDeserializerLabel ty
BeamDeserializerLabel (forall syntax.
(forall be. BeamDeserializers be -> Value -> Parser syntax)
-> BeamDeserializer syntax
BeamDeserializer forall be'. BeamDeserializers be' -> Value -> Parser ty
parse))

-- | Deserializers for SQL92 syntaxes
sql92Deserializers :: forall be
                    . BeamMigrateSqlBackend be
                   => BeamDeserializers be
sql92Deserializers :: forall be. BeamMigrateSqlBackend be => BeamDeserializers be
sql92Deserializers = forall a. Monoid a => [a] -> a
mconcat
                   [ forall ty be.
Typeable ty =>
(forall be'. BeamDeserializers be' -> Value -> Parser ty)
-> BeamDeserializers be
beamDeserializer forall be'.
BeamDeserializers be'
-> Value
-> Parser
     (Sql92ExpressionCastTargetSyntax
        (Sql92ExpressionSyntax (BeamSqlBackendSyntax be)))
deserializeSql92DataType
                   , forall ty be.
Typeable ty =>
(forall be'. BeamDeserializers be' -> Value -> Parser ty)
-> BeamDeserializers be
beamDeserializer forall be'.
BeamDeserializers be'
-> Value
-> Parser
     (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
        (Sql92CreateTableColumnSchemaSyntax
           (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
deserializeSql92ConstraintDefinition
                   , forall ty be.
Typeable ty =>
(forall be'. BeamDeserializers be' -> Value -> Parser ty)
-> BeamDeserializers be
beamDeserializer forall be'.
BeamDeserializers be'
-> Value
-> Parser
     (Sql92ColumnConstraintDefinitionConstraintSyntax
        (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
           (Sql92CreateTableColumnSchemaSyntax
              (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
deserializeSql92Constraint
                   , forall ty be.
Typeable ty =>
(forall be'. BeamDeserializers be' -> Value -> Parser ty)
-> BeamDeserializers be
beamDeserializer forall be'.
BeamDeserializers be'
-> Value
-> Parser
     (Sql92ColumnConstraintMatchTypeSyntax
        (Sql92ColumnConstraintDefinitionConstraintSyntax
           (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
              (Sql92CreateTableColumnSchemaSyntax
                 (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))))
deserializeSql92MatchType
                   , forall ty be.
Typeable ty =>
(forall be'. BeamDeserializers be' -> Value -> Parser ty)
-> BeamDeserializers be
beamDeserializer forall be'.
BeamDeserializers be'
-> Value
-> Parser
     (Sql92ColumnConstraintReferentialActionSyntax
        (Sql92ColumnConstraintDefinitionConstraintSyntax
           (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
              (Sql92CreateTableColumnSchemaSyntax
                 (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))))
deserializeSql92ReferentialAction
                   , forall ty be.
Typeable ty =>
(forall be'. BeamDeserializers be' -> Value -> Parser ty)
-> BeamDeserializers be
beamDeserializer forall be'.
BeamDeserializers be'
-> Value
-> Parser
     (Sql92ColumnConstraintDefinitionAttributesSyntax
        (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
           (Sql92CreateTableColumnSchemaSyntax
              (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
deserializeSql92Attributes ]
  where
#if MIN_VERSION_aeson(2,0,0)
    makeKey :: Text -> Key
makeKey = Text -> Key
DAK.fromText
#else
    makeKey = id
#endif
    parseSub :: Text -> Object -> Text -> (Object -> Parser b) -> Parser b
parseSub Text
nm Object
o Text
key Object -> Parser b
parse =
      forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject (Text -> String
unpack (Text
nm forall a. Semigroup a => a -> a -> a
<> Text
"." forall a. Semigroup a => a -> a -> a
<> Text
key)) Object -> Parser b
parse forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Text -> Key
makeKey Text
key

    deserializeSql92DataType :: BeamDeserializers be' -> Value
                             -> Parser (BeamSqlBackendDataTypeSyntax be)
    deserializeSql92DataType :: forall be'.
BeamDeserializers be'
-> Value
-> Parser
     (Sql92ExpressionCastTargetSyntax
        (Sql92ExpressionSyntax (BeamSqlBackendSyntax be)))
deserializeSql92DataType BeamDeserializers be'
_ Value
o =
      Value
-> Parser
     (Sql92ColumnSchemaColumnTypeSyntax
        (Sql92CreateTableColumnSchemaSyntax
           (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
deserializeSql92DataTypeObject Value
o forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
      forall {a} {f :: * -> *} {a}.
(Eq a, IsString a, IsSql92DataTypeSyntax a, MonadPlus f) =>
a -> f a
deserializeSql92DataTypeScalar Value
o

    deserializeSql92DataTypeScalar :: a -> f a
deserializeSql92DataTypeScalar a
"int" = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall dataType. IsSql92DataTypeSyntax dataType => dataType
intType
    deserializeSql92DataTypeScalar a
"smallint" = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall dataType. IsSql92DataTypeSyntax dataType => dataType
smallIntType
    deserializeSql92DataTypeScalar a
"double" = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall dataType. IsSql92DataTypeSyntax dataType => dataType
dateType
    deserializeSql92DataTypeScalar a
"real" = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall dataType. IsSql92DataTypeSyntax dataType => dataType
realType
    deserializeSql92DataTypeScalar a
"date" = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall dataType. IsSql92DataTypeSyntax dataType => dataType
dateType
    deserializeSql92DataTypeScalar a
_ = forall (m :: * -> *) a. MonadPlus m => m a
mzero

    deserializeSql92DataTypeObject :: Value
-> Parser
     (Sql92ColumnSchemaColumnTypeSyntax
        (Sql92CreateTableColumnSchemaSyntax
           (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
deserializeSql92DataTypeObject =
      forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Sql92DataType" forall a b. (a -> b) -> a -> b
$ \Object
o ->
      let ==> :: Text
-> (Object
    -> Parser
         (Sql92ColumnSchemaColumnTypeSyntax
            (Sql92CreateTableColumnSchemaSyntax
               (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
-> Parser
     (Sql92ColumnSchemaColumnTypeSyntax
        (Sql92CreateTableColumnSchemaSyntax
           (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
(==>) = forall {b}.
Text -> Object -> Text -> (Object -> Parser b) -> Parser b
parseSub Text
"Sql92DataType" Object
o
      in (forall dataType. IsSql92DataTypeSyntax dataType => Text -> dataType
domainType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"domain") forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
         (Text
"char" Text
-> (Object
    -> Parser
         (Sql92ColumnSchemaColumnTypeSyntax
            (Sql92CreateTableColumnSchemaSyntax
               (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
-> Parser
     (Sql92ColumnSchemaColumnTypeSyntax
        (Sql92CreateTableColumnSchemaSyntax
           (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
==> \Object
v ->
             forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> Maybe Text -> dataType
charType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"prec" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"collation") forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
         (Text
"varchar" Text
-> (Object
    -> Parser
         (Sql92ColumnSchemaColumnTypeSyntax
            (Sql92CreateTableColumnSchemaSyntax
               (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
-> Parser
     (Sql92ColumnSchemaColumnTypeSyntax
        (Sql92CreateTableColumnSchemaSyntax
           (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
==> \Object
v ->
             forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> Maybe Text -> dataType
varCharType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"prec" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"collation") forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
         (Text
"national-char" Text
-> (Object
    -> Parser
         (Sql92ColumnSchemaColumnTypeSyntax
            (Sql92CreateTableColumnSchemaSyntax
               (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
-> Parser
     (Sql92ColumnSchemaColumnTypeSyntax
        (Sql92CreateTableColumnSchemaSyntax
           (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
==> \Object
v ->
             forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
nationalCharType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"prec") forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
         (Text
"national-varchar" Text
-> (Object
    -> Parser
         (Sql92ColumnSchemaColumnTypeSyntax
            (Sql92CreateTableColumnSchemaSyntax
               (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
-> Parser
     (Sql92ColumnSchemaColumnTypeSyntax
        (Sql92CreateTableColumnSchemaSyntax
           (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
==> \Object
v ->
             forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
nationalVarCharType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"prec") forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
         (Text
"bit" Text
-> (Object
    -> Parser
         (Sql92ColumnSchemaColumnTypeSyntax
            (Sql92CreateTableColumnSchemaSyntax
               (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
-> Parser
     (Sql92ColumnSchemaColumnTypeSyntax
        (Sql92CreateTableColumnSchemaSyntax
           (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
==> \Object
v ->
             forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
bitType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"prec") forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
         (Text
"varbit" Text
-> (Object
    -> Parser
         (Sql92ColumnSchemaColumnTypeSyntax
            (Sql92CreateTableColumnSchemaSyntax
               (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
-> Parser
     (Sql92ColumnSchemaColumnTypeSyntax
        (Sql92CreateTableColumnSchemaSyntax
           (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
==> \Object
v ->
             forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
varBitType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"prec") forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
         (Text
"numeric" Text
-> (Object
    -> Parser
         (Sql92ColumnSchemaColumnTypeSyntax
            (Sql92CreateTableColumnSchemaSyntax
               (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
-> Parser
     (Sql92ColumnSchemaColumnTypeSyntax
        (Sql92CreateTableColumnSchemaSyntax
           (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
==> \Object
v ->
             forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe (Word, Maybe Word) -> dataType
numericType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {a} {a}.
(FromJSON a, FromJSON a) =>
Object -> Parser (Maybe (a, Maybe a))
deserializePrecAndDecimal Object
v) forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
         (Text
"decimal" Text
-> (Object
    -> Parser
         (Sql92ColumnSchemaColumnTypeSyntax
            (Sql92CreateTableColumnSchemaSyntax
               (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
-> Parser
     (Sql92ColumnSchemaColumnTypeSyntax
        (Sql92CreateTableColumnSchemaSyntax
           (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
==> \Object
v ->
             forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe (Word, Maybe Word) -> dataType
decimalType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {a} {a}.
(FromJSON a, FromJSON a) =>
Object -> Parser (Maybe (a, Maybe a))
deserializePrecAndDecimal Object
v) forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
         (Text
"float" Text
-> (Object
    -> Parser
         (Sql92ColumnSchemaColumnTypeSyntax
            (Sql92CreateTableColumnSchemaSyntax
               (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
-> Parser
     (Sql92ColumnSchemaColumnTypeSyntax
        (Sql92CreateTableColumnSchemaSyntax
           (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
==> \Object
v ->
             forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> dataType
floatType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"prec") forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
         (Text
"time" Text
-> (Object
    -> Parser
         (Sql92ColumnSchemaColumnTypeSyntax
            (Sql92CreateTableColumnSchemaSyntax
               (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
-> Parser
     (Sql92ColumnSchemaColumnTypeSyntax
        (Sql92CreateTableColumnSchemaSyntax
           (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
==> \Object
v ->
             forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> Bool -> dataType
timeType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"prec" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"timezone") forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
         (Text
"timestamp" Text
-> (Object
    -> Parser
         (Sql92ColumnSchemaColumnTypeSyntax
            (Sql92CreateTableColumnSchemaSyntax
               (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
-> Parser
     (Sql92ColumnSchemaColumnTypeSyntax
        (Sql92CreateTableColumnSchemaSyntax
           (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
==> \Object
v ->
             forall dataType.
IsSql92DataTypeSyntax dataType =>
Maybe Word -> Bool -> dataType
timestampType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"prec" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"timezone")

    deserializePrecAndDecimal :: Object -> Parser (Maybe (a, Maybe a))
deserializePrecAndDecimal Object
o =
      forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (((,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"prec" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"decimal")) forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                ((,forall a. Maybe a
Nothing) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"prec")) forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
      forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing

    deserializeSql92ConstraintDefinition :: BeamDeserializers be' -> Value
                                         -> Parser (BeamSqlBackendColumnConstraintDefinitionSyntax be)
    deserializeSql92ConstraintDefinition :: forall be'.
BeamDeserializers be'
-> Value
-> Parser
     (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
        (Sql92CreateTableColumnSchemaSyntax
           (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
deserializeSql92ConstraintDefinition BeamDeserializers be'
d =
      forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Sql92ColumnConstraintDefinition" forall a b. (a -> b) -> a -> b
$ \Object
o ->
      forall constraint.
IsSql92ColumnConstraintDefinitionSyntax constraint =>
Maybe Text
-> Sql92ColumnConstraintDefinitionConstraintSyntax constraint
-> Maybe
     (Sql92ColumnConstraintDefinitionAttributesSyntax constraint)
-> constraint
constraintDefinitionSyntax forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"name"
                                 forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (forall a be.
Typeable a =>
BeamDeserializers be -> Value -> Parser a
beamDeserialize BeamDeserializers be'
d forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"constraint")
                                 forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (forall a be.
Typeable a =>
BeamDeserializers be -> Maybe Value -> Parser (Maybe a)
beamDeserializeMaybe BeamDeserializers be'
d forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"attributes")

    deserializeSql92Constraint :: BeamDeserializers be' -> Value
                               -> Parser (BeamSqlBackendConstraintSyntax be)
    deserializeSql92Constraint :: forall be'.
BeamDeserializers be'
-> Value
-> Parser
     (Sql92ColumnConstraintDefinitionConstraintSyntax
        (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
           (Sql92CreateTableColumnSchemaSyntax
              (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
deserializeSql92Constraint BeamDeserializers be'
d Value
o =
      case Value
o of
        Value
"not-null" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall constraint.
IsSql92ColumnConstraintSyntax constraint =>
constraint
notNullConstraintSyntax
        Value
"unique" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall constraint.
IsSql92ColumnConstraintSyntax constraint =>
constraint
uniqueColumnConstraintSyntax
        Value
_ -> forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Sql92ColumnConstraint" Object
-> Parser
     (Sql92ColumnConstraintDefinitionConstraintSyntax
        (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
           (Sql92CreateTableColumnSchemaSyntax
              (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
parseObject Value
o
      where
        parseObject :: Object
-> Parser
     (Sql92ColumnConstraintDefinitionConstraintSyntax
        (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
           (Sql92CreateTableColumnSchemaSyntax
              (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
parseObject Object
v =
          let ==> :: Text
-> (Object
    -> Parser
         (Sql92ColumnConstraintDefinitionConstraintSyntax
            (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
               (Sql92CreateTableColumnSchemaSyntax
                  (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))))
-> Parser
     (Sql92ColumnConstraintDefinitionConstraintSyntax
        (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
           (Sql92CreateTableColumnSchemaSyntax
              (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
(==>) = forall {b}.
Text -> Object -> Text -> (Object -> Parser b) -> Parser b
parseSub Text
"Sql92ColumnConstraint" Object
v
          in forall constraint.
IsSql92ColumnConstraintSyntax constraint =>
Sql92ColumnConstraintExpressionSyntax constraint -> constraint
checkColumnConstraintSyntax forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall a be.
Typeable a =>
BeamDeserializers be -> Value -> Parser a
beamDeserialize BeamDeserializers be'
d forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"check-column") forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
             (Text
"references" Text
-> (Object
    -> Parser
         (Sql92ColumnConstraintDefinitionConstraintSyntax
            (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
               (Sql92CreateTableColumnSchemaSyntax
                  (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))))
-> Parser
     (Sql92ColumnConstraintDefinitionConstraintSyntax
        (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
           (Sql92CreateTableColumnSchemaSyntax
              (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
==> \Object
v' ->
                 forall constraint.
IsSql92ColumnConstraintSyntax constraint =>
Text
-> [Text]
-> Maybe (Sql92ColumnConstraintMatchTypeSyntax constraint)
-> Maybe (Sql92ColumnConstraintReferentialActionSyntax constraint)
-> Maybe (Sql92ColumnConstraintReferentialActionSyntax constraint)
-> constraint
referencesConstraintSyntax forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v' forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"table" forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v' forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"fields"
                                            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (forall a be.
Typeable a =>
BeamDeserializers be -> Maybe Value -> Parser (Maybe a)
beamDeserializeMaybe BeamDeserializers be'
d forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Object
v' forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"match-type")
                                            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (forall a be.
Typeable a =>
BeamDeserializers be -> Maybe Value -> Parser (Maybe a)
beamDeserializeMaybe BeamDeserializers be'
d forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Object
v' forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"on-update")
                                            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (forall a be.
Typeable a =>
BeamDeserializers be -> Maybe Value -> Parser (Maybe a)
beamDeserializeMaybe BeamDeserializers be'
d forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Object
v' forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"on-delete"))

    deserializeSql92MatchType :: BeamDeserializers be' -> Value
                              -> Parser (BeamSqlBackendMatchTypeSyntax be)
    deserializeSql92MatchType :: forall be'.
BeamDeserializers be'
-> Value
-> Parser
     (Sql92ColumnConstraintMatchTypeSyntax
        (Sql92ColumnConstraintDefinitionConstraintSyntax
           (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
              (Sql92CreateTableColumnSchemaSyntax
                 (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))))
deserializeSql92MatchType BeamDeserializers be'
_ Value
v =
      case Value
v of
        Value
"full" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall match. IsSql92MatchTypeSyntax match => match
fullMatchSyntax
        Value
"partial" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall match. IsSql92MatchTypeSyntax match => match
partialMatchSyntax
        Value
_ -> forall (m :: * -> *) a. MonadPlus m => m a
mzero

    deserializeSql92ReferentialAction :: BeamDeserializers be' -> Value
                                      -> Parser (BeamSqlBackendReferentialActionSyntax be)
    deserializeSql92ReferentialAction :: forall be'.
BeamDeserializers be'
-> Value
-> Parser
     (Sql92ColumnConstraintReferentialActionSyntax
        (Sql92ColumnConstraintDefinitionConstraintSyntax
           (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
              (Sql92CreateTableColumnSchemaSyntax
                 (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))))
deserializeSql92ReferentialAction BeamDeserializers be'
_ Value
v =
      case Value
v of
        Value
"cascade" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall refAction.
IsSql92ReferentialActionSyntax refAction =>
refAction
referentialActionCascadeSyntax
        Value
"set-null" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall refAction.
IsSql92ReferentialActionSyntax refAction =>
refAction
referentialActionSetNullSyntax
        Value
"set-default" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall refAction.
IsSql92ReferentialActionSyntax refAction =>
refAction
referentialActionSetDefaultSyntax
        Value
"nothing" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall refAction.
IsSql92ReferentialActionSyntax refAction =>
refAction
referentialActionNoActionSyntax
        Value
_ -> forall (m :: * -> *) a. MonadPlus m => m a
mzero

    deserializeSql92Attributes :: BeamDeserializers be' -> Value
                               -> Parser (BeamSqlBackendConstraintAttributesSyntax be)
    deserializeSql92Attributes :: forall be'.
BeamDeserializers be'
-> Value
-> Parser
     (Sql92ColumnConstraintDefinitionAttributesSyntax
        (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
           (Sql92CreateTableColumnSchemaSyntax
              (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be)))))
deserializeSql92Attributes BeamDeserializers be'
_ =
      forall a. String -> (Array -> Parser a) -> Value -> Parser a
withArray String
"Sql92Attributes" forall a b. (a -> b) -> a -> b
$ \Array
a ->
      forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\Value
o Sql92ColumnConstraintDefinitionAttributesSyntax
  (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
     (Sql92CreateTableColumnSchemaSyntax
        (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
accum ->
                      case Value
o of
                        Value
"initially-deferred" -> forall attrs. IsSql92ConstraintAttributesSyntax attrs => attrs
initiallyDeferredAttributeSyntax forall a. Semigroup a => a -> a -> a
<> Sql92ColumnConstraintDefinitionAttributesSyntax
  (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
     (Sql92CreateTableColumnSchemaSyntax
        (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
accum
                        Value
"initially-immediate" -> forall attrs. IsSql92ConstraintAttributesSyntax attrs => attrs
initiallyImmediateAttributeSyntax forall a. Semigroup a => a -> a -> a
<> Sql92ColumnConstraintDefinitionAttributesSyntax
  (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
     (Sql92CreateTableColumnSchemaSyntax
        (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
accum
                        Value
"not-deferrable" -> forall attrs. IsSql92ConstraintAttributesSyntax attrs => attrs
notDeferrableAttributeSyntax forall a. Semigroup a => a -> a -> a
<> Sql92ColumnConstraintDefinitionAttributesSyntax
  (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
     (Sql92CreateTableColumnSchemaSyntax
        (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
accum
                        Value
"deferrable" -> forall attrs. IsSql92ConstraintAttributesSyntax attrs => attrs
deferrableAttributeSyntax forall a. Semigroup a => a -> a -> a
<> Sql92ColumnConstraintDefinitionAttributesSyntax
  (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
     (Sql92CreateTableColumnSchemaSyntax
        (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
accum
                        Value
_ -> Sql92ColumnConstraintDefinitionAttributesSyntax
  (Sql92ColumnSchemaColumnConstraintDefinitionSyntax
     (Sql92CreateTableColumnSchemaSyntax
        (Sql92DdlCommandCreateTableSyntax (BeamSqlBackendSyntax be))))
accum
                  ) forall a. Monoid a => a
mempty Array
a)

-- | Deserializes data types that are instances of 'IsSql99DataTypeSyntax'
sql99DataTypeDeserializers
  :: forall be
   . BeamMigrateSql99Backend be
  => BeamDeserializers be
sql99DataTypeDeserializers :: forall be. BeamMigrateSql99Backend be => BeamDeserializers be
sql99DataTypeDeserializers =
  forall ty be.
Typeable ty =>
(forall be'. BeamDeserializers be' -> Value -> Parser ty)
-> BeamDeserializers be
beamDeserializer forall a b. (a -> b) -> a -> b
$ \BeamDeserializers be'
d Value
v ->
  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. a -> a
id @(BeamSqlBackendDataTypeSyntax be)) forall a b. (a -> b) -> a -> b
$
  case Value
v of
    Value
"clob" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall dataType. IsSql99DataTypeSyntax dataType => dataType
characterLargeObjectType
    Value
"blob" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall dataType. IsSql99DataTypeSyntax dataType => dataType
binaryLargeObjectType
    Value
_ -> forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Sql99DataType" (forall {a} {be}.
(IsSql99DataTypeSyntax a, Typeable a) =>
BeamDeserializers be -> Object -> Parser a
parseObject BeamDeserializers be'
d) Value
v
  where
    parseObject :: BeamDeserializers be -> Object -> Parser a
parseObject BeamDeserializers be
d Object
v =
      forall dataType.
IsSql99DataTypeSyntax dataType =>
dataType -> Int -> dataType
arrayType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall a be.
Typeable a =>
BeamDeserializers be -> Value -> Parser a
beamDeserialize BeamDeserializers be
d forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"of") forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"count" forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
      forall dataType.
IsSql99DataTypeSyntax dataType =>
[(Text, dataType)] -> dataType
rowType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (do Value
rowTypes <- Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"row"
                      let parseArray :: Array -> Parser [(Text, a)]
parseArray Array
a =
                            forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM (forall a. Vector a -> [a]
V.toList Array
a) forall a b. (a -> b) -> a -> b
$ \Value
a' -> do
                            (Text
nm, Value
a'') <- forall a. FromJSON a => Value -> Parser a
parseJSON Value
a'
                            (Text
nm,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a be.
Typeable a =>
BeamDeserializers be -> Value -> Parser a
beamDeserialize BeamDeserializers be
d Value
a''
                      forall a. String -> (Array -> Parser a) -> Value -> Parser a
withArray String
"Sql99DataType.rowType" Array -> Parser [(Text, a)]
parseArray Value
rowTypes)

-- | Deserialize data types that are instances of 'IsSql2003BinaryAndVarBinaryDataTypeSyntax'
sql2003BinaryAndVarBinaryDataTypeDeserializers
  :: forall be
   . ( BeamMigrateSqlBackend be, BeamSqlT021Backend be )
  => BeamDeserializers be
sql2003BinaryAndVarBinaryDataTypeDeserializers :: forall be.
(BeamMigrateSqlBackend be, BeamSqlT021Backend be) =>
BeamDeserializers be
sql2003BinaryAndVarBinaryDataTypeDeserializers =
  forall ty be.
Typeable ty =>
(forall be'. BeamDeserializers be' -> Value -> Parser ty)
-> BeamDeserializers be
beamDeserializer forall a b. (a -> b) -> a -> b
$ \BeamDeserializers be'
_ Value
v ->
  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. a -> a
id @(BeamSqlBackendDataTypeSyntax be)) forall a b. (a -> b) -> a -> b
$
  forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Sql2003DataType"
    (\Object
o -> (forall dataType.
IsSql2003BinaryAndVarBinaryDataTypeSyntax dataType =>
Maybe Word -> dataType
binaryType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"binary") forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
           (forall dataType.
IsSql2003BinaryAndVarBinaryDataTypeSyntax dataType =>
Maybe Word -> dataType
varBinaryType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"varbinary"))
    Value
v

-- | Deserialize data types that are instance of 'IsSql2008BigIntDataTypeSyntax'
sql2008BigIntDataTypeDeserializers
  :: forall be
   . ( BeamMigrateSqlBackend be, BeamSqlT071Backend be )
  => BeamDeserializers be
sql2008BigIntDataTypeDeserializers :: forall be.
(BeamMigrateSqlBackend be, BeamSqlT071Backend be) =>
BeamDeserializers be
sql2008BigIntDataTypeDeserializers =
  forall ty be.
Typeable ty =>
(forall be'. BeamDeserializers be' -> Value -> Parser ty)
-> BeamDeserializers be
beamDeserializer forall a b. (a -> b) -> a -> b
$ \BeamDeserializers be'
_ Value
v ->
  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. a -> a
id @(BeamSqlBackendDataTypeSyntax be)) forall a b. (a -> b) -> a -> b
$
  case Value
v of
    Value
"bigint" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall dataType. IsSql2008BigIntDataTypeSyntax dataType => dataType
bigIntType
    Value
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Sql2008DataType.bigint: expected 'bigint'"

-- $serialization
--   Below we provide various instances of Beam SQL syntax types that produce an
--   aeson 'Value' that reflects the call tree. This allows us to read back
--   these data types in various syntaxes.
--
--   Because these are formatted as standard beam syntaxes, backends can easily
--   serialize their data to disk. For an example of what we mean by this, see
--   the instance of 'IsSql92DataTypeSyntax' for 'SqliteDataTypeSyntax' in
--   @beam-sqlite@.


-- $deserialization
--
--   Deserialization requires that knowledge of every type of data we can
--   deserialize is stored in one place. While this is not much of an issue when
--   compiling full Haskell applications, due to the type class mechanism,
--   beam-migrate tools load backends dynamically. This means that we need a
--   separate way to discover deserialization instances for types we care about.
--
--   Values of the 'BeamDeserializers' type represent a set of deserializers all
--   related to one kind of command syntax. You can ask for the deserializers to
--   deserialize any type from an aeson 'Value'. The deserialization will
--   succeed only if a deserializer for the requested type exists and the
--   deserializer was able to parse the 'Value'.
--
--   'BeamDeserializers' compose monoidally. Thus, you can extend any
--   'BeamDeserializers' with your own custom deserializers, by 'mappend'ing it
--   with a new 'BeamDeserializers', created by calling 'beamDeserializer'.