Skip to content
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

✨ index access #106

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
✅ modify and query sqlite_tbl by number index
  • Loading branch information
kkharji committed Sep 2, 2021
commit a322be98cb18c0e37bcbb123464fcaca3f2c76e4
5 changes: 2 additions & 3 deletions lua/sqlite/tbl/indexer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ end
---@param pk sqlite_schema_key
---@return function(row, altkey):table
local tbl_row_extender = function(tbl, pk)
-- print(vim.inspect(pk))
return function(row, reqkey)
row = row or {}
local mt = {
Expand Down Expand Up @@ -129,7 +128,7 @@ return function(tbl)
return
end

if vt == "nil" and kt == "string" then
if vt == "nil" and kt == "string" or kt == "number" then
tbl:remove { [pk.name] = arg }
end

Expand All @@ -146,5 +145,5 @@ return function(tbl)
end
end

return setmetatable({ config = {} }, mt)
return setmetatable({ _config = {}, _state = {} }, mt)
end
42 changes: 42 additions & 0 deletions test/auto/tbl_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,48 @@ describe("sqlite.tbl", function()
end)
end)

describe("number_index", function()
local t = tbl("number_idx", { id = true, name = "integer" }, db)

it("passes string_index tests", function()
t[1] = { name = "sam" }
eq({ id = 1, name = "sam" }, t[1])
eq("sam", t:where({ id = 1 }).name, "should have been set")

t[2].name = "John"
eq({ id = 2, name = "John" }, t[2])
eq("John", t:where({ id = 2 }).name, "should have been set")
eq("John", t[2].name, "should have been set")

t[2] = nil
eq(nil, t:where { id = 2 }, "should be empty")
eq({}, t[2], "should be empty")

t[1].name, t[2].name, t[2].name = "sam", "tami", "ram"
eq(
{
{ id = 1, name = "sam" },
{ id = 2, name = "tami" },
},
t[{
where = { name = { "sam", "tami", "ram" } },
order_by = { asc = { "id" } },
limit = 2,
}]
)
t[{ id = { 1, 2, 3 } }] = { name = "none" }
eq(
{
{ id = 1, name = "none" },
{ id = 2, name = "none" },
},
t[{
order_by = { asc = { "id" } },
limit = 2,
}]
)
end)
end)
-- vim.loop.fs_unlink(db_path)
end)

Expand Down