Comparison

util/sql.lua @ 6760:e45a58c72609

util.sql: Use appropriate collation for the chosen character set - fixes MySQL silently ignoring our SET NAMES command when we use utf8mb4
author Matthew Wild <mwild1@gmail.com>
date Wed, 08 Jul 2015 15:06:20 +0100
parent 6759:fb952032f83e
child 6762:ea43a5af31ca
comparison
equal deleted inserted replaced
6759:fb952032f83e 6760:e45a58c72609
266 end); 266 end);
267 end 267 end
268 local set_names_query = "SET NAMES '%s';" 268 local set_names_query = "SET NAMES '%s';"
269 local charset = "utf8"; 269 local charset = "utf8";
270 if driver == "MySQL" then 270 if driver == "MySQL" then
271 set_names_query = set_names_query:gsub(";$", " COLLATE 'utf8_bin';");
272 local ok, charsets = self:transaction(function() 271 local ok, charsets = self:transaction(function()
273 return self:select"SELECT `CHARACTER_SET_NAME` FROM `information_schema`.`CHARACTER_SETS` WHERE `CHARACTER_SET_NAME` LIKE 'utf8%' ORDER BY MAXLEN DESC LIMIT 1;"; 272 return self:select"SELECT `CHARACTER_SET_NAME` FROM `information_schema`.`CHARACTER_SETS` WHERE `CHARACTER_SET_NAME` LIKE 'utf8%' ORDER BY MAXLEN DESC LIMIT 1;";
274 end); 273 end);
275 local row = ok and charsets(); 274 local row = ok and charsets();
276 charset = row and row[1] or charset; 275 charset = row and row[1] or charset;
276 set_names_query = set_names_query:gsub(";$", (" COLLATE '%s';"):format(charset.."_bin"));
277 end 277 end
278 self.charset = charset; 278 self.charset = charset;
279 return self:transaction(function() return self:execute(set_names_query:format(charset)); end); 279 return self:transaction(function() return self:execute(set_names_query:format(charset)); end);
280 end 280 end
281 local engine_mt = { __index = engine }; 281 local engine_mt = { __index = engine };