Skip to content

Commit

Permalink
Merge pull request #132 from itsumura-h/feature/131_index
Browse files Browse the repository at this point in the history
add index
  • Loading branch information
itsumura-h authored Jan 13, 2021
2 parents 1c53354 + 37d6d90 commit 0e854d4
Show file tree
Hide file tree
Showing 21 changed files with 109 additions and 304 deletions.
2 changes: 1 addition & 1 deletion allographer.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.13.6"
version = "0.14.0"
author = "Hidenobu Itsumura @dumblepytech1 as 'medy'"
description = "A Nim query builder library inspired by Laravel/PHP and Orator/Python"
license = "MIT"
Expand Down
3 changes: 2 additions & 1 deletion documents/schema_builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ alter(
|`.nullable()`|Designate that the column allows NULL values|
|`.default(value)`|Declare a default value for a column|
|`.unsigned()`|Set INTEGER to UNSIGNED|
|`.unique()`|Adding a unique index|
|`.unique()`|Adding an unique index|
|`.index()`|Adding an index|

## Foreign Key Constraints
For example, let's define a `user_id` column on the table that references the `id` column on a `users` table:
Expand Down
10 changes: 6 additions & 4 deletions example/migrate.nim
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import strformat, json, progress
import strformat, json, progress, oids
import bcrypt
import ../src/allographer/query_builder
import ../src/allographer/schema_builder
# import allographer/query_builder
# import allographer/schema_builder


# マイグレーション
Expand All @@ -14,13 +12,15 @@ schema([
], reset=true),
table("Users",[
Column().increments("id"),
Column().string("oid").index().nullable(),
Column().string("oid2").index(),
Column().string("Name").nullable(),
Column().string("email").nullable(),
Column().string("password").nullable(),
Column().string("address").nullable(),
Column().date("birth_date").nullable(),
Column().foreign("auth_id").reference("id").on("Auth").onDelete(SET_NULL)
], reset=true)
], reset=true),
])

# シーダー
Expand All @@ -41,6 +41,8 @@ for i in 1..total:
let authId = if i mod 2 == 0: 1 else: 2
insertData.add(
%*{
"oid": $(genOid()),
"oid2": $(genOid()),
"Name": &"user{i}",
"email": &"user{i}@gmail.com",
"password": password,
Expand Down
2 changes: 1 addition & 1 deletion src/allographer/query_builder/exec.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json, strutils, strformat, algorithm, options, asyncdispatch
import base, builders
import ../util
import ../utils
import ../connection


Expand Down
2 changes: 1 addition & 1 deletion src/allographer/query_builder/generators.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from strformat import `&`
from strutils import contains, isUpperAscii

import base
from ../util import wrapUpper
from ../utils import wrapUpper


# ==================== SELECT ====================
Expand Down
2 changes: 1 addition & 1 deletion src/allographer/schema_builder/alter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import
./alters/sqlite_alter,
./alters/mysql_alter,
./alters/postgres_alter
import ../util
import ../utils
import ../connection


Expand Down
2 changes: 1 addition & 1 deletion src/allographer/schema_builder/alters/mysql_alter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import strformat, re, strutils
import ../table
import ../column
import ../migrates/mysql_migrate
import ../../util
import ../../utils
import ../../connection

proc add(column:Column, table:string) =
Expand Down
2 changes: 1 addition & 1 deletion src/allographer/schema_builder/alters/postgres_alter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import strformat, re, strutils
import ../table
import ../column
import ../migrates/postgres_migrate
import ../../util
import ../../utils
import ../../connection

proc add(column:Column, table:string) =
Expand Down
2 changes: 1 addition & 1 deletion src/allographer/schema_builder/alters/sqlite_alter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import strformat, re, strutils
import ../table
import ../column
import ../migrates/sqlite_migrate
import ../../util
import ../../utils
import ../../connection

proc add(column:Column, table:string) =
Expand Down
5 changes: 5 additions & 0 deletions src/allographer/schema_builder/column.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type
Column* = ref object
name*: string
typ*: RdbTypekind
isIndex*: bool
isNullable*: bool
isUnsigned*: bool
isDefault*: bool
Expand Down Expand Up @@ -93,6 +94,10 @@ proc default*(c:Column):Column =
c.isDefault = true
return c

proc index*(c:Column):Column =
c.isIndex = true
return c

proc nullable*(c: Column): Column =
c.isNullable = true
return c
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json, strformat
import ../column, utils
import ../column, generator_util
import ../../utils

# =============================================================================
# int
Expand Down Expand Up @@ -374,6 +375,10 @@ proc foreignGenerator*(name:string, table:string, column:string,
elif foreignOnDelete == NO_ACTION:
onDeleteString = "NO ACTION"


result = &", FOREIGN KEY(`{name}`) REFERENCES {table}({column})"
result.add(&" ON DELETE {onDeleteString}")

proc indexGenerate*(table, column:string):string =
var table = table
wrapUpper(table)
return &"CREATE INDEX {column}_index ON {table}({column})"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json, strformat
import ../column
import utils
import ../../util
import generator_util
import ../../utils

# =============================================================================
# int
Expand Down Expand Up @@ -403,3 +403,8 @@ proc foreignGenerator*(name:string, tableName:string, column:string,
wrapUpper(tableName)
result = &", FOREIGN KEY(\"{name}\") REFERENCES {tableName}({column})"
result.add(&" ON DELETE {onDeleteString}")

proc indexGenerate*(table, column:string):string =
var table = table
wrapUpper(table)
return &"CREATE INDEX {column}_index ON {table}({column})"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json, strformat
import ../column
import ../../utils
from db_common import DbError

# =============================================================================
Expand Down Expand Up @@ -285,3 +286,8 @@ proc foreignGenerator*(name:string, table:string, column:string,

result = &", FOREIGN KEY('{name}') REFERENCES {table}({column})"
result.add(&" ON DELETE {onDeleteString}")

proc indexGenerate*(table, column:string):string =
var table = table
wrapUpper(table)
return &"CREATE INDEX {column}_index ON {table}({column})"
5 changes: 4 additions & 1 deletion src/allographer/schema_builder/migrates/mysql_migrate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import
../table,
../column
import ../generators/mysql_generators
import ../../util
import ../../utils


proc generateColumnString*(column:Column):string =
Expand Down Expand Up @@ -279,3 +279,6 @@ proc migrate*(this:Table):string =
var tableName = this.name
wrapUpper(tableName)
return &"CREATE TABLE {tableName} ({columnString}{foreignString})"

proc createIndex*(table, column:string):string =
return indexGenerate(table, column)
Loading

0 comments on commit 0e854d4

Please sign in to comment.