# HG changeset patch # User Kim Alvefur # Date 1382994436 -3600 # Node ID 39b187e7e8926cecbc8dc9de85e511478063f5cd # Parent fbba2997aabb9395d317f18b9fe057ae99efef0f mod_storage_sql2, util.sql: Move code for setting encoding to util.sql diff -r fbba2997aabb -r 39b187e7e892 plugins/mod_storage_sql2.lua --- a/plugins/mod_storage_sql2.lua Mon Oct 28 21:37:30 2013 +0100 +++ b/plugins/mod_storage_sql2.lua Mon Oct 28 22:07:16 2013 +0100 @@ -82,17 +82,7 @@ ProsodyArchiveTable:create(engine); end); end -local function set_encoding() - if params.driver == "SQLite3" then return end - local set_names_query = "SET NAMES 'utf8';"; - if params.driver == "MySQL" then - set_names_query = set_names_query:gsub(";$", " COLLATE 'utf8_bin';"); - end - local success,err = engine:transaction(function() return engine:execute(set_names_query); end); - if not success then - module:log("error", "Failed to set database connection encoding to UTF8: %s", err); - end -end + local function upgrade_table() if params.driver == "MySQL" then local success,err = engine:transaction(function() @@ -146,7 +136,7 @@ --local dburi = db2uri(params); engine = mod_sql:create_engine(params); - set_encoding(); + engine:set_encoding(); -- Automatically create table, ignore failure (table probably already exists) create_table(); diff -r fbba2997aabb -r 39b187e7e892 util/sql.lua --- a/util/sql.lua Mon Oct 28 21:37:30 2013 +0100 +++ b/util/sql.lua Mon Oct 28 22:07:16 2013 +0100 @@ -276,6 +276,17 @@ end return success; 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 + set_names_query = set_names_query:gsub(";$", " COLLATE 'utf8_bin';"); + 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 +end local engine_mt = { __index = engine }; local function db2uri(params)