mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-08 02:40:28 +00:00
Update redbean sqlite de/serialize to report errors and only be available when configured
This commit is contained in:
parent
062b2d776e
commit
6c633e2423
1 changed files with 21 additions and 15 deletions
|
@ -908,6 +908,12 @@ static int pusherr(lua_State *L, int rc) {
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int pusherrstr(lua_State *L, char *str) {
|
||||||
|
lua_pushnil(L);
|
||||||
|
lua_pushstring(L, str);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
static int db_wal_checkpoint(lua_State *L) {
|
static int db_wal_checkpoint(lua_State *L) {
|
||||||
sdb *db = lsqlite_checkdb(L, 1);
|
sdb *db = lsqlite_checkdb(L, 1);
|
||||||
int eMode = luaL_optinteger(L, 2, SQLITE_CHECKPOINT_PASSIVE);
|
int eMode = luaL_optinteger(L, 2, SQLITE_CHECKPOINT_PASSIVE);
|
||||||
|
@ -1748,16 +1754,15 @@ static int db_gc(lua_State *L) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef SQLITE_ENABLE_DESERIALIZE
|
||||||
|
|
||||||
static int db_serialize(lua_State *L) {
|
static int db_serialize(lua_State *L) {
|
||||||
sdb *db = lsqlite_getdb(L, 1);
|
sdb *db = lsqlite_checkdb(L, 1);
|
||||||
sqlite_int64 size = 0;
|
sqlite_int64 size = 0;
|
||||||
|
|
||||||
if (db->db == NULL) /* ignore closed databases */
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
char *buffer = (char *)sqlite3_serialize(db->db, "main", &size, 0);
|
char *buffer = (char *)sqlite3_serialize(db->db, "main", &size, 0);
|
||||||
if (buffer == NULL) /* ignore failed database serialization */
|
if (buffer == NULL)
|
||||||
return 0;
|
return pusherrstr(L, "failed to serialize");
|
||||||
|
|
||||||
lua_pushlstring(L, buffer, size);
|
lua_pushlstring(L, buffer, size);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
|
@ -1765,15 +1770,12 @@ static int db_serialize(lua_State *L) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static int db_deserialize(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;
|
size_t size = 0;
|
||||||
|
|
||||||
if (db->db == NULL) /* ignore closed databases */
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
const char *buffer = luaL_checklstring(L, 2, &size);
|
const char *buffer = luaL_checklstring(L, 2, &size);
|
||||||
if (buffer == NULL || size == 0) /* ignore empty database content */
|
if (buffer == NULL || size == 0)
|
||||||
return 0;
|
return pusherrstr(L, "failed to deserialize");
|
||||||
|
|
||||||
const char *sqlbuf = memcpy(sqlite3_malloc(size), buffer, size);
|
const char *sqlbuf = memcpy(sqlite3_malloc(size), buffer, size);
|
||||||
sqlite3_deserialize(db->db, "main", (void *)sqlbuf, size, size,
|
sqlite3_deserialize(db->db, "main", (void *)sqlbuf, size, size,
|
||||||
|
@ -1781,6 +1783,8 @@ static int db_deserialize(lua_State *L) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef SQLITE_ENABLE_SESSION
|
#ifdef SQLITE_ENABLE_SESSION
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -2634,9 +2638,6 @@ static const luaL_Reg dblib[] = {
|
||||||
{"close", db_close },
|
{"close", db_close },
|
||||||
{"close_vm", db_close_vm },
|
{"close_vm", db_close_vm },
|
||||||
|
|
||||||
{"serialize", db_serialize },
|
|
||||||
{"deserialize", db_deserialize },
|
|
||||||
|
|
||||||
#ifdef SQLITE_ENABLE_SESSION
|
#ifdef SQLITE_ENABLE_SESSION
|
||||||
{"create_session", db_create_session },
|
{"create_session", db_create_session },
|
||||||
{"create_rebaser", db_create_rebaser },
|
{"create_rebaser", db_create_rebaser },
|
||||||
|
@ -2646,6 +2647,11 @@ static const luaL_Reg dblib[] = {
|
||||||
{"iterate_changeset", db_iterate_changeset },
|
{"iterate_changeset", db_iterate_changeset },
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef SQLITE_ENABLE_DESERIALIZE
|
||||||
|
{"serialize", db_serialize },
|
||||||
|
{"deserialize", db_deserialize },
|
||||||
|
#endif
|
||||||
|
|
||||||
{"__tostring", db_tostring },
|
{"__tostring", db_tostring },
|
||||||
{"__gc", db_gc },
|
{"__gc", db_gc },
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue