Comparison

util/sql.lua @ 7275:187ba2e9c012

util.sql: Don't break out of result retreival loops
author Kim Alvefur <zash@zash.se>
date Sun, 13 Mar 2016 18:31:53 +0100
parent 7272:a23ca90d1984
child 7276:30dfaf36ea6d
comparison
equal deleted inserted replaced
7274:e0727512bb99 7275:187ba2e9c012
297 end 297 end
298 function engine:set_encoding() -- to UTF-8 298 function engine:set_encoding() -- to UTF-8
299 local driver = self.params.driver; 299 local driver = self.params.driver;
300 if driver == "SQLite3" then 300 if driver == "SQLite3" then
301 return self:transaction(function() 301 return self:transaction(function()
302 if self:select"PRAGMA encoding;"()[1] == "UTF-8" then 302 for encoding in self:select"PRAGMA encoding;" do
303 self.charset = "utf8"; 303 if encoding[1] == "UTF-8" then
304 self.charset = "utf8";
305 end
304 end 306 end
305 end); 307 end);
306 end 308 end
307 local set_names_query = "SET NAMES '%s';" 309 local set_names_query = "SET NAMES '%s';"
308 local charset = "utf8"; 310 local charset = "utf8";
309 if driver == "MySQL" then 311 if driver == "MySQL" then
310 local ok, charsets = self:transaction(function() 312 self:transaction(function()
311 return self:select"SELECT `CHARACTER_SET_NAME` FROM `information_schema`.`CHARACTER_SETS` WHERE `CHARACTER_SET_NAME` LIKE 'utf8%' ORDER BY MAXLEN DESC LIMIT 1;"; 313 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
314 charset = row and row[1] or charset;
315 end
312 end); 316 end);
313 local row = ok and charsets();
314 charset = row and row[1] or charset;
315 set_names_query = set_names_query:gsub(";$", (" COLLATE '%s';"):format(charset.."_bin")); 317 set_names_query = set_names_query:gsub(";$", (" COLLATE '%s';"):format(charset.."_bin"));
316 end 318 end
317 self.charset = charset; 319 self.charset = charset;
318 log("debug", "Using encoding '%s' for database connection", charset); 320 log("debug", "Using encoding '%s' for database connection", charset);
319 local ok, err = self:transaction(function() return self:execute(set_names_query:format(charset)); end); 321 local ok, err = self:transaction(function() return self:execute(set_names_query:format(charset)); end);
323 325
324 if driver == "MySQL" then 326 if driver == "MySQL" then
325 local ok, actual_charset = self:transaction(function () 327 local ok, actual_charset = self:transaction(function ()
326 return self:select"SHOW SESSION VARIABLES LIKE 'character_set_client'"; 328 return self:select"SHOW SESSION VARIABLES LIKE 'character_set_client'";
327 end); 329 end);
330 local charset_ok;
328 for row in actual_charset do 331 for row in actual_charset do
329 if row[2] ~= charset then 332 if row[2] ~= charset then
330 log("error", "MySQL %s is actually %q (expected %q)", row[1], row[2], charset); 333 log("error", "MySQL %s is actually %q (expected %q)", row[1], row[2], charset);
331 return false, "Failed to set connection encoding"; 334 charset_ok = false;
332 end 335 end
336 end
337 if not charset_ok then
338 return false, "Failed to set connection encoding";
333 end 339 end
334 end 340 end
335 341
336 return true; 342 return true;
337 end 343 end