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

Number parameters turn to strings #2

Closed
kedorlaomer opened this issue May 3, 2021 · 3 comments
Closed

Number parameters turn to strings #2

kedorlaomer opened this issue May 3, 2021 · 3 comments

Comments

@kedorlaomer
Copy link

Code like

db:update("INSERT INTO some_table VALUES(?)", {42})

inserts the string "42" instead of the number 42 (at least with Lua 5.3). The reason is the following snippet of bind_one_param:

static int bind_one_param(lua_State *L, sqlite3_stmt *stmt, int index)
{ 
  int status = SQLITE_OK;

  if (lua_isstring(L, -1)) /* <------ is true for all numbers */
  { 
    size_t len;
    const char *text = lua_tolstring(L, -1, &len);
    status = sqlite3_bind_text(stmt, index, text, len, SQLITE_TRANSIENT);
#if LUA_VERSION_NUM >= 503
  }
  else if (lua_isinteger(L, -1)) /* <------- not reachable  */
  { 
    status = sqlite3_bind_int64(stmt, index, lua_tointeger(L, -1));
#endif
  }
  else if (lua_isnumber(L, -1))
  { 
    status = sqlite3_bind_double(stmt, index, lua_tonumber(L, -1));
  }
  else if (lua_isnil(L, -1))

The reference manual for Lua 5.3 states:

int lua_isstring (lua_State *L, int index);

Returns 1 if the value at the given index is a string or a number (which is always convertible to a string), and 0 otherwise.

The fix: Ask for lua_isinteger and lua_isnumber first. In my timezone, it's evening and I feel admittedly too lazy to go the official way of forking, branching and submitting an official pull request for such a small thing.

Also: I love your bindings (I have looked previously and was reluctant to use sqlite because the others are ugly).

@akojo
Copy link
Owner

akojo commented Dec 15, 2021

Nice catch! That should be handled correctly by SQLite type affinity rules, but is a bug in the code nevertheless.

@akojo akojo closed this as completed in cb2eb2e Dec 15, 2021
@kedorlaomer
Copy link
Author

This depends. If the only way the database has been accessed is via clutch, then type affinity doesn't catch in. This is how I found this bug.

@akojo
Copy link
Owner

akojo commented Dec 16, 2021

Yep, this is now fixed in latest version.

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

No branches or pull requests

2 participants