Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db_postgres.getRow proc issues warning when SQL returns 0 rows #11821

Closed
chrisheller opened this issue Jul 24, 2019 · 0 comments · Fixed by #11859
Closed

db_postgres.getRow proc issues warning when SQL returns 0 rows #11821

chrisheller opened this issue Jul 24, 2019 · 0 comments · Fixed by #11859

Comments

@chrisheller
Copy link
Contributor

When calling getRow and no results are returned, postgres will issue a warning message to stderr : "row number 0 is out of range 0..-1". Other than the warning, there doesn't appear to be any problems caused by this, so its a minor issue.

let row = db.getRow(sql"select * from some_table where 1 = 2")
# "row number 0 is out of range 0..-1" printed to stderr

This is happening because setRow is called without checking if there are any rows returned. The following changes resolve the issue by calling pqntuples to check how many rows were returned.

--- /tmp/original_db_postgres.nim	2019-07-24 16:35:38.000000000 -0700
+++ /nim-0.20.0/lib/impure/db_postgres.nim	2019-07-24 16:36:44.000000000 -0700
@@ -392,7 +392,8 @@
   var res = setupQuery(db, query, args)
   var L = pqnfields(res)
   result = newRow(L)
-  setRow(res, result, 0, L)
+  if pqntuples(res) > 0:
+    setRow(res, result, 0, L)
   pqclear(res)
 
 proc getRow*(db: DbConn, stmtName: SqlPrepared,
@@ -400,7 +401,8 @@
   var res = setupQuery(db, stmtName, args)
   var L = pqNfields(res)
   result = newRow(L)
-  setRow(res, result, 0, L)
+  if pqntuples(res) > 0:
+    setRow(res, result, 0, L)
   pqClear(res)
 
 proc getAllRows*(db: DbConn, query: SqlQuery,

If no one sees any issues with that, I will make a pull request for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants