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
|
if not modules then modules = { } end modules ['util-sql'] = {
version = 1.001,
comment = "companion to m-sql.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
-- todo: templates as table (saves splitting)
-- Of course we could use a library but we don't want another depedency and there is
-- a bit of flux in these libraries. Also, we want the data back in a way that we
-- like.
--
-- This is the first of set of sql related modules that are providing functionality
-- for a web based framework that we use for typesetting (related) services. We're
-- talking of session management, job ticket processing, storage, (xml) file processing
-- and dealing with data from databases (often ambitiously called database publishing).
--
-- There is no generic solution for such services, but from our perspective, as we use
-- context in a regular tds tree (the standard distribution) it makes sense to put shared
-- code in the context distribution. That way we don't need to reinvent wheels every time.
-- We use the template mechanism from util-tpl which inturn is just using the dos cq
-- windows convention of %whatever% variables that I've used for ages.
-- util-sql-imp-client.lua
-- util-sql-imp-library.lua
-- util-sql-imp-swiglib.lua
-- util-sql-imp-lmxsql.lua
-- local sql = require("util-sql")
--
-- local converter = sql.makeconverter {
-- { name = "id", type = "number" },
-- { name = "data",type = "string" },
-- }
--
-- local execute = sql.methods.swiglib.execute
-- -- local execute = sql.methods.library.execute
-- -- local execute = sql.methods.client.execute
-- -- local execute = sql.methods.lmxsql.execute
--
-- result = execute {
-- presets = {
-- host = "localhost",
-- username = "root",
-- password = "test",
-- database = "test",
-- id = "test", -- forces persistent session
-- },
-- template = "select * from `test` where `id` > %criterium% ;",
-- variables = {
-- criterium = 2,
-- },
-- converter = converter
-- }
--
-- inspect(result)
local format, match = string.format, string.match
local random = math.random
local rawset, setmetatable, getmetatable, load, type = rawset, setmetatable, getmetatable, load, type
local P, S, V, C, Cs, Ct, Cc, Cg, Cf, patterns, lpegmatch = lpeg.P, lpeg.S, lpeg.V, lpeg.C, lpeg.Cs, lpeg.Ct, lpeg.Cc, lpeg.Cg, lpeg.Cf, lpeg.patterns, lpeg.match
local concat = table.concat
local osuuid = os.uuid
local osclock = os.clock or os.time
local ostime = os.time
local setmetatableindex = table.setmetatableindex
local trace_sql = false trackers.register("sql.trace", function(v) trace_sql = v end)
local trace_queries = false trackers.register("sql.queries",function(v) trace_queries = v end)
local report_state = logs.reporter("sql")
-- trace_sql = true
-- trace_queries = true
utilities.sql = utilities.sql or { }
local sql = utilities.sql
local replacetemplate = utilities.templates.replace
local loadtemplate = utilities.templates.load
local methods = { }
sql.methods = methods
local helpers = { }
sql.helpers = helpers
local serialize = table.fastserialize
local deserialize = table.deserialize
sql.serialize = serialize
sql.deserialize = deserialize
helpers.serialize = serialize -- bonus
helpers.deserialize = deserialize -- bonus
local defaults = { __index =
{
resultfile = "result.dat",
templatefile = "template.sql",
queryfile = "query.sql",
variables = { },
username = "default",
password = "default",
host = "localhost",
port = 3306,
database = "default",
},
}
setmetatableindex(sql.methods,function(t,k)
report_state("start loading method %a",k)
require("util-sql-imp-"..k)
report_state("loading method %a done",k)
return rawget(t,k)
end)
-- converters
local converters = { }
sql.converters = converters
local function makeconverter(entries,celltemplate,wraptemplate)
local shortcuts = { }
local assignments = { }
local key = false
for i=1,#entries do
local entry = entries[i]
local name = entry.name
local kind = entry.type or entry.kind
local value = format(celltemplate,i,i)
if kind == "boolean" then
assignments[#assignments+1] = format("[%q] = booleanstring(%s),",name,value)
elseif kind == "number" then
assignments[#assignments+1] = format("[%q] = tonumber(%s),",name,value)
elseif type(kind) == "function" then
local c = #converters + 1
converters[c] = kind
shortcuts[#shortcuts+1] = format("local fun_%s = converters[%s]",c,c)
assignments[#assignments+1] = format("[%q] = fun_%s(%s),",name,c,value)
elseif type(kind) == "table" then
local c = #converters + 1
converters[c] = kind
shortcuts[#shortcuts+1] = format("local tab_%s = converters[%s]",c,c)
assignments[#assignments+1] = format("[%q] = tab_%s[%s],",name,#converters,value)
elseif kind == "deserialize" then
assignments[#assignments+1] = format("[%q] = deserialize(%s),",name,value)
elseif kind == "key" then
-- hashed instead of indexed
key = value
elseif kind == "entry" then
-- so we can (efficiently) extend the hashed table
local default = entry.default or ""
if type(default) == "string" then
assignments[#assignments+1] = format("[%q] = %q,",name,default)
else
assignments[#assignments+1] = format("[%q] = %s,",name,tostring(default))
end
else
assignments[#assignments+1] = format("[%q] = %s,",name,value)
end
end
local code = format(wraptemplate,concat(shortcuts,"\n"),key and "{ }" or "data",key or "i",concat(assignments,"\n "))
-- print(code)
local func = load(code)
return func and func()
end
function sql.makeconverter(entries)
local fields = { }
for i=1,#entries do
fields[i] = format("`%s`",entries[i].name)
end
fields = concat(fields, ", ")
local converter = {
fields = fields
}
setmetatableindex(converter, function(t,k)
local sqlmethod = methods[k]
local v = makeconverter(entries,sqlmethod.celltemplate,sqlmethod.wraptemplate)
t[k] = v
return v
end)
return converter, fields
end
-- helper for libraries:
local function validspecification(specification)
local presets = specification.presets
if type(presets) == "string" then
presets = dofile(presets)
end
if type(presets) == "table" then
setmetatable(presets,defaults)
setmetatable(specification,{ __index = presets })
else
setmetatable(specification,defaults)
end
return true
end
helpers.validspecification = validspecification
local whitespace = patterns.whitespace^0
local eol = patterns.eol
local separator = P(";")
local escaped = patterns.escaped
local dquote = patterns.dquote
local squote = patterns.squote
local dsquote = squote * squote
---- quoted = patterns.quoted
local quoted = dquote * (escaped + (1-dquote))^0 * dquote
+ squote * (escaped + dsquote + (1-squote))^0 * squote
local comment = P("--") * (1-eol) / ""
local query = whitespace
* Cs((quoted + comment + 1 - separator)^1 * Cc(";"))
* whitespace
local splitter = Ct(query * (separator * query)^0)
helpers.querysplitter = splitter
-- I will add a bit more checking.
local function validspecification(specification)
local presets = specification.presets
if type(presets) == "string" then
presets = dofile(presets)
end
if type(presets) == "table" then
local m = getmetatable(presets)
if m then
setmetatable(m,defaults)
else
setmetatable(presets,defaults)
end
setmetatable(specification,{ __index = presets })
else
setmetatable(specification,defaults)
end
local templatefile = specification.templatefile or "query"
local queryfile = specification.queryfile or presets.queryfile or file.nameonly(templatefile) .. "-temp.sql"
local resultfile = specification.resultfile or presets.resultfile or file.nameonly(templatefile) .. "-temp.dat"
specification.queryfile = queryfile
specification.resultfile = resultfile
if trace_sql then
report_state("template file: %s",templatefile or "<none>")
report_state("query file: %s",queryfile)
report_state("result file: %s",resultfile)
end
return true
end
local function preparetemplate(specification)
local template = specification.template
if template then
local query = replacetemplate(template,specification.variables,'sql')
if not query then
report_state("error in template: %s",template)
elseif trace_queries then
report_state("query from template: %s",query)
end
return query
end
local templatefile = specification.templatefile
if templatefile then
local query = loadtemplate(templatefile,specification.variables,'sql')
if not query then
report_state("error in template file %a",templatefile)
elseif trace_queries then
report_state("query from template file %a: %s",templatefile,query)
end
return query
end
report_state("no query template or templatefile")
end
helpers.preparetemplate = preparetemplate
-- -- -- we delay setting this -- -- --
local currentmethod
local currentserver
local function firstexecute(...)
local execute = methods[currentmethod].execute
sql.execute = execute
return execute(...)
end
function sql.setmethod(method)
currentmethod = method
sql.execute = firstexecute
end
function sql.setserver(server)
currentserver = server
end
function sql.getmethod()
return currentmethod
end
function sql.getserver()
return currentserver
end
sql.setmethod("library")
sql.setserver("mysql")
-- helper:
function sql.usedatabase(presets,datatable)
local name = datatable or presets.datatable
if name then
local method = presets.method and sql.methods[presets.method] or sql.methods.client
local base = presets.database or "test"
local basename = format("`%s`.`%s`",base,name)
local execute = nil
local m_execute = method.execute
if method.usesfiles then
local queryfile = presets.queryfile or format("%s-temp.sql",name)
local resultfile = presets.resultfile or format("%s-temp.dat",name)
execute = function(specification) -- variables template
if not specification.presets then specification.presets = presets end
if not specification.queryfile then specification.queryfile = queryfile end
if not specification.resultfile then specification.resultfile = queryfile end
return m_execute(specification)
end
else
execute = function(specification) -- variables template
if not specification.presets then specification.presets = presets end
return m_execute(specification)
end
end
local function unpackdata(records,name)
if records then
name = name or "data"
for i=1,#records do
local record = records[i]
local data = record[name]
if data then
record[name] = deserialize(data)
end
end
end
end
return {
presets = preset,
base = base,
name = name,
basename = basename,
execute = execute,
serialize = serialize,
deserialize = deserialize,
unpackdata = unpackdata,
}
else
report_state("missing name in usedatabase specification")
end
end
-- local data = utilities.sql.prepare {
-- templatefile = "test.sql",
-- variables = { },
-- host = "...",
-- username = "...",
-- password = "...",
-- database = "...",
-- }
-- local presets = {
-- host = "...",
-- username = "...",
-- password = "...",
-- database = "...",
-- }
--
-- local data = utilities.sql.prepare {
-- templatefile = "test.sql",
-- variables = { },
-- presets = presets,
-- }
-- local data = utilities.sql.prepare {
-- templatefile = "test.sql",
-- variables = { },
-- presets = dofile(...),
-- }
-- local data = utilities.sql.prepare {
-- templatefile = "test.sql",
-- variables = { },
-- presets = "...",
-- }
-- for i=1,10 do
-- local dummy = uuid() -- else same every time, don't ask
-- end
sql.tokens = {
length = 42, -- but in practice we will reserve some 50 characters
new = function()
return format("%s-%x06",osuuid(),random(0xFFFFF)) -- 36 + 1 + 6 = 42
end,
}
-- -- --
-- local func, code = sql.makeconverter {
-- { name = "a", type = "number" },
-- { name = "b", type = "string" },
-- { name = "c", type = "boolean" },
-- { name = "d", type = { x = "1" } },
-- { name = "e", type = os.fulltime },
-- }
--
-- print(code)
-- -- --
if tex and tex.systemmodes then
local droptable = table.drop
local threshold = 16 * 1024 -- use slower but less memory hungry variant
function sql.prepare(specification,tag)
-- could go into tuc if needed
-- todo: serialize per column
local tag = tag or specification.tag or "last"
local filename = format("%s-sql-result-%s.tuc",tex.jobname,tag)
if tex.systemmodes["first"] then
local data, keys = sql.execute(specification)
if not data then
data = { }
end
if not keys then
keys = { }
end
io.savedata(filename,droptable({ data = data, keys = keys },#keys*#data>threshold))
return data, keys
else
local result = table.load(filename)
return result.data, result.keys
end
end
else
sql.prepare = sql.execute
end
return sql
|