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
commita0d18144edee980a55660b4c9dfe34462f1e1d17 (patch)
tree1a08554eba2aa185ebb83a3e6001057df585a0d6 /util/sql.lua
parent22be28318711e0846c9a00ef3a308119f1f3f1b1 (diff)
downloadprosody-a0d18144edee980a55660b4c9dfe34462f1e1d17.tar.gz
prosody-a0d18144edee980a55660b4c9dfe34462f1e1d17.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 };