2005-11-22 (Tue) [長年日記]
_ encoding support for MySQL adapter
もりきゅうさんの記事を参考に MySQLアダプタにencodingオプションを追加するパッチを作って、tracに投げてみた。
development: adapter: mysql database: depot_development socket: /var/run/mysqld/mysqld.sock username: shugo password: encoding: cp932
みたいな感じで設定する。
"表"のquoteもOKで、binary_test.rbもちゃんと通ったけど、なぜかencoding: sjisだとquote のテストでコケる。 "\\"が"\201_"になるみたいだけど、MySQLの変換テーブルのせい?
quoteまわりはPostgreSQLアダプタもあやしい気がするけど、Mysql.quoteじゃなくてMysql#quoteを使うべきだということになかなか気付かずハマったのと、MySQLのバイナリのquote方法をしらべるので力尽きたので、今日はこれまで。
_ encoding support for MySQL adapter(2)
MySQL#real_connect は self が返るので、そのパッチだと mysql == conn ですね。 つまり @connection.quote でいいわけです。
[3 日坊主日記 - encoding support for MySQL adapter , 高速バスの予約より引用]
がーん、そうだったんですか。 と思ったらもうcommitされてるし。
reconnect!も考えると、こんな感じですかね。
Index: lib/active_record/connection_adapters/mysql_adapter.rb
===================================================================
--- lib/active_record/connection_adapters/mysql_adapter.rb (revision 3155)
+++ lib/active_record/connection_adapters/mysql_adapter.rb (working copy)
@@ -38,17 +38,7 @@
mysql = Mysql.init
mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslkey]
- if config[:encoding]
- begin
- mysql.options(Mysql::SET_CHARSET_NAME, config[:encoding])
- rescue
- raise ActiveRecord::ConnectionFailed, 'The :encoding option is only available for MySQL 4.1 and later with the mysql-ruby driver. Again, this does not work with the ruby-mysql driver or MySQL < 4.1.'
- end
- end
-
- conn = mysql.real_connect(host, username, password, database, port, socket)
- conn.query("SET NAMES '#{config[:encoding]}'") if config[:encoding]
- ConnectionAdapters::MysqlAdapter.new(conn, logger, [host, username, password, database, port, socket], mysql)
+ ConnectionAdapters::MysqlAdapter.new(mysql, logger, [host, username, password, database, port, socket], config[:encoding])
end
end
@@ -97,10 +87,11 @@
"MySQL server has gone away"
]
- def initialize(connection, logger, connection_options=nil, mysql=Mysql)
+ def initialize(connection, logger, connection_options=nil, encoding=nil)
super(connection, logger)
@connection_options = connection_options
- @mysql = mysql
+ @encoding = encoding
+ connect
end
def adapter_name #:nodoc:
@@ -144,7 +135,7 @@
end
def quote_string(string) #:nodoc:
- @mysql.quote(string)
+ @connection.quote(string)
end
def quoted_true
@@ -170,7 +161,7 @@
@connection.ping
else
@connection.close rescue nil
- @connection.real_connect(*@connection_options)
+ connect
end
end
@@ -318,6 +309,18 @@
private
+ def connect
+ if @encoding
+ begin
+ @connection.options(Mysql::SET_CHARSET_NAME, @encoding)
+ rescue
+ raise ActiveRecord::ConnectionFailed, 'The :encoding option is only available for MySQL 4.1 and later with the mysql-ruby driver. Again, this does not work with the ruby-mysql driver or MySQL < 4.1.'
+ end
+ end
+ @connection.real_connect(*@connection_options)
+ @connection.query("SET NAMES '#{@encoding}'") if @encoding
+ end
+
def select(sql, name = nil)
@connection.query_with_result = true
result = execute(sql, name)
追記:
PostgreSQLの方も修正してパッチをtracにつっこんどきました。
コンビニエンス関数は普通に使うけどなー。