Skip to content

Commit

Permalink
Merge pull request #473 from mrjones2014/mrj/472/fix-db-ids
Browse files Browse the repository at this point in the history
fix(frecency)!: Use byte strings for DB IDs
  • Loading branch information
mrjones2014 authored Sep 9, 2024
2 parents 3c32be7 + 4e73d86 commit 1d6f444
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@ jobs:
with:
repository: Olivine-Labs/luassert
path: vendor/luassert
- name: Checkout sqlite.lua
uses: actions/checkout@v3
with:
repository: kkharji/sqlite.lua
path: vendor/sqlite
- name: Run Unit Tests
run: make test
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ ensure-test-deps:
@mkdir -p vendor
@if test ! -d ./vendor/plenary.nvim; then git clone [email protected]:nvim-lua/plenary.nvim.git ./vendor/plenary.nvim/; fi
@if test ! -d ./vendor/luassert; then git clone [email protected]:Olivine-Labs/luassert.git ./vendor/luassert/; fi
@if test ! -d ./vendor/sqlite; then git clone [email protected]:kkharji/sqlite.lua.git ./vendor/sqlite/; fi

.PHONY: update-test-deps
update-test-deps: ensure-test-deps
@cd ./vendor/plenary.nvim/ && git pull && cd ..
@cd ./vendor/luassert/ && git pull && cd ..
@cd ./vendor/sqlite/ && git pull && cd ..

.PHONY: ensure-doc-deps
ensure-doc-deps:
Expand Down
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
16 changes: 16 additions & 0 deletions tests/legendary/to_bytes_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
local assert = require('luassert')
local wrapper = require('legendary.api.db.wrapper')

describe('to_bytes function', function()
local test_data = {
['test string'] = '11610111511632115116114105110103',
['with icons 󰊢'] = '119105116104321059911111011532243176138162238158168238153135',
['with quote chars \'"'] = '11910511610432113117111116101329910497114115323934',
['with emoji 🦀'] = '1191051161043210110911110610532240159166128',
}
for input, output in pairs(test_data) do
it(string.format('for input `%s`, output should be "%s"', input, output), function()
assert.are.same(wrapper.to_bytes(input), output)
end)
end
end)
1 change: 1 addition & 0 deletions tests/testrc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ vim.cmd([[
set rtp+=.
set rtp+=vendor/plenary.nvim
set rtp+=vendor/luassert
set rtp+=vendor/sqlite
runtime plugin/plenary.vim
]])
require('plenary.busted')

0 comments on commit 1d6f444

Please sign in to comment.