Update SQLite deserialize to allow resizing restored DB (#701)

Using either option (FREEONCLOSE or RESIZEABLE) requires using
sqlite3_malloc. Ref. #436
This commit is contained in:
Paul Kulchenko 2022-11-28 12:53:20 -08:00 committed by GitHub
parent b850b14300
commit bcae817215
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 3 deletions

View file

@ -7,6 +7,11 @@ backup = sqlite3.open_memory()
buffer = LoadAsset("backup.sqlite3") buffer = LoadAsset("backup.sqlite3")
backup:deserialize(buffer) backup:deserialize(buffer)
-- insert more values
for i = 1, 250 do
backup:exec("INSERT INTO test (content) VALUES ('Hello more')")
end
-- See .init.lua for CREATE TABLE setup. -- See .init.lua for CREATE TABLE setup.
for row in backup:nrows("SELECT * FROM test") do for row in backup:nrows("SELECT * FROM test") do
Write(row.id.." "..row.content.."<br>") Write(row.id.." "..row.content.."<br>")

View file

@ -1812,8 +1812,9 @@ static int db_deserialize(lua_State *L) {
if (buffer == NULL || size == 0) /* ignore empty database content */ if (buffer == NULL || size == 0) /* ignore empty database content */
return 0; return 0;
sqlite3_deserialize(db->db, "main", buffer, size, size, 0); const char *sqlbuf = memcpy(sqlite3_malloc(size), buffer, size);
free(buffer); sqlite3_deserialize(db->db, "main", sqlbuf, size, size,
SQLITE_DESERIALIZE_FREEONCLOSE + SQLITE_DESERIALIZE_RESIZEABLE);
return 0; return 0;
} }