mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-06 11:18:30 +00:00
Add database and statement readonly checks to sqlite in redbean (#914)
This commit is contained in:
parent
d02cba2451
commit
b893479417
2 changed files with 69 additions and 15 deletions
|
@ -234,6 +234,12 @@ static int dbvm_isopen(lua_State *L) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
static int dbvm_readonly(lua_State *L) {
|
||||
sdb_vm *svm = lsqlite_checkvm(L, 1);
|
||||
lua_pushboolean(L, sqlite3_stmt_readonly(svm->vm));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int dbvm_tostring(lua_State *L) {
|
||||
char buff[40];
|
||||
sdb_vm *svm = lsqlite_getvm(L, 1);
|
||||
|
@ -908,6 +914,21 @@ static int pusherr(lua_State *L, int rc) {
|
|||
return 2;
|
||||
}
|
||||
|
||||
static int pusherrstr(lua_State *L, char *str) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, str);
|
||||
return 2;
|
||||
}
|
||||
|
||||
static int db_readonly(lua_State *L) {
|
||||
sdb *db = lsqlite_checkdb(L, 1);
|
||||
const char *zDb = luaL_optstring(L, 2, "main");
|
||||
int res = sqlite3_db_readonly(db->db, zDb);
|
||||
if (res == -1) return pusherrstr(L, "unknown (not attached) database name");
|
||||
lua_pushboolean(L, res);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int db_wal_checkpoint(lua_State *L) {
|
||||
sdb *db = lsqlite_checkdb(L, 1);
|
||||
int eMode = luaL_optinteger(L, 2, SQLITE_CHECKPOINT_PASSIVE);
|
||||
|
@ -1748,16 +1769,15 @@ static int db_gc(lua_State *L) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
#ifdef SQLITE_ENABLE_DESERIALIZE
|
||||
|
||||
static int db_serialize(lua_State *L) {
|
||||
sdb *db = lsqlite_getdb(L, 1);
|
||||
sdb *db = lsqlite_checkdb(L, 1);
|
||||
sqlite_int64 size = 0;
|
||||
|
||||
if (db->db == NULL) /* ignore closed databases */
|
||||
return 0;
|
||||
|
||||
char *buffer = (char *)sqlite3_serialize(db->db, "main", &size, 0);
|
||||
if (buffer == NULL) /* ignore failed database serialization */
|
||||
return 0;
|
||||
if (buffer == NULL)
|
||||
return pusherrstr(L, "failed to serialize");
|
||||
|
||||
lua_pushlstring(L, buffer, size);
|
||||
free(buffer);
|
||||
|
@ -1765,15 +1785,12 @@ static int db_serialize(lua_State *L) {
|
|||
}
|
||||
|
||||
static int db_deserialize(lua_State *L) {
|
||||
sdb *db = lsqlite_getdb(L, 1);
|
||||
sdb *db = lsqlite_checkdb(L, 1);
|
||||
size_t size = 0;
|
||||
|
||||
if (db->db == NULL) /* ignore closed databases */
|
||||
return 0;
|
||||
|
||||
const char *buffer = luaL_checklstring(L, 2, &size);
|
||||
if (buffer == NULL || size == 0) /* ignore empty database content */
|
||||
return 0;
|
||||
if (buffer == NULL || size == 0)
|
||||
return pusherrstr(L, "failed to deserialize");
|
||||
|
||||
const char *sqlbuf = memcpy(sqlite3_malloc(size), buffer, size);
|
||||
sqlite3_deserialize(db->db, "main", (void *)sqlbuf, size, size,
|
||||
|
@ -1781,6 +1798,8 @@ static int db_deserialize(lua_State *L) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef SQLITE_ENABLE_SESSION
|
||||
|
||||
/*
|
||||
|
@ -2602,6 +2621,7 @@ static const struct {
|
|||
|
||||
static const luaL_Reg dblib[] = {
|
||||
{"isopen", db_isopen },
|
||||
{"readonly", db_readonly },
|
||||
{"last_insert_rowid", db_last_insert_rowid },
|
||||
{"changes", db_changes },
|
||||
{"total_changes", db_total_changes },
|
||||
|
@ -2634,9 +2654,6 @@ static const luaL_Reg dblib[] = {
|
|||
{"close", db_close },
|
||||
{"close_vm", db_close_vm },
|
||||
|
||||
{"serialize", db_serialize },
|
||||
{"deserialize", db_deserialize },
|
||||
|
||||
#ifdef SQLITE_ENABLE_SESSION
|
||||
{"create_session", db_create_session },
|
||||
{"create_rebaser", db_create_rebaser },
|
||||
|
@ -2646,6 +2663,11 @@ static const luaL_Reg dblib[] = {
|
|||
{"iterate_changeset", db_iterate_changeset },
|
||||
#endif
|
||||
|
||||
#ifdef SQLITE_ENABLE_DESERIALIZE
|
||||
{"serialize", db_serialize },
|
||||
{"deserialize", db_deserialize },
|
||||
#endif
|
||||
|
||||
{"__tostring", db_tostring },
|
||||
{"__gc", db_gc },
|
||||
|
||||
|
@ -2654,6 +2676,7 @@ static const luaL_Reg dblib[] = {
|
|||
|
||||
static const luaL_Reg vmlib[] = {
|
||||
{"isopen", dbvm_isopen },
|
||||
{"readonly", dbvm_readonly },
|
||||
|
||||
{"step", dbvm_step },
|
||||
{"reset", dbvm_reset },
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue