-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathparser.lua
More file actions
574 lines (500 loc) · 15 KB
/
parser.lua
File metadata and controls
574 lines (500 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
local u = require "sqlite.utils"
local json = require "sqlite.json"
local tinsert = table.insert
local tconcat = table.concat
local a = require "sqlite.assert"
local M = {}
---@brief [[
---Internal functions for parsing sql statement from lua table.
---methods = { select, update, delete, insert, create, alter, drop }
---accepts tbl name and options.
---options = {join, keys, values, set, where, select}
---hopfully returning valid sql statement :D
---@brief ]]
---@tag parser.lua
---handle sqlite datatype interop
M.sqlvalue = function(v)
return type(v) == "boolean" and (v == true and 1 or 0) or (v == nil and "null" or v)
end
M.luavalue = function(v, key_type)
if key_type == "luatable" or key_type == "json" then
return json.decode(v)
elseif key_type == "boolean" then
if v == 0 then
return false
else
return true
end
-- return v == 0 and false or true
end
return v
end
---string.format specifier based on value type
---@param v any: the value
---@param nonbind boolean?: whether to return the specifier or just return the value.
---@return string
local specifier = function(v, nonbind)
local type = type(v)
if type == "number" then
local _, b = math.modf(v)
return b == 0 and "%d" or "%f"
elseif type == "string" and not nonbind then
return v:find "'" and [["%s"]] or "'%s'"
elseif nonbind then
return v
else
return ""
end
end
local bind = function(o)
o = o or {}
o.s = o.s or ", "
if not o.kv then
o.v = o.v ~= nil and M.sqlvalue(o.v) or "?"
return ("%s = " .. specifier(o.v)):format(o.k, o.v)
else
local res = {}
for k, v in u.opairs(o.kv) do
k = o.k ~= nil and o.k or k
v = M.sqlvalue(v)
v = o.nonbind and ":" .. k or v
tinsert(res, ("%s" .. (o.nonbind and nil or " = ") .. specifier(v, o.nonbind)):format(k, v))
end
return tconcat(res, o.s)
end
end
---format glob pattern as part of where clause
local pcontains = function(defs)
if not defs then
return {}
end
local items = {}
for k, v in u.opairs(defs) do
local head = "%s glob " .. specifier(k)
if type(v) == "table" then
local val = u.map(v, function(value)
return head:format(k, M.sqlvalue(value))
end)
tinsert(items, tconcat(val, " or "))
else
tinsert(items, head:format(k, v))
end
end
return tconcat(items, " ")
end
---Format values part of sql statement
---@params defs table: key/value pairs defining sqlite table keys.
---@params defs kv: whether to bind by named keys.
local pkeys = function(defs, kv)
kv = kv == nil and true or kv
if not defs or not kv then
return {}
end
defs = u.is_nested(defs) and defs[1] or defs
local keys = {}
for k, _ in u.opairs(defs) do
tinsert(keys, k)
end
return ("(%s)"):format(tconcat(keys, ", "))
end
---Format values part of sql statement, usually used with select method.
---@params defs table: key/value pairs defining sqlite table keys.
---@params defs kv: whether to bind by named keys.
local pvalues = function(defs, kv)
kv = kv == nil and true or kv -- TODO: check if defs is key value pairs instead
if not defs or not kv then
return {}
end
defs = u.is_nested(defs) and defs[1] or defs
local keys = {}
for k, v in u.opairs(defs) do
if type(v) == "function" then
tinsert(keys, v())
else
tinsert(keys, ":" .. k)
end
end
return ("values(%s)"):format(tconcat(keys, ", "))
end
---Format where part of a sql statement.
---@params defs table: key/value pairs defining sqlite table keys.
---@params name string: the name of the sqlite table
---@params join table: used as boolean, controling whether to use name.key or just key.
local pwhere = function(defs, name, join, contains)
if not defs and not contains then
return {}
end
local where = {}
if defs then
for k, v in u.opairs(defs) do
k = join and name .. "." .. k or k
if type(v) ~= "table" then
if type(v) == "string" and (v:sub(1, 1) == "<" or v:sub(1, 1) == ">") then
tinsert(where, k .. " " .. v)
else
tinsert(where, bind { v = v, k = k, s = " and " })
end
else
if type(k) == "number" then
tinsert(where, table.concat(v, " "))
else
tinsert(where, "(" .. bind { kv = v, k = k, s = " or " } .. ")")
end
end
end
end
if contains then
tinsert(where, pcontains(contains))
end
return ("where %s"):format(tconcat(where, " and "))
end
local plimit = function(defs)
if not defs then
return {}
end
local type = type(defs)
local istbl = (type == "table" and defs[2])
local offset = "limit %s offset %s"
local limit = "limit %s"
return istbl and offset:format(defs[1], defs[2]) or limit:format(type == "number" and defs or defs[1])
end
---Format set part of sql statement, usually used with update method.
---@params defs table: key/value pairs defining sqlite table keys.
local pset = function(defs)
if not defs then
return {}
end
return "set " .. bind { kv = defs, nonbind = true }
end
---Format join part of a sql statement.
---@params defs table: key/value pairs defining sqlite table keys.
---@params name string: the name of the sqlite table
local pjoin = function(defs, name)
if not defs or not name then
return {}
end
local target
local on = (function()
for k, v in pairs(defs) do
if k ~= name then
target = k
return ("%s.%s ="):format(k, v)
end
end
end)()
local select = (function()
for k, v in pairs(defs) do
if k == name then
return ("%s.%s"):format(k, v)
end
end
end)()
return ("inner join %s on %s %s"):format(target, on, select)
end
local porder_by = function(defs)
-- TODO: what if nulls? should append "nulls last"
if not defs then
return {}
end
local fmt = "%s %s"
local items = {}
for v, k in u.opairs(defs) do
if type(k) == "table" then
for _, _k in u.opairs(k) do
tinsert(items, fmt:format(_k, v))
end
else
tinsert(items, fmt:format(k, v))
end
end
return ("order by %s"):format(tconcat(items, ", "))
end
local partial = function(method, tbl, opts)
opts = opts or {}
return tconcat(
u.flatten {
method,
pkeys(opts.values),
pvalues(opts.values, opts.named),
pset(opts.set),
pwhere(opts.where, tbl, opts.join, opts.contains),
porder_by(opts.order_by),
plimit(opts.limit),
},
" "
)
end
local pselect = function(select)
local t = type(select)
if t == "table" and next(select) ~= nil then
local items = {}
for k, v in pairs(select) do
if type(k) == "number" then
tinsert(items, v)
else
local z = type(v) == "function" and v() or v
tinsert(items, ("%s as %s"):format(z, k))
end
end
return tconcat(items, ", ")
end
return t == "string" and select or "*"
end
---Parse select statement to extracts data from a database
---@param tbl string: table name
---@param opts table: lists of options: valid{ select, join, order_by, limit, where }
---@return string: the select sql statement.
M.select = function(tbl, opts)
opts = opts or {}
local cmd = opts.unique and "select distinct %s" or "select %s"
local select = pselect(opts.select)
local stmt = (cmd .. " from %s"):format(select, tbl)
local method = opts.join and stmt .. " " .. pjoin(opts.join, tbl) or stmt
return partial(method, tbl, opts)
end
---Parse select statement to update data in the database
---@param tbl string: table name
---@param opts table: lists of options: valid{ set, where }
---@return string: the update sql statement.
M.update = function(tbl, opts)
local method = ("update %s"):format(tbl)
return partial(method, tbl, opts)
end
---Parse insert statement to insert data into a database
---@param tbl string: table name
---@param opts table: lists of options: valid{ where }
---@return string: the insert sql statement.
M.insert = function(tbl, opts)
local method = ("insert into %s"):format(tbl)
return partial(method, tbl, opts)
end
---Parse delete statement to deletes data from a database
---@param tbl string: table name
---@param opts table: lists of options: valid{ where }
---@return string: the delete sql statement.
M.delete = function(tbl, opts)
opts = opts or {}
local method = ("delete from %s"):format(tbl)
local where = pwhere(opts.where)
return type(where) == "string" and method .. " " .. where or method
end
local format_action = function(value, update)
local stmt = update and "on update" or "on delete"
local preappend = (value:match "default" or value:match "null") and " set " or " "
return stmt .. preappend .. value
end
local opts_to_str = function(tbl)
local f = {
pk = function()
return "primary key"
end,
type = function(v)
return v
end,
unique = function()
return "unique"
end,
required = function(v)
if v then
return "not null"
end
end,
default = function(v)
v = type(v) == "function" and "(" .. tostring(v()) .. ")" or v
local str = "default "
if tbl["required"] then
return "on conflict replace " .. str .. v
else
return str .. v
end
end,
reference = function(v)
return ("references %s"):format(v:gsub("%.", "(") .. ")")
end,
on_update = function(v)
return format_action(v, true)
end,
on_delete = function(v)
return format_action(v)
end,
}
f.primary = f.pk
local res = {}
if type(tbl[1]) == "string" then
res[1] = tbl[1]
end
local check = function(type)
local v = tbl[type]
if v then
res[#res + 1] = f[type](v)
end
end
check "type"
check "unique"
check "required"
check "pk"
check "primary"
check "default"
check "reference"
check "on_update"
check "on_delete"
return tconcat(res, " ")
end
---Parse table create statement
---@param tbl string: table name
---@param defs table: keys and type pairs
---@return string: the create sql statement.
M.create = function(tbl, defs, ignore_ensure)
if not defs then
return
end
local items = {}
tbl = (defs.ensure and not ignore_ensure) and "if not exists " .. tbl or tbl
for k, v in u.opairs(defs) do
if k ~= "ensure" then
local t = type(v)
if t == "boolean" then
tinsert(items, k .. " integer not null primary key")
elseif t ~= "table" then
tinsert(items, string.format("%s %s", k, v))
else
if u.is_list(v) then
tinsert(items, ("%s %s"):format(k, tconcat(v, " ")))
else
tinsert(items, ("%s %s"):format(k, opts_to_str(v)))
end
end
end
end
return ("CREATE TABLE %s(%s)"):format(tbl, tconcat(items, ", "))
end
---Parse table drop statement
---@param tbl string: table name
---@return string: the drop sql statement.
M.drop = function(tbl)
return "drop table " .. tbl
end
-- local same_type = function(new, old)
-- if not new or not old then
-- return false
-- end
-- local tnew, told = type(new), type(old)
-- if tnew == told then
-- if tnew == "string" then
-- return new == old
-- elseif tnew == "table" then
-- if new[1] and old[1] then
-- return (new[1] == old[1])
-- elseif new.type and old.type then
-- return (new.type == old.type)
-- elseif new.type and old[1] then
-- return (new.type == old[1])
-- elseif new[1] and old.type then
-- return (new[1] == old.type)
-- end
-- end
-- else
-- if tnew == "table" and told == "string" then
-- if new.type == old then
-- return true
-- elseif new[1] == old then
-- return true
-- end
-- elseif tnew == "string" and told == "table" then
-- return old.type == new or old[1] == new
-- end
-- end
-- -- return false
-- end
---Alter a given table, only support changing key definition
---@param tname string
---@param new sqlite_schema_dict
---@param old sqlite_schema_dict
M.table_alter_key_defs = function(tname, new, old, dry)
local tmpname = tname .. "_new"
local create = M.create(tmpname, new, true)
local drop = M.drop(tname)
local move = "INSERT INTO %s(%s) SELECT %s FROM %s"
local rename = ("ALTER TABLE %s RENAME TO %s"):format(tmpname, tname)
local with_foregin_key = false
for _, def in pairs(new) do
if type(def) == "table" and def.reference then
with_foregin_key = true
end
end
local stmt = "PRAGMA foreign_keys=off; BEGIN TRANSACTION; %s; COMMIT;"
if not with_foregin_key then
stmt = stmt .. " PRAGMA foreign_keys=on"
end
local keys = { new = u.okeys(new), old = u.okeys(old) }
local idx = { new = {}, old = {} }
local len = { new = #keys.new, old = #keys.old }
-- local facts = { extra_key = len.new > len.old, drop_key = len.old > len.new }
a.auto_alter_should_have_equal_len(len.new, len.old, tname)
for _, varient in ipairs { "new", "old" } do
for k, v in pairs(keys[varient]) do
idx[varient][v] = k
end
end
for i, v in ipairs(keys.new) do
if idx.old[v] and idx.old[v] ~= i then
local tmp = keys.old[i]
keys.old[i] = v
keys.old[idx.old[v]] = tmp
end
end
local update_null_vals = {}
local update_null_stmt = "UPDATE %s SET %s=%s where %s IS NULL"
for key, def in pairs(new) do
if type(def) == "table" and def.default and not def.required then
tinsert(update_null_vals, update_null_stmt:format(tmpname, key, def.default, key))
end
end
update_null_vals = #update_null_vals == 0 and "" or tconcat(update_null_vals, "; ")
local new_keys, old_keys = tconcat(keys.new, ", "), tconcat(keys.old, ", ")
local insert = move:format(tmpname, new_keys, old_keys, tname)
stmt = stmt:format(tconcat({ create, insert, update_null_vals, drop, rename }, "; "))
return not dry and stmt or insert
end
---Pre-process data insert to sql db.
---for now it's mainly used to for parsing lua tables and boolean values.
---It throws when a schema key is required and doesn't exists.
---@param rows tinserted row.
---@param schema table tbl schema with extra info
---@return table pre processed rows
M.pre_insert = function(rows, schema)
local res = {}
rows = u.is_nested(rows) and rows or { rows }
for i, row in ipairs(rows) do
res[i] = u.map(row, function(v, k)
local column_def = schema[k]
a.should_have_column_def(column_def, k, schema)
a.missing_req_key(v, column_def)
local is_json = column_def.type == "luatable" or column_def.type == "json"
return is_json and json.encode(v) or M.sqlvalue(v)
end)
end
return res
end
---Postprocess data queried from a sql db. for now it is mainly used
---to for parsing json values to lua table.
---@param rows tinserted row.
---@param schema table tbl schema
---@return table pre processed rows
---@TODO support boolean values.
M.post_select = function(rows, schema)
local is_nested = u.is_nested(rows)
rows = is_nested and rows or { rows }
for _, row in ipairs(rows) do
for k, v in pairs(row) do
local info = schema[k]
if info then
row[k] = M.luavalue(v, info.type)
else
row[k] = v
end
end
end
return is_nested and rows or rows[1]
end
return M