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

sqlite function in insert break other values #87

Closed
kkharji opened this issue Aug 19, 2021 · 2 comments · Fixed by #88
Closed

sqlite function in insert break other values #87

kkharji opened this issue Aug 19, 2021 · 2 comments · Fixed by #88
Labels
bug Something isn't working

Comments

@kkharji
Copy link
Owner

kkharji commented Aug 19, 2021

Apparently the following parsed statement is not good enough to pass:

insert into timestamps (fid, date) values(:fid, julianday('now'))"

it does insert the date, but sadly ignore fid.

🤔 not sure how this would be solved 😭

function DB:insert(tbl_name, rows)
  --- ...
  clib.wrap_stmts(self.conn, function()
    for _, v in ipairs(items) do
      local s = stmt:parse(self.conn, P.insert(tbl_name, { values = v }))
      --> P.insert(....) => "insert into timestamps (fid, date) values(:fid, julianday('now'))"
      --> stmt:parse(..) => "insert into timestamps (fid, date) values(NULL, julianday('now'))"
      s:bind(v)
      s:step()
      s:bind_clear()
      table.insert(ret_vals, s:finalize())
    end
  end)
end

On the parser side, instead of binding to :date it uses the value in place.

local pvalues = function(defs, kv)
  --- ....
  local keys = {}
  for k, v in u.opairs(defs) do
    if type(v) == "string" and v:match "%a+%(.+%)" then
      tinsert(keys, v)
    else
      tinsert(keys, ":" .. k)
    end
  end

  return ("values(%s)"):format(tconcat(keys, ", "))
end
@kkharji kkharji added the bug Something isn't working label Aug 19, 2021
@kkharji
Copy link
Owner Author

kkharji commented Aug 19, 2021

Options:

  • At parser level by having all the key values set in place, when one of the keys is a sql function.
  • At db.insert level remove keys that match type(v) == "string" and v:match "%a+%(.+%)"
  • Research more ....

edit: s:expand() outputs:

insert into timestamps (fid, date) values(NULL, julianday('now'))

That's interesting, I wonder if I should be fixing the issue on sql statement level 🤔

@kkharji
Copy link
Owner Author

kkharji commented Aug 19, 2021

😆 😆 😆 f####
https:/tami5/sql.nvim/blob/2feef57bef147cf502b4491e3fc6c58a05f3096e/lua/sql/stmt.lua#L307-L315 . We should've errored out, here's 3 hours I will never get back

pushing fix soon ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant