/* ** Usage: sqlite_reset VM ** ** Reset a virtual machine and prepare it to be run again. */ static int test_reset( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */ ){ sqlite_vm *vm; int rc; char *zErrMsg = 0; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " VM\"", 0); return TCL_ERROR; } if( getVmPointer(interp, argv[1], &vm) ) return TCL_ERROR; rc = sqlite_reset(vm, &zErrMsg); if( rc ){ char zBuf[50]; sprintf(zBuf, "(%d) ", rc); Tcl_AppendResult(interp, zBuf, zErrMsg, 0); sqlite_freemem(zErrMsg); return TCL_ERROR; } return TCL_OK; }
/* ** Execute an SQL statement. ** Return a Cursor object if the statement is a query, otherwise ** return the number of tuples affected by the statement. */ static int conn_execute(lua_State *L) { conn_data *conn = getconnection(L); const char *statement = luaL_checkstring(L, 2); int res; sqlite_vm *vm; char *errmsg; int numcols; const char **col_info; res = sqlite_compile(conn->sql_conn, statement, NULL, &vm, &errmsg); if (res != SQLITE_OK) { lua_pushnil(L); lua_pushliteral(L, LUASQL_PREFIX); lua_pushstring(L, errmsg); sqlite_freemem(errmsg); lua_concat(L, 2); return 2; } /* process first result to retrive query information and type */ res = sqlite_step(vm, &numcols, NULL, &col_info); /* real query? if empty, must have numcols!=0 */ if ((res == SQLITE_ROW) || ((res == SQLITE_DONE) && numcols)) { sqlite_reset(vm, NULL); return create_cursor(L, 1, conn, vm, numcols, col_info); } if (res == SQLITE_DONE) /* and numcols==0, INSERT,UPDATE,DELETE statement */ { sqlite_finalize(vm, NULL); /* return number of columns changed */ lua_pushnumber(L, sqlite_changes(conn->sql_conn)); return 1; } /* error */ sqlite_finalize(vm, &errmsg); lua_pushnil(L); lua_pushliteral(L, LUASQL_PREFIX); lua_pushstring(L, errmsg); sqlite_freemem(errmsg); lua_concat(L, 2); return 2; }