Skip to content

Commit d446fdf

Browse files
committed
Update pushnull patch
1 parent 1c4d35a commit d446fdf

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

mysql/driver.c

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@
4242
#define TIMEOUT_INFINITY 365 * 86400 * 100.0
4343
static const char mysql_driver_label[] = "__tnt_mysql_driver";
4444

45+
extern int luaL_nil_ref;
46+
47+
/**
48+
* Push ffi's NULL (cdata<void *>: NULL) onto the stack.
49+
* Can be used as replacement of nil in Lua tables.
50+
* @param L stack
51+
*/
52+
static inline void
53+
luaL_pushnull(struct lua_State *L)
54+
{
55+
lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_nil_ref);
56+
}
57+
4558
static int
4659
save_pushstring_wrapped(struct lua_State *L)
4760
{
@@ -119,17 +132,17 @@ lua_mysql_push_value(struct lua_State *L, MYSQL_FIELD *field,
119132
}
120133

121134
case MYSQL_TYPE_NULL:
122-
lua_pushnil(L);
135+
luaL_pushnull(L);
123136
break;
124137

125138
case MYSQL_TYPE_LONGLONG: {
126-
long long v = atoll(data);
127-
if (field->flags & UNSIGNED_FLAG) {
128-
luaL_pushuint64(L, v);
129-
} else {
130-
luaL_pushint64(L, v);
131-
}
132-
break;
139+
long long v = atoll(data);
140+
if (field->flags & UNSIGNED_FLAG) {
141+
luaL_pushuint64(L, v);
142+
} else {
143+
luaL_pushint64(L, v);
144+
}
145+
break;
133146
}
134147

135148
/* AS string */
@@ -168,11 +181,13 @@ lua_mysql_fetch_result(struct lua_State *L)
168181
unsigned long *len = mysql_fetch_lengths(result);
169182
unsigned col_no;
170183
for (col_no = 0; col_no < mysql_num_fields(result); ++col_no) {
171-
if (!row[col_no])
172-
continue;
173184
lua_pushstring(L, fields[col_no].name);
174-
lua_mysql_push_value(L, fields + col_no,
175-
row[col_no], len[col_no]);
185+
if (!row[col_no]) {
186+
luaL_pushnull(L);
187+
} else {
188+
lua_mysql_push_value(L, fields + col_no,
189+
row[col_no], len[col_no]);
190+
}
176191
lua_settable(L, -3);
177192
}
178193
lua_settable(L, -3);
@@ -261,12 +276,14 @@ lua_mysql_stmt_push_row(struct lua_State *L)
261276
lua_newtable(L);
262277
unsigned col_no;
263278
for (col_no = 0; col_no < col_count; ++col_no) {
264-
if (*results[col_no].is_null)
265-
continue;
266279
lua_pushstring(L, fields[col_no].name);
267-
lua_mysql_push_value(L, fields + col_no,
268-
results[col_no].buffer,
269-
*results[col_no].length);
280+
if (*results[col_no].is_null) {
281+
luaL_pushnull(L);
282+
} else {
283+
lua_mysql_push_value(L, fields + col_no,
284+
results[col_no].buffer,
285+
*results[col_no].length);
286+
}
270287
lua_settable(L, -3);
271288
}
272289
return 1;

0 commit comments

Comments
 (0)