提交 0845b506 编写于 作者: A Aaron Patterson

adding client_encoding method for discovering the encoding set for this...

adding client_encoding method for discovering the encoding set for this client, testing exec_insert on a string
上级 f9d3f018
...@@ -196,6 +196,7 @@ def initialize(connection, logger, connection_options, config) ...@@ -196,6 +196,7 @@ def initialize(connection, logger, connection_options, config)
@connection_options, @config = connection_options, config @connection_options, @config = connection_options, config
@quoted_column_names, @quoted_table_names = {}, {} @quoted_column_names, @quoted_table_names = {}, {}
@statements = {} @statements = {}
@client_encoding = nil
connect connect
end end
...@@ -330,6 +331,63 @@ def clear_cache! ...@@ -330,6 +331,63 @@ def clear_cache!
@statements.clear @statements.clear
end end
if "<3".respond_to?(:encode)
# Taken from here:
# https://github.com/tmtm/ruby-mysql/blob/master/lib/mysql/charset.rb
# Author: TOMITA Masahiro <tommy@tmtm.org>
ENCODINGS = {
"armscii8" => nil,
"ascii" => Encoding::US_ASCII,
"big5" => Encoding::Big5,
"binary" => Encoding::ASCII_8BIT,
"cp1250" => Encoding::Windows_1250,
"cp1251" => Encoding::Windows_1251,
"cp1256" => Encoding::Windows_1256,
"cp1257" => Encoding::Windows_1257,
"cp850" => Encoding::CP850,
"cp852" => Encoding::CP852,
"cp866" => Encoding::IBM866,
"cp932" => Encoding::Windows_31J,
"dec8" => nil,
"eucjpms" => Encoding::EucJP_ms,
"euckr" => Encoding::EUC_KR,
"gb2312" => Encoding::EUC_CN,
"gbk" => Encoding::GBK,
"geostd8" => nil,
"greek" => Encoding::ISO_8859_7,
"hebrew" => Encoding::ISO_8859_8,
"hp8" => nil,
"keybcs2" => nil,
"koi8r" => Encoding::KOI8_R,
"koi8u" => Encoding::KOI8_U,
"latin1" => Encoding::ISO_8859_1,
"latin2" => Encoding::ISO_8859_2,
"latin5" => Encoding::ISO_8859_9,
"latin7" => Encoding::ISO_8859_13,
"macce" => Encoding::MacCentEuro,
"macroman" => Encoding::MacRoman,
"sjis" => Encoding::SHIFT_JIS,
"swe7" => nil,
"tis620" => Encoding::TIS_620,
"ucs2" => Encoding::UTF_16BE,
"ujis" => Encoding::EucJP_ms,
"utf8" => Encoding::UTF_8,
"utf8mb4" => Encoding::UTF_8,
}
else
ENCODINGS = Hash.new { |h,k| h[k] = k }
end
# Get the client encoding for this database
def client_encoding
return @client_encoding if @client_encoding
result = exec_query(
"SHOW VARIABLES WHERE Variable_name = 'character_set_client'",
'SCHEMA')
@client_encoding = ENCODINGS[result.rows.last.last]
end
def exec_query(sql, name = 'SQL', binds = []) def exec_query(sql, name = 'SQL', binds = [])
log(sql, name, binds) do log(sql, name, binds) do
result = nil result = nil
...@@ -363,6 +421,10 @@ def exec_query(sql, name = 'SQL', binds = []) ...@@ -363,6 +421,10 @@ def exec_query(sql, name = 'SQL', binds = [])
end end
end end
def exec_insert(sql, name, binds)
exec_query(sql, name, binds)
end
def exec_without_stmt(sql, name = 'SQL') # :nodoc: def exec_without_stmt(sql, name = 'SQL') # :nodoc:
# Some queries, like SHOW CREATE TABLE don't work through the prepared # Some queries, like SHOW CREATE TABLE don't work through the prepared
# statement API. For those queries, we need to use this method. :'( # statement API. For those queries, we need to use this method. :'(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册