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

Support inserting/reading blobs #108

Open
kkharji opened this issue Sep 1, 2021 · 1 comment
Open

Support inserting/reading blobs #108

kkharji opened this issue Sep 1, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@kkharji
Copy link
Owner

kkharji commented Sep 1, 2021

It should've worked, but it seems that we have the wrong type, need more tests

@kkharji kkharji changed the title look into binding blobs possible bug with binding blobs Sep 2, 2021
@kkharji kkharji added the enhancement New feature or request label Sep 28, 2021
@kkharji kkharji changed the title possible bug with binding blobs Support inserting/reading blobs Sep 28, 2021
@kkharji
Copy link
Owner Author

kkharji commented Sep 28, 2021

currently this is how it's done

function M:init()
  self.stmt = require("sqlite.stmt")
  self.clib = require('sqlite.defs')
  self.db = require('sqlite.db').new(vim.fn.stdpath('cache') ..'/luacache.db')
  self.db:with_open(function ()
    self.db:create("luacache", { id = true, chunk = "blob", size = "integer", ensure = true })
  end)
end

function M:save(chunk)
  return self.db:with_open(function ()
    local statement = "replace into luacache(id, chunk, size) values(?, ?, ?)"
    local sobj = self.stmt:parse(self.db.conn, statement)
    self.clib.bind_int(sobj.pstmt, 1, 1)
    self.clib.bind_blob(sobj.pstmt, 2, chunk, #chunk + 1, nil)
    self.clib.bind_int(sobj.pstmt, 3, #chunk)
    sobj:step()
    sobj:bind_clear()
    return sobj:finalize()
  end)
end

function M:dump()
  return self.db:with_open(function ()
    local ret = {}
    local stmt = self.stmt:parse(self.db.conn, "select * from luacache where id = 1")
    stmt:step()
    for i = 0, stmt:nkeys() - 1 do
      ret[stmt:key(i)] = stmt:val(i)
    end
    local chunk = self.clib.to_str(ret.chunk, ret.size)
    stmt:reset()
    stmt:finalize()
    return chunk
  end)
end

function M:clear()
  return self.db:with_open(function ()
    return self.db:delete("luacache")
  end)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant