mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-09 03:10:27 +00:00
Change demo for sqlite serialization
As explained in https://github.com/jart/cosmopolitan/pull/436#issuecomment-1164706893 the original use case is not possible with sqlite serialization, as an in-memory database cannot be shared across multiple processes. Thereby, this use case simply creates a backup of the in-memory database created in '.init.lua' and loads it to do a query.
This commit is contained in:
parent
710c979bfa
commit
da7cab4c28
4 changed files with 31 additions and 34 deletions
|
@ -9,35 +9,16 @@ HidePath('/usr/share/ssl/')
|
|||
-- LaunchBrowser('/tool/net/demo/index.html')
|
||||
|
||||
-- sql database (see sql.lua)
|
||||
function StoreSqlite(database, path)
|
||||
local buffer = database:serialize()
|
||||
return StoreAsset(path, buffer)
|
||||
end
|
||||
|
||||
function LoadSqlite(path)
|
||||
local database = sqlite3.open_memory()
|
||||
local buffer = LoadAsset(path)
|
||||
database:deserialize(buffer)
|
||||
return database
|
||||
end
|
||||
|
||||
database = "database.sqlite3"
|
||||
-- Check if there is already a database
|
||||
if GetAssetSize(database) ~= nil then
|
||||
db = LoadSqlite(database)
|
||||
db:exec[[
|
||||
INSERT INTO test (content) VALUES ('World');
|
||||
]]
|
||||
else
|
||||
db = sqlite3.open_memory()
|
||||
db:exec[[
|
||||
CREATE TABLE test (
|
||||
id INTEGER PRIMARY KEY,
|
||||
content TEXT
|
||||
);
|
||||
INSERT INTO test (content) VALUES ('Hello');
|
||||
]]
|
||||
end
|
||||
db = sqlite3.open_memory()
|
||||
db:exec[[
|
||||
CREATE TABLE test (
|
||||
id INTEGER PRIMARY KEY,
|
||||
content TEXT
|
||||
);
|
||||
INSERT INTO test (content) VALUES ('Hello World');
|
||||
INSERT INTO test (content) VALUES ('Hello Lua');
|
||||
INSERT INTO test (content) VALUES ('Hello Sqlite3');
|
||||
]]
|
||||
|
||||
-- this intercepts all requests if it's defined
|
||||
function OnHttpRequest()
|
||||
|
@ -54,11 +35,6 @@ function OnHttpRequest()
|
|||
SetHeader('Server', 'redbean!')
|
||||
end
|
||||
|
||||
function OnServerStop()
|
||||
-- make sure we store the database on exit
|
||||
StoreSqlite(db, database)
|
||||
end
|
||||
|
||||
function Adder(x, y)
|
||||
return x + y
|
||||
end
|
||||
|
|
13
tool/net/demo/sql-backup.lua
Normal file
13
tool/net/demo/sql-backup.lua
Normal file
|
@ -0,0 +1,13 @@
|
|||
if GetAssetSize("backup.sqlite3") == nil then
|
||||
Write("no backup exists. call <a href='sql-backupstore.lua'>sql-backupstore.lua</a> first.")
|
||||
return
|
||||
end
|
||||
|
||||
backup = sqlite3.open_memory()
|
||||
buffer = LoadAsset("backup.sqlite3")
|
||||
backup:deserialize(buffer)
|
||||
|
||||
-- See .init.lua for CREATE TABLE setup.
|
||||
for row in backup:nrows("SELECT * FROM test") do
|
||||
Write(row.id.." "..row.content.."<br>")
|
||||
end
|
4
tool/net/demo/sql-backupstore.lua
Normal file
4
tool/net/demo/sql-backupstore.lua
Normal file
|
@ -0,0 +1,4 @@
|
|||
buffer = db:serialize()
|
||||
StoreAsset("backup.sqlite3", buffer)
|
||||
|
||||
Write("backup created. size: "..GetAssetSize("backup.sqlite3"))
|
|
@ -167,6 +167,8 @@ o/tinylinux/tool/net/redbean.com: \
|
|||
o/$(MODE)/tool/net/demo/.init.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/.reload.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/sql.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/sql-backup.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/sql-backupstore.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/unix-rawsocket.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/unix-subprocess.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/unix-webserver.lua.zip.o \
|
||||
|
@ -215,6 +217,8 @@ o/$(MODE)/tool/net/redbean-demo.com.dbg: \
|
|||
o/$(MODE)/tool/net/largon2.o \
|
||||
o/$(MODE)/tool/net/net.pkg \
|
||||
o/$(MODE)/tool/net/demo/sql.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/sql-backup.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/sql-backupstore.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/unix-rawsocket.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/unix-subprocess.lua.zip.o \
|
||||
o/$(MODE)/tool/net/demo/unix-webserver.lua.zip.o \
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue