Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/mwild1/luadbi
Browse files Browse the repository at this point in the history
  • Loading branch information
sparked435 committed Mar 4, 2024
2 parents 3211c61 + cf8ab89 commit eee6ef2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ placeholders and bind parameters for all database operations.
Currently LuaDBI supports DB2, Oracle, MySQL, PostgreSQL and SQLite
databases with native database drivers.

This version supports Lua 5.1, 5.2, and 5.3.
This version supports Lua 5.1 through 5.4.

[![Build Status](https://travis-ci.org/mwild1/luadbi.svg?branch=master)](https://travis-ci.org/mwild1/luadbi)

Expand Down
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 eee6ef2

Please sign in to comment.