{-# language DeriveFunctor #-}
{-# language DerivingStrategies #-}
{-# language DisambiguateRecordFields #-}
{-# language NamedFieldPuns #-}
{-# language StandaloneKindSignatures #-}

module Rel8.Schema.Table
  ( TableSchema(..)
  , ppTable
  )
where

-- base
import Data.Kind ( Type )
import Prelude

-- opaleye
import qualified Opaleye.Internal.HaskellDB.Sql as Opaleye
import qualified Opaleye.Internal.HaskellDB.Sql.Print as Opaleye

-- pretty
import Text.PrettyPrint ( Doc )


-- | The schema for a table. This is used to specify the name and schema that a
-- table belongs to (the @FROM@ part of a SQL query), along with the schema of
-- the columns within this table.
-- 
-- For each selectable table in your database, you should provide a
-- @TableSchema@ in order to interact with the table via Rel8.
type TableSchema :: Type -> Type
data TableSchema names = TableSchema
  { TableSchema names -> String
name :: String
    -- ^ The name of the table.
  , TableSchema names -> Maybe String
schema :: Maybe String
    -- ^ The schema that this table belongs to. If 'Nothing', whatever is on
    -- the connection's @search_path@ will be used.
  , TableSchema names -> names
columns :: names
    -- ^ The columns of the table. Typically you would use a a higher-kinded
    -- data type here, parameterized by the 'Rel8.ColumnSchema.ColumnSchema' functor.
  }
  deriving stock a -> TableSchema b -> TableSchema a
(a -> b) -> TableSchema a -> TableSchema b
(forall a b. (a -> b) -> TableSchema a -> TableSchema b)
-> (forall a b. a -> TableSchema b -> TableSchema a)
-> Functor TableSchema
forall a b. a -> TableSchema b -> TableSchema a
forall a b. (a -> b) -> TableSchema a -> TableSchema b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> TableSchema b -> TableSchema a
$c<$ :: forall a b. a -> TableSchema b -> TableSchema a
fmap :: (a -> b) -> TableSchema a -> TableSchema b
$cfmap :: forall a b. (a -> b) -> TableSchema a -> TableSchema b
Functor


ppTable :: TableSchema a -> Doc
ppTable :: TableSchema a -> Doc
ppTable TableSchema {String
name :: String
name :: forall names. TableSchema names -> String
name, Maybe String
schema :: Maybe String
schema :: forall names. TableSchema names -> Maybe String
schema} = SqlTable -> Doc
Opaleye.ppTable SqlTable :: Maybe String -> String -> SqlTable
Opaleye.SqlTable
  { sqlTableSchemaName :: Maybe String
sqlTableSchemaName = Maybe String
schema
  , sqlTableName :: String
sqlTableName = String
name
  }