Skip to content

✨ index access #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions lua/sqlite/assert.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ local errors = {
missing_db_object = "%s's db object is not set. set it with `%s:set_db(db)` and try again.",
outdated_schema = "`%s` does not exists in {`%s`}, schema is outdateset `self.db.tbl_schemas[table_name]` or reload",
auto_alter_more_less_keys = "schema defined ~= db schema. Please drop `%s` table first or set ensure to false.",
miss_match_pk_type = "Primary key ('%s') is of type '%s', '%s' can't be used to access %s table. RECEIVED KEY: %s",
no_primary_key = "%s has no primary key.",
}

for key, value in pairs(errors) do
Expand Down Expand Up @@ -80,4 +82,22 @@ M.auto_alter_should_have_equal_len = function(len_new, len_old, tname)
end
end

M.should_match_pk_type = function(name, kt, pk, key)
local knotstr = kt ~= "string"
local knotnum = kt ~= "number"
local pt = pk.type
if not pk or not pk.type then
return error(errors.no_primary_key:format(name))
end

if
kt ~= "boolean"
and (knotstr and (pt == "string" or pt == "text") or knotnum and (pt == "number" or pt == "integer"))
then
return error(errors.miss_match_pk_type:format(pk.name, pk.type, kt, name, key))
end

return true
end

return M
24 changes: 19 additions & 5 deletions lua/sqlite/db.lua
Original file line number Diff line number Diff line change
Expand Up @@ -591,15 +591,25 @@ function sqlite.db:select(tbl_name, spec, schema)

spec = spec or {}
spec.select = spec.keys and spec.keys or spec.select
local select = p.select(tbl_name, spec)
local st = ""

local stmt = s:parse(self.conn, p.select(tbl_name, spec))
s.each(stmt, function()
table.insert(ret, s.kv(stmt))
local stmt = s:parse(self.conn, select, tbl_name)
stmt:each(function()
table.insert(ret, stmt:kv())
end)
s.reset(stmt)
if s.finalize(stmt) then
if tbl_name == "todos_indexer" then
st = stmt:expand()
end

stmt:reset()
if stmt:finalize() then
self.modified = false
end
if tbl_name == "todos_indexer" and spec.id == 3 then
error(st)
end

return p.post_select(ret, schema)
end)
end
Expand Down Expand Up @@ -643,6 +653,10 @@ function sqlite.db:table(tbl_name, opts)
return self:tbl(tbl_name, opts)
end

function sqlite.db:last_insert_rowid()
return tonumber(clib.last_insert_rowid(self.conn))
end

---Sqlite functions sugar wrappers. See `sql/strfun`
sqlite.db.lib = require "sqlite.strfun"

Expand Down
2 changes: 1 addition & 1 deletion lua/sqlite/defs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ ffi.cdef [[
]]

---@class sqlite3 @sqlite3 db object
---@class sqlite_blob @sqlite3 blob object
---@class sqlite_blob* @sqlite3 blob object

M.to_str = function(ptr, len)
if ptr == nil then
Expand Down
2 changes: 2 additions & 0 deletions lua/sqlite/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ end
---@param o sqlite_tbl
---@return any
M.run = function(func, o)
func = func or function() end
a.should_have_db_object(o.db, o.name)
local exec = function()
local valid_schema = o.tbl_schema and next(o.tbl_schema) ~= nil
Expand Down Expand Up @@ -95,6 +96,7 @@ M.run = function(func, o)
o.db_schema = o.db:schema(o.name)
end

rawset(o, "last_id", o.db:last_insert_rowid())
--- Run wrapped function
return func()
end
Expand Down
9 changes: 8 additions & 1 deletion lua/sqlite/stmt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ function sqlstmt:parse(conn, str)

assert(
code == flags.ok,
("sqlite.lua: sql statement parse, , stmt: `%s`, err: `(`%s`)`"):format(o.str, clib.last_errmsg(o.conn))
string.format(
"sqlite.lua\n(parse error): `%s` code == %d\nstatement == '%s'",
clib.to_str(clib.errmsg(self.conn)),
code,
self.str
)
)

o.pstmt = pstmt[0]
return o

end

---Resets the parsed statement. required for parsed statements to be re-executed.
Expand Down
29 changes: 15 additions & 14 deletions lua/sqlite/tbl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
local u = require "sqlite.utils"
local h = require "sqlite.helpers"

local indexer = require "sqlite.tbl.indexer"
local sqlite = {}

---@class sqlite_tbl @Main sql table class
Expand All @@ -20,6 +21,8 @@ local sqlite = {}
sqlite.tbl = {}
sqlite.tbl.__index = sqlite.tbl

-- TODO: Add examples to index access in sqlite.tbl.new

---Create new |sqlite_tbl| object. This object encouraged to be extend and
---modified by the user. overwritten method can be still accessed via
---pre-appending `__` e.g. redefining |sqlite_tbl:get|, result in
Expand Down Expand Up @@ -52,27 +55,19 @@ sqlite.tbl.__index = sqlite.tbl
---@return sqlite_tbl
function sqlite.tbl.new(name, schema, db)
schema = schema or {}

local t = setmetatable({
schema = u.if_nil(schema.schema, schema)
---@type sqlite_tbl
local tbl = setmetatable({
db = db,
name = name,
tbl_schema = u.if_nil(schema.schema, schema),
tbl_schema = schema,
}, sqlite.tbl)

if db then
h.run(function() end, t)
h.run(nil, tbl)
end

return setmetatable({}, {
__index = function(_, key, ...)
if type(key) == "string" then
key = key:sub(1, 2) == "__" and key:sub(3, -1) or key
if t[key] then
return t[key]
end
end
end,
})
return indexer(tbl)
end

---Create or change table schema. If no {schema} is given,
Expand Down Expand Up @@ -468,6 +463,12 @@ function sqlite.tbl:set_db(db)
self.db = db
end

function sqlite.tbl:last_id()
h.run(function()
rawset(self, "last_id", self.db:last_insert_rowid())
end, self)
end

sqlite.tbl = setmetatable(sqlite.tbl, {
__call = function(_, ...)
return sqlite.tbl.new(...)
Expand Down
113 changes: 0 additions & 113 deletions lua/sqlite/tbl/extend.lua

This file was deleted.

Loading