diff options
author | Kim Alvefur <zash@zash.se> | 2016-02-22 15:24:20 +0100 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2016-02-22 15:24:20 +0100 |
commit | 82a913c6816be2b0503368afac85b8b20335db0e (patch) | |
tree | 3bb6d5bb05f75122414728e4605bbdb9aacacae0 /util | |
parent | 2675b63e1b14a929277d2ad8bcf6f1ec17187de2 (diff) | |
parent | a1bba9eeab3263061a2b1e1634847b3ed4002e20 (diff) | |
download | prosody-82a913c6816be2b0503368afac85b8b20335db0e.tar.gz prosody-82a913c6816be2b0503368afac85b8b20335db0e.zip |
Merge 0.10->trunk
Diffstat (limited to 'util')
-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 |