Skip to content

Commit

Permalink
compileCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
amitaibu committed Aug 26, 2024
1 parent 5a11fd3 commit fa31d5b
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions ihp-ide/IHP/SchemaCompiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -592,11 +592,27 @@ columnPlaceholder column@Column { columnType } = if columnPlaceholderNeedsTypeca
columnPlaceholderNeedsTypecast Column { columnType = PArray {} } = True
columnPlaceholderNeedsTypecast _ = False

compileCreate :: CreateTable -> Text
compileCreate table@(CreateTable { name, columns }) =
compileCreate :: (?schema :: Schema) => CreateTable -> Text
compileCreate table@(CreateTable { name, columns, inherits }) =
let
writableColumns = onlyWritableColumns columns
modelName = tableNameToModelName name

-- @todo: Find a better way.
colName = modelName |> pluralize |> Text.toLower

parentColumns = case inherits of
Nothing -> []
Just parentTableName ->
let parentTableDef = findTableByName parentTableName
in case parentTableDef of
Just parentTable ->
parentTable.unsafeGetCreateTable.columns |> filter (\column -> column.name /= "meta" && Text.toLower column.name /= colName && column.name /= "id")

Nothing -> error $ "Parent table " <> cs parentTableName <> " not found for table " <> cs name <> "."

allColumns = columns <> parentColumns

writableColumns = onlyWritableColumns allColumns
columnNames = commaSep (map (.name) writableColumns)
values = commaSep (map columnPlaceholder writableColumns)

Expand Down

0 comments on commit fa31d5b

Please sign in to comment.