diff options
Diffstat (limited to 'tex/context/base/mkiv/util-sql-loggers.lua')
-rw-r--r-- | tex/context/base/mkiv/util-sql-loggers.lua | 96 |
1 files changed, 69 insertions, 27 deletions
diff --git a/tex/context/base/mkiv/util-sql-loggers.lua b/tex/context/base/mkiv/util-sql-loggers.lua index ceb1ff75c..4473f8971 100644 --- a/tex/context/base/mkiv/util-sql-loggers.lua +++ b/tex/context/base/mkiv/util-sql-loggers.lua @@ -65,12 +65,21 @@ local template = [[ DEFAULT CHARSET = utf8 ; ]] +local sqlite_template = [[ + CREATE TABLE IF NOT EXISTS %basename% ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT, + `time` INTEGER NOT NULL, + `type` INTEGER NOT NULL, + `action` TEXT NOT NULL, + `data` TEXT + ) ; +]] + function loggers.createdb(presets,datatable) local db = checkeddb(presets,datatable) - db.execute { - template = template, + template = db.usedmethod == "sqlite" and sqlite_template or template, variables = { basename = db.basename, }, @@ -115,7 +124,11 @@ local template =[[ ) ; ]] -function loggers.save(db,data) -- beware, we pass type and action in the data (saves a table) +-- beware, when we either pass a dat afield explicitly or we're using +-- a flat table and then nill type and action in the data (which +-- saves a table) + +function loggers.save(db,data) if data then @@ -123,8 +136,16 @@ function loggers.save(db,data) -- beware, we pass type and action in the data (s local kind = totype[data.type] local action = data.action or "unknown" - data.type = nil - data.action = nil + local extra = data.data + + if extra then + -- we have a dedicated data table + data = extra + else + -- we have a flat table + data.type = nil + data.action = nil + end db.execute { template = template, @@ -141,28 +162,49 @@ function loggers.save(db,data) -- beware, we pass type and action in the data (s 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 =[[ + DELETE FROM %basename% %WHERE% ; +]] + +function loggers.cleanup(db,specification) + + specification = specification or { } + + local today = os.date("*t") + local before = specification.before or today + local where = { } + + if type(before) == "number" then + before = os.date(before) + end + + before = os.time { + day = before.day or today.day, + month = before.month or today.month, + year = before.year or today.year, + hour = before.hour or 0, + minute = before.minute or 0, + second = before.second or 0, + isdst = true, + } + + where[#where+1] = format("`time` < %s",before) + + db.execute { + template = template, + variables = { + basename = db.basename, + WHERE = format("WHERE\n%s",concat(where," AND ")), + }, + } + + if db.usedmethod == "sqlite" then + db.execute { + template = "VACUUM ;", + } + end + +end local template_nop =[[ SELECT |