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
|
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.
-- buffer template
local format = string.format
local rawset, setmetatable, loadstring = rawset, setmetatable, loadstring
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%"]],
-- mysql = [[mysql --user="%username%" --password="%password%" --host="%host%" --port=%port% --database="%database%" < "%queryfile%"]],
}
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)
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)
return true
else
-- maybe push an error
os.remove(specification.queryfile)
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)
-- return os.resultof(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
-- todo: new, etc
function sql.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
function sql.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.splitdata = splitdata
-- -- --
-- 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 { { '\\"','\\\\""' }, {'"','""'} }
local u_pattern = lpeg.replacer { { '\\\\','\\' } }
function sql.escape(str)
return lpegmatch(e_replace,str)
end
function sql.unescape(str)
return lpegmatch(u_replace,str)
end
function sql.serialize(t)
return lpegmatch(e_pattern,fastserialize(t,"return"))
end
function sql.deserialize(data)
data = lpegmatch(u_pattern,data)
data = data and loadstring(data)
data = data and data()
return data
end
-- -- --
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
|