Skip to content

Commit 56c5aac

Browse files
authored
feat: support sqlite open v2 (#132)
1 parent d53bdff commit 56c5aac

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

doc/sqlite.txt

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
================================================================================
2-
*sqlite.readme*
2+
README *sqlite.readme*
33

44
SQLite/LuaJIT binding and highly opinionated wrapper for storing, retrieving,
55
caching, persisting, querying, and connecting to SQLite databases.
@@ -69,7 +69,7 @@ sqlite_db_status *sqlite_db_status*
6969

7070

7171
================================================================================
72-
*sqlite.db.lua*
72+
LUA *sqlite.db.lua*
7373

7474
Main sqlite.lua object and methods.
7575

@@ -95,6 +95,9 @@ sqlite.db.new({uri}, {opts}) *sqlite.db.new()*
9595

9696
```lua
9797
local db = sqlite.new("path/to/db" or "$env_var", { ... } or nil)
98+
-- configure open mode through opts.open_mode = "ro", "rw", "rwc", default "rwc"
99+
-- for more customize behaviour, set opts.open_mode to a list of db.flags
100+
-- see https://sqlite.org/c3ref/open.html#urifilenamesinsqlite3open
98101
```
99102

100103

@@ -151,6 +154,9 @@ sqlite.db:open({uri}, {opts}) *sqlite.db:open()*
151154
local db = sqlite.db:open("./pathto/dbfile" or "$ENV_VARABLE" or nil, {...})
152155
-- reopen connection if closed.
153156
db:open()
157+
-- configure open mode through opts.open_mode = "ro", "rw", "rwc", default "rwc"
158+
-- for more customize behaviour, set opts.open_mode to a list of db.flags
159+
-- see https://sqlite.org/c3ref/open.html#urifilenamesinsqlite3open
154160
```
155161

156162

@@ -511,7 +517,7 @@ sqlite.db.lib() *sqlite.db.lib()*
511517

512518

513519
================================================================================
514-
*sqlite.tbl.lua*
520+
LUA *sqlite.tbl.lua*
515521

516522
Abstraction to produce more readable code.
517523
```lua

lua/sqlite/db.lua

+11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ local tbl = require "sqlite.tbl"
3333
---<pre>
3434
---```lua
3535
--- local db = sqlite.new("path/to/db" or "$env_var", { ... } or nil)
36+
--- -- configure open mode through opts.open_mode = "ro", "rw", "rwc", default "rwc"
37+
--- -- for more customize behaviour, set opts.open_mode to a list of db.flags
38+
--- -- see https://sqlite.org/c3ref/open.html#urifilenamesinsqlite3open
3639
---```
3740
---</pre>
3841
---@param uri string: uri to db file.
@@ -87,6 +90,9 @@ end
8790
--- opts = {} or nil -- custom sqlite3 options, see |sqlite_opts|
8891
--- --- if opts.keep_open, make connection and keep it open.
8992
--- --- if opts.lazy, then just provide logical object
93+
--- --- configure open mode through opts.open_mode = "ro", "rw", "rwc", default "rwc"
94+
--- --- for more customize behaviour, set opts.open_mode to a list of db.flags
95+
--- --- see https://sqlite.org/c3ref/open.html#urifilenamesinsqlite3open
9096
--- }
9197
--- --- Overwrite method and access it using through pre-appending "__"
9298
--- db.select = function(...) db:__select(...) end
@@ -138,6 +144,9 @@ end
138144
--- local db = sqlite.db:open("./pathto/dbfile" or "$ENV_VARABLE" or nil, {...})
139145
--- -- reopen connection if closed.
140146
--- db:open()
147+
--- -- configure open mode through opts.open_mode = "ro", "rw", "rwc", default "rwc"
148+
--- -- for more customize behaviour, set opts.open_mode to a list of db.flags
149+
--- -- see https://sqlite.org/c3ref/open.html#urifilenamesinsqlite3open
141150
---```
142151
---</pre>
143152
---@param uri string: (optional) {uri} == {nil} then in-memory db.
@@ -650,4 +659,6 @@ sqlite.db = setmetatable(sqlite.db, {
650659
__call = sqlite.db.extend,
651660
})
652661

662+
sqlite.db.flags = clib.flags
663+
653664
return sqlite.db

lua/sqlite/defs.lua

+17-3
Original file line numberDiff line numberDiff line change
@@ -655,18 +655,32 @@ M.last_errcode = function(conn_ptr)
655655
return clib.sqlite3_errcode(conn_ptr)
656656
end
657657

658+
-- Open Modes
659+
M.open_modes = {
660+
["ro"] = bit.bor(M.flags.open_readonly, M.flags.open_uri),
661+
["rw"] = bit.bor(M.flags.open_readwrite, M.flags.open_uri),
662+
["rwc"] = bit.bor(M.flags.open_readwrite, M.flags.open_create, M.flags.open_uri),
663+
}
664+
658665
---Create new connection and modify `sqlite_db` object
659666
---@param uri string
660667
---@param opts sqlite_db.opts
661668
---@return sqlite_blob*
662-
---@TODO: support open_v2 to enable control over how the database file is opened.
663669
M.connect = function(uri, opts)
664670
opts = opts or {}
665671
local conn = M.get_new_db_ptr()
666-
local code = clib.sqlite3_open(uri, conn)
672+
local open_mode = opts.open_mode
673+
opts.open_mode = nil
674+
if type(open_mode) == "table" then
675+
open_mode = bit.bor(unpack(open_mode))
676+
else
677+
open_mode = M.open_modes[open_mode or "rwc"]
678+
end
679+
680+
local code = clib.sqlite3_open_v2(uri, conn, open_mode, nil)
667681

668682
if code ~= M.flags.ok then
669-
error(("sqlite.lua: couldn't connect to sql database, ERR:"):format(code))
683+
error(("sqlite.lua: couldn't connect to sql database, ERR: %s"):format(M.last_errmsg(conn[0])))
670684
end
671685

672686
for k, v in pairs(opts) do

lua/sqlite/examples/bookmarks.lua

+1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ end
269269
if entries:count() == 0 then
270270
entries:seed()
271271
end
272+
272273
---Edit an entry --- simple abstraction over entries.update { where = {id = 3}, set = { title = "none" } }
273274
entries:edit(3, { title = "none" })
274275

0 commit comments

Comments
 (0)