Skip to content

Commit

Permalink
Merge pull request #75 from mwild1/issue64_mysql_integers
Browse files Browse the repository at this point in the history
Attempt to provide partial fix for Issue #64.
  • Loading branch information
sparked435 authored Mar 1, 2024
2 parents 8c68d2c + 8aa475c commit cf8ab89
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
22 changes: 18 additions & 4 deletions dbd/mysql/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ static int statement_execute(lua_State *L) {
const char *str = NULL;
size_t *str_len = NULL;
double *num = NULL;
long *inum = NULL;
int *boolean = NULL;
char err[64];

Expand All @@ -226,22 +227,35 @@ static int statement_execute(lua_State *L) {
*boolean = lua_toboolean(L, p);

bind[i].buffer_type = MYSQL_TYPE_LONG;
bind[i].is_null = (int*)0;
bind[i].is_null = (my_bool*)0;
bind[i].buffer = (char *)boolean;
bind[i].length = 0;
break;

case LUA_TNUMBER:
#if LUA_VERSION_NUM > 502
if (lua_isinteger(L, p)) {
inum = (long *)(buffer + offset);
offset += sizeof(long);
*inum = lua_tointeger(L, p);

bind[i].buffer_type = MYSQL_TYPE_LONG;
bind[i].is_null = (my_bool*)0;
bind[i].buffer = (char *)inum;
bind[i].length = 0;
break;
}
#endif
/*
* num needs to be it's own
* memory here
*/
num = (double *)(buffer + offset);
offset += sizeof(double);
*num = lua_tonumber(L, p);

bind[i].buffer_type = MYSQL_TYPE_DOUBLE;
bind[i].is_null = (int*)0;
bind[i].is_null = (my_bool*)0;
bind[i].buffer = (char *)num;
bind[i].length = 0;
break;
Expand All @@ -252,7 +266,7 @@ static int statement_execute(lua_State *L) {
str = lua_tolstring(L, p, str_len);

bind[i].buffer_type = MYSQL_TYPE_STRING;
bind[i].is_null = (int*)0;
bind[i].is_null = (my_bool*)0;
bind[i].buffer = (char *)str;
bind[i].length = str_len;
break;
Expand Down
42 changes: 42 additions & 0 deletions tests/run_tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local sql_code = {
['select'] = "select * from select_tests where name = %s;",
['select_multi'] = "select * from select_tests where flag = %s;",
['select_count'] = "select count(*) as total from insert_tests;",
['select_limit'] = "select * from select_tests limit %s;",
['insert'] = "insert into insert_tests ( val ) values ( %s );",
['insert_returning'] = "insert into insert_tests ( val ) values ( %s ) returning id;",
['insert_select'] = "select * from insert_tests where id = %s;",
Expand Down Expand Up @@ -157,6 +158,46 @@ local function test_select()
end


--
-- Added to expose the MySQL limit bug, see Github issue #64
--
local function test_select_limit()

local sth, err = dbh:prepare(code('select_limit'))
local count = 0
local success

assert.is_nil(err)
assert.is_not_nil(sth)
success, err = sth:execute(1)

assert.is_true(success)
assert.is_nil(err)

for row in sth:rows(true) do
count = count + 1

if config.have_booleans then
assert.is_true(row['flag'])
else
assert.equals(1, row['flag'])
end

assert.equals('Row 1', row['name'])
assert.is_number(row['maths'])
end

assert.equals(1, count)

if config.have_rowcount then
assert.equals(sth:rowcount(), count)
end

sth:close()

end


local function test_select_multi()

local sth, err = dbh:prepare(code('select_multi'))
Expand Down Expand Up @@ -618,6 +659,7 @@ describe("MySQL #mysql", function()
it( "Tests syntax error", syntax_error )
it( "Tests value encoding", test_encoding )
it( "Tests simple selects", test_select )
it( "Tests selects with limit", test_select_limit )
it( "Tests multi-row selects", test_select_multi )
it( "Tests inserts", test_insert )
it( "Tests inserts of NULL", test_insert_null )
Expand Down

0 comments on commit cf8ab89

Please sign in to comment.