{-# LANGUAGE GeneralizedNewtypeDeriving #-}

{- |
Copyright : Flipstone Technology Partners 2023
License   : MIT
Stability : Stable

@since 1.0.0.0
-}
module Orville.PostgreSQL.PgCatalog.PgSequence
  ( PgSequence (..)
  , pgSequenceTable
  , sequencePgClassOidField
  )
where

import Data.Int (Int64)
import qualified Database.PostgreSQL.LibPQ as LibPQ

import qualified Orville.PostgreSQL as Orville
import Orville.PostgreSQL.PgCatalog.OidField (oidField, oidTypeField)

{- |
  The Haskell representation of data read from the @pg_catalog.pg_sequence@
  table. Rows in this table are sequences in PostgreSQL.

@since 1.0.0.0
-}
data PgSequence = PgSequence
  { PgSequence -> Oid
pgSequenceClassOid :: LibPQ.Oid
  -- ^ The PostgreSQL @oid@ of the @pg_class@ for this sequence.
  , PgSequence -> Oid
pgSequenceTypeOid :: LibPQ.Oid
  -- ^ The PostgreSQL @oid@ of the data type of the sequence. References
  -- @pg_type.oid@.
  , PgSequence -> Int64
pgSequenceStart :: Int64
  -- ^ The start value of the sequence.
  , PgSequence -> Int64
pgSequenceIncrement :: Int64
  -- ^ The increment value of the sequence.
  , PgSequence -> Int64
pgSequenceMax :: Int64
  -- ^ The max value of the sequence.
  , PgSequence -> Int64
pgSequenceMin :: Int64
  -- ^ The min value of the sequence.
  , PgSequence -> Int64
pgSequenceCache :: Int64
  -- ^ The cache size of the sequence.
  , PgSequence -> Bool
pgSequenceCycle :: Bool
  -- ^ Whether the sequence cycles.
  }

{- |
  An Orville 'Orville.TableDefinition' for querying the
  @pg_catalog.pg_sequence@ table.

@since 1.0.0.0
-}
pgSequenceTable :: Orville.TableDefinition (Orville.HasKey LibPQ.Oid) PgSequence PgSequence
pgSequenceTable :: TableDefinition (HasKey Oid) PgSequence PgSequence
pgSequenceTable =
  String
-> TableDefinition (HasKey Oid) PgSequence PgSequence
-> TableDefinition (HasKey Oid) PgSequence PgSequence
forall key writeEntity readEntity.
String
-> TableDefinition key writeEntity readEntity
-> TableDefinition key writeEntity readEntity
Orville.setTableSchema String
"pg_catalog" (TableDefinition (HasKey Oid) PgSequence PgSequence
 -> TableDefinition (HasKey Oid) PgSequence PgSequence)
-> TableDefinition (HasKey Oid) PgSequence PgSequence
-> TableDefinition (HasKey Oid) PgSequence PgSequence
forall a b. (a -> b) -> a -> b
$
    String
-> PrimaryKey Oid
-> SqlMarshaller PgSequence PgSequence
-> TableDefinition (HasKey Oid) PgSequence PgSequence
forall key writeEntity readEntity.
String
-> PrimaryKey key
-> SqlMarshaller writeEntity readEntity
-> TableDefinition (HasKey key) writeEntity readEntity
Orville.mkTableDefinition
      String
"pg_sequence"
      (FieldDefinition NotNull Oid -> PrimaryKey Oid
forall key. FieldDefinition NotNull key -> PrimaryKey key
Orville.primaryKey FieldDefinition NotNull Oid
oidField)
      SqlMarshaller PgSequence PgSequence
pgSequenceMarshaller

pgSequenceMarshaller :: Orville.SqlMarshaller PgSequence PgSequence
pgSequenceMarshaller :: SqlMarshaller PgSequence PgSequence
pgSequenceMarshaller =
  Oid
-> Oid
-> Int64
-> Int64
-> Int64
-> Int64
-> Int64
-> Bool
-> PgSequence
PgSequence
    (Oid
 -> Oid
 -> Int64
 -> Int64
 -> Int64
 -> Int64
 -> Int64
 -> Bool
 -> PgSequence)
-> SqlMarshaller PgSequence Oid
-> SqlMarshaller
     PgSequence
     (Oid
      -> Int64 -> Int64 -> Int64 -> Int64 -> Int64 -> Bool -> PgSequence)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (PgSequence -> Oid)
-> FieldDefinition NotNull Oid -> SqlMarshaller PgSequence Oid
forall writeEntity fieldValue nullability.
(writeEntity -> fieldValue)
-> FieldDefinition nullability fieldValue
-> SqlMarshaller writeEntity fieldValue
Orville.marshallField PgSequence -> Oid
pgSequenceClassOid FieldDefinition NotNull Oid
sequencePgClassOidField
    SqlMarshaller
  PgSequence
  (Oid
   -> Int64 -> Int64 -> Int64 -> Int64 -> Int64 -> Bool -> PgSequence)
-> SqlMarshaller PgSequence Oid
-> SqlMarshaller
     PgSequence
     (Int64 -> Int64 -> Int64 -> Int64 -> Int64 -> Bool -> PgSequence)
forall a b.
SqlMarshaller PgSequence (a -> b)
-> SqlMarshaller PgSequence a -> SqlMarshaller PgSequence b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (PgSequence -> Oid)
-> FieldDefinition NotNull Oid -> SqlMarshaller PgSequence Oid
forall writeEntity fieldValue nullability.
(writeEntity -> fieldValue)
-> FieldDefinition nullability fieldValue
-> SqlMarshaller writeEntity fieldValue
Orville.marshallField PgSequence -> Oid
pgSequenceTypeOid FieldDefinition NotNull Oid
sequenceTypeOidField
    SqlMarshaller
  PgSequence
  (Int64 -> Int64 -> Int64 -> Int64 -> Int64 -> Bool -> PgSequence)
-> SqlMarshaller PgSequence Int64
-> SqlMarshaller
     PgSequence (Int64 -> Int64 -> Int64 -> Int64 -> Bool -> PgSequence)
forall a b.
SqlMarshaller PgSequence (a -> b)
-> SqlMarshaller PgSequence a -> SqlMarshaller PgSequence b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (PgSequence -> Int64)
-> FieldDefinition NotNull Int64 -> SqlMarshaller PgSequence Int64
forall writeEntity fieldValue nullability.
(writeEntity -> fieldValue)
-> FieldDefinition nullability fieldValue
-> SqlMarshaller writeEntity fieldValue
Orville.marshallField PgSequence -> Int64
pgSequenceStart FieldDefinition NotNull Int64
sequenceStartField
    SqlMarshaller
  PgSequence (Int64 -> Int64 -> Int64 -> Int64 -> Bool -> PgSequence)
-> SqlMarshaller PgSequence Int64
-> SqlMarshaller
     PgSequence (Int64 -> Int64 -> Int64 -> Bool -> PgSequence)
forall a b.
SqlMarshaller PgSequence (a -> b)
-> SqlMarshaller PgSequence a -> SqlMarshaller PgSequence b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (PgSequence -> Int64)
-> FieldDefinition NotNull Int64 -> SqlMarshaller PgSequence Int64
forall writeEntity fieldValue nullability.
(writeEntity -> fieldValue)
-> FieldDefinition nullability fieldValue
-> SqlMarshaller writeEntity fieldValue
Orville.marshallField PgSequence -> Int64
pgSequenceIncrement FieldDefinition NotNull Int64
sequenceIncrementField
    SqlMarshaller
  PgSequence (Int64 -> Int64 -> Int64 -> Bool -> PgSequence)
-> SqlMarshaller PgSequence Int64
-> SqlMarshaller PgSequence (Int64 -> Int64 -> Bool -> PgSequence)
forall a b.
SqlMarshaller PgSequence (a -> b)
-> SqlMarshaller PgSequence a -> SqlMarshaller PgSequence b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (PgSequence -> Int64)
-> FieldDefinition NotNull Int64 -> SqlMarshaller PgSequence Int64
forall writeEntity fieldValue nullability.
(writeEntity -> fieldValue)
-> FieldDefinition nullability fieldValue
-> SqlMarshaller writeEntity fieldValue
Orville.marshallField PgSequence -> Int64
pgSequenceMax FieldDefinition NotNull Int64
sequenceMaxField
    SqlMarshaller PgSequence (Int64 -> Int64 -> Bool -> PgSequence)
-> SqlMarshaller PgSequence Int64
-> SqlMarshaller PgSequence (Int64 -> Bool -> PgSequence)
forall a b.
SqlMarshaller PgSequence (a -> b)
-> SqlMarshaller PgSequence a -> SqlMarshaller PgSequence b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (PgSequence -> Int64)
-> FieldDefinition NotNull Int64 -> SqlMarshaller PgSequence Int64
forall writeEntity fieldValue nullability.
(writeEntity -> fieldValue)
-> FieldDefinition nullability fieldValue
-> SqlMarshaller writeEntity fieldValue
Orville.marshallField PgSequence -> Int64
pgSequenceMin FieldDefinition NotNull Int64
sequenceMinField
    SqlMarshaller PgSequence (Int64 -> Bool -> PgSequence)
-> SqlMarshaller PgSequence Int64
-> SqlMarshaller PgSequence (Bool -> PgSequence)
forall a b.
SqlMarshaller PgSequence (a -> b)
-> SqlMarshaller PgSequence a -> SqlMarshaller PgSequence b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (PgSequence -> Int64)
-> FieldDefinition NotNull Int64 -> SqlMarshaller PgSequence Int64
forall writeEntity fieldValue nullability.
(writeEntity -> fieldValue)
-> FieldDefinition nullability fieldValue
-> SqlMarshaller writeEntity fieldValue
Orville.marshallField PgSequence -> Int64
pgSequenceCache FieldDefinition NotNull Int64
sequenceCacheField
    SqlMarshaller PgSequence (Bool -> PgSequence)
-> SqlMarshaller PgSequence Bool
-> SqlMarshaller PgSequence PgSequence
forall a b.
SqlMarshaller PgSequence (a -> b)
-> SqlMarshaller PgSequence a -> SqlMarshaller PgSequence b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (PgSequence -> Bool)
-> FieldDefinition NotNull Bool -> SqlMarshaller PgSequence Bool
forall writeEntity fieldValue nullability.
(writeEntity -> fieldValue)
-> FieldDefinition nullability fieldValue
-> SqlMarshaller writeEntity fieldValue
Orville.marshallField PgSequence -> Bool
pgSequenceCycle FieldDefinition NotNull Bool
sequenceCycleField

{- |
  The @seqrelid@ column of the @pg_cataglog.pg_sequence@ table.

@since 1.0.0.0
-}
sequencePgClassOidField :: Orville.FieldDefinition Orville.NotNull LibPQ.Oid
sequencePgClassOidField :: FieldDefinition NotNull Oid
sequencePgClassOidField =
  String -> FieldDefinition NotNull Oid
oidTypeField String
"seqrelid"

{- |
  The @seqtypid@ column of the @pg_catalog.pg_sequence@ table.

@since 1.0.0.0
-}
sequenceTypeOidField :: Orville.FieldDefinition Orville.NotNull LibPQ.Oid
sequenceTypeOidField :: FieldDefinition NotNull Oid
sequenceTypeOidField =
  String -> FieldDefinition NotNull Oid
oidTypeField String
"seqtypid"

{- |
  The @seqstart@ column of the @pg_catalog.pg_sequence@ table.

@since 1.0.0.0
-}
sequenceStartField :: Orville.FieldDefinition Orville.NotNull Int64
sequenceStartField :: FieldDefinition NotNull Int64
sequenceStartField =
  String -> FieldDefinition NotNull Int64
Orville.bigIntegerField String
"seqstart"

{- |
  The @seqincrement@ column of the @pg_catalog.pg_sequence@ table.

@since 1.0.0.0
-}
sequenceIncrementField :: Orville.FieldDefinition Orville.NotNull Int64
sequenceIncrementField :: FieldDefinition NotNull Int64
sequenceIncrementField =
  String -> FieldDefinition NotNull Int64
Orville.bigIntegerField String
"seqincrement"

{- |
  The @seqmax@ column of the @pg_catalog.pg_sequence@ table.

@since 1.0.0.0
-}
sequenceMaxField :: Orville.FieldDefinition Orville.NotNull Int64
sequenceMaxField :: FieldDefinition NotNull Int64
sequenceMaxField =
  String -> FieldDefinition NotNull Int64
Orville.bigIntegerField String
"seqmax"

{- |
  The @seqmin@ column of the @pg_catalog.pg_sequence@ table.

@since 1.0.0.0
-}
sequenceMinField :: Orville.FieldDefinition Orville.NotNull Int64
sequenceMinField :: FieldDefinition NotNull Int64
sequenceMinField =
  String -> FieldDefinition NotNull Int64
Orville.bigIntegerField String
"seqmin"

{- |
  The @seqcache@ column of the @pg_catalog.pg_sequence@ table.

@since 1.0.0.0
-}
sequenceCacheField :: Orville.FieldDefinition Orville.NotNull Int64
sequenceCacheField :: FieldDefinition NotNull Int64
sequenceCacheField =
  String -> FieldDefinition NotNull Int64
Orville.bigIntegerField String
"seqcache"

{- |
  The @seqcycle@ column of the @pg_catalog.pg_sequence@ table.

@since 1.0.0.0
-}
sequenceCycleField :: Orville.FieldDefinition Orville.NotNull Bool
sequenceCycleField :: FieldDefinition NotNull Bool
sequenceCycleField =
  String -> FieldDefinition NotNull Bool
Orville.booleanField String
"seqcycle"