Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions doc/sqlite.txt
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -69,7 +69,7 @@ sqlite_db_status *sqlite_db_status*


================================================================================
*sqlite.db.lua*
LUA *sqlite.db.lua*

Main sqlite.lua object and methods.

Expand All @@ -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
```


Expand Down Expand Up @@ -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
```


Expand Down Expand Up @@ -511,7 +517,7 @@ sqlite.db.lib() *sqlite.db.lib()*


================================================================================
*sqlite.tbl.lua*
LUA *sqlite.tbl.lua*

Abstraction to produce more readable code.
```lua
Expand Down
11 changes: 11 additions & 0 deletions lua/sqlite/db.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ local tbl = require "sqlite.tbl"
---<pre>
---```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
---```
---</pre>
---@param uri string: uri to db file.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
---```
---</pre>
---@param uri string: (optional) {uri} == {nil} then in-memory db.
Expand Down Expand Up @@ -650,4 +659,6 @@ sqlite.db = setmetatable(sqlite.db, {
__call = sqlite.db.extend,
})

sqlite.db.flags = clib.flags

return sqlite.db
20 changes: 17 additions & 3 deletions lua/sqlite/defs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -655,18 +655,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
Expand Down
1 change: 1 addition & 0 deletions lua/sqlite/examples/bookmarks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ end
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" })

Expand Down