nim-lang / db_connector

Unified db connector in Nim
MIT License
18 stars 5 forks source link

db_sqlite: Empty string in query causes unclear error #2

Open Toromino opened 2 years ago

Toromino commented 2 years ago

Function exec in db_sqlite.nim throws an unclear error when passing an empty query.

Example

let db = open("nimskey.db", "", "", "")
db.exec(sql(""))

Current Output

/usr/lib/nim/lib/impure/db_sqlite.nim(271) exec
/usr/lib/nim/lib/impure/db_sqlite.nim(198) dbError
Error: unhandled exception: not an error [DbError]

Possible Solution

Let the empty string pass, or throw a more clear exception.

Additional Information

$ nim -v
Nim Compiler Version 1.6.0
Zectbumo commented 2 years ago

This is a bit of the fault of the sqlite3 lib since passing an empty string to prepare_v2() statement causes a misuse error (21) but doesn't set the error message which is SQLITE_OK message "not an error".

sqlite.h:

(#define SQLITE_MISUSE      21   /* Library used incorrectly */)

main.c:

static const char* const aMsg[] = {
    /* SQLITE_OK          */ "not an error",
...
    /* SQLITE_MISUSE      */ "bad parameter or other API misuse",

The wrapped sqlite3_exec() lib function which is a convenience function that does: prepare(), step() and finalize() doesn't return this error and returns an SQLITE_OK even when passed a string that only contains whitespace. The impure nim library exec() doesn't call the wrapped sqlite3.exec() but instead makes it's own prepare(), step() and finalize() calls which means it does not inherit the same behavior as the wrapped sqlite3_exec().

Proposed solutions: 1) make db_sqlite.exec() act like sqlite3.exec() 2) return the proper error message for SQLITE_MISUSE: "bad parameter or other API misuse" 3) actually use the sqlite3.exec() to inherit the behavior.

I'm leaning towards option 3