From 137f76396600f44532054db2829d1e7ae2fa2cb6 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Tue, 7 Jul 2015 17:43:14 +0100 Subject: util.sql: Allow onconnect callback to fail connection to the DB by returning false, err --- util/sql.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'util') diff --git a/util/sql.lua b/util/sql.lua index 92032f43..dedef435 100644 --- a/util/sql.lua +++ b/util/sql.lua @@ -111,7 +111,10 @@ function engine:connect() self.conn = dbh; self.prepared = {}; self:set_encoding(); - self:onconnect(); + local ok, err = self:onconnect(); + if ok == false then + return ok, err; + end return true; end function engine:onconnect() -- cgit v1.2.3 From 7a4ec3c57c8e38c49f6527ea00abea9a5dd3ada6 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 8 Jul 2015 15:04:23 +0100 Subject: util.sql: Create table with same charset as the charset we selected for our connection, also use corresponding _bin collation --- util/sql.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util') diff --git a/util/sql.lua b/util/sql.lua index dedef435..562ac709 100644 --- a/util/sql.lua +++ b/util/sql.lua @@ -245,7 +245,7 @@ function engine:_create_table(table) if self.params.driver == "PostgreSQL" then sql = sql:gsub("`", "\""); elseif self.params.driver == "MySQL" then - sql = sql:gsub(";$", " CHARACTER SET 'utf8' COLLATE 'utf8_bin';"); + sql = sql:gsub(";$", (" CHARACTER SET '%s' COLLATE '%s_bin';"):format(self.charset, self.charset)); end local success,err = self:execute(sql); if not success then return success,err; end -- cgit v1.2.3 From eeb1c1be540af288105580944eb3664b20abfd21 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 8 Jul 2015 15:06:20 +0100 Subject: util.sql: Use appropriate collation for the chosen character set - fixes MySQL silently ignoring our SET NAMES command when we use utf8mb4 --- util/sql.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util') diff --git a/util/sql.lua b/util/sql.lua index 562ac709..fc1191f9 100644 --- a/util/sql.lua +++ b/util/sql.lua @@ -268,12 +268,12 @@ function engine:set_encoding() -- to UTF-8 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 `information_schema`.`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; + set_names_query = set_names_query:gsub(";$", (" COLLATE '%s';"):format(charset.."_bin")); end self.charset = charset; return self:transaction(function() return self:execute(set_names_query:format(charset)); end); -- cgit v1.2.3 From 9f5bd334c59da59813016d3f98cb31a5c144fcda Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 8 Jul 2015 15:10:25 +0100 Subject: util.sql: Return failure if set_encoding() fails --- util/sql.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'util') diff --git a/util/sql.lua b/util/sql.lua index fc1191f9..d0da9302 100644 --- a/util/sql.lua +++ b/util/sql.lua @@ -110,7 +110,10 @@ function engine:connect() dbh:autocommit(false); -- don't commit automatically self.conn = dbh; self.prepared = {}; - self:set_encoding(); + local ok, err = self:set_encoding(); + if not ok then + return ok, err; + end local ok, err = self:onconnect(); if ok == false then return ok, err; -- cgit v1.2.3 From eed81c2f941254646a0e77e6ca3f9a7b95710772 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 8 Jul 2015 15:14:24 +0100 Subject: util.sql: Make set_encoding() return failure of SET NAMES --- util/sql.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'util') diff --git a/util/sql.lua b/util/sql.lua index d0da9302..6622ad56 100644 --- a/util/sql.lua +++ b/util/sql.lua @@ -279,7 +279,13 @@ function engine:set_encoding() -- to UTF-8 set_names_query = set_names_query:gsub(";$", (" COLLATE '%s';"):format(charset.."_bin")); end self.charset = charset; - return self:transaction(function() return self:execute(set_names_query:format(charset)); end); + log("debug", "Using encoding '%s' for database connection", charset); + local ok, err = self:transaction(function() return self:execute(set_names_query:format(charset)); end); + if not ok then + return ok, err; + end + + return true; end local engine_mt = { __index = engine }; -- cgit v1.2.3 From 5a90847cd7b5df458a34000b187dcd11522940ea Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 8 Jul 2015 15:15:32 +0100 Subject: util.sql: Log debug message when connecting to database --- util/sql.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'util') diff --git a/util/sql.lua b/util/sql.lua index 6622ad56..037dbc76 100644 --- a/util/sql.lua +++ b/util/sql.lua @@ -101,6 +101,7 @@ function engine:connect() local params = self.params; assert(params.driver, "no driver") + log("error", "Connecting to [%s] %s...", params.driver, params.database); local dbh, err = DBI.Connect( params.driver, params.database, params.username, params.password, -- cgit v1.2.3 From c23293f38250ac5f85a813452a6d4973ea94b5fe Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 8 Jul 2015 15:16:17 +0100 Subject: util.sql: Fix log level of debug message --- util/sql.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util') diff --git a/util/sql.lua b/util/sql.lua index 037dbc76..693b67d9 100644 --- a/util/sql.lua +++ b/util/sql.lua @@ -101,7 +101,7 @@ function engine:connect() local params = self.params; assert(params.driver, "no driver") - log("error", "Connecting to [%s] %s...", params.driver, params.database); + log("debug", "Connecting to [%s] %s...", params.driver, params.database); local dbh, err = DBI.Connect( params.driver, params.database, params.username, params.password, -- cgit v1.2.3 From 376dd433962a1a897a239d06dbc91d3e03290fe3 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 8 Jul 2015 15:25:42 +0100 Subject: util.sql: Add safety check to ensure our chosen connection charset is actually being used (MySQL) --- util/sql.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'util') diff --git a/util/sql.lua b/util/sql.lua index 693b67d9..f934bbdf 100644 --- a/util/sql.lua +++ b/util/sql.lua @@ -286,6 +286,18 @@ function engine:set_encoding() -- to UTF-8 return ok, err; end + if driver == "MySQL" then + local ok, actual_charset = self:transaction(function () + return self:select"SHOW SESSION VARIABLES LIKE 'character_set_client'"; + end); + 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"; + end + end + end + return true; end local engine_mt = { __index = engine }; -- cgit v1.2.3