From 5dc8f65a6769169fda67a38c1d0b8371122ff5e0 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Tue, 19 Nov 2019 15:57:56 +0100 Subject: [PATCH] B #3946: Set encoding for the db connections (onedb) --- src/onedb/onedb | 13 ++++++++++++- src/onedb/onedb.rb | 3 ++- src/onedb/onedb_backend.rb | 25 ++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/onedb/onedb b/src/onedb/onedb index dfcde33960..4b797933d5 100755 --- a/src/onedb/onedb +++ b/src/onedb/onedb @@ -163,6 +163,17 @@ DBNAME={ } } +ENCODING={ + :name => "encoding", + :large => "--encoding charset", + :format => String, + :description => "MySQL encoding to use for the connection", + :proc => lambda { |o, options| + options[:backend] = :mysql + options[:encoding] = o + } +} + ############################################################################### # Slave MySQL options ############################################################################### @@ -321,7 +332,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do # Global options ########################################################################### set :option, CommandParser::OPTIONS - set :option, [SQLITE, SERVER, PORT, USERNAME, PASSWORD, DBNAME] + set :option, [SQLITE, SERVER, PORT, USERNAME, PASSWORD, DBNAME, ENCODING] ########################################################################### # Backup diff --git a/src/onedb/onedb.rb b/src/onedb/onedb.rb index 782a6f4b82..023e215a77 100644 --- a/src/onedb/onedb.rb +++ b/src/onedb/onedb.rb @@ -52,7 +52,8 @@ class OneDB :port => ops[:port], :user => ops[:user], :passwd => passwd, - :db_name => ops[:db_name] + :db_name => ops[:db_name], + :encoding=> ops[:encoding] ) else raise "You need to specify the SQLite or MySQL connection options." diff --git a/src/onedb/onedb_backend.rb b/src/onedb/onedb_backend.rb index be275f2333..7ccdde0dd1 100644 --- a/src/onedb/onedb_backend.rb +++ b/src/onedb/onedb_backend.rb @@ -214,6 +214,7 @@ class BackEndMySQL < OneDBBacKEnd @user = opts[:user] @passwd = opts[:passwd] @db_name = opts[:db_name] + @encoding= opts[:encoding] # Check for errors: error = false @@ -226,8 +227,10 @@ class BackEndMySQL < OneDBBacKEnd end # Check for defaults: - @server = "localhost" if @server.nil? - @port = 0 if @port.nil? + @server = "localhost" if @server.nil? + @port = 0 if @port.nil? + + encoding if @encoding.nil? # Clean leading and trailing quotes, if any @server = @server [1..-2] if @server [0] == ?" @@ -282,6 +285,19 @@ class BackEndMySQL < OneDBBacKEnd puts end + def encoding + @encoding = '' + + connect_db + + @db.fetch('select default_character_set_name FROM information_schema.SCHEMATA'\ + " WHERE schema_name = \"#{@db_name}\"") do |row| + @encoding = row[:default_character_set_name] + end + + @encoding + end + def restore(bck_file, force=nil, federated=false) connect_db @@ -323,7 +339,10 @@ class BackEndMySQL < OneDBBacKEnd endpoint = "mysql2://#{@user}:#{passwd}@#{@server}:#{@port}/#{@db_name}" begin - @db = Sequel.connect(endpoint) + options = {} + options[:encoding] = @encoding unless @encoding.empty? + + @db = Sequel.connect(endpoint, options) rescue Exception => e raise "Error connecting to DB: " + e.message end