MCPcopy Create free account
hub / github.com/bloomberg/comdb2 / db_exec

Function db_exec

lua/sp.c:3728–3782  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3726}
3727
3728static int db_exec(Lua lua)
3729{
3730 luaL_checkudata(lua, 1, dbtypes.db);
3731 lua_remove(lua, 1);
3732
3733 SP sp = getsp(lua);
3734
3735 const char *sql = lua_tostring(lua, -1);
3736 if (sql == NULL) {
3737 luabb_error(lua, sp, "expected sql string");
3738 return 2;
3739 }
3740
3741 while (isspace(*sql))
3742 ++sql;
3743
3744 sqlite3_stmt *stmt = NULL;
3745 int rc = lua_prepare_sql(sp, sql, &stmt);
3746 if (rc != 0) {
3747 lua_pushnil(lua);
3748 lua_pushinteger(lua, rc);
3749 return 2;
3750 }
3751 dbstmt_t *dbstmt = new_dbstmt(lua, sp, stmt);
3752 if (dbstmt->readonly) {
3753 // dbstmt:fetch() will run it
3754 lua_pushinteger(lua, 0);
3755 return 2;
3756 }
3757
3758 // a write stmt - run it now
3759 setup_first_sqlite_step(sp, dbstmt, 0);
3760 sqlite3 *sqldb = getdb(sp);
3761 lua_begin_step(sp->clnt, sp, stmt);
3762 while ((rc = sqlite3_maybe_step(sp->clnt, stmt)) == SQLITE_ROW) {
3763 lua_another_step(sp->clnt, stmt, rc);
3764 }
3765 lua_end_step(sp->clnt, sp, stmt);
3766 if (rc == SQLITE_DONE) {
3767 dbstmt->rows_changed = sqlite3_changes(sqldb);
3768 sp->rc = 0;
3769 } else {
3770 const char *err;
3771 sp->rc = lua_check_errors(sp->clnt, sqldb, stmt, &err);
3772 sp->error = strdup(err);
3773 }
3774
3775 if (sp->rc) {
3776 lua_pushnil(lua);
3777 lua_pushinteger(lua, sp->rc);
3778 } else {
3779 lua_pushinteger(lua, 0); /* Success return code */
3780 }
3781 return 2;
3782}
3783
3784static int db_prepare(Lua L)
3785{

Callers 1

dbtable_emit_intFunction · 0.85

Calls 15

luaL_checkudataFunction · 0.85
lua_removeFunction · 0.85
luabb_errorFunction · 0.85
lua_prepare_sqlFunction · 0.85
lua_pushnilFunction · 0.85
lua_pushintegerFunction · 0.85
new_dbstmtFunction · 0.85
setup_first_sqlite_stepFunction · 0.85
lua_begin_stepFunction · 0.85
sqlite3_maybe_stepFunction · 0.85
lua_another_stepFunction · 0.85
lua_end_stepFunction · 0.85

Tested by

no test coverage detected