|
42 | 42 | #define TIMEOUT_INFINITY 365 * 86400 * 100.0 |
43 | 43 | static const char mysql_driver_label[] = "__tnt_mysql_driver"; |
44 | 44 |
|
| 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 | + |
45 | 58 | static int |
46 | 59 | save_pushstring_wrapped(struct lua_State *L) |
47 | 60 | { |
@@ -119,17 +132,17 @@ lua_mysql_push_value(struct lua_State *L, MYSQL_FIELD *field, |
119 | 132 | } |
120 | 133 |
|
121 | 134 | case MYSQL_TYPE_NULL: |
122 | | - lua_pushnil(L); |
| 135 | + luaL_pushnull(L); |
123 | 136 | break; |
124 | 137 |
|
125 | 138 | 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; |
133 | 146 | } |
134 | 147 |
|
135 | 148 | /* AS string */ |
@@ -168,11 +181,13 @@ lua_mysql_fetch_result(struct lua_State *L) |
168 | 181 | unsigned long *len = mysql_fetch_lengths(result); |
169 | 182 | unsigned col_no; |
170 | 183 | for (col_no = 0; col_no < mysql_num_fields(result); ++col_no) { |
171 | | - if (!row[col_no]) |
172 | | - continue; |
173 | 184 | 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 | + } |
176 | 191 | lua_settable(L, -3); |
177 | 192 | } |
178 | 193 | lua_settable(L, -3); |
@@ -261,12 +276,14 @@ lua_mysql_stmt_push_row(struct lua_State *L) |
261 | 276 | lua_newtable(L); |
262 | 277 | unsigned col_no; |
263 | 278 | for (col_no = 0; col_no < col_count; ++col_no) { |
264 | | - if (*results[col_no].is_null) |
265 | | - continue; |
266 | 279 | 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 | + } |
270 | 287 | lua_settable(L, -3); |
271 | 288 | } |
272 | 289 | return 1; |
|
0 commit comments