Skip to content

Commit a322be9

Browse files
committed
✅ modify and query sqlite_tbl by number index
1 parent 2d84d32 commit a322be9

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

lua/sqlite/tbl/indexer.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ end
4646
---@param pk sqlite_schema_key
4747
---@return function(row, altkey):table
4848
local tbl_row_extender = function(tbl, pk)
49-
-- print(vim.inspect(pk))
5049
return function(row, reqkey)
5150
row = row or {}
5251
local mt = {
@@ -129,7 +128,7 @@ return function(tbl)
129128
return
130129
end
131130

132-
if vt == "nil" and kt == "string" then
131+
if vt == "nil" and kt == "string" or kt == "number" then
133132
tbl:remove { [pk.name] = arg }
134133
end
135134

@@ -146,5 +145,5 @@ return function(tbl)
146145
end
147146
end
148147

149-
return setmetatable({ config = {} }, mt)
148+
return setmetatable({ _config = {}, _state = {} }, mt)
150149
end

test/auto/tbl_spec.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,48 @@ describe("sqlite.tbl", function()
10401040
end)
10411041
end)
10421042

1043+
describe("number_index", function()
1044+
local t = tbl("number_idx", { id = true, name = "integer" }, db)
1045+
1046+
it("passes string_index tests", function()
1047+
t[1] = { name = "sam" }
1048+
eq({ id = 1, name = "sam" }, t[1])
1049+
eq("sam", t:where({ id = 1 }).name, "should have been set")
1050+
1051+
t[2].name = "John"
1052+
eq({ id = 2, name = "John" }, t[2])
1053+
eq("John", t:where({ id = 2 }).name, "should have been set")
1054+
eq("John", t[2].name, "should have been set")
1055+
1056+
t[2] = nil
1057+
eq(nil, t:where { id = 2 }, "should be empty")
1058+
eq({}, t[2], "should be empty")
1059+
1060+
t[1].name, t[2].name, t[2].name = "sam", "tami", "ram"
1061+
eq(
1062+
{
1063+
{ id = 1, name = "sam" },
1064+
{ id = 2, name = "tami" },
1065+
},
1066+
t[{
1067+
where = { name = { "sam", "tami", "ram" } },
1068+
order_by = { asc = { "id" } },
1069+
limit = 2,
1070+
}]
1071+
)
1072+
t[{ id = { 1, 2, 3 } }] = { name = "none" }
1073+
eq(
1074+
{
1075+
{ id = 1, name = "none" },
1076+
{ id = 2, name = "none" },
1077+
},
1078+
t[{
1079+
order_by = { asc = { "id" } },
1080+
limit = 2,
1081+
}]
1082+
)
1083+
end)
1084+
end)
10431085
-- vim.loop.fs_unlink(db_path)
10441086
end)
10451087

0 commit comments

Comments
 (0)