diff options
author | Kim Alvefur <zash@zash.se> | 2016-02-21 19:25:01 +0100 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2016-02-21 19:25:01 +0100 |
commit | 2af32832aeb6ad8b1786f5a81f8bdd2f735e0969 (patch) | |
tree | 755e27db2e69d2a9c4041a7589931d9b2e0a17e2 /util/sql.lua | |
parent | 91b41656897ad4daa8627e6cb4eda68a8c44733a (diff) | |
download | prosody-2af32832aeb6ad8b1786f5a81f8bdd2f735e0969.tar.gz prosody-2af32832aeb6ad8b1786f5a81f8bdd2f735e0969.zip |
util.sql: Raw query debug logging (needs to be explicitly enabled by a plugin)
Diffstat (limited to 'util/sql.lua')
-rw-r--r-- | util/sql.lua | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/util/sql.lua b/util/sql.lua index 2d5e1774..b4d14537 100644 --- a/util/sql.lua +++ b/util/sql.lua @@ -147,6 +147,11 @@ local result_mt = { __index = { rowcount = function(self) return self.__stmt:rowcount(); end; } }; +local function debugquery(where, sql, ...) + local i = 0; local a = {...} + log("debug", "[%s] %s", where, sql:gsub("%?", function () i = i + 1; local v = a[i]; if type(v) == "string" then v = ("%q"):format(v); end return tostring(v); end)); +end + function engine:execute_query(sql, ...) if self.params.driver == "PostgreSQL" then sql = sql:gsub("`", "\""); @@ -172,6 +177,26 @@ engine.insert = engine.execute_update; engine.select = engine.execute_query; engine.delete = engine.execute_update; engine.update = engine.execute_update; +local function debugwrap(name, f) + return function (self, sql, ...) + debugquery(name, sql, ...) + return f(self, sql, ...) + end +end +function engine:debug(enable) + self._debug = enable; + if enable then + engine.insert = debugwrap("insert", engine.execute_update); + engine.select = debugwrap("select", engine.execute_query); + engine.delete = debugwrap("delete", engine.execute_update); + engine.update = debugwrap("update", engine.execute_update); + else + engine.insert = engine.execute_update; + engine.select = engine.execute_query; + engine.delete = engine.execute_update; + engine.update = engine.execute_update; + end +end function engine:_transaction(func, ...) if not self.conn then local ok, err = self:connect(); @@ -221,7 +246,9 @@ function engine:_create_index(index) if index.unique then sql = sql:gsub("^CREATE", "CREATE UNIQUE"); end - --print(sql); + if self._debug then + debugquery("create", sql); + end return self:execute(sql); end function engine:_create_table(table) @@ -252,6 +279,9 @@ function engine:_create_table(table) elseif self.params.driver == "MySQL" then sql = sql:gsub(";$", (" CHARACTER SET '%s' COLLATE '%s_bin';"):format(self.charset, self.charset)); end + if self._debug then + debugquery("create", sql); + end local success,err = self:execute(sql); if not success then return success,err; end for i,v in ipairs(table.__table__) do |