util.sql: SQLCipher support

This enables use of encrypted databases if LuaDBI or LuaSQLite3 has been
linked against SQLCipher. Using `LD_PRELOAD` may work as well.

Requires SQLCipher >= 4.0.0 due to the use of UPSERT
This commit is contained in:
Kim Alvefur 2025-01-23 16:38:56 +01:00
parent 2d4ea8ac9b
commit 654edd37bb
3 changed files with 13 additions and 0 deletions

View file

@ -46,6 +46,7 @@ TRUNK
- New 'keyval+' combined keyval/map store type
- Performance improvements in internal archive stores
- Ability to use SQLite3 storage using LuaSQLite3 instead of LuaDBI
- SQLCipher support
### Module API

View file

@ -84,6 +84,12 @@ function engine:connect()
dbh:autocommit(false); -- don't commit automatically
self.conn = dbh;
self.prepared = {};
if params.password then
local ok, err = self:execute(("PRAGMA key='%s'"):format(dbh:quote(params.password)));
if not ok then
return ok, err;
end
end
local ok, err = self:set_encoding();
if not ok then
return ok, err;

View file

@ -114,6 +114,12 @@ function engine:connect()
if not dbh then return nil, err; end
self.conn = dbh;
self.prepared = {};
if params.password then
local ok, err = self:execute(("PRAGMA key='%s'"):format((params.password:gsub("'", "''"))));
if not ok then
return ok, err;
end
end
local ok, err = self:set_encoding();
if not ok then
return ok, err;