aboutsummaryrefslogtreecommitdiffstats
path: root/util/sql.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2013-10-29 11:42:55 +0100
committerKim Alvefur <zash@zash.se>2013-10-29 11:42:55 +0100
commite20e010643e56b1f1a2a86146d09db84b170d1d6 (patch)
tree1a08554eba2aa185ebb83a3e6001057df585a0d6 /util/sql.lua
parent4c4a45a987e4687deba06719036b72966a6085ac (diff)
downloadprosody-e20e010643e56b1f1a2a86146d09db84b170d1d6.tar.gz
prosody-e20e010643e56b1f1a2a86146d09db84b170d1d6.zip
util.sql: Find out if MySQL supports utf8mb4 and use that
Diffstat (limited to 'util/sql.lua')
-rw-r--r--util/sql.lua17
1 files changed, 11 insertions, 6 deletions
diff --git a/util/sql.lua b/util/sql.lua
index cab1bcec..4e63bed7 100644
--- a/util/sql.lua
+++ b/util/sql.lua
@@ -291,14 +291,19 @@ function engine:_create_table(table)
end
function engine:set_encoding() -- to UTF-8
if self.params.driver == "SQLite3" then return end
- local set_names_query = "SET NAMES 'utf8';";
- if self.params.driver == "MySQL" then
+ local driver = self.params.driver;
+ local set_names_query = "SET NAMES '%s';"
+ local charset = "utf8";
+ if driver == "MySQL" then
set_names_query = set_names_query:gsub(";$", " COLLATE 'utf8_bin';");
+ local ok, charsets = self:transaction(function()
+ return self:select"SELECT `CHARACTER_SET_NAME` FROM `CHARACTER_SETS` WHERE `CHARACTER_SET_NAME` LIKE 'utf8%' ORDER BY MAXLEN DESC LIMIT 1;";
+ end);
+ local row = ok and charsets();
+ charset = row and row[1] or charset;
end
- local success,err = engine:transaction(function() return engine:execute(set_names_query); end);
- if not success then
- log("error", "Failed to set database connection encoding to UTF8: %s", err);
- end
+ self.charset = charset;
+ return self:transaction(function() return engine:execute(set_names_query:format(charset)); end);
end
local engine_mt = { __index = engine };