Skip to content

Commit

Permalink
add uuid and strForeign (#168)
Browse files Browse the repository at this point in the history
* add uuid and strForeign
  • Loading branch information
itsumura-h authored Sep 9, 2021
1 parent e7e1fce commit 2a9d591
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SQLITE_HOST="/root/project/db.sqlite3"
SQLITE_HOST=":memory:"
SQLITE_HOST="/root/project/db.sqlite3"
# SQLITE_HOST=":memory:"
MY_HOST=mysql
MARIA_HOST=mariadb
MY_PORT=3306
Expand Down
16 changes: 12 additions & 4 deletions documents/schema_builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ from database import rdb
rdb.schema([
table("auth", [
Column().increments("id"),
Column().uuid("uuid"),
Column().string("name"),
Column().timestamps()
]),
table("users", [
Column().increments("id"),
Column().string("name"),
Column().foreign("auth_id").reference("id").on("auth").onDelete(SET_NULL)
Column().strForeign("uuid").reference("uuid").on("auth").onDelete(SET_NULL)
])
])
```
Expand Down Expand Up @@ -187,6 +189,7 @@ template seeder*(rdb:Rdb, tableName, column:string, body:untyped):untyped
## char
|Command|Description|
|---|---|
|`uuid("name")`|VARCHAR indexed and unique column.|
|`char("name", 100)`|CHAR equivalent column with an optional length.|
|`string("name")`|VARCHAR equivalent column.|
|`string("name", 100)`|VARCHAR equivalent column with a optional length.|
Expand Down Expand Up @@ -224,10 +227,15 @@ template seeder*(rdb:Rdb, tableName, column:string, body:untyped):untyped
## Foreign Key Constraints
For example, let's define a `user_id` column on the table that references the `id` column on a `users` table:
```nim
Schema().foreign("user_id")
.reference("id")
.on("users")
.onDelete(SET_NULL)
Column().foreign("user_id")
.reference("id")
.on("users")
.onDelete(SET_NULL)
Column().strForeign("uuid")
.reference("uuid")
.on("users")
.onDelete(SET_NULL)
```

arg of `onDelete` is enum
Expand Down
27 changes: 24 additions & 3 deletions src/allographer/schema_builder/column.nim
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type
rdbEnumField = "rdbEnumField"
rdbJson = "rdbJson"
rdbForeign = "rdbForeign"
rdbStrForeign = "rdbStrForeign"

ForeignOnDelete* = enum
RESTRICT = "RESTRICT"
Expand Down Expand Up @@ -182,6 +183,14 @@ proc string*(self:Column, name:string, length=255):Column =
self.info = %*{"maxLength": length}
return self

proc uuid*(self:Column, name:string):Column =
self.name = name
self.typ = rdbString
self.isUnique = true
self.isIndex = true
self.info = %*{"maxLength": 255}
return self

proc text*(self:Column, name:string):Column =
self.name = name
self.typ = rdbText
Expand Down Expand Up @@ -262,12 +271,24 @@ proc foreign*(self:Column, name:string):Column =
self.name = name
self.previousName = name
self.typ = rdbForeign
self.isUnique = true
self.isIndex = true
return self

proc strForeign*(self:Column, name:string, length=255):Column =
self.name = name
self.previousName = name
self.typ = rdbStrForeign
self.isUnique = true
self.isIndex = true
self.info = %*{"maxLength": length}
return self

proc reference*(self:Column, column:string):Column =
self.info = %*{
"column": column
}
if self.info.isNil:
self.info = %*{"column": column}
else:
self.info["column"] = %column
return self

proc on*(self:Column, table:string):Column =
Expand Down
11 changes: 9 additions & 2 deletions src/allographer/schema_builder/generators/mysql_generators.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json, strformat
import json, strformat, strutils
import ../column, generator_util
import ../../utils

Expand Down Expand Up @@ -367,6 +367,12 @@ proc foreignColumnGenerator*(name:string, isDefault:bool, default:int):string =
if isDefault:
result.add(&" DEFAULT {default}")

proc strForeignColumnGenerator*(name:string, maxLength:int, isDefault:bool, default:string):string =
result = &"`{name}` VARCHAR({maxLength})"
result.add(" UNIQUE")
if isDefault:
result.add(&" DEFAULT {default}")

proc foreignGenerator*(name:string, table:string, column:string,
foreignOnDelete:ForeignOnDelete):string =
var onDeleteString = "RESTRICT"
Expand Down Expand Up @@ -409,5 +415,6 @@ proc alterDeleteForeignGenerator*(table, column:string):string =

proc indexGenerate*(table, column:string):string =
var table = table
let smallTable = table.toLowerAscii()
myWrapUpper(table)
return &"CREATE INDEX {column}_index ON {table}({column})"
return &"CREATE INDEX {smallTable}_{column}_index ON {table}({column})"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json, strformat
import json, strformat, strutils
import ../column
import generator_util
import ../../utils
Expand Down Expand Up @@ -391,6 +391,12 @@ proc foreignColumnGenerator*(name:string, isDefault:bool, default:int):string =
if isDefault:
result.add(&" DEFAULT {default}")

proc strForeignColumnGenerator*(name:string, maxLength:int, isDefault:bool, default:string):string =
result = &"\"{name}\" VARCHAR({maxLength})"
result.add(" UNIQUE")
if isDefault:
result.add(&" DEFAULT {default}")

proc foreignGenerator*(table, column, refTable, refColumn:string,
foreignOnDelete:ForeignOnDelete):string =
var onDeleteString = "RESTRICT"
Expand Down Expand Up @@ -435,5 +441,6 @@ proc alterDeleteForeignGenerator*(table, column:string):string =

proc indexGenerate*(table, column:string):string =
var table = table
let smallTable = table.toLowerAscii()
pgWrapUpper(table)
return &"CREATE INDEX {column}_index ON {table}({column})"
return &"CREATE INDEX {smallTable}_{column}_index ON {table}({column})"
11 changes: 9 additions & 2 deletions src/allographer/schema_builder/generators/sqlite_generators.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json, strformat
import json, strformat, strutils
import ../column
import ../../utils
from db_common import DbError
Expand Down Expand Up @@ -276,6 +276,12 @@ proc foreignColumnGenerator*(name:string, isDefault:bool, default:int):string =
if isDefault:
result.add(&" DEFAULT {default}")

proc strForeignColumnGenerator*(name:string, isDefault:bool, default:string):string =
result = &"'{name}' VARCHAR"
result.add(" UNIQUE")
if isDefault:
result.add(&" DEFAULT {default}")

proc foreignGenerator*(name:string, table:string, column:string,
foreignOnDelete:ForeignOnDelete):string =
var onDeleteString = "RESTRICT"
Expand All @@ -294,5 +300,6 @@ proc alterAddForeignGenerator*(table:string, column:string):string =

proc indexGenerate*(table, column:string):string =
var table = table
let smallTable = table.toLowerAscii()
liteWrapUpper(table)
return &"CREATE INDEX {column}_index ON {table}({column})"
return &"CREATE INDEX {smallTable}_{column}_index ON {table}({column})"
15 changes: 12 additions & 3 deletions src/allographer/schema_builder/migrates/mysql_migrate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,19 @@ proc generateColumnString*(column:Column):string =
columnString.add(
foreignColumnGenerator(column.name, column.isDefault, column.defaultInt)
)
of rdbStrForeign:
columnString.add(
strForeignColumnGenerator(
column.name,
column.info["maxLength"].getInt,
column.isDefault,
column.defaultString
)
)
return columnString

proc generateForeignString(column:Column):string =
if column.typ == rdbForeign:
if column.typ == rdbForeign or column.typ == rdbStrForeign:
return foreignGenerator(
column.name,
column.info["table"].getStr(),
Expand All @@ -265,7 +274,7 @@ proc generateForeignString(column:Column):string =
)

proc generateAlterForeignString(table:string, column:Column):string =
if column.typ == rdbForeign:
if column.typ == rdbForeign or column.typ == rdbStrForeign:
return alterAddForeignGenerator(
table,
column.name,
Expand All @@ -282,7 +291,7 @@ proc migrate*(this:Table):string =
columnString.add(
generateColumnString(column)
)
if column.typ == rdbForeign:
if column.typ == rdbForeign or column.typ == rdbStrForeign:
if columnString.len > 0 or foreignString.len > 0: foreignString.add(", ")
foreignString.add(
generateForeignString(column)
Expand Down
15 changes: 12 additions & 3 deletions src/allographer/schema_builder/migrates/postgres_migrate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,19 @@ proc generateColumnString*(column:Column, tableName=""):string =
columnString.add(
foreignColumnGenerator(column.name, column.isDefault, column.defaultInt)
)
of rdbStrForeign:
columnString.add(
strForeignColumnGenerator(
column.name,
column.info["maxLength"].getInt,
column.isDefault,
column.defaultString
)
)
return columnString

proc generateForeignString(table:string, column:Column):string =
if column.typ == rdbForeign:
if column.typ == rdbForeign or column.typ == rdbStrForeign:
return foreignGenerator(
table,
column.name,
Expand All @@ -286,7 +295,7 @@ proc generateForeignString(table:string, column:Column):string =
)

proc generateAlterForeignString(table:string, column:Column):string =
if column.typ == rdbForeign:
if column.typ == rdbForeign or column.typ == rdbStrForeign:
return alterAddForeignGenerator(
table,
column.name,
Expand All @@ -303,7 +312,7 @@ proc migrate*(self:Table):string =
columnString.add(
generateColumnString(column, self.name)
)
if column.typ == rdbForeign:
if column.typ == rdbForeign or column.typ == rdbStrForeign:
if columnString.len > 0 or foreignString.len > 0: foreignString.add(", ")
foreignString.add(
generateForeignString(self.name, column)
Expand Down
10 changes: 7 additions & 3 deletions src/allographer/schema_builder/migrates/sqlite_migrate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,14 @@ proc generateColumnString*(column:Column):string =
columnString.add(
foreignColumnGenerator(column.name, column.isDefault, column.defaultInt)
)
of rdbStrForeign:
columnString.add(
strForeignColumnGenerator(column.name, column.isDefault, column.defaultString)
)
return columnString

proc generateForeignString(column:Column):string =
if column.typ == rdbForeign:
if column.typ == rdbForeign or column.typ == rdbStrForeign:
return foreignGenerator(
column.name,
column.info["table"].getStr(),
Expand All @@ -264,7 +268,7 @@ proc generateForeignString(column:Column):string =
)

proc generateAlterForeignString(column:Column):string =
if column.typ == rdbForeign:
if column.typ == rdbForeign or column.typ == rdbStrForeign:
return alterAddForeignGenerator(
column.info["table"].getStr(),
column.info["column"].getStr(),
Expand All @@ -278,7 +282,7 @@ proc migrate*(this:Table):string =
columnString.add(
generateColumnString(column)
)
if column.typ == rdbForeign:
if column.typ == rdbForeign or column.typ == rdbStrForeign:
if columnString.len > 0 or foreignString.len > 0: foreignString.add(", ")
foreignString.add(
generateForeignString(column)
Expand Down
2 changes: 1 addition & 1 deletion tests/connections.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let
timeout = getEnv("DB_TIMEOUT").parseInt

let
sqliteDb = dbopen(SQLite3, sqliteHost, maxConnections=maxConnections, shouldDisplayLog=true)
sqliteDb = dbopen(SQLite3, ":memory:", maxConnections=maxConnections, shouldDisplayLog=true)
# mysqlDb = dbopen(MySQL, database, user, password, mysqlHost, mysqlPort, maxConnections, timeout)
# mariaDb = dbopen(MariaDB, database, user, password, mariadbHost, mysqlPort, maxConnections, timeout)
# postgresDb = dbopen(PostgreSQL, database, user, password, pgHost, pgPort, maxConnections, timeout)
Expand Down
Loading

0 comments on commit 2a9d591

Please sign in to comment.