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
|
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"
}
-- 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.
-- Todo: buffer templates when files.
local format = string.format
local rawset, setmetatable, loadstring, type = rawset, setmetatable, loadstring, 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 osclock = os.clock or os.time
local fastserialize = table.fastserialize
local lpegmatch = lpeg.match
local trace_sql = false trackers.register("sql.trace",function(v) trace_sql = v end)
local report_state = logs.reporter("sql")
utilities.sql = utilities.sql or { }
local sql = utilities.sql
local replacetemplate = utilities.templates.replace
local loadtemplate = utilities.templates.load
local defaults = { __index =
{
resultfile = "result.dat",
templatefile = "template.sql",
queryfile = "query.sql",
variables = { },
username = "default",
password = "default",
host = "localhost",
port = 3306,
database = "default",
},
}
local engine = "mysql"
local runners = { -- --defaults-extra-file="%inifile"
mysql = [[mysql --user="%username%" --password="%password%" --host="%host%" --port=%port% --database="%database%" < "%queryfile%" > "%resultfile%"]],
}
sql.runners = runners
-- Experiments with an p/action demonstrated that there is not much gain. We could do a runtime
-- capture but creating all the small tables is not faster and it doesn't work well anyway.
local separator = P("\t")
local newline = patterns.newline
local entry = C((1-separator-newline)^0) -- C 10% faster than Cs
local empty = Cc("")
local getfirst = Ct( entry * (separator * (entry+empty))^0) + newline
local skipfirst = (1-newline)^1 * newline
local getfirstline = C((1-newline)^0)
local cache = { }
local function splitdata(data) -- todo: hash on first line
if data == "" then
if trace_sql then
report_state("no data")
end
return { }, { }
end
local first = lpegmatch(getfirstline,data)
if not first then
if trace_sql then
report_state("no data")
end
return { }, { }
end
local p = cache[first]
if p then
-- report_state("reusing: %s",first)
local entries = lpegmatch(p.parser,data)
return entries or { }, p.keys
elseif p == false then
return { }, { }
elseif p == nil then
local keys = lpegmatch(getfirst,first) or { }
if #keys == 0 then
if trace_sql then
report_state("no banner")
end
cache[first] = false
return { }, { }
end
-- quite generic, could be a helper
local n = #keys
if n == 0 then
report_state("no fields")
cache[first] = false
return { }, { }
end
if n == 1 then
local key = keys[1]
if trace_sql then
report_state("one field with name",key)
end
p = Cg(Cc(key) * entry)
else
for i=1,n do
local key = keys[i]
if trace_sql then
report_state("field %s has name %q",i,key)
end
local s = Cg(Cc(key) * entry)
if p then
p = p * separator * s
else
p = s
end
end
end
p = Cf(Ct("") * p,rawset) * newline^1
p = skipfirst * Ct(p^0)
cache[first] = { parser = p, keys = keys }
local entries = lpegmatch(p,data)
return entries or { }, keys
end
end
-- 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
setmetatable(presets,defaults)
setmetatable(specification,{ __index = presets })
else
setmetatable(specification,defaults)
end
local templatefile = specification.templatefile
local queryfile = specification.queryfile or file.nameonly(templatefile) .. "-temp.sql"
local resultfile = specification.resultfile or file.nameonly(templatefile) .. "-temp.dat"
specification.queryfile = queryfile
specification.resultfile = resultfile
if trace_sql then
report_state("template file: %q",templatefile or "<none>")
report_state("query file: %q",queryfile)
report_state("result file: %q",resultfile)
end
return true
end
local function dataprepared(specification)
local query = false
if specification.template then
query = replacetemplate(specification.template,specification.variables)
elseif specification.templatefile then
query = loadtemplate(specification.templatefile,specification.variables)
end
if query then
io.savedata(specification.queryfile,query)
os.remove(specification.resultfile)
return true
else
-- maybe push an error
os.remove(specification.queryfile)
os.remove(specification.resultfile)
end
end
local function datafetched(specification)
local command = replacetemplate(runners[engine],specification)
if trace_sql then
local t = osclock()
report_state("command: %s",command)
os.execute(command)
report_state("fetchtime: %.3f sec",osclock()-t) -- not okay under linux
else
os.execute(command)
end
return true
end
local function dataloaded(specification)
if trace_sql then
local t = osclock()
local data = io.loaddata(specification.resultfile) or ""
report_state("datasize: %.3f MB",#data/1024/1024)
report_state("loadtime: %.3f sec",osclock()-t)
return data
else
return io.loaddata(specification.resultfile) or ""
end
end
local function dataconverted(data)
if trace_sql then
local t = osclock()
local data, keys = splitdata(data)
report_state("converttime: %.3f",osclock()-t)
report_state("keys: %s ",#keys)
report_state("entries: %s ",#data)
return data, keys
else
return splitdata(data)
end
end
sql.splitdata = splitdata
local methods = { }
sql.methods = methods
-- todo: new, etc
local function fetch(specification)
if trace_sql then
report_state("fetching")
end
if not validspecification(specification) then
report("error in specification")
return
end
if not dataprepared(specification) then
report("error in preparation")
return
end
if not datafetched(specification) then
report("error in fetching")
return
end
local data = dataloaded(specification)
-- local data = datafetched(specification)
if not data then
report("error in loading")
return
end
local data, keys = dataconverted(data)
if not data then
report("error in converting")
return
end
return data, keys
end
-- local function reuse(specification)
-- if trace_sql then
-- report_state("reusing")
-- end
-- if not validspecification(specification) then
-- report("error in specification")
-- return
-- end
-- local data = dataloaded(specification)
-- if not data then
-- report("error in loading")
-- return
-- end
-- local data, keys = dataconverted(data)
-- if not data then
-- report("error in converting")
-- return
-- end
-- return data, keys
-- end
sql.fetch = fetch
methods.client = {
fetch = fetch,
}
local mysql = nil
local cache = { }
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
local function dataprepared(specification)
local query = false
if specification.template then
query = replacetemplate(specification.template,specification.variables)
elseif specification.templatefile then
query = loadtemplate(specification.templatefile,specification.variables)
end
if query then
return query
end
end
local function connect(session,specification)
return session:connect(
specification.database or "",
specification.username or "",
specification.password or "",
specification.host or "",
specification.port
)
end
local function datafetched(specification,query)
local id = specification.id
local session, connection
-- id = nil
if id then
local c = cache[id]
if c then
session = c.session
connection = c.connection
end
if not connection then
session = mysql()
connection = connect(session,specification)
cache[id] = { session = session, connection = connection }
end
else
session = mysql()
connection = connect(session,specification)
end
if not connection then
return { }, { }
end
local result, message = connection:execute(query)
if not result and id then
if session then
session:close()
end
if connection then
connection:close()
end
session = mysql() -- maybe not needed
connection = connect(session,specification)
cache[id] = { session = session, connection = connection }
result, message = connection:execute(query)
end
local data, keys
if result and type(result) ~= "number" then
keys = result:getcolnames()
if keys then
local n = result:numrows() or 0
if n == 0 then
data = { }
elseif n == 1 then
data = { result:fetch({},"a") }
else
data = { }
for i=1,n do
data[i] = result:fetch({},"a")
end
end
end
result:close()
elseif message then
report_state("message %s",message)
end
if not keys then
keys = { }
end
if not data then
data = { }
end
if not id then
connection:close()
session:close()
end
return data, keys
end
local function fetch(specification)
if not mysql then
local lib = require("luasql.mysql")
if lib then
mysql = lib.mysql
else
report_state("error in loading luasql.mysql")
end
end
if trace_sql then
report_state("fetching")
end
if not validspecification(specification) then
report("error in specification")
return
end
local query = dataprepared(specification)
if not query then
report("error in preparation")
return
end
local data, keys = datafetched(specification,query)
if not data then
report("error in fetching")
return
end
return data, keys
end
methods.library = {
fetch = fetch,
}
-- -- --
-- 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 = "...",
-- }
-- -- --
local e_pattern = lpeg.replacer { { '\\"','\\\\""' }, {'"','""'}, {'\\\n', "\\n" }, {'\\\r', "\\r" }, {'\t', " " } }
local u_pattern = lpeg.replacer { { '\\\\','\\' } }
local u_pattern = lpeg.replacer { { '\\\\','\\' }, { "\n","\\n" } }
-- library:
function methods.library.serialize(t)
local str = fastserialize(t,"return")
local escaped = lpegmatch(e_pattern,str)
-- print("LIBRARY PUT STR",str)
-- print("LIBRARY PUT ESC",escaped)
return escaped
end
function methods.library.deserialize(str)
local unescaped = lpegmatch(u_pattern,str)
-- print("LIBRARY GET STR",str)
-- print("LIBRARY GET UES",unescaped)
if not unescaped then
return
end
local code = loadstring(unescaped)
-- print("INVALID CODE")
if not code then
return
end
code = code()
-- table.print(code)
if not code then
return
end
return code
end
-- client
local e_pattern = lpeg.replacer { { '\\"','\\\\""' }, {'"','""'}, {'\\\n', "\\n" }, {'\\\r', "\\r" } }
local u_pattern = lpeg.replacer { { '\\\\','\\' } }
function methods.client.serialize(t)
return lpegmatch(e_pattern,fastserialize(t,"return"))
end
function methods.client.deserialize(str)
local unescaped = lpegmatch(u_pattern,str)
if not unescaped then
return
end
local code = loadstring(unescaped)
if not code then
return
end
code = code()
if not code then
return
end
return code
end
sql.serialize = methods.client.serialize
sql.deserialize = methods.client.deserialize
function sql.escape(str)
return lpegmatch(e_pattern,str)
end
function sql.unescape(str)
return lpegmatch(u_pattern,str)
end
-- local s = sql.serialize { a = 1, b = { 4, { 5, 6 } }, c = { d = 7, e = 'f"g\nh' } }
-- local u = sql.unescape(s)
-- local t = sql.deserialize(s)
-- inspect(s)
-- inspect(u)
-- inspect(t)
-- -- --
if tex and tex.systemmodes then
function sql.prepare(specification)
if tex.systemmodes["first"] then
return sql.fetch(specification)
else
return sql.reuse(specification)
end
end
else
sql.prepare = sql.fetch
end
|