summaryrefslogtreecommitdiff
path: root/tex/context/base/util-sql.lua
diff options
context:
space:
mode:
authorMarius <mariausol@gmail.com>2012-09-04 19:20:28 +0300
committerMarius <mariausol@gmail.com>2012-09-04 19:20:28 +0300
commitedd2b22c9d92fd77d47b5fb6517a230470ec7038 (patch)
treec4d5386d55a53d423cfd6cb4157b6bda2813d8c7 /tex/context/base/util-sql.lua
parente0f42793d14b7571ae6221a15e691d28b3f76d8f (diff)
downloadcontext-edd2b22c9d92fd77d47b5fb6517a230470ec7038.tar.gz
beta 2012.09.04 18:08
Diffstat (limited to 'tex/context/base/util-sql.lua')
-rw-r--r--tex/context/base/util-sql.lua85
1 files changed, 70 insertions, 15 deletions
diff --git a/tex/context/base/util-sql.lua b/tex/context/base/util-sql.lua
index 1e07bfc3c..3daa60d0b 100644
--- a/tex/context/base/util-sql.lua
+++ b/tex/context/base/util-sql.lua
@@ -10,7 +10,7 @@ if not modules then modules = { } end modules ['util-sql'] = {
-- there is a bit of flux in these libraries. Also, we want the data back in
-- a way that we like.
--- buffer template
+-- Todo: buffer templates when files.
local format = string.format
local rawset, setmetatable, loadstring, type = rawset, setmetatable, loadstring, type
@@ -48,7 +48,6 @@ 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
@@ -151,7 +150,7 @@ local function validspecification(specification)
specification.queryfile = queryfile
specification.resultfile = resultfile
if trace_sql then
- report_state("template file: %q",templatefile)
+ report_state("template file: %q",templatefile or "<none>")
report_state("query file: %q",queryfile)
report_state("result file: %q",resultfile)
end
@@ -167,10 +166,12 @@ local function dataprepared(specification)
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
@@ -183,7 +184,6 @@ local function datafetched(specification)
report_state("fetchtime: %.3f sec",osclock()-t) -- not okay under linux
else
os.execute(command)
- -- return os.resultof(command)
end
return true
end
@@ -455,28 +455,83 @@ methods.library = {
-- -- --
-local e_pattern = lpeg.replacer { { '\\"','\\\\""' }, {'"','""'} }
+local e_pattern = lpeg.replacer { { '\\"','\\\\""' }, {'"','""'}, {'\\\n', "\\n" }, {'\\\r', "\\r" }, {'\t', " " } }
local u_pattern = lpeg.replacer { { '\\\\','\\' } }
+local u_pattern = lpeg.replacer { { '\\\\','\\' }, { "\n","\\n" } }
-function sql.escape(str)
- return lpegmatch(e_replace,str)
+-- 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 sql.unescape(str)
- return lpegmatch(u_replace,str)
+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
-function sql.serialize(t)
+-- 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 sql.deserialize(data)
- data = lpegmatch(u_pattern,data)
- data = data and loadstring(data)
- data = data and data()
- return data
+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