Example #1
0
static int
db_exec(lua_State *T)
{
	struct db *db;
	const char *sql;
	size_t len;
	unsigned int bind;

	luaL_checktype(T, 1, LUA_TUSERDATA);
	sql = luaL_checklstring(T, 2, &len);
	bind = lua_gettop(T) > 2;
	if (bind) {
		luaL_checktype(T, 3, LUA_TTABLE);
		lua_settop(T, 3);
		lua_replace(T, 2);
	}

	db = db_unbox(T, 1);
	if (db == NULL)
		return db_closed(T);
	if (db->T != NULL)
		return db_busy(T);

	db->T = T;
	db->req.prep.sql = sql;
	db->req.prep.len = len+1;
	lem_async_do(&db->a, db_prepare_work, db_exec_prepare_reap);

	return lua_yield(T, bind ? 2 : 1);
}
Example #2
0
static int
db_prepare(lua_State *T)
{
	struct db *db;
	const char *sql;
	size_t len;

	luaL_checktype(T, 1, LUA_TUSERDATA);
	sql = luaL_checklstring(T, 2, &len);

	db = db_unbox(T, 1);
	if (db == NULL)
		return db_closed(T);
	if (db->T != NULL)
		return db_busy(T);

	db->T = T;
	db->refs++;
	db->req.prep.sql = sql;
	db->req.prep.len = len+1;
	lem_async_do(&db->a, db_prepare_work, db_prepare_reap);

	lua_settop(T, 2);
	lua_pushvalue(T, lua_upvalueindex(1));
	return lua_yield(T, 3);
}
Example #3
0
static int
db_autocommit(lua_State *T)
{
	struct db *db;

	luaL_checktype(T, 1, LUA_TUSERDATA);
	db = db_unbox(T, 1);
	if (db == NULL)
		return db_closed(T);
	if (db->T != NULL)
		return db_busy(T);

	lua_pushboolean(T, sqlite3_get_autocommit(db->handle));
	return 1;
}
Example #4
0
static int
db_changes(lua_State *T)
{
	struct db *db;

	luaL_checktype(T, 1, LUA_TUSERDATA);
	db = db_unbox(T, 1);
	if (db == NULL)
		return db_closed(T);
	if (db->T != NULL)
		return db_busy(T);

	lua_pushinteger(T, sqlite3_changes(db->handle));
	return 1;
}
Example #5
0
static int
db_last_insert_rowid(lua_State *T)
{
	struct db *db;

	luaL_checktype(T, 1, LUA_TUSERDATA);
	db = db_unbox(T, 1);
	if (db == NULL)
		return db_closed(T);
	if (db->T != NULL)
		return db_busy(T);

	lua_pushnumber(T, sqlite3_last_insert_rowid(db->handle));
	return 1;
}
Example #6
0
static int
db_close(lua_State *T)
{
	struct box *box;
	struct db *db;

	luaL_checktype(T, 1, LUA_TUSERDATA);
	box = lua_touserdata(T, 1);
	db = box->db;
	if (db == NULL)
		return db_closed(T);
	if (db->T != NULL)
		return db_busy(T);

	db_unref(db);
	box->db = NULL;

	lua_pushboolean(T, 1);
	return 1;
}