{-|
Module: IHP.IDE.SchemaDesigner.Types
Description: Types for representing an AST of SQL DDL
Copyright: (c) digitally induced GmbH, 2020
-}
module IHP.IDE.SchemaDesigner.Types where

import IHP.Prelude

data Statement
    =
    -- | CREATE TABLE name ( columns );
      StatementCreateTable { Statement -> CreateTable
unsafeGetCreateTable :: CreateTable }
    -- | CREATE TYPE name AS ENUM ( values );
    | CreateEnumType { Statement -> Text
name :: Text, Statement -> [Text]
values :: [Text] }
    -- | CREATE EXTENSION IF NOT EXISTS "name";
    | CreateExtension { name :: Text, Statement -> Bool
ifNotExists :: Bool }
    -- | ALTER TABLE tableName ADD CONSTRAINT constraintName constraint;
    | AddConstraint { Statement -> Text
tableName :: Text, Statement -> Text
constraintName :: Text, Statement -> Constraint
constraint :: Constraint }
    | UnknownStatement { Statement -> Text
raw :: Text }
    | Comment { Statement -> Text
content :: Text }
    -- | CREATE INDEX indexName ON tableName (columnName);
    | CreateIndex { Statement -> Text
indexName :: Text, tableName :: Text, Statement -> [Text]
columnNames :: [Text] }
    deriving (Statement -> Statement -> Bool
(Statement -> Statement -> Bool)
-> (Statement -> Statement -> Bool) -> Eq Statement
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Statement -> Statement -> Bool
$c/= :: Statement -> Statement -> Bool
== :: Statement -> Statement -> Bool
$c== :: Statement -> Statement -> Bool
Eq, Int -> Statement -> ShowS
[Statement] -> ShowS
Statement -> String
(Int -> Statement -> ShowS)
-> (Statement -> String)
-> ([Statement] -> ShowS)
-> Show Statement
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Statement] -> ShowS
$cshowList :: [Statement] -> ShowS
show :: Statement -> String
$cshow :: Statement -> String
showsPrec :: Int -> Statement -> ShowS
$cshowsPrec :: Int -> Statement -> ShowS
Show)

data CreateTable
  = CreateTable
      { CreateTable -> Text
name :: Text
      , CreateTable -> [Column]
columns :: [Column]
      , CreateTable -> PrimaryKeyConstraint
primaryKeyConstraint :: PrimaryKeyConstraint
      , CreateTable -> [Constraint]
constraints :: [Constraint]
      }
  deriving (CreateTable -> CreateTable -> Bool
(CreateTable -> CreateTable -> Bool)
-> (CreateTable -> CreateTable -> Bool) -> Eq CreateTable
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CreateTable -> CreateTable -> Bool
$c/= :: CreateTable -> CreateTable -> Bool
== :: CreateTable -> CreateTable -> Bool
$c== :: CreateTable -> CreateTable -> Bool
Eq, Int -> CreateTable -> ShowS
[CreateTable] -> ShowS
CreateTable -> String
(Int -> CreateTable -> ShowS)
-> (CreateTable -> String)
-> ([CreateTable] -> ShowS)
-> Show CreateTable
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CreateTable] -> ShowS
$cshowList :: [CreateTable] -> ShowS
show :: CreateTable -> String
$cshow :: CreateTable -> String
showsPrec :: Int -> CreateTable -> ShowS
$cshowsPrec :: Int -> CreateTable -> ShowS
Show)

data Column = Column
    { Column -> Text
name :: Text
    , Column -> PostgresType
columnType :: PostgresType
    , Column -> Maybe Expression
defaultValue :: Maybe Expression
    , Column -> Bool
notNull :: Bool
    , Column -> Bool
isUnique :: Bool
    }
    deriving (Column -> Column -> Bool
(Column -> Column -> Bool)
-> (Column -> Column -> Bool) -> Eq Column
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Column -> Column -> Bool
$c/= :: Column -> Column -> Bool
== :: Column -> Column -> Bool
$c== :: Column -> Column -> Bool
Eq, Int -> Column -> ShowS
[Column] -> ShowS
Column -> String
(Int -> Column -> ShowS)
-> (Column -> String) -> ([Column] -> ShowS) -> Show Column
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Column] -> ShowS
$cshowList :: [Column] -> ShowS
show :: Column -> String
$cshow :: Column -> String
showsPrec :: Int -> Column -> ShowS
$cshowsPrec :: Int -> Column -> ShowS
Show)

data OnDelete
    = NoAction
    | Restrict
    | SetNull
    | SetDefault
    | Cascade
    deriving (Int -> OnDelete -> ShowS
[OnDelete] -> ShowS
OnDelete -> String
(Int -> OnDelete -> ShowS)
-> (OnDelete -> String) -> ([OnDelete] -> ShowS) -> Show OnDelete
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [OnDelete] -> ShowS
$cshowList :: [OnDelete] -> ShowS
show :: OnDelete -> String
$cshow :: OnDelete -> String
showsPrec :: Int -> OnDelete -> ShowS
$cshowsPrec :: Int -> OnDelete -> ShowS
Show, OnDelete -> OnDelete -> Bool
(OnDelete -> OnDelete -> Bool)
-> (OnDelete -> OnDelete -> Bool) -> Eq OnDelete
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: OnDelete -> OnDelete -> Bool
$c/= :: OnDelete -> OnDelete -> Bool
== :: OnDelete -> OnDelete -> Bool
$c== :: OnDelete -> OnDelete -> Bool
Eq)

newtype PrimaryKeyConstraint
  = PrimaryKeyConstraint { PrimaryKeyConstraint -> [Text]
primaryKeyColumnNames :: [Text] }
  deriving (PrimaryKeyConstraint -> PrimaryKeyConstraint -> Bool
(PrimaryKeyConstraint -> PrimaryKeyConstraint -> Bool)
-> (PrimaryKeyConstraint -> PrimaryKeyConstraint -> Bool)
-> Eq PrimaryKeyConstraint
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PrimaryKeyConstraint -> PrimaryKeyConstraint -> Bool
$c/= :: PrimaryKeyConstraint -> PrimaryKeyConstraint -> Bool
== :: PrimaryKeyConstraint -> PrimaryKeyConstraint -> Bool
$c== :: PrimaryKeyConstraint -> PrimaryKeyConstraint -> Bool
Eq, Int -> PrimaryKeyConstraint -> ShowS
[PrimaryKeyConstraint] -> ShowS
PrimaryKeyConstraint -> String
(Int -> PrimaryKeyConstraint -> ShowS)
-> (PrimaryKeyConstraint -> String)
-> ([PrimaryKeyConstraint] -> ShowS)
-> Show PrimaryKeyConstraint
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PrimaryKeyConstraint] -> ShowS
$cshowList :: [PrimaryKeyConstraint] -> ShowS
show :: PrimaryKeyConstraint -> String
$cshow :: PrimaryKeyConstraint -> String
showsPrec :: Int -> PrimaryKeyConstraint -> ShowS
$cshowsPrec :: Int -> PrimaryKeyConstraint -> ShowS
Show)

data Constraint
    -- | FOREIGN KEY (columnName) REFERENCES referenceTable (referenceColumn) ON DELETE onDelete;
    = ForeignKeyConstraint
        { Constraint -> Text
columnName :: Text
        , Constraint -> Text
referenceTable :: Text
        , Constraint -> Maybe Text
referenceColumn :: Maybe Text
        , Constraint -> Maybe OnDelete
onDelete :: Maybe OnDelete
        }
    | UniqueConstraint
        { Constraint -> [Text]
columnNames :: [Text] }
    | CheckConstraint { Constraint -> Expression
checkExpression :: Expression }
    deriving (Constraint -> Constraint -> Bool
(Constraint -> Constraint -> Bool)
-> (Constraint -> Constraint -> Bool) -> Eq Constraint
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Constraint -> Constraint -> Bool
$c/= :: Constraint -> Constraint -> Bool
== :: Constraint -> Constraint -> Bool
$c== :: Constraint -> Constraint -> Bool
Eq, Int -> Constraint -> ShowS
[Constraint] -> ShowS
Constraint -> String
(Int -> Constraint -> ShowS)
-> (Constraint -> String)
-> ([Constraint] -> ShowS)
-> Show Constraint
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Constraint] -> ShowS
$cshowList :: [Constraint] -> ShowS
show :: Constraint -> String
$cshow :: Constraint -> String
showsPrec :: Int -> Constraint -> ShowS
$cshowsPrec :: Int -> Constraint -> ShowS
Show)

data Expression =
    -- | Sql string like @'hello'@
    TextExpression Text
    -- | Simple variable like @users@
    | VarExpression Text
    -- | Simple call, like @COALESCE(name, 'unknown name')@
    | CallExpression Text [Expression]
    -- | Not equal operator, a <> b
    | NotEqExpression Expression Expression
    deriving (Expression -> Expression -> Bool
(Expression -> Expression -> Bool)
-> (Expression -> Expression -> Bool) -> Eq Expression
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Expression -> Expression -> Bool
$c/= :: Expression -> Expression -> Bool
== :: Expression -> Expression -> Bool
$c== :: Expression -> Expression -> Bool
Eq, Int -> Expression -> ShowS
[Expression] -> ShowS
Expression -> String
(Int -> Expression -> ShowS)
-> (Expression -> String)
-> ([Expression] -> ShowS)
-> Show Expression
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Expression] -> ShowS
$cshowList :: [Expression] -> ShowS
show :: Expression -> String
$cshow :: Expression -> String
showsPrec :: Int -> Expression -> ShowS
$cshowsPrec :: Int -> Expression -> ShowS
Show)

data PostgresType
    = PUUID
    | PText
    | PInt
    | PSmallInt
    | PBigInt
    | PBoolean
    | PTimestampWithTimezone
    | PTimestamp
    | PReal
    | PDouble
    | PPoint
    | PDate
    | PBinary
    | PTime
    | PNumeric { PostgresType -> Maybe Int
precision :: Maybe Int, PostgresType -> Maybe Int
scale :: Maybe Int }
    | PVaryingN Int
    | PCharacterN Int
    | PSerial
    | PBigserial
    | PJSONB
    | PInet
    | PArray PostgresType
    | PCustomType Text
    deriving (PostgresType -> PostgresType -> Bool
(PostgresType -> PostgresType -> Bool)
-> (PostgresType -> PostgresType -> Bool) -> Eq PostgresType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PostgresType -> PostgresType -> Bool
$c/= :: PostgresType -> PostgresType -> Bool
== :: PostgresType -> PostgresType -> Bool
$c== :: PostgresType -> PostgresType -> Bool
Eq, Int -> PostgresType -> ShowS
[PostgresType] -> ShowS
PostgresType -> String
(Int -> PostgresType -> ShowS)
-> (PostgresType -> String)
-> ([PostgresType] -> ShowS)
-> Show PostgresType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PostgresType] -> ShowS
$cshowList :: [PostgresType] -> ShowS
show :: PostgresType -> String
$cshow :: PostgresType -> String
showsPrec :: Int -> PostgresType -> ShowS
$cshowsPrec :: Int -> PostgresType -> ShowS
Show)