aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2005-11-21 18:51:27 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2005-11-21 18:51:27 +0000
commitf1a184fe880c4ff0db1b003f8f4562728809e1bd (patch)
tree047a1d1198962876cc881067cc076293c98fa17f /activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
parent658066225492589a157c4c4cc208257692732ba7 (diff)
downloadrails-f1a184fe880c4ff0db1b003f8f4562728809e1bd.tar.gz
rails-f1a184fe880c4ff0db1b003f8f4562728809e1bd.tar.bz2
rails-f1a184fe880c4ff0db1b003f8f4562728809e1bd.zip
MySQL: introduce :encoding option to specify the character set for client, connection, and results. Only available for MySQL 4.1 and later with the mysql-ruby driver. Do SHOW CHARACTER SET in mysql client to see available encodings. Closes #2975.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3152 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/mysql_adapter.rb')
-rwxr-xr-xactiverecord/lib/active_record/connection_adapters/mysql_adapter.rb26
1 files changed, 23 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index ceb42a659c..177d201259 100755
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -38,7 +38,17 @@ module ActiveRecord
mysql = Mysql.init
mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslkey]
- ConnectionAdapters::MysqlAdapter.new(mysql.real_connect(host, username, password, database, port, socket), logger, [host, username, password, database, port, socket])
+ 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)
end
end
@@ -87,9 +97,10 @@ module ActiveRecord
"MySQL server has gone away"
]
- def initialize(connection, logger, connection_options=nil)
+ def initialize(connection, logger, connection_options=nil, mysql=Mysql)
super(connection, logger)
@connection_options = connection_options
+ @mysql = mysql
end
def adapter_name #:nodoc:
@@ -119,12 +130,21 @@ module ActiveRecord
# QUOTING ==================================================
+ def quote(value, column = nil)
+ if value.kind_of?(String) && column && column.type == :binary
+ s = column.class.string_to_binary(value).unpack("H*")[0]
+ "x'#{s}'"
+ else
+ super
+ end
+ end
+
def quote_column_name(name) #:nodoc:
"`#{name}`"
end
def quote_string(string) #:nodoc:
- Mysql::quote(string)
+ @mysql.quote(string)
end
def quoted_true