Skip to content

Commit

Permalink
fix!(frecency): Use byte strings for DB IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjones2014 committed Sep 6, 2024
1 parent 8ed193e commit a875162
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lua/legendary/api/db/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ function M.update_item_score(item)
M.db_wrapper:update(item)
end

function M.sql_escape(str)
return M.db_wrapper.sql_escape(str)
function M.to_bytes(str)
return M.db_wrapper.to_bytes(str)
end

function M.get_client()
Expand Down
10 changes: 7 additions & 3 deletions lua/legendary/api/db/wrapper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,18 @@ local function row_id(row)
return (not vim.tbl_isempty(row)) and row[1].id or nil
end

function M.sql_escape(str)
return string.format("'%s'", string.gsub(str, "'", "\\'"))
function M.to_bytes(str)
local result = ''
for c in str:gmatch('.') do
result = result .. string.byte(c)
end
return result
end

---Update the stored data for an item
---@param item LegendaryItem
function M:update(item)
local item_id = M.sql_escape(item:frecency_id())
local item_id = M.to_bytes(item:frecency_id())
Log.trace('Updating item with ID "%s"', item_id)
local entry_id = row_id(self:transaction(self.queries.item_get_entries, { where = { item_id = item_id } }))
if not entry_id then
Expand Down
4 changes: 2 additions & 2 deletions lua/legendary/data/itemlist.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ function ItemList:sort_inplace(opts)
---@param item1 LegendaryItem
---@param item2 LegendaryItem
function(item1, item2)
local item1_id = DbClient.sql_escape(item1:frecency_id())
local item2_id = DbClient.sql_escape(item2:frecency_id())
local item1_id = DbClient.to_bytes(item1:frecency_id())
local item2_id = DbClient.to_bytes(item2:frecency_id())
local item1_score = frecency_scores[item1_id] or 0
local item2_score = frecency_scores[item2_id] or 0
return item1_score > item2_score
Expand Down

0 comments on commit a875162

Please sign in to comment.