Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix surreal #254

Merged
merged 1 commit into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/allographer/schema_builder/enums.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ type ForeignOnDelete* = enum
SET_NULL
NO_ACTION


type DefaultDateTime* = enum
None
Current
CurrentOnUpdate

type UsecaseType* = enum
Create
Alter
Expand Down
8 changes: 8 additions & 0 deletions src/allographer/schema_builder/models/column.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Column* = ref object
defaultFloat*: float
defaultString*: string
defaultJson*: JsonNode
defaultDatetime*: DefaultDateTime
foreignOnDelete*: ForeignOnDelete
info*: JsonNode
checksum*:string
Expand Down Expand Up @@ -335,6 +336,13 @@ proc default*(c: Column, value:JsonNode): Column =
return c


proc default*(c: Column, value:DefaultDateTime): Column =
if not c.isAutoIncrement:
c.isDefault = true
c.defaultDatetime = value
return c


proc default*(c: Column):Column =
if not c.isAutoIncrement:
c.isDefault = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ./sub/create_column_query
proc createTable*(self: SurrealSchema, isReset:bool) =
var queries:seq[string]
queries.add(&"DEFINE TABLE `{self.table.name}` SCHEMAFULL")

for i, column in self.table.columns:
queries.add(createColumnString(self.table, column))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ proc createIncrementsColumn(column:Column, table:Table):seq[string] =

proc createIntColumn(column:Column, table:Table):seq[string] =
var query = ""

if column.isAutoIncrement:
query.add(&"""
INSERT INTO `_autoincrement_sequences` {{table: "{table.name}", column: "{column.name}", max_index: 0}};
Expand All @@ -42,7 +42,7 @@ proc createIntColumn(column:Column, table:Table):seq[string] =
UPDATE `_autoincrement_sequences` MERGE {{max_index: $val}} WHERE `table` = "{table.name}" AND `column` = "{column.name}";
}};
""")

query.add(&"DEFINE FIELD `{column.name}` ON TABLE `{table.name}` TYPE int")
return @[query]

Expand All @@ -56,7 +56,7 @@ proc createIntColumn(column:Column, table:Table):seq[string] =
query.add(&" AND $value >= 0")
else:
query.add(&" ASSERT $value >= 0")

if column.isDefault:
query.add(&" VALUE $value OR {column.defaultInt}")
elif column.isNullable:
Expand Down Expand Up @@ -161,7 +161,7 @@ proc createCharColumn(column:Column, table:Table):seq[string] =
query.add(" VALUE $value OR NULL")
else:
query.add(" VALUE $value OR ''")

if column.isAutoIncrement:
notAllowedOption("autoincrement", "decimal", column.name)

Expand Down Expand Up @@ -239,35 +239,16 @@ proc createTextColumn(column:Column, table:Table):seq[string] =
# =============================================================================
# date
# =============================================================================
# proc createDateColumn(column:Column, table:Table):seq[string] =
# var query = &"DEFINE FIELD `{column.name}` ON TABLE `{table.name}` TYPE string"

# if not column.isNullable:
# query.add(" ASSERT $value != NONE")

# if column.isDefault:
# query.add(&" VALUE $value OR '{column.defaultString}'")

# result.add(query)

# if column.isIndex:
# result.add(&"DEFINE INDEX `{table.name}_{column.name}_index` ON TABLE `{table.name}` COLUMNS `{column.name}`")

# if column.isUnique:
# result.add(&"DEFINE INDEX `{table.name}_{column.name}_unique` ON TABLE `{table.name}` COLUMNS `{column.name}` UNIQUE")

# if column.isUnsigned:
# notAllowedOption("unsigned", "varchar", column.name)


proc createDatetimeColumn(column:Column, table:Table):seq[string] =
var query = &"DEFINE FIELD `{column.name}` ON TABLE `{table.name}` TYPE datetime"

if not column.isNullable:
query.add(" ASSERT $value != NONE")

if column.isDefault:
if column.isDefault and column.defaultDatetime == Current:
query.add(&" VALUE $value OR time::now()")
elif column.isDefault and column.defaultDatetime == CurrentOnUpdate:
query.add(&" VALUE time::now()")
elif column.isNullable:
query.add(" VALUE $value OR NULL")
else:
Expand All @@ -288,50 +269,8 @@ proc createDatetimeColumn(column:Column, table:Table):seq[string] =
notAllowedOption("unsigned", "varchar", column.name)


# proc createTimeColumn(column:Column, table:Table):seq[string] =
# var query = &"DEFINE FIELD `{column.name}` ON TABLE `{table.name}` TYPE string"

# if not column.isNullable:
# query.add(" ASSERT $value != NONE")

# if column.isDefault:
# query.add(&" VALUE $value OR '{column.defaultString}'")

# result.add(query)

# if column.isIndex:
# result.add(&"DEFINE INDEX `{table.name}_{column.name}_index` ON TABLE `{table.name}` COLUMNS `{column.name}`")

# if column.isUnique:
# result.add(&"DEFINE INDEX `{table.name}_{column.name}_unique` ON TABLE `{table.name}` COLUMNS `{column.name}` UNIQUE")

# if column.isUnsigned:
# notAllowedOption("unsigned", "varchar", column.name)


# proc createTimestampColumn(column:Column, table:Table):seq[string] =
# var query = &"DEFINE FIELD `{column.name}` ON TABLE `{table.name}` TYPE string"

# if not column.isNullable:
# query.add(" ASSERT $value != NONE")

# if column.isDefault:
# query.add(&" VALUE $value OR '{column.defaultString}'")

# result.add(query)

# if column.isIndex:
# result.add(&"DEFINE INDEX `{table.name}_{column.name}_index` ON TABLE `{table.name}` COLUMNS `{column.name}`")

# if column.isUnique:
# result.add(&"DEFINE INDEX `{table.name}_{column.name}_unique` ON TABLE `{table.name}` COLUMNS `{column.name}` UNIQUE")

# if column.isUnsigned:
# notAllowedOption("unsigned", "varchar", column.name)


proc createTimestampsColumn(column:Column, table:Table):seq[string] =
result.add(&"DEFINE FIELD `created_at` ON TABLE `{table.name}` TYPE datetime VALUE time::now()")
result.add(&"DEFINE FIELD `created_at` ON TABLE `{table.name}` TYPE datetime VALUE $value OR time::now()")
result.add(&"DEFINE INDEX `{table.name}_created_at_index` ON TABLE `{table.name}` COLUMNS `created_at`")

result.add(&"DEFINE FIELD `updated_at` ON TABLE `{table.name}` TYPE datetime VALUE time::now()")
Expand Down
Loading
Loading