Skip to content

Commit

Permalink
Merge pull request #95 from itsumura-h/feature/rm_double_access
Browse files Browse the repository at this point in the history
fix double access in SELECT query
  • Loading branch information
itsumura-h authored Aug 2, 2020
2 parents 18604c6 + 594e6ba commit af0a5ac
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 44 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.12.3" # https://github.com/itsumura-h/nim-allographer/pull/85
version = "0.12.4" # https://github.com/itsumura-h/nim-allographer/pull/85
author = "Hidenobu Itsumura @dumblepytech1 as 'medy'"
description = "A Nim query builder library inspired by Laravel/PHP and Orator/Python"
license = "MIT"
Expand Down
5 changes: 1 addition & 4 deletions example/run.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ schema([
Column().string("name").nullable(),
Column().string("email").nullable(),
Column().string("password").nullable(),
Column().string("salt").nullable(),
Column().string("address").nullable(),
Column().date("birth_date").nullable(),
Column().foreign("auth_id").reference("id").on("auth").onDelete(SET_NULL)
Expand Down Expand Up @@ -42,7 +41,6 @@ for i in 1..total:
"name": &"user{i}",
"email": &"user{i}@gmail.com",
"password": password,
"salt": salt,
"auth_id": authId
}
)
Expand All @@ -51,7 +49,7 @@ pb.finish()

RDB().table("users").insert(insertData)

discard RDB().table("users").get()
echo RDB().table("users").get()

echo RDB()
.table("users")
Expand Down Expand Up @@ -123,7 +121,6 @@ type User = ref object
name:string
email:string
password:string
salt:string
address:string
birth_date:string
auth_id:int
Expand Down
81 changes: 43 additions & 38 deletions src/allographer/query_builder/exec.nim
Original file line number Diff line number Diff line change
Expand Up @@ -136,70 +136,75 @@ proc toJson(results:openArray[string], columns:openArray[JsonNode]):JsonNode =
proc getAllRows(sqlString:string, args:varargs[string]): seq[JsonNode] =
let db = db()
defer: db.close()
let results = db.getAllRows(sql sqlString, args) # seq[seq[string]]
if results.len == 0:
echoErrorMsg(sqlString & $args)
return newSeq[JsonNode](0)

var db_columns: DbColumns
for _ in db.instantRows(db_columns, sql sqlString, args):
discard
var rows = newSeq[seq[string]]()
for row in db.instantRows(db_columns, sql sqlString, args):
var columns = newSeq[string](row.len)
for i in 0..row.len()-1:
columns[i] = row[i]
rows.add(columns)

if rows.len == 0:
echoErrorMsg(sqlString & $args)
return newSeq[JsonNode](0)

let columns = getColumns(db_columns)
return toJson(results, columns) # seq[JsonNode]
return toJson(rows, columns) # seq[JsonNode]

proc getAllRows(db:DbConn, sqlString:string, args:varargs[string]): seq[JsonNode] =
## used in transaction
let results = db.getAllRows(sql sqlString, args) # seq[seq[string]]
if results.len == 0:
var db_columns: DbColumns
var rows = newSeq[seq[string]]()
for row in db.instantRows(db_columns, sql sqlString, args):
var columns = newSeq[string](row.len)
for i in 0..row.len()-1:
columns[i] = row[i]
rows.add(columns)

if rows.len == 0:
echoErrorMsg(sqlString & $args)
return newSeq[JsonNode](0)

var db_columns: DbColumns
for _ in db.instantRows(db_columns, sql sqlString, args):
discard

let columns = getColumns(db_columns)
return toJson(results, columns) # seq[JsonNode]
return toJson(rows, columns) # seq[JsonNode]


proc getRow(sqlString:string, args:varargs[string]): JsonNode =
let db = db()
defer: db.close()
# TODO fix when Nim is upgraded https://github.com/nim-lang/Nim/pull/12806
# let results = db.getRow(sql sqlString, args)
let r = db.getAllRows(sql sqlString, args)
var results: seq[string]
if r.len > 0:
results = r[0]
else:
echoErrorMsg(sqlString & $args)
return newJNull()

var db_columns: DbColumns
for _ in db.instantRows(db_columns, sql sqlString, args):
discard
var rows = newSeq[seq[string]]()
for row in db.instantRows(db_columns, sql sqlString, args):
var columns = newSeq[string](row.len)
for i in 0..row.len()-1:
columns[i] = row[i]
rows.add(columns)

if rows.len == 0:
echoErrorMsg(sqlString & $args)
return newJNull()

let columns = getColumns(db_columns)
return toJson(results, columns)
return toJson(rows, columns)[0]

proc getRow(db:DbConn, sqlString:string, args:varargs[string]): JsonNode =
# TODO fix when Nim is upgraded https://github.com/nim-lang/Nim/pull/12806
# let results = db.getRow(sql sqlString, args)
let r = db.getAllRows(sql sqlString, args)
var results: seq[string]
if r.len > 0:
results = r[0]
else:
## used in transaction
var db_columns: DbColumns
var rows = newSeq[seq[string]]()
for row in db.instantRows(db_columns, sql sqlString, args):
var columns = newSeq[string](row.len)
for i in 0..row.len()-1:
columns[i] = row[i]
rows.add(columns)

if rows.len == 0:
echoErrorMsg(sqlString & $args)
return newJNull()

var db_columns: DbColumns
for _ in db.instantRows(db_columns, sql sqlString, args):
discard

let columns = getColumns(db_columns)
return toJson(results, columns)
return toJson(rows, columns)[0]

proc orm(rows:openArray[JsonNode], typ:typedesc):seq[typ.type] =
var response = newSeq[typ.type](rows.len)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_transaction.nim
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ suite "transaction":
%*{"name": "George", "birth_date": "1943-02-25", "address": "London"},
]
)
echo RDB().table("users").where("id", ">", 10).get()
echo RDB().table("users").where("id", ">", 10).get()

0 comments on commit af0a5ac

Please sign in to comment.