sqlite.lua icon indicating copy to clipboard operation
sqlite.lua copied to clipboard

Support inserting/reading blobs

Open kkharji opened this issue 2 years ago • 1 comments

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

kkharji avatar Sep 01 '21 14:09 kkharji

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

kkharji avatar Sep 28 '21 13:09 kkharji