** This returns dbrow or nil on end of result set. There is no way to signal ** error. I'll throw runtime error -- seems appropriate, SQL encountered some ** runtime error (type conversion, row corruption, etc). SPs probably not ** written to handle this situation anyway. */
| 1329 | ** written to handle this situation anyway. |
| 1330 | */ |
| 1331 | static int lua_sql_step(Lua lua, sqlite3_stmt *stmt) |
| 1332 | { |
| 1333 | SP sp = getsp(lua); |
| 1334 | struct sqlclntstate *clnt = sp->clnt; |
| 1335 | int rc = sqlite3_maybe_step(clnt, stmt); |
| 1336 | |
| 1337 | if (rc == SQLITE_DONE) { |
| 1338 | lua_end_step(clnt, sp, stmt); |
| 1339 | return rc; |
| 1340 | } else if (rc != SQLITE_ROW) { |
| 1341 | return luaL_error(lua, sqlite3_errmsg(getdb(sp))); |
| 1342 | } else { |
| 1343 | lua_another_step(clnt, stmt, rc); |
| 1344 | } |
| 1345 | |
| 1346 | lua_newtable(lua); |
| 1347 | |
| 1348 | int ncols = column_count(clnt, stmt); |
| 1349 | for (int col = 0; col < ncols; col++) { |
| 1350 | int type = column_type(clnt, stmt, col); |
| 1351 | switch (type) { |
| 1352 | case SQLITE_NULL: { |
| 1353 | int sqltype = |
| 1354 | sqlite_str_to_type(sqlite3_column_decltype(stmt, col)); |
| 1355 | luabb_pushnull(lua, sqlite_type_to_dbtype(sqltype)); |
| 1356 | break; |
| 1357 | } |
| 1358 | case SQLITE_INTEGER: { |
| 1359 | long long ival = column_int64(clnt, stmt, col); |
| 1360 | luabb_pushinteger(lua, ival); |
| 1361 | break; |
| 1362 | } |
| 1363 | case SQLITE_FLOAT: { |
| 1364 | double dval = column_double(clnt, stmt, col); |
| 1365 | luabb_pushreal(lua, dval); |
| 1366 | break; |
| 1367 | } |
| 1368 | case SQLITE_TEXT: { |
| 1369 | char *tval = (char *)column_text(clnt, stmt, col); |
| 1370 | luabb_pushcstring(lua, tval); |
| 1371 | break; |
| 1372 | } |
| 1373 | case SQLITE_DATETIME: { |
| 1374 | cdb2_client_datetime_t cdt; |
| 1375 | datetime_t datetime; |
| 1376 | const dttz_t *dt = column_datetime(clnt, stmt, col); |
| 1377 | dttz_to_client_datetime(dt, clnt_tzname(clnt, stmt), &cdt); |
| 1378 | client_datetime_to_datetime_t(&cdt, &datetime, 0); |
| 1379 | luabb_pushdatetime(lua, &datetime); |
| 1380 | break; |
| 1381 | } |
| 1382 | case SQLITE_DATETIMEUS: { |
| 1383 | cdb2_client_datetimeus_t cdt; |
| 1384 | datetime_t datetime; |
| 1385 | const dttz_t *dt = column_datetime(clnt, stmt, col); |
| 1386 | dttz_to_client_datetimeus(dt, clnt_tzname(clnt, stmt), &cdt); |
| 1387 | client_datetimeus_to_datetime_t(&cdt, &datetime, 0); |
| 1388 | luabb_pushdatetime(lua, &datetime); |
no test coverage detected