diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 00000000..e94aeff9 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +lua 5.3.6 diff --git a/lua/sqlite/parser.lua b/lua/sqlite/parser.lua index 1c677e15..b20d08ff 100644 --- a/lua/sqlite/parser.lua +++ b/lua/sqlite/parser.lua @@ -125,8 +125,8 @@ local pvalues = function(defs, kv) local keys = {} for k, v in u.opairs(defs) do - if type(v) == "string" and v:match "^[%S]+%(.*%)$" then - tinsert(keys, v) + if type(v) == "function" then + tinsert(keys, v()) else tinsert(keys, ":" .. k) end @@ -270,7 +270,8 @@ local pselect = function(select) if type(k) == "number" then tinsert(items, v) else - tinsert(items, ("%s as %s"):format(v, k)) + local z = type(v) == "function" and v() or v + tinsert(items, ("%s as %s"):format(z, k)) end end @@ -346,7 +347,7 @@ local opts_to_str = function(tbl) end end, default = function(v) - v = (type(v) == "string" and v:match "^[%S]+%(.*%)$") and "(" .. tostring(v) .. ")" or v + v = type(v) == "function" and "(" .. tostring(v()) .. ")" or v local str = "default " if tbl["required"] then return "on conflict replace " .. str .. v diff --git a/lua/sqlite/stmt.lua b/lua/sqlite/stmt.lua index a2d3d3d1..32383b13 100644 --- a/lua/sqlite/stmt.lua +++ b/lua/sqlite/stmt.lua @@ -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 "^[%S]+%(.*%)$") and flags.ok or self:bind(index, v)) ~= flags.ok then + if ((type(v) == "function") 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 diff --git a/lua/sqlite/strfun.lua b/lua/sqlite/strfun.lua index 420c9e18..38b25344 100644 --- a/lua/sqlite/strfun.lua +++ b/lua/sqlite/strfun.lua @@ -67,7 +67,7 @@ end ---@usage `sqlstrftime('%s','now') - strftime('%s','2014-10-07 02:34:56')` -> 2110042 M.strftime = function(format, timestring) local str = [[strftime('%s', %s)]] - return customstr(str:format(format, ts(timestring))) + return M.fn(str:format(format, ts(timestring))) end ---Return the number of days since noon in Greenwich on November 24, 4714 B.C. @@ -132,4 +132,10 @@ M.cast = function(source, as) return string.format("cast(%s as %s)", source, as) end +M.fn = function(s) + return function() + return s + end +end + return M diff --git a/test/auto/db_spec.lua b/test/auto/db_spec.lua index f7412b65..d1ba12a4 100644 --- a/test/auto/db_spec.lua +++ b/test/auto/db_spec.lua @@ -384,7 +384,10 @@ describe("sqlite.db", function() it("evaluates sqlite functions", function() db:eval "create table testa(id integer primary key, date text, title text)" - db:insert("testa", { title = "fd", date = db.lib.strftime "%H:%M:%S" }) + db:insert("testa", { + title = "fd", + date = db.lib.strftime "%H:%M:%S", + }) local res = db:eval [[select * from testa]] eq(os.date "!%H:%M:%S", res[1].date) @@ -546,9 +549,9 @@ describe("sqlite.db", function() end) describe(":select", function() - if vim.fn.executable("curl") then - pending("'curl' program is not available") - return + if vim.fn.executable "curl" then + pending "'curl' program is not available" + return end local db = sql:open(path) local posts, users diff --git a/test/auto/parser_spec.lua b/test/auto/parser_spec.lua index 4ffc56f9..d7185b91 100644 --- a/test/auto/parser_spec.lua +++ b/test/auto/parser_spec.lua @@ -1,4 +1,5 @@ local p = require "sqlite.parser" +local fn = require("sqlite.strfun").fn local eq = assert.are.same describe("parse", function() @@ -8,7 +9,7 @@ describe("parse", function() eq( "insert into todo (date, id) values(date('now'), :id)", p.insert(tbl, { values = { - date = "date('now')", + date = fn "date('now')", id = 1, } }) ) @@ -257,6 +258,20 @@ describe("parse", function() end) end) + describe("[select]", function() + it('works with [select] = "a single string"', function() + local defs = { + select = { + "id", + z = fn "date('now')", + }, + } + local expected = "select id, date('now') as z from people" + local passed = p.select("people", defs) + eq(expected, passed, "should be identical") + end) + end) + describe("[order by]", function() it("works with signle table name", function() local defs = {