module IHP.IDE.CodeGen.Types where

import IHP.Prelude
import IHP.IDE.SchemaDesigner.Types

data GeneratorAction
    = CreateFile { GeneratorAction -> Text
filePath :: Text, GeneratorAction -> Text
fileContent :: Text }
    | AppendToFile { filePath :: Text, fileContent :: Text }
    | AppendToMarker { GeneratorAction -> Text
marker :: Text, filePath :: Text, fileContent :: Text }
    | AddImport { filePath :: Text, fileContent :: Text }
    | AddAction { filePath :: Text, fileContent :: Text }
    | AddToDataConstructor { GeneratorAction -> Text
dataConstructor :: Text, filePath :: Text, fileContent :: Text }
    | AddMountToFrontController { filePath :: Text, GeneratorAction -> Text
applicationName :: Text }
    | EnsureDirectory { GeneratorAction -> Text
directory :: Text }
    | RunShellCommand { GeneratorAction -> Text
shellCommand :: Text }
    deriving (Int -> GeneratorAction -> ShowS
[GeneratorAction] -> ShowS
GeneratorAction -> String
(Int -> GeneratorAction -> ShowS)
-> (GeneratorAction -> String)
-> ([GeneratorAction] -> ShowS)
-> Show GeneratorAction
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GeneratorAction] -> ShowS
$cshowList :: [GeneratorAction] -> ShowS
show :: GeneratorAction -> String
$cshow :: GeneratorAction -> String
showsPrec :: Int -> GeneratorAction -> ShowS
$cshowsPrec :: Int -> GeneratorAction -> ShowS
Show, GeneratorAction -> GeneratorAction -> Bool
(GeneratorAction -> GeneratorAction -> Bool)
-> (GeneratorAction -> GeneratorAction -> Bool)
-> Eq GeneratorAction
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GeneratorAction -> GeneratorAction -> Bool
$c/= :: GeneratorAction -> GeneratorAction -> Bool
== :: GeneratorAction -> GeneratorAction -> Bool
$c== :: GeneratorAction -> GeneratorAction -> Bool
Eq)



fieldsForTable :: [Statement] -> Text -> Maybe [Text]
fieldsForTable :: [Statement] -> Text -> Maybe [Text]
fieldsForTable [Statement]
database Text
name =
    case [Statement] -> Text -> Maybe Statement
getTable [Statement]
database Text
name of
        Just (StatementCreateTable CreateTable { [Column]
$sel:columns:CreateTable :: CreateTable -> [Column]
columns :: [Column]
columns, PrimaryKeyConstraint
$sel:primaryKeyConstraint:CreateTable :: CreateTable -> PrimaryKeyConstraint
primaryKeyConstraint :: PrimaryKeyConstraint
primaryKeyConstraint }) -> [Column]
columns
                [Column] -> ([Column] -> [Column]) -> [Column]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> (Column -> Bool) -> [Column] -> [Column]
forall a. (a -> Bool) -> [a] -> [a]
filter (PrimaryKeyConstraint -> Column -> Bool
columnRelevantForCreateOrEdit PrimaryKeyConstraint
primaryKeyConstraint)
                [Column] -> ([Column] -> [Text]) -> [Text]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> (Column -> Text) -> [Column] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Proxy "name" -> Column -> Text
forall model (name :: Symbol) value.
(KnownSymbol name, HasField name model value) =>
Proxy name -> model -> value
get IsLabel "name" (Proxy "name")
Proxy "name"
#name)
                [Text] -> ([Text] -> [Text]) -> [Text]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
columnNameToFieldName
                [Text] -> ([Text] -> Maybe [Text]) -> Maybe [Text]
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> [Text] -> Maybe [Text]
forall a. a -> Maybe a
Just
        Maybe Statement
_ -> Maybe [Text]
forall a. Maybe a
Nothing
-- | Returns True when a column should be part of the generated controller or forms
--
-- Returrns @False@ for primary keys, or fields such as @created_at@
columnRelevantForCreateOrEdit :: PrimaryKeyConstraint -> Column -> Bool
columnRelevantForCreateOrEdit :: PrimaryKeyConstraint -> Column -> Bool
columnRelevantForCreateOrEdit PrimaryKeyConstraint
_ Column
column
    | (Proxy "columnType" -> Column -> PostgresType
forall model (name :: Symbol) value.
(KnownSymbol name, HasField name model value) =>
Proxy name -> model -> value
get IsLabel "columnType" (Proxy "columnType")
Proxy "columnType"
#columnType Column
column PostgresType -> PostgresType -> Bool
forall a. Eq a => a -> a -> Bool
== PostgresType
PTimestamp Bool -> Bool -> Bool
|| Proxy "columnType" -> Column -> PostgresType
forall model (name :: Symbol) value.
(KnownSymbol name, HasField name model value) =>
Proxy name -> model -> value
get IsLabel "columnType" (Proxy "columnType")
Proxy "columnType"
#columnType Column
column PostgresType -> PostgresType -> Bool
forall a. Eq a => a -> a -> Bool
== PostgresType
PTimestampWithTimezone)
    Bool -> Bool -> Bool
&& (Maybe Expression -> Bool
forall a. Maybe a -> Bool
isJust (Proxy "defaultValue" -> Column -> Maybe Expression
forall model (name :: Symbol) value.
(KnownSymbol name, HasField name model value) =>
Proxy name -> model -> value
get IsLabel "defaultValue" (Proxy "defaultValue")
Proxy "defaultValue"
#defaultValue Column
column))
    = Bool
False
columnRelevantForCreateOrEdit (PrimaryKeyConstraint [Text]
primaryKeyColumns) Column
column =
    Proxy "name" -> Column -> Text
forall model (name :: Symbol) value.
(KnownSymbol name, HasField name model value) =>
Proxy name -> model -> value
get IsLabel "name" (Proxy "name")
Proxy "name"
#name Column
column Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Text]
primaryKeyColumns

getTable :: [Statement] -> Text -> Maybe Statement
getTable :: [Statement] -> Text -> Maybe Statement
getTable [Statement]
schema Text
name = (Statement -> Bool) -> [Statement] -> Maybe Statement
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find Statement -> Bool
isTable [Statement]
schema
    where
        isTable :: Statement -> Bool
        isTable :: Statement -> Bool
isTable table :: Statement
table@(StatementCreateTable CreateTable { $sel:name:CreateTable :: CreateTable -> Text
name = Text
name' }) | Text
name Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
name' = Bool
True
        isTable Statement
_ = Bool
False