diff --git a/allographer.nimble b/allographer.nimble index c90754c4..15c590a5 100644 --- a/allographer.nimble +++ b/allographer.nimble @@ -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" diff --git a/example/run.nim b/example/run.nim index f9c15edc..9b86c4b8 100644 --- a/example/run.nim +++ b/example/run.nim @@ -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) @@ -42,7 +41,6 @@ for i in 1..total: "name": &"user{i}", "email": &"user{i}@gmail.com", "password": password, - "salt": salt, "auth_id": authId } ) @@ -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") @@ -123,7 +121,6 @@ type User = ref object name:string email:string password:string - salt:string address:string birth_date:string auth_id:int diff --git a/src/allographer/query_builder/exec.nim b/src/allographer/query_builder/exec.nim index 3a308f8d..b859b0c0 100644 --- a/src/allographer/query_builder/exec.nim +++ b/src/allographer/query_builder/exec.nim @@ -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) diff --git a/tests/test_transaction.nim b/tests/test_transaction.nim index a6a0e011..3155db8d 100644 --- a/tests/test_transaction.nim +++ b/tests/test_transaction.nim @@ -102,4 +102,4 @@ suite "transaction": %*{"name": "George", "birth_date": "1943-02-25", "address": "London"}, ] ) - echo RDB().table("users").where("id", ">", 10).get() \ No newline at end of file + echo RDB().table("users").where("id", ">", 10).get()