{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE TemplateHaskell #-}

module Hasura.RQL.Types.Source.Table
  ( SourceTableInfo (..),
    stiName,
    stiType,
    stiColumns,
    stiLogicalModels,
    stiPrimaryKey,
    stiForeignKeys,
    stiDescription,
    stiInsertable,
    stiUpdatable,
    stiDeletable,
    SourceTableType (..),
    SourceForeignKeys (..),
    unSourceForeignKeys,
    SourceConstraint (..),
    scForeignTable,
    scColumnMapping,
  )
where

--------------------------------------------------------------------------------

import Autodocodec
import Autodocodec.OpenAPI ()
import Control.Lens.TH (makeLenses)
import Data.Aeson (FromJSON, ToJSON)
import Data.HashMap.Strict (HashMap)
import Data.Hashable (Hashable)
import Data.List.NonEmpty (NonEmpty)
import Data.List.NonEmpty qualified as NonEmpty
import Data.OpenApi (ToSchema)
import Data.Text (Text)
import GHC.Generics (Generic)
import Hasura.LogicalModel.Metadata
import Hasura.RQL.Types.Backend (Backend (..), ConstraintName)
import Hasura.RQL.Types.Source.Column (SourceColumnInfo)
import Hasura.RQL.Types.Source.TableType
import Prelude

--------------------------------------------------------------------------------

-- | Table schema data from the 'SchemaResponse'.
data SourceTableInfo b = SourceTableInfo
  { forall (b :: BackendType). SourceTableInfo b -> TableName b
_stiName :: TableName b,
    forall (b :: BackendType). SourceTableInfo b -> SourceTableType
_stiType :: SourceTableType,
    forall (b :: BackendType).
SourceTableInfo b -> [SourceColumnInfo b]
_stiColumns :: [SourceColumnInfo b],
    forall (b :: BackendType).
SourceTableInfo b -> [LogicalModelMetadata b]
_stiLogicalModels :: [LogicalModelMetadata b],
    forall (b :: BackendType).
SourceTableInfo b -> Maybe (NonEmpty (Column b))
_stiPrimaryKey :: Maybe (NonEmpty (Column b)),
    forall (b :: BackendType). SourceTableInfo b -> SourceForeignKeys b
_stiForeignKeys :: SourceForeignKeys b,
    forall (b :: BackendType). SourceTableInfo b -> Maybe Text
_stiDescription :: Maybe Text,
    forall (b :: BackendType). SourceTableInfo b -> Bool
_stiInsertable :: Bool,
    forall (b :: BackendType). SourceTableInfo b -> Bool
_stiUpdatable :: Bool,
    forall (b :: BackendType). SourceTableInfo b -> Bool
_stiDeletable :: Bool
  }
  deriving stock ((forall x. SourceTableInfo b -> Rep (SourceTableInfo b) x)
-> (forall x. Rep (SourceTableInfo b) x -> SourceTableInfo b)
-> Generic (SourceTableInfo b)
forall x. Rep (SourceTableInfo b) x -> SourceTableInfo b
forall x. SourceTableInfo b -> Rep (SourceTableInfo b) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (b :: BackendType) x.
Rep (SourceTableInfo b) x -> SourceTableInfo b
forall (b :: BackendType) x.
SourceTableInfo b -> Rep (SourceTableInfo b) x
$cfrom :: forall (b :: BackendType) x.
SourceTableInfo b -> Rep (SourceTableInfo b) x
from :: forall x. SourceTableInfo b -> Rep (SourceTableInfo b) x
$cto :: forall (b :: BackendType) x.
Rep (SourceTableInfo b) x -> SourceTableInfo b
to :: forall x. Rep (SourceTableInfo b) x -> SourceTableInfo b
Generic)
  deriving (Value -> Parser [SourceTableInfo b]
Value -> Parser (SourceTableInfo b)
(Value -> Parser (SourceTableInfo b))
-> (Value -> Parser [SourceTableInfo b])
-> FromJSON (SourceTableInfo b)
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
forall (b :: BackendType).
Backend b =>
Value -> Parser [SourceTableInfo b]
forall (b :: BackendType).
Backend b =>
Value -> Parser (SourceTableInfo b)
$cparseJSON :: forall (b :: BackendType).
Backend b =>
Value -> Parser (SourceTableInfo b)
parseJSON :: Value -> Parser (SourceTableInfo b)
$cparseJSONList :: forall (b :: BackendType).
Backend b =>
Value -> Parser [SourceTableInfo b]
parseJSONList :: Value -> Parser [SourceTableInfo b]
FromJSON, [SourceTableInfo b] -> Value
[SourceTableInfo b] -> Encoding
SourceTableInfo b -> Value
SourceTableInfo b -> Encoding
(SourceTableInfo b -> Value)
-> (SourceTableInfo b -> Encoding)
-> ([SourceTableInfo b] -> Value)
-> ([SourceTableInfo b] -> Encoding)
-> ToJSON (SourceTableInfo b)
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
forall (b :: BackendType).
Backend b =>
[SourceTableInfo b] -> Value
forall (b :: BackendType).
Backend b =>
[SourceTableInfo b] -> Encoding
forall (b :: BackendType). Backend b => SourceTableInfo b -> Value
forall (b :: BackendType).
Backend b =>
SourceTableInfo b -> Encoding
$ctoJSON :: forall (b :: BackendType). Backend b => SourceTableInfo b -> Value
toJSON :: SourceTableInfo b -> Value
$ctoEncoding :: forall (b :: BackendType).
Backend b =>
SourceTableInfo b -> Encoding
toEncoding :: SourceTableInfo b -> Encoding
$ctoJSONList :: forall (b :: BackendType).
Backend b =>
[SourceTableInfo b] -> Value
toJSONList :: [SourceTableInfo b] -> Value
$ctoEncodingList :: forall (b :: BackendType).
Backend b =>
[SourceTableInfo b] -> Encoding
toEncodingList :: [SourceTableInfo b] -> Encoding
ToJSON, Typeable (SourceTableInfo b)
Typeable (SourceTableInfo b)
-> (Proxy (SourceTableInfo b)
    -> Declare (Definitions Schema) NamedSchema)
-> ToSchema (SourceTableInfo b)
Proxy (SourceTableInfo b)
-> Declare (Definitions Schema) NamedSchema
forall a.
Typeable a
-> (Proxy a -> Declare (Definitions Schema) NamedSchema)
-> ToSchema a
forall {b :: BackendType}.
Backend b =>
Typeable (SourceTableInfo b)
forall (b :: BackendType).
Backend b =>
Proxy (SourceTableInfo b)
-> Declare (Definitions Schema) NamedSchema
$cdeclareNamedSchema :: forall (b :: BackendType).
Backend b =>
Proxy (SourceTableInfo b)
-> Declare (Definitions Schema) NamedSchema
declareNamedSchema :: Proxy (SourceTableInfo b)
-> Declare (Definitions Schema) NamedSchema
ToSchema) via Autodocodec (SourceTableInfo b)

deriving stock instance (Backend b) => Eq (SourceTableInfo b)

deriving stock instance (Backend b) => Show (SourceTableInfo b)

instance (Backend b) => HasCodec (SourceTableInfo b) where
  codec :: JSONCodec (SourceTableInfo b)
codec =
    Text
-> ObjectCodec (SourceTableInfo b) (SourceTableInfo b)
-> JSONCodec (SourceTableInfo b)
forall input output.
Text -> ObjectCodec input output -> ValueCodec input output
object Text
"TableInfo" (ObjectCodec (SourceTableInfo b) (SourceTableInfo b)
 -> JSONCodec (SourceTableInfo b))
-> ObjectCodec (SourceTableInfo b) (SourceTableInfo b)
-> JSONCodec (SourceTableInfo b)
forall a b. (a -> b) -> a -> b
$
      TableName b
-> SourceTableType
-> [SourceColumnInfo b]
-> [LogicalModelMetadata b]
-> Maybe (NonEmpty (Column b))
-> SourceForeignKeys b
-> Maybe Text
-> Bool
-> Bool
-> Bool
-> SourceTableInfo b
forall (b :: BackendType).
TableName b
-> SourceTableType
-> [SourceColumnInfo b]
-> [LogicalModelMetadata b]
-> Maybe (NonEmpty (Column b))
-> SourceForeignKeys b
-> Maybe Text
-> Bool
-> Bool
-> Bool
-> SourceTableInfo b
SourceTableInfo
        (TableName b
 -> SourceTableType
 -> [SourceColumnInfo b]
 -> [LogicalModelMetadata b]
 -> Maybe (NonEmpty (Column b))
 -> SourceForeignKeys b
 -> Maybe Text
 -> Bool
 -> Bool
 -> Bool
 -> SourceTableInfo b)
-> Codec Object (SourceTableInfo b) (TableName b)
-> Codec
     Object
     (SourceTableInfo b)
     (SourceTableType
      -> [SourceColumnInfo b]
      -> [LogicalModelMetadata b]
      -> Maybe (NonEmpty (Column b))
      -> SourceForeignKeys b
      -> Maybe Text
      -> Bool
      -> Bool
      -> Bool
      -> SourceTableInfo b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Text -> ObjectCodec (TableName b) (TableName b)
forall output.
HasCodec output =>
Text -> Text -> ObjectCodec output output
requiredField Text
"name" Text
"The name of the table" ObjectCodec (TableName b) (TableName b)
-> (SourceTableInfo b -> TableName b)
-> Codec Object (SourceTableInfo b) (TableName b)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= SourceTableInfo b -> TableName b
forall (b :: BackendType). SourceTableInfo b -> TableName b
_stiName
        Codec
  Object
  (SourceTableInfo b)
  (SourceTableType
   -> [SourceColumnInfo b]
   -> [LogicalModelMetadata b]
   -> Maybe (NonEmpty (Column b))
   -> SourceForeignKeys b
   -> Maybe Text
   -> Bool
   -> Bool
   -> Bool
   -> SourceTableInfo b)
-> Codec Object (SourceTableInfo b) SourceTableType
-> Codec
     Object
     (SourceTableInfo b)
     ([SourceColumnInfo b]
      -> [LogicalModelMetadata b]
      -> Maybe (NonEmpty (Column b))
      -> SourceForeignKeys b
      -> Maybe Text
      -> Bool
      -> Bool
      -> Bool
      -> SourceTableInfo b)
forall a b.
Codec Object (SourceTableInfo b) (a -> b)
-> Codec Object (SourceTableInfo b) a
-> Codec Object (SourceTableInfo b) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> SourceTableType
-> Text
-> ObjectCodec SourceTableType SourceTableType
forall output.
HasCodec output =>
Text -> output -> Text -> ObjectCodec output output
optionalFieldWithDefault Text
"type" SourceTableType
Table Text
"The type of table" ObjectCodec SourceTableType SourceTableType
-> (SourceTableInfo b -> SourceTableType)
-> Codec Object (SourceTableInfo b) SourceTableType
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= SourceTableInfo b -> SourceTableType
forall (b :: BackendType). SourceTableInfo b -> SourceTableType
_stiType
        Codec
  Object
  (SourceTableInfo b)
  ([SourceColumnInfo b]
   -> [LogicalModelMetadata b]
   -> Maybe (NonEmpty (Column b))
   -> SourceForeignKeys b
   -> Maybe Text
   -> Bool
   -> Bool
   -> Bool
   -> SourceTableInfo b)
-> Codec Object (SourceTableInfo b) [SourceColumnInfo b]
-> Codec
     Object
     (SourceTableInfo b)
     ([LogicalModelMetadata b]
      -> Maybe (NonEmpty (Column b))
      -> SourceForeignKeys b
      -> Maybe Text
      -> Bool
      -> Bool
      -> Bool
      -> SourceTableInfo b)
forall a b.
Codec Object (SourceTableInfo b) (a -> b)
-> Codec Object (SourceTableInfo b) a
-> Codec Object (SourceTableInfo b) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> Text -> ObjectCodec [SourceColumnInfo b] [SourceColumnInfo b]
forall output.
HasCodec output =>
Text -> Text -> ObjectCodec output output
requiredField Text
"columns" Text
"The columns of the table" ObjectCodec [SourceColumnInfo b] [SourceColumnInfo b]
-> (SourceTableInfo b -> [SourceColumnInfo b])
-> Codec Object (SourceTableInfo b) [SourceColumnInfo b]
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= SourceTableInfo b -> [SourceColumnInfo b]
forall (b :: BackendType).
SourceTableInfo b -> [SourceColumnInfo b]
_stiColumns
        Codec
  Object
  (SourceTableInfo b)
  ([LogicalModelMetadata b]
   -> Maybe (NonEmpty (Column b))
   -> SourceForeignKeys b
   -> Maybe Text
   -> Bool
   -> Bool
   -> Bool
   -> SourceTableInfo b)
-> Codec Object (SourceTableInfo b) [LogicalModelMetadata b]
-> Codec
     Object
     (SourceTableInfo b)
     (Maybe (NonEmpty (Column b))
      -> SourceForeignKeys b
      -> Maybe Text
      -> Bool
      -> Bool
      -> Bool
      -> SourceTableInfo b)
forall a b.
Codec Object (SourceTableInfo b) (a -> b)
-> Codec Object (SourceTableInfo b) a
-> Codec Object (SourceTableInfo b) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> [LogicalModelMetadata b]
-> Text
-> ObjectCodec [LogicalModelMetadata b] [LogicalModelMetadata b]
forall output.
(Eq output, HasCodec output) =>
Text -> output -> Text -> ObjectCodec output output
optionalFieldWithOmittedDefault Text
"logical_models" [] Text
"The logical models referenced by the table's column types" ObjectCodec [LogicalModelMetadata b] [LogicalModelMetadata b]
-> (SourceTableInfo b -> [LogicalModelMetadata b])
-> Codec Object (SourceTableInfo b) [LogicalModelMetadata b]
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= SourceTableInfo b -> [LogicalModelMetadata b]
forall (b :: BackendType).
SourceTableInfo b -> [LogicalModelMetadata b]
_stiLogicalModels
        Codec
  Object
  (SourceTableInfo b)
  (Maybe (NonEmpty (Column b))
   -> SourceForeignKeys b
   -> Maybe Text
   -> Bool
   -> Bool
   -> Bool
   -> SourceTableInfo b)
-> Codec Object (SourceTableInfo b) (Maybe (NonEmpty (Column b)))
-> Codec
     Object
     (SourceTableInfo b)
     (SourceForeignKeys b
      -> Maybe Text -> Bool -> Bool -> Bool -> SourceTableInfo b)
forall a b.
Codec Object (SourceTableInfo b) (a -> b)
-> Codec Object (SourceTableInfo b) a
-> Codec Object (SourceTableInfo b) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Codec Object [Column b] [Column b]
-> Codec
     Object (Maybe (NonEmpty (Column b))) (Maybe (NonEmpty (Column b)))
forall context a.
Codec context [a] [a]
-> Codec context (Maybe (NonEmpty a)) (Maybe (NonEmpty a))
dimapMaybeNonEmpty (Text -> [Column b] -> Text -> Codec Object [Column b] [Column b]
forall output.
(Eq output, HasCodec output) =>
Text -> output -> Text -> ObjectCodec output output
optionalFieldWithOmittedDefault Text
"primary_key" [] Text
"The primary key of the table") Codec
  Object (Maybe (NonEmpty (Column b))) (Maybe (NonEmpty (Column b)))
-> (SourceTableInfo b -> Maybe (NonEmpty (Column b)))
-> Codec Object (SourceTableInfo b) (Maybe (NonEmpty (Column b)))
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= SourceTableInfo b -> Maybe (NonEmpty (Column b))
forall (b :: BackendType).
SourceTableInfo b -> Maybe (NonEmpty (Column b))
_stiPrimaryKey
        Codec
  Object
  (SourceTableInfo b)
  (SourceForeignKeys b
   -> Maybe Text -> Bool -> Bool -> Bool -> SourceTableInfo b)
-> Codec Object (SourceTableInfo b) (SourceForeignKeys b)
-> Codec
     Object
     (SourceTableInfo b)
     (Maybe Text -> Bool -> Bool -> Bool -> SourceTableInfo b)
forall a b.
Codec Object (SourceTableInfo b) (a -> b)
-> Codec Object (SourceTableInfo b) a
-> Codec Object (SourceTableInfo b) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> SourceForeignKeys b
-> Text
-> ObjectCodec (SourceForeignKeys b) (SourceForeignKeys b)
forall output.
(Eq output, HasCodec output) =>
Text -> output -> Text -> ObjectCodec output output
optionalFieldWithOmittedDefault Text
"foreign_keys" (HashMap (ConstraintName b) (SourceConstraint b)
-> SourceForeignKeys b
forall (b :: BackendType).
HashMap (ConstraintName b) (SourceConstraint b)
-> SourceForeignKeys b
SourceForeignKeys HashMap (ConstraintName b) (SourceConstraint b)
forall a. Monoid a => a
mempty) Text
"Foreign key constraints" ObjectCodec (SourceForeignKeys b) (SourceForeignKeys b)
-> (SourceTableInfo b -> SourceForeignKeys b)
-> Codec Object (SourceTableInfo b) (SourceForeignKeys b)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= SourceTableInfo b -> SourceForeignKeys b
forall (b :: BackendType). SourceTableInfo b -> SourceForeignKeys b
_stiForeignKeys
        Codec
  Object
  (SourceTableInfo b)
  (Maybe Text -> Bool -> Bool -> Bool -> SourceTableInfo b)
-> Codec Object (SourceTableInfo b) (Maybe Text)
-> Codec
     Object
     (SourceTableInfo b)
     (Bool -> Bool -> Bool -> SourceTableInfo b)
forall a b.
Codec Object (SourceTableInfo b) (a -> b)
-> Codec Object (SourceTableInfo b) a
-> Codec Object (SourceTableInfo b) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Text -> ObjectCodec (Maybe Text) (Maybe Text)
forall output.
HasCodec output =>
Text -> Text -> ObjectCodec (Maybe output) (Maybe output)
optionalFieldOrNull Text
"description" Text
"Description of the table" ObjectCodec (Maybe Text) (Maybe Text)
-> (SourceTableInfo b -> Maybe Text)
-> Codec Object (SourceTableInfo b) (Maybe Text)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= SourceTableInfo b -> Maybe Text
forall (b :: BackendType). SourceTableInfo b -> Maybe Text
_stiDescription
        Codec
  Object
  (SourceTableInfo b)
  (Bool -> Bool -> Bool -> SourceTableInfo b)
-> Codec Object (SourceTableInfo b) Bool
-> Codec
     Object (SourceTableInfo b) (Bool -> Bool -> SourceTableInfo b)
forall a b.
Codec Object (SourceTableInfo b) (a -> b)
-> Codec Object (SourceTableInfo b) a
-> Codec Object (SourceTableInfo b) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Bool -> Text -> ObjectCodec Bool Bool
forall output.
HasCodec output =>
Text -> output -> Text -> ObjectCodec output output
optionalFieldWithDefault Text
"insertable" Bool
False Text
"Whether or not new rows can be inserted into the table" ObjectCodec Bool Bool
-> (SourceTableInfo b -> Bool)
-> Codec Object (SourceTableInfo b) Bool
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= SourceTableInfo b -> Bool
forall (b :: BackendType). SourceTableInfo b -> Bool
_stiInsertable
        Codec
  Object (SourceTableInfo b) (Bool -> Bool -> SourceTableInfo b)
-> Codec Object (SourceTableInfo b) Bool
-> Codec Object (SourceTableInfo b) (Bool -> SourceTableInfo b)
forall a b.
Codec Object (SourceTableInfo b) (a -> b)
-> Codec Object (SourceTableInfo b) a
-> Codec Object (SourceTableInfo b) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Bool -> Text -> ObjectCodec Bool Bool
forall output.
HasCodec output =>
Text -> output -> Text -> ObjectCodec output output
optionalFieldWithDefault Text
"updatable" Bool
False Text
"Whether or not existing rows can be updated in the table" ObjectCodec Bool Bool
-> (SourceTableInfo b -> Bool)
-> Codec Object (SourceTableInfo b) Bool
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= SourceTableInfo b -> Bool
forall (b :: BackendType). SourceTableInfo b -> Bool
_stiUpdatable
        Codec Object (SourceTableInfo b) (Bool -> SourceTableInfo b)
-> Codec Object (SourceTableInfo b) Bool
-> ObjectCodec (SourceTableInfo b) (SourceTableInfo b)
forall a b.
Codec Object (SourceTableInfo b) (a -> b)
-> Codec Object (SourceTableInfo b) a
-> Codec Object (SourceTableInfo b) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Bool -> Text -> ObjectCodec Bool Bool
forall output.
HasCodec output =>
Text -> output -> Text -> ObjectCodec output output
optionalFieldWithDefault Text
"deletable" Bool
False Text
"Whether or not existing rows can be deleted in the table" ObjectCodec Bool Bool
-> (SourceTableInfo b -> Bool)
-> Codec Object (SourceTableInfo b) Bool
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= SourceTableInfo b -> Bool
forall (b :: BackendType). SourceTableInfo b -> Bool
_stiDeletable
    where
      dimapMaybeNonEmpty :: Codec context [a] [a] -> Codec context (Maybe (NonEmpty a)) (Maybe (NonEmpty a))
      dimapMaybeNonEmpty :: forall context a.
Codec context [a] [a]
-> Codec context (Maybe (NonEmpty a)) (Maybe (NonEmpty a))
dimapMaybeNonEmpty = ([a] -> Maybe (NonEmpty a))
-> (Maybe (NonEmpty a) -> [a])
-> Codec context [a] [a]
-> Codec context (Maybe (NonEmpty a)) (Maybe (NonEmpty a))
forall oldOutput newOutput newInput oldInput context.
(oldOutput -> newOutput)
-> (newInput -> oldInput)
-> Codec context oldInput oldOutput
-> Codec context newInput newOutput
dimapCodec [a] -> Maybe (NonEmpty a)
forall a. [a] -> Maybe (NonEmpty a)
NonEmpty.nonEmpty ([a] -> (NonEmpty a -> [a]) -> Maybe (NonEmpty a) -> [a]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
NonEmpty.toList)

--------------------------------------------------------------------------------

newtype SourceForeignKeys b = SourceForeignKeys {forall (b :: BackendType).
SourceForeignKeys b
-> HashMap (ConstraintName b) (SourceConstraint b)
_unSourceForeignKeys :: HashMap (ConstraintName b) (SourceConstraint b)}
  deriving stock ((forall x. SourceForeignKeys b -> Rep (SourceForeignKeys b) x)
-> (forall x. Rep (SourceForeignKeys b) x -> SourceForeignKeys b)
-> Generic (SourceForeignKeys b)
forall x. Rep (SourceForeignKeys b) x -> SourceForeignKeys b
forall x. SourceForeignKeys b -> Rep (SourceForeignKeys b) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (b :: BackendType) x.
Rep (SourceForeignKeys b) x -> SourceForeignKeys b
forall (b :: BackendType) x.
SourceForeignKeys b -> Rep (SourceForeignKeys b) x
$cfrom :: forall (b :: BackendType) x.
SourceForeignKeys b -> Rep (SourceForeignKeys b) x
from :: forall x. SourceForeignKeys b -> Rep (SourceForeignKeys b) x
$cto :: forall (b :: BackendType) x.
Rep (SourceForeignKeys b) x -> SourceForeignKeys b
to :: forall x. Rep (SourceForeignKeys b) x -> SourceForeignKeys b
Generic)
  deriving anyclass (Eq (SourceForeignKeys b)
Eq (SourceForeignKeys b)
-> (Int -> SourceForeignKeys b -> Int)
-> (SourceForeignKeys b -> Int)
-> Hashable (SourceForeignKeys b)
Int -> SourceForeignKeys b -> Int
SourceForeignKeys b -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
forall (b :: BackendType). Backend b => Eq (SourceForeignKeys b)
forall (b :: BackendType).
Backend b =>
Int -> SourceForeignKeys b -> Int
forall (b :: BackendType). Backend b => SourceForeignKeys b -> Int
$chashWithSalt :: forall (b :: BackendType).
Backend b =>
Int -> SourceForeignKeys b -> Int
hashWithSalt :: Int -> SourceForeignKeys b -> Int
$chash :: forall (b :: BackendType). Backend b => SourceForeignKeys b -> Int
hash :: SourceForeignKeys b -> Int
Hashable)
  deriving (Value -> Parser [SourceForeignKeys b]
Value -> Parser (SourceForeignKeys b)
(Value -> Parser (SourceForeignKeys b))
-> (Value -> Parser [SourceForeignKeys b])
-> FromJSON (SourceForeignKeys b)
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
forall (b :: BackendType).
Backend b =>
Value -> Parser [SourceForeignKeys b]
forall (b :: BackendType).
Backend b =>
Value -> Parser (SourceForeignKeys b)
$cparseJSON :: forall (b :: BackendType).
Backend b =>
Value -> Parser (SourceForeignKeys b)
parseJSON :: Value -> Parser (SourceForeignKeys b)
$cparseJSONList :: forall (b :: BackendType).
Backend b =>
Value -> Parser [SourceForeignKeys b]
parseJSONList :: Value -> Parser [SourceForeignKeys b]
FromJSON, [SourceForeignKeys b] -> Value
[SourceForeignKeys b] -> Encoding
SourceForeignKeys b -> Value
SourceForeignKeys b -> Encoding
(SourceForeignKeys b -> Value)
-> (SourceForeignKeys b -> Encoding)
-> ([SourceForeignKeys b] -> Value)
-> ([SourceForeignKeys b] -> Encoding)
-> ToJSON (SourceForeignKeys b)
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
forall (b :: BackendType).
Backend b =>
[SourceForeignKeys b] -> Value
forall (b :: BackendType).
Backend b =>
[SourceForeignKeys b] -> Encoding
forall (b :: BackendType).
Backend b =>
SourceForeignKeys b -> Value
forall (b :: BackendType).
Backend b =>
SourceForeignKeys b -> Encoding
$ctoJSON :: forall (b :: BackendType).
Backend b =>
SourceForeignKeys b -> Value
toJSON :: SourceForeignKeys b -> Value
$ctoEncoding :: forall (b :: BackendType).
Backend b =>
SourceForeignKeys b -> Encoding
toEncoding :: SourceForeignKeys b -> Encoding
$ctoJSONList :: forall (b :: BackendType).
Backend b =>
[SourceForeignKeys b] -> Value
toJSONList :: [SourceForeignKeys b] -> Value
$ctoEncodingList :: forall (b :: BackendType).
Backend b =>
[SourceForeignKeys b] -> Encoding
toEncodingList :: [SourceForeignKeys b] -> Encoding
ToJSON) via Autodocodec (SourceForeignKeys b)

deriving stock instance (Backend b) => Eq (SourceForeignKeys b)

deriving stock instance (Backend b) => Ord (SourceForeignKeys b)

deriving stock instance (Backend b) => Show (SourceForeignKeys b)

instance (Backend b) => HasCodec (SourceForeignKeys b) where
  codec :: JSONCodec (SourceForeignKeys b)
codec = (HashMap (ConstraintName b) (SourceConstraint b)
 -> SourceForeignKeys b)
-> (SourceForeignKeys b
    -> HashMap (ConstraintName b) (SourceConstraint b))
-> Codec
     Value
     (HashMap (ConstraintName b) (SourceConstraint b))
     (HashMap (ConstraintName b) (SourceConstraint b))
-> JSONCodec (SourceForeignKeys b)
forall oldOutput newOutput newInput oldInput context.
(oldOutput -> newOutput)
-> (newInput -> oldInput)
-> Codec context oldInput oldOutput
-> Codec context newInput newOutput
dimapCodec HashMap (ConstraintName b) (SourceConstraint b)
-> SourceForeignKeys b
forall (b :: BackendType).
HashMap (ConstraintName b) (SourceConstraint b)
-> SourceForeignKeys b
SourceForeignKeys SourceForeignKeys b
-> HashMap (ConstraintName b) (SourceConstraint b)
forall (b :: BackendType).
SourceForeignKeys b
-> HashMap (ConstraintName b) (SourceConstraint b)
_unSourceForeignKeys (Codec
   Value
   (HashMap (ConstraintName b) (SourceConstraint b))
   (HashMap (ConstraintName b) (SourceConstraint b))
 -> JSONCodec (SourceForeignKeys b))
-> Codec
     Value
     (HashMap (ConstraintName b) (SourceConstraint b))
     (HashMap (ConstraintName b) (SourceConstraint b))
-> JSONCodec (SourceForeignKeys b)
forall a b. (a -> b) -> a -> b
$ forall value. HasCodec value => JSONCodec value
codec @(HashMap (ConstraintName b) (SourceConstraint b))

--------------------------------------------------------------------------------

data SourceConstraint b = SourceConstraint
  { forall (b :: BackendType). SourceConstraint b -> TableName b
_scForeignTable :: TableName b,
    forall (b :: BackendType).
SourceConstraint b -> HashMap (Column b) (Column b)
_scColumnMapping :: HashMap (Column b) (Column b)
  }
  deriving stock ((forall x. SourceConstraint b -> Rep (SourceConstraint b) x)
-> (forall x. Rep (SourceConstraint b) x -> SourceConstraint b)
-> Generic (SourceConstraint b)
forall x. Rep (SourceConstraint b) x -> SourceConstraint b
forall x. SourceConstraint b -> Rep (SourceConstraint b) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (b :: BackendType) x.
Rep (SourceConstraint b) x -> SourceConstraint b
forall (b :: BackendType) x.
SourceConstraint b -> Rep (SourceConstraint b) x
$cfrom :: forall (b :: BackendType) x.
SourceConstraint b -> Rep (SourceConstraint b) x
from :: forall x. SourceConstraint b -> Rep (SourceConstraint b) x
$cto :: forall (b :: BackendType) x.
Rep (SourceConstraint b) x -> SourceConstraint b
to :: forall x. Rep (SourceConstraint b) x -> SourceConstraint b
Generic)
  deriving anyclass (Eq (SourceConstraint b)
Eq (SourceConstraint b)
-> (Int -> SourceConstraint b -> Int)
-> (SourceConstraint b -> Int)
-> Hashable (SourceConstraint b)
Int -> SourceConstraint b -> Int
SourceConstraint b -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
forall (b :: BackendType). Backend b => Eq (SourceConstraint b)
forall (b :: BackendType).
Backend b =>
Int -> SourceConstraint b -> Int
forall (b :: BackendType). Backend b => SourceConstraint b -> Int
$chashWithSalt :: forall (b :: BackendType).
Backend b =>
Int -> SourceConstraint b -> Int
hashWithSalt :: Int -> SourceConstraint b -> Int
$chash :: forall (b :: BackendType). Backend b => SourceConstraint b -> Int
hash :: SourceConstraint b -> Int
Hashable)
  deriving (Value -> Parser [SourceConstraint b]
Value -> Parser (SourceConstraint b)
(Value -> Parser (SourceConstraint b))
-> (Value -> Parser [SourceConstraint b])
-> FromJSON (SourceConstraint b)
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
forall (b :: BackendType).
Backend b =>
Value -> Parser [SourceConstraint b]
forall (b :: BackendType).
Backend b =>
Value -> Parser (SourceConstraint b)
$cparseJSON :: forall (b :: BackendType).
Backend b =>
Value -> Parser (SourceConstraint b)
parseJSON :: Value -> Parser (SourceConstraint b)
$cparseJSONList :: forall (b :: BackendType).
Backend b =>
Value -> Parser [SourceConstraint b]
parseJSONList :: Value -> Parser [SourceConstraint b]
FromJSON, [SourceConstraint b] -> Value
[SourceConstraint b] -> Encoding
SourceConstraint b -> Value
SourceConstraint b -> Encoding
(SourceConstraint b -> Value)
-> (SourceConstraint b -> Encoding)
-> ([SourceConstraint b] -> Value)
-> ([SourceConstraint b] -> Encoding)
-> ToJSON (SourceConstraint b)
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
forall (b :: BackendType).
Backend b =>
[SourceConstraint b] -> Value
forall (b :: BackendType).
Backend b =>
[SourceConstraint b] -> Encoding
forall (b :: BackendType). Backend b => SourceConstraint b -> Value
forall (b :: BackendType).
Backend b =>
SourceConstraint b -> Encoding
$ctoJSON :: forall (b :: BackendType). Backend b => SourceConstraint b -> Value
toJSON :: SourceConstraint b -> Value
$ctoEncoding :: forall (b :: BackendType).
Backend b =>
SourceConstraint b -> Encoding
toEncoding :: SourceConstraint b -> Encoding
$ctoJSONList :: forall (b :: BackendType).
Backend b =>
[SourceConstraint b] -> Value
toJSONList :: [SourceConstraint b] -> Value
$ctoEncodingList :: forall (b :: BackendType).
Backend b =>
[SourceConstraint b] -> Encoding
toEncodingList :: [SourceConstraint b] -> Encoding
ToJSON) via Autodocodec (SourceConstraint b)

deriving stock instance (Backend b) => Eq (SourceConstraint b)

deriving stock instance (Backend b) => Ord (SourceConstraint b)

deriving stock instance (Backend b) => Show (SourceConstraint b)

instance (Backend b) => HasCodec (SourceConstraint b) where
  codec :: JSONCodec (SourceConstraint b)
codec =
    Text
-> ObjectCodec (SourceConstraint b) (SourceConstraint b)
-> JSONCodec (SourceConstraint b)
forall input output.
Text -> ObjectCodec input output -> ValueCodec input output
object Text
"SourceConstraint" (ObjectCodec (SourceConstraint b) (SourceConstraint b)
 -> JSONCodec (SourceConstraint b))
-> ObjectCodec (SourceConstraint b) (SourceConstraint b)
-> JSONCodec (SourceConstraint b)
forall a b. (a -> b) -> a -> b
$
      TableName b -> HashMap (Column b) (Column b) -> SourceConstraint b
forall (b :: BackendType).
TableName b -> HashMap (Column b) (Column b) -> SourceConstraint b
SourceConstraint
        (TableName b
 -> HashMap (Column b) (Column b) -> SourceConstraint b)
-> Codec Object (SourceConstraint b) (TableName b)
-> Codec
     Object
     (SourceConstraint b)
     (HashMap (Column b) (Column b) -> SourceConstraint b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Text -> ObjectCodec (TableName b) (TableName b)
forall output.
HasCodec output =>
Text -> Text -> ObjectCodec output output
requiredField Text
"foreign_table" Text
"The table referenced by the foreign key in the child table." ObjectCodec (TableName b) (TableName b)
-> (SourceConstraint b -> TableName b)
-> Codec Object (SourceConstraint b) (TableName b)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= SourceConstraint b -> TableName b
forall (b :: BackendType). SourceConstraint b -> TableName b
_scForeignTable
        Codec
  Object
  (SourceConstraint b)
  (HashMap (Column b) (Column b) -> SourceConstraint b)
-> Codec
     Object (SourceConstraint b) (HashMap (Column b) (Column b))
-> ObjectCodec (SourceConstraint b) (SourceConstraint b)
forall a b.
Codec Object (SourceConstraint b) (a -> b)
-> Codec Object (SourceConstraint b) a
-> Codec Object (SourceConstraint b) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> Text
-> ObjectCodec
     (HashMap (Column b) (Column b)) (HashMap (Column b) (Column b))
forall output.
HasCodec output =>
Text -> Text -> ObjectCodec output output
requiredField Text
"column_mapping" Text
"The columns on which you want want to define the foreign key." ObjectCodec
  (HashMap (Column b) (Column b)) (HashMap (Column b) (Column b))
-> (SourceConstraint b -> HashMap (Column b) (Column b))
-> Codec
     Object (SourceConstraint b) (HashMap (Column b) (Column b))
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= SourceConstraint b -> HashMap (Column b) (Column b)
forall (b :: BackendType).
SourceConstraint b -> HashMap (Column b) (Column b)
_scColumnMapping

--------------------------------------------------------------------------------

$(makeLenses ''SourceTableInfo)
$(makeLenses ''SourceConstraint)
$(makeLenses ''SourceForeignKeys)