From da7cab4c28d5056d67b4899cbb37a051b79763e9 Mon Sep 17 00:00:00 2001
From: oltdaniel <53529846+oltdaniel@users.noreply.github.com>
Date: Fri, 24 Jun 2022 13:29:19 +0200
Subject: [PATCH] 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.
---
tool/net/demo/.init.lua | 44 +++++++------------------------
tool/net/demo/sql-backup.lua | 13 +++++++++
tool/net/demo/sql-backupstore.lua | 4 +++
tool/net/net.mk | 4 +++
4 files changed, 31 insertions(+), 34 deletions(-)
create mode 100644 tool/net/demo/sql-backup.lua
create mode 100644 tool/net/demo/sql-backupstore.lua
diff --git a/tool/net/demo/.init.lua b/tool/net/demo/.init.lua
index 19bf04366..304d0f89a 100644
--- a/tool/net/demo/.init.lua
+++ b/tool/net/demo/.init.lua
@@ -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
diff --git a/tool/net/demo/sql-backup.lua b/tool/net/demo/sql-backup.lua
new file mode 100644
index 000000000..e8dea9f54
--- /dev/null
+++ b/tool/net/demo/sql-backup.lua
@@ -0,0 +1,13 @@
+if GetAssetSize("backup.sqlite3") == nil then
+ Write("no backup exists. call sql-backupstore.lua 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.."
")
+end
\ No newline at end of file
diff --git a/tool/net/demo/sql-backupstore.lua b/tool/net/demo/sql-backupstore.lua
new file mode 100644
index 000000000..e99bd0010
--- /dev/null
+++ b/tool/net/demo/sql-backupstore.lua
@@ -0,0 +1,4 @@
+buffer = db:serialize()
+StoreAsset("backup.sqlite3", buffer)
+
+Write("backup created. size: "..GetAssetSize("backup.sqlite3"))
\ No newline at end of file
diff --git a/tool/net/net.mk b/tool/net/net.mk
index e6460106e..00cbd38c0 100644
--- a/tool/net/net.mk
+++ b/tool/net/net.mk
@@ -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 \