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
|
if not modules then modules = { } end modules ['util-sql-loggers'] = {
version = 1.001,
comment = "companion to lmx-*",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
-- This is experimental code and currently part of the base installation simply
-- because it's easier to dirtribute this way. Eventually it will be documented
-- and the related scripts will show up as well.
local tonumber = tonumber
local format = string.format
local concat = table.concat
local ostime, uuid, osfulltime = os.time, os.uuid, os.fulltime
local random = math.random
local sql = utilities.sql
local loggers = { }
sql.loggers = loggers
local trace_sql = false trackers.register("sql.loggers.trace", function(v) trace_sql = v end)
local report = logs.reporter("sql","loggers")
loggers.newtoken = sql.tokens.new
local makeconverter = sql.makeconverter
local function checkeddb(presets,datatable)
return sql.usedatabase(presets,datatable or presets.datatable or "loggers")
end
loggers.usedb = checkeddb
local totype = {
["error"] = 1, [1] = 1, ["1"] = 1,
["warning"] = 2, [2] = 2, ["2"] = 2,
["debug"] = 3, [3] = 3, ["3"] = 3,
["info"] = 4, [4] = 4, ["4"] = 4,
}
local fromtype = {
["error"] = "error", [1] = "error", ["1"] = "error",
["warning"] = "warning", [2] = "warning", ["2"] = "warning",
["debug"] = "debug", [3] = "debug", ["3"] = "debug",
["info"] = "info", [4] = "info", ["4"] = "info",
}
table.setmetatableindex(totype, function() return 4 end)
table.setmetatableindex(fromtype,function() return "info" end)
loggers.totype = totype
loggers.fromtype = fromtype
local template =[[
CREATE TABLE IF NOT EXISTS %basename% (
`id` int(11) NOT NULL AUTO_INCREMENT,
`time` int(11) NOT NULL,
`type` int(11) NOT NULL,
`action` varchar(15) NOT NULL,
`data` longtext,
PRIMARY KEY (`id`),
UNIQUE KEY `id_unique_key` (`id`)
)
DEFAULT CHARSET = utf8 ;
]]
function loggers.createdb(presets,datatable)
local db = checkeddb(presets,datatable)
db.execute {
template = template,
variables = {
basename = db.basename,
},
}
report("datatable %a created in %a",db.name,db.base)
return db
end
local template =[[
DROP TABLE IF EXISTS %basename% ;
]]
function loggers.deletedb(presets,datatable)
local db = checkeddb(presets,datatable)
db.execute {
template = template,
variables = {
basename = db.basename,
},
}
report("datatable %a removed in %a",db.name,db.base)
end
local template =[[
INSERT INTO %basename% (
`time`,
`type`,
`action`,
`data`
) VALUES (
%time%,
%type%,
'%action%',
'%[data]%'
) ;
]]
function loggers.save(db,data) -- beware, we pass type and action in the data (saves a table)
if data then
local time = ostime()
local kind = totype[data.type]
local action = data.action or "unknown"
data.type = nil
data.action = nil
db.execute {
template = template,
variables = {
basename = db.basename,
time = ostime(),
type = kind,
action = action,
data = data and db.serialize(data,"return") or "",
},
}
end
end
-- local template =[[
-- REMOVE FROM
-- %basename%
-- WHERE
-- `token` = '%token%' ;
-- ]]
--
-- function loggers.remove(db,token)
--
-- db.execute {
-- template = template,
-- variables = {
-- basename = db.basename,
-- token = token,
-- },
-- }
--
-- if trace_sql then
-- report("removed: %s",token)
-- end
--
-- end
local template_nop =[[
SELECT
`time`,
`type`,
`action`,
`data`
FROM
%basename%
ORDER BY
`time`, `type`, `action`
DESC LIMIT
%limit% ;
]]
local template_yes =[[
SELECT
`time`,
`type`,
`action`,
`data`
FROM
%basename%
%WHERE%
ORDER BY
`time`, `type`, `action`
DESC LIMIT
%limit% ;
]]
local converter = makeconverter {
-- { name = "time", type = os.localtime },
{ name = "time", type = "number" },
{ name = "type", type = fromtype },
{ name = "action", type = "string" },
{ name = "data", type = "deserialize" },
}
function loggers.collect(db,specification)
specification = specification or { }
local start = specification.start
local stop = specification.stop
local limit = specification.limit or 100
local kind = specification.type
local action = specification.action
local filtered = start or stop
local where = { }
if filtered then
local today = os.date("*t")
if type(start) ~= "table" then
start = { }
end
start = os.time {
day = start.day or today.day,
month = start.month or today.month,
year = start.year or today.year,
hour = start.hour or 0,
minute = start.minute or 0,
second = start.second or 0,
isdst = true,
}
if type(stop) ~= "table" then
stop = { }
end
stop = os.time {
day = stop.day or today.day,
month = stop.month or today.month,
year = stop.year or today.year,
hour = stop.hour or 24,
minute = stop.minute or 0,
second = stop.second or 0,
isdst = true,
}
-- report("filter: %s => %s",start,stop)
where[#where+1] = format("`time` BETWEEN %s AND %s",start,stop)
end
if kind then
where[#where+1] = format("`type` = %s",totype[kind])
end
if action then
where[#where+1] = format("`action` = '%s'",action)
end
local records = db.execute {
template = filtered and template_yes or template_nop,
converter = converter,
variables = {
basename = db.basename,
limit = limit,
WHERE = #where > 0 and format("WHERE\n%s",concat(where," AND ")) or "",
},
}
if trace_sql then
report("collected: %s loggers",#records)
end
return records, keys
end
|