Skip to content

Commit

Permalink
⚡ improve handling of sqlite builtin functions
Browse files Browse the repository at this point in the history
closes #114
  • Loading branch information
kkharji committed Sep 9, 2021
1 parent f43b038 commit ca8233f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lua/sqlite/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ M.run = function(func, o)
if o.tbl_exists == nil then
o.tbl_exists = o.db:exists(o.name)
o.mtime = o.db.uri and (luv.fs_stat(o.db.uri) or { mtime = {} }).mtime.sec or nil
o.has_content = o.tbl_exists and o.db:eval(fmt("select count(*) from %s", o.name))[1]["count(*)"] ~= 0 or 0
rawset(
o,
"has_content",
o.tbl_exists and o.db:eval(fmt("select count(*) from %s", o.name))[1]["count(*)"] ~= 0 or 0
)
-- o.has_content =
M.check_for_auto_alter(o, valid_schema)
end

Expand Down
4 changes: 2 additions & 2 deletions lua/sqlite/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ local pvalues = function(defs, kv)

local keys = {}
for k, v in u.opairs(defs) do
if type(v) == "string" and v:match "%a+%(.+%)" then
if type(v) == "string" and v:match "^[%S]+%(.*%)$" then
tinsert(keys, v)
else
tinsert(keys, ":" .. k)
Expand Down Expand Up @@ -341,7 +341,7 @@ local opts_to_str = function(tbl)
end
end,
default = function(v)
v = (type(v) == "string" and v:match "^%a+%(.+%)$") and "(" .. tostring(v) .. ")" or v
v = (type(v) == "string" and v:match "^[%S]+%(.*%)$") and "(" .. tostring(v) .. ")" or v
local str = "default "
if tbl["required"] then
return "on conflict replace " .. str .. v
Expand Down
2 changes: 1 addition & 1 deletion lua/sqlite/stmt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ function sqlstmt:bind(...)

for k, v in pairs(names) do
local index = parameter_index_cache[k] or table.remove(anon_indices, 1)
if ((type(v) == "string" and v:match "%a+%(.+%)") and flags.ok or self:bind(index, v)) ~= flags.ok then
if ((type(v) == "string" and v:match "^[%S]+%(.*%)$") and flags.ok or self:bind(index, v)) ~= flags.ok then
error("sqlite.lua error at stmt:bind(), failed to bind a given value '%s'. Please report issue."):format(v)
end
end
Expand Down

0 comments on commit ca8233f

Please sign in to comment.