Skip to content

Commit

Permalink
fix clean up (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsumura-h authored Jul 18, 2022
1 parent 88c220b commit 78611d5
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 64 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.23.2"
version = "0.23.3"
author = "Hidenobu Itsumura @dumblepytech1 as 'medy'"
description = "A Nim query builder library inspired by Laravel/PHP and Orator/Python"
license = "MIT"
Expand Down
78 changes: 39 additions & 39 deletions src/allographer/query_builder/exec.nim
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ proc orm[T](row:Option[JsonNode], typ:typedesc[T]):Option[T] =
else:
return none(T)

proc cleanUp(self:Rdb) =
self.query = newJNull()
self.sqlString = ""
self.placeHolder = newSeq[string]()
# proc cleanUp(self:Rdb) =
# self.query = newJNull()
# self.sqlString = ""
# self.placeHolder = newSeq[string]()

# =============================================================================

Expand All @@ -116,7 +116,7 @@ proc toSql*(self: Rdb): string =

proc columns*(self:Rdb):Future[seq[string]] {.async.} =
## get columns sequence from table
defer: self.cleanUp()
# defer: self.cleanUp()
let sql = self.columnBuilder()
try:
self.log.logger(sql, self.placeHolder)
Expand All @@ -127,7 +127,7 @@ proc columns*(self:Rdb):Future[seq[string]] {.async.} =
return newSeq[string]()

proc get*(self: Rdb):Future[seq[JsonNode]] {.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
let sql = self.selectBuilder()
try:
self.log.logger(sql, self.placeHolder)
Expand All @@ -138,7 +138,7 @@ proc get*(self: Rdb):Future[seq[JsonNode]] {.async.} =
return newSeq[JsonNode](0)

proc get*[T](self: Rdb, typ: typedesc[T]): Future[seq[T]] {.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
let sql = self.selectBuilder()
try:
self.log.logger(sql, self.placeHolder)
Expand All @@ -149,7 +149,7 @@ proc get*[T](self: Rdb, typ: typedesc[T]): Future[seq[T]] {.async.} =
return newSeq[typ.type](0)

proc getPlain*(self:Rdb):Future[seq[seq[string]]] {.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
let sql = self.selectBuilder()
try:
self.log.logger(sql, self.placeHolder)
Expand All @@ -161,7 +161,7 @@ proc getPlain*(self:Rdb):Future[seq[seq[string]]] {.async.} =

proc getRaw*(self: Rdb):Future[seq[JsonNode]]{.async.} =
## It is only used with raw()
defer: self.cleanUp()
# defer: self.cleanUp()
try:
self.log.logger(self.sqlString, self.placeHolder)
return getAllRows(self, self.sqlString, self.placeHolder).await
Expand All @@ -172,7 +172,7 @@ proc getRaw*(self: Rdb):Future[seq[JsonNode]]{.async.} =

proc getRaw*[T](self: Rdb, typ: typedesc[T]):Future[seq[T]]{.async.} =
## It is only used with raw()
defer: self.cleanUp()
# defer: self.cleanUp()
try:
self.log.logger(self.sqlString, self.placeHolder)
return getAllRows(self, self.sqlString, self.placeHolder).await.orm(typ)
Expand All @@ -182,7 +182,7 @@ proc getRaw*[T](self: Rdb, typ: typedesc[T]):Future[seq[T]]{.async.} =
return newSeq[typ.type](0)

proc first*(self: Rdb):Future[Option[JsonNode]] {.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
let sql = self.selectFirstBuilder()
try:
self.log.logger(sql, self.placeHolder)
Expand All @@ -193,7 +193,7 @@ proc first*(self: Rdb):Future[Option[JsonNode]] {.async.} =
return none(JsonNode)

proc first*[T](self: Rdb, typ: typedesc[T]):Future[Option[T]] {.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
let sql = self.selectFirstBuilder()
try:
self.log.logger(sql, self.placeHolder)
Expand All @@ -204,7 +204,7 @@ proc first*[T](self: Rdb, typ: typedesc[T]):Future[Option[T]] {.async.} =
return none(typ.type)

proc firstPlain*(self: Rdb):Future[seq[string]]{.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
let sql = self.selectFirstBuilder()
try:
self.log.logger(sql, self.placeHolder)
Expand All @@ -215,7 +215,7 @@ proc firstPlain*(self: Rdb):Future[seq[string]]{.async.} =
return newSeq[string](0)

proc find*(self: Rdb, id: string, key="id"):Future[Option[JsonNode]]{.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
self.placeHolder.add(id)
let sql = self.selectFindBuilder(key)
try:
Expand All @@ -230,7 +230,7 @@ proc find*(self: Rdb, id: int, key="id"):Future[Option[JsonNode]]{.async.} =
return self.find($id, key).await

proc find*[T](self: Rdb, id: int, typ:typedesc[T], key="id"):Future[Option[T]]{.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
self.placeHolder.add($id)
let sql = self.selectFindBuilder(key)
try:
Expand All @@ -242,7 +242,7 @@ proc find*[T](self: Rdb, id: int, typ:typedesc[T], key="id"):Future[Option[T]]{.
return none(typ.type)

proc findPlain*(self:Rdb, id:int, key="id"):Future[seq[string]]{.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
self.placeHolder.add($id)
let sql = self.selectFindBuilder(key)
try:
Expand Down Expand Up @@ -281,38 +281,38 @@ proc insertId(self:Rdb, sqlString:string, placeHolder:seq[string], key:string):F
return rows[0][0].parseInt

proc insert*(self: Rdb, items: JsonNode){.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
let sql = self.insertValueBuilder(items)
self.log.logger(sql, self.placeHolder)
self.conn.exec(self.driver, sql, self.placeHolder).await

proc insert*(self: Rdb, rows: seq[JsonNode]){.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
let sql = self.insertValuesBuilder(rows)
self.log.logger(sql, self.placeHolder)
self.conn.exec(self.driver, sql, self.placeHolder).await

proc inserts*(self: Rdb, rows: seq[JsonNode]){.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
for row in rows:
let sqlString = self.insertValueBuilder(row)
self.log.logger(sqlString, self.placeHolder)
self.conn.exec(self.driver, sqlString, self.placeHolder).await
self.placeHolder = @[]

proc insertId*(self: Rdb, items: JsonNode, key="id"):Future[int] {.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
let sql = self.insertValueBuilder(items)
return self.insertId(sql, self.placeHolder, key).await

proc insertId*(self: Rdb, rows: seq[JsonNode], key="id"):Future[int] {.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
let sql = self.insertValuesBuilder(rows)
result = self.insertId(sql, self.placeHolder, key).await
self.placeHolder = @[]

proc insertsID*(self: Rdb, rows: seq[JsonNode], key="id"):Future[seq[int]]{.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
var response = newSeq[int](rows.len)
for i, row in rows:
let sqlString = self.insertValueBuilder(row)
Expand All @@ -323,7 +323,7 @@ proc insertsID*(self: Rdb, rows: seq[JsonNode], key="id"):Future[seq[int]]{.asyn
# ==================== UPDATE ====================

proc updateSql*(self: Rdb, items: JsonNode):string =
defer: self.cleanUp()
# defer: self.cleanUp()
var updatePlaceHolder: seq[string]
for item in items.pairs:
if item.val.kind == JInt:
Expand All @@ -344,7 +344,7 @@ proc updateSql*(self: Rdb, items: JsonNode):string =
echo result

proc update*(self: Rdb, items: JsonNode){.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
var updatePlaceHolder: seq[string]
for item in items.pairs:
if item.val.kind == JInt:
Expand All @@ -371,13 +371,13 @@ proc deleteSql*(self: Rdb):string =
echo result

proc delete*(self: Rdb){.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
let sql = self.deleteBuilder()
self.log.logger(sql, self.placeHolder)
self.conn.exec(self.driver, sql, self.placeHolder).await

proc delete*(self: Rdb, id: int, key="id"){.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
self.placeHolder.add($id)
let sql = self.deleteByIdBuilder(id, key)
self.log.logger(sql, self.placeHolder)
Expand All @@ -387,33 +387,33 @@ proc delete*(self: Rdb, id: int, key="id"){.async.} =

proc exec*(self: Rdb){.async.} =
## It is only used with raw()
defer: self.cleanUp()
# defer: self.cleanUp()
self.log.logger(self.sqlString, self.placeHolder)
self.conn.exec(self.driver, self.sqlString, self.placeHolder).await


# ==================== Transaction ====================

proc begin*(self:Rdb):Future[int] {.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
self.log.logger("BEGIN")
let connI = self.conn.transactionStart(self.driver).await
return connI

proc rollback*(self:Rdb, connI:int) {.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
self.log.logger("ROLLBACK")
self.conn.transactionEnd(self.driver, connI, "ROLLBACK").await

proc commit*(self:Rdb, connI:int) {.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
self.log.logger("COMMIT")
self.conn.transactionEnd(self.driver, connI, "COMMIT").await

# ==================== Aggregates ====================

proc count*(self:Rdb):Future[int]{.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
let sql = self.countBuilder()
self.log.logger(sql, self.placeHolder)
let response = self.getRow(sql, self.placeHolder).await
Expand All @@ -429,7 +429,7 @@ proc count*(self:Rdb):Future[int]{.async.} =
return 0

proc max*(self:Rdb, column:string):Future[Option[string]]{.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
let sql = self.maxBuilder(column)
self.log.logger(sql, self.placeHolder)
let response = self.getRow(sql, self.placeHolder).await
Expand All @@ -445,7 +445,7 @@ proc max*(self:Rdb, column:string):Future[Option[string]]{.async.} =
return none(string)

proc min*(self:Rdb, column:string):Future[Option[string]]{.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
let sql = self.minBuilder(column)
self.log.logger(sql, self.placeHolder)
let response = await self.getRow(sql, self.placeHolder)
Expand All @@ -461,7 +461,7 @@ proc min*(self:Rdb, column:string):Future[Option[string]]{.async.} =
return none(string)

proc avg*(self:Rdb, column:string):Future[Option[float]]{.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
let sql = self.avgBuilder(column)
self.log.logger(sql, self.placeHolder)
let response = await self.getRow(sql, self.placeHolder)
Expand All @@ -475,7 +475,7 @@ proc avg*(self:Rdb, column:string):Future[Option[float]]{.async.} =
return none(float)

proc sum*(self:Rdb, column:string):Future[Option[float]]{.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
let sql = self.sumBuilder(column)
self.log.logger(sql, self.placeHolder)
let response = await self.getRow(sql, self.placeHolder)
Expand All @@ -494,7 +494,7 @@ proc sum*(self:Rdb, column:string):Future[Option[float]]{.async.} =
from grammars import table, where, limit, offset, orderBy, Order

proc paginate*(self:Rdb, display:int, page:int=1):Future[JsonNode]{.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
let tableName = self.query["table"].getStr()
if not page > 0: raise newException(Exception, "Arg page should be larger than 0")
let total = self.table(tableName).count().await
Expand Down Expand Up @@ -547,7 +547,7 @@ proc getLastItem(self:Rdb, keyArg:string, order:Order=Asc):Future[int]{.async.}


proc fastPaginate*(self:Rdb, display:int, key="id", order:Order=Asc):Future[JsonNode]{.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
var sql = self.selectBuilder()
if order == Asc:
sql = &"{sql} ORDER BY {key} ASC LIMIT {display + 1}"
Expand Down Expand Up @@ -581,7 +581,7 @@ proc fastPaginate*(self:Rdb, display:int, key="id", order:Order=Asc):Future[Json


proc fastPaginateNext*(self:Rdb, display, id:int, key="id", order:Order=Asc):Future[JsonNode]{.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
if not id > 0: raise newException(Exception, "Arg id should be larger than 0")
var sql = @[self.selectBuilder()][0]
let firstItem = await getFirstItem(self, key, order)
Expand Down Expand Up @@ -645,7 +645,7 @@ SELECT * FROM (


proc fastPaginateBack*(self:Rdb, display, id:int, key="id", order:Order=Asc):Future[JsonNode]{.async.} =
defer: self.cleanUp()
# defer: self.cleanUp()
if not id > 0: raise newException(Exception, "Arg id should be larger than 0")
var sql = @[self.selectBuilder()][0]
let lastItem = await self.getLastItem(key, order)
Expand Down
8 changes: 5 additions & 3 deletions src/allographer/query_builder/generators.nim
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,13 @@ proc insertValuesSql*(self: Rdb, rows: openArray[JsonNode]): Rdb =
# ==================== UPDATE ====================

proc updateSql*(self: Rdb): Rdb =
self.sqlString.add("UPDATE")
var sqlString = ""
sqlString.add("UPDATE")

var table = self.query["table"].getStr()
wrapUpper(table, self.driver)
self.sqlString.add(&" {table} SET ")
sqlString.add(&" {table} SET ")
self.sqlString = sqlString
return self


Expand All @@ -404,7 +406,7 @@ proc updateValuesSql*(self: Rdb, items:JsonNode): Rdb =
# ==================== DELETE ====================

proc deleteSql*(self: Rdb): Rdb =
self.sqlString.add("DELETE")
self.sqlString = "DELETE"
return self


Expand Down
9 changes: 4 additions & 5 deletions src/allographer/query_builder/grammars.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ TOP(LIMIT)
]#

proc table*(self:Rdb, tableArg: string): Rdb =
if self.query.kind == JNull:
self.query = newJObject()
self.query = newJObject()

self.query["table"] = %tableArg
self.sqlString = ""
self.placeHolder = @[]
return self


# ============================== Raw query ==============================

proc raw*(self:Rdb, sql:string, arges:varargs[string]): Rdb =
self.query = newJObject()
self.sqlString = sql
self.placeHolder = @arges
return self
Expand All @@ -34,9 +36,6 @@ proc raw*(self:Rdb, sql:string, arges:varargs[string]): Rdb =
# ============================== SELECT ==============================

proc select*(self: Rdb, columnsArg: varargs[string]): Rdb =
if self.query.kind == JNull:
self.query = newJObject()

if columnsArg.len == 0:
self.query["select"] = %["*"]
else:
Expand Down
4 changes: 2 additions & 2 deletions src/allographer/schema_builder.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import
./schema_builder/schema,
./schema_builder/grammers
./schema_builder/grammars

export
schema,
grammers
grammars
File renamed without changes.
2 changes: 1 addition & 1 deletion src/allographer/schema_builder/queries/mysql/impl.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json, strformat
import ../../grammers
import ../../grammars
import ../generator_utils

# =============================================================================
Expand Down
Loading

0 comments on commit 78611d5

Please sign in to comment.