From 807ba86d6f4a9b592f3540339d2a740163e0b397 Mon Sep 17 00:00:00 2001 From: tami5 Date: Thu, 3 Mar 2022 21:39:16 +0300 Subject: [PATCH 1/2] feat: sqlite_open_v2 (take 2) cleanup after https://github.com/tami5/sqlite.lua/commit/9ccd2a6538d8a201d1ea08784bd866950e1b4130 Co-authored-By: Simon Hauser --- lua/sqlite/db.lua | 11 +++++++++++ lua/sqlite/defs.lua | 20 +++++++++++++++++--- lua/sqlite/examples/bookmarks.lua | 5 +---- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/lua/sqlite/db.lua b/lua/sqlite/db.lua index e0728723..2d6b22c9 100644 --- a/lua/sqlite/db.lua +++ b/lua/sqlite/db.lua @@ -33,6 +33,9 @@ local tbl = require "sqlite.tbl" ---
 ---```lua
 --- local db = sqlite.new("path/to/db" or "$env_var", { ... } or nil)
+--- -- configure open mode through opts.open_mode = "ro", "rw", "rwc", default "rwc"
+--- -- for more customize behaviour, set opts.open_mode to a list of db.flags
+--- -- see https://sqlite.org/c3ref/open.html#urifilenamesinsqlite3open
 ---```
 ---
---@param uri string: uri to db file. @@ -87,6 +90,9 @@ end --- opts = {} or nil -- custom sqlite3 options, see |sqlite_opts| --- --- if opts.keep_open, make connection and keep it open. --- --- if opts.lazy, then just provide logical object +--- --- configure open mode through opts.open_mode = "ro", "rw", "rwc", default "rwc" +--- --- for more customize behaviour, set opts.open_mode to a list of db.flags +--- --- see https://sqlite.org/c3ref/open.html#urifilenamesinsqlite3open --- } --- --- Overwrite method and access it using through pre-appending "__" --- db.select = function(...) db:__select(...) end @@ -138,6 +144,9 @@ end --- local db = sqlite.db:open("./pathto/dbfile" or "$ENV_VARABLE" or nil, {...}) --- -- reopen connection if closed. --- db:open() +--- -- configure open mode through opts.open_mode = "ro", "rw", "rwc", default "rwc" +--- -- for more customize behaviour, set opts.open_mode to a list of db.flags +--- -- see https://sqlite.org/c3ref/open.html#urifilenamesinsqlite3open ---``` --- ---@param uri string: (optional) {uri} == {nil} then in-memory db. @@ -650,4 +659,6 @@ sqlite.db = setmetatable(sqlite.db, { __call = sqlite.db.extend, }) +sqlite.db.flags = clib.flags + return sqlite.db diff --git a/lua/sqlite/defs.lua b/lua/sqlite/defs.lua index d9fabcc6..022e3c1d 100644 --- a/lua/sqlite/defs.lua +++ b/lua/sqlite/defs.lua @@ -653,18 +653,32 @@ M.last_errcode = function(conn_ptr) return clib.sqlite3_errcode(conn_ptr) end +-- Open Modes +M.open_modes = { + ["ro"] = bit.bor(M.flags.open_readonly, M.flags.open_uri), + ["rw"] = bit.bor(M.flags.open_readwrite, M.flags.open_uri), + ["rwc"] = bit.bor(M.flags.open_readwrite, M.flags.open_create, M.flags.open_uri), +} + ---Create new connection and modify `sqlite_db` object ---@param uri string ---@param opts sqlite_db.opts ---@return sqlite_blob* ----@TODO: support open_v2 to enable control over how the database file is opened. M.connect = function(uri, opts) opts = opts or {} local conn = M.get_new_db_ptr() - local code = clib.sqlite3_open(uri, conn) + local open_mode = opts.open_mode + opts.open_mode = nil + if type(open_mode) == "table" then + open_mode = bit.bor(unpack(open_mode)) + else + open_mode = M.open_modes[open_mode or "rwc"] + end + + local code = clib.sqlite3_open_v2(uri, conn, open_mode, nil) if code ~= M.flags.ok then - error(("sqlite.lua: couldn't connect to sql database, ERR:"):format(code)) + error(("sqlite.lua: couldn't connect to sql database, ERR: %s"):format(M.last_errmsg(conn[0]))) end for k, v in pairs(opts) do diff --git a/lua/sqlite/examples/bookmarks.lua b/lua/sqlite/examples/bookmarks.lua index 14101143..4ee54a12 100644 --- a/lua/sqlite/examples/bookmarks.lua +++ b/lua/sqlite/examples/bookmarks.lua @@ -25,7 +25,6 @@ local julianday, strftime = sqlite.lib.julianday, sqlite.lib.strftime ---@alias BMType '"web"' | '"file"' | '"dir"' - --[[ Datashapes --------------------------------------------- ---@class BMCollection @@ -160,7 +159,7 @@ function entries:inc(id) local row = entries:where { id = id } entries:update { where = { id = id }, - set = { count = row.count + 1 } + set = { count = row.count + 1 }, } ts:insert(id) ts:trim(id) @@ -169,7 +168,6 @@ end ---Add a row ---@param row BMEntry function entries:add(row) - if row.collection and not collection:where { title = row.collection } then collection:insert { title = row.collection } end @@ -272,7 +270,6 @@ if entries:count() == 0 then entries:seed() end - ---Edit an entry --- simple abstraction over entries.update { where = {id = 3}, set = { title = "none" } } entries:edit(3, { title = "none" }) From 9b72af77a6656a0522f9b423935c8d28a03b2680 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sat, 23 Jul 2022 21:00:01 +0000 Subject: [PATCH 2/2] :card_file_box: update doc/sqlite.txt --- doc/sqlite.txt | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/doc/sqlite.txt b/doc/sqlite.txt index 2ad710e3..f72df0f2 100644 --- a/doc/sqlite.txt +++ b/doc/sqlite.txt @@ -1,5 +1,5 @@ ================================================================================ - *sqlite.readme* +README *sqlite.readme* SQLite/LuaJIT binding and highly opinionated wrapper for storing, retrieving, caching, persisting, querying, and connecting to SQLite databases. @@ -69,7 +69,7 @@ sqlite_db_status *sqlite_db_status* ================================================================================ - *sqlite.db.lua* +LUA *sqlite.db.lua* Main sqlite.lua object and methods. @@ -95,6 +95,9 @@ sqlite.db.new({uri}, {opts}) *sqlite.db.new()* ```lua local db = sqlite.new("path/to/db" or "$env_var", { ... } or nil) + -- configure open mode through opts.open_mode = "ro", "rw", "rwc", default "rwc" + -- for more customize behaviour, set opts.open_mode to a list of db.flags + -- see https://sqlite.org/c3ref/open.html#urifilenamesinsqlite3open ``` @@ -151,6 +154,9 @@ sqlite.db:open({uri}, {opts}) *sqlite.db:open()* local db = sqlite.db:open("./pathto/dbfile" or "$ENV_VARABLE" or nil, {...}) -- reopen connection if closed. db:open() + -- configure open mode through opts.open_mode = "ro", "rw", "rwc", default "rwc" + -- for more customize behaviour, set opts.open_mode to a list of db.flags + -- see https://sqlite.org/c3ref/open.html#urifilenamesinsqlite3open ``` @@ -511,7 +517,7 @@ sqlite.db.lib() *sqlite.db.lib()* ================================================================================ - *sqlite.tbl.lua* +LUA *sqlite.tbl.lua* Abstraction to produce more readable code. ```lua