aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2016-03-14 14:20:37 +0100
committerKim Alvefur <zash@zash.se>2016-03-14 14:20:37 +0100
commit3ec49d31afc719ec2bec5dee640e41c11f52fe62 (patch)
treed4ea3062e7b9fa8bd642485d15f1b745082f621f /util
parent17c850ba33a950b7a4482a6c656cef734768bf8a (diff)
parentd1578059cb3152347cddb265cf4b224741dc3997 (diff)
downloadprosody-3ec49d31afc719ec2bec5dee640e41c11f52fe62.tar.gz
prosody-3ec49d31afc719ec2bec5dee640e41c11f52fe62.zip
Merge 0.10->trunk
Diffstat (limited to 'util')
-rw-r--r--util/sql.lua40
1 files changed, 25 insertions, 15 deletions
diff --git a/util/sql.lua b/util/sql.lua
index 9981ac3c..dcf665fb 100644
--- a/util/sql.lua
+++ b/util/sql.lua
@@ -25,8 +25,8 @@ local function is_column(x) return getmetatable(x)==column_mt; end
local function is_index(x) return getmetatable(x)==index_mt; end
local function is_table(x) return getmetatable(x)==table_mt; end
local function is_query(x) return getmetatable(x)==query_mt; end
-local function Integer(n) return "Integer()" end
-local function String(n) return "String()" end
+local function Integer() return "Integer()" end
+local function String() return "String()" end
local function Column(definition)
return setmetatable(definition, column_mt);
@@ -124,6 +124,14 @@ end
function engine:onconnect()
-- Override from create_engine()
end
+
+function engine:prepquery(sql)
+ if self.params.driver == "PostgreSQL" then
+ sql = sql:gsub("`", "\"");
+ end
+ return sql;
+end
+
function engine:execute(sql, ...)
local success, err = self:connect();
if not success then return success, err; end
@@ -153,17 +161,13 @@ local function debugquery(where, sql, ...)
end
function engine:execute_query(sql, ...)
- if self.params.driver == "PostgreSQL" then
- sql = sql:gsub("`", "\"");
- end
+ sql = self:prepquery(sql);
local stmt = assert(self.conn:prepare(sql));
assert(stmt:execute(...));
return stmt:rows();
end
function engine:execute_update(sql, ...)
- if self.params.driver == "PostgreSQL" then
- sql = sql:gsub("`", "\"");
- end
+ sql = self:prepquery(sql);
local prepared = self.prepared;
local stmt = prepared[sql];
if not stmt then
@@ -295,19 +299,21 @@ function engine:set_encoding() -- to UTF-8
local driver = self.params.driver;
if driver == "SQLite3" then
return self:transaction(function()
- if self:select"PRAGMA encoding;"()[1] == "UTF-8" then
- self.charset = "utf8";
+ for encoding in self:select"PRAGMA encoding;" do
+ if encoding[1] == "UTF-8" then
+ self.charset = "utf8";
+ end
end
end);
end
local set_names_query = "SET NAMES '%s';"
local charset = "utf8";
if driver == "MySQL" then
- local ok, charsets = self:transaction(function()
- return self:select"SELECT `CHARACTER_SET_NAME` FROM `information_schema`.`CHARACTER_SETS` WHERE `CHARACTER_SET_NAME` LIKE 'utf8%' ORDER BY MAXLEN DESC LIMIT 1;";
+ self:transaction(function()
+ for row in self:select"SELECT `CHARACTER_SET_NAME` FROM `information_schema`.`CHARACTER_SETS` WHERE `CHARACTER_SET_NAME` LIKE 'utf8%' ORDER BY MAXLEN DESC LIMIT 1;" do
+ charset = row and row[1] or charset;
+ end
end);
- local row = ok and charsets();
- charset = row and row[1] or charset;
set_names_query = set_names_query:gsub(";$", (" COLLATE '%s';"):format(charset.."_bin"));
end
self.charset = charset;
@@ -321,12 +327,16 @@ function engine:set_encoding() -- to UTF-8
local ok, actual_charset = self:transaction(function ()
return self:select"SHOW SESSION VARIABLES LIKE 'character_set_client'";
end);
+ local charset_ok;
for row in actual_charset do
if row[2] ~= charset then
log("error", "MySQL %s is actually %q (expected %q)", row[1], row[2], charset);
- return false, "Failed to set connection encoding";
+ charset_ok = false;
end
end
+ if not charset_ok then
+ return false, "Failed to set connection encoding";
+ end
end
return true;