Skip to content

Commit

Permalink
MySQL: introduce :encoding option to specify the character set for cl…
Browse files Browse the repository at this point in the history
…ient, 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
  • Loading branch information
jeremy committed Nov 21, 2005
1 parent 6580662 commit f1a184f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN* *SVN*


* 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. #2975 [Shugo Maeda]

* Add tasks to create, drop and rebuild the MySQL and PostgreSQL test databases. [Marcel Molina Jr.] * Add tasks to create, drop and rebuild the MySQL and PostgreSQL test databases. [Marcel Molina Jr.]


* Correct boolean handling in generated reader methods. #2945 [don.park@gmail.com, Stefan Kaes] * Correct boolean handling in generated reader methods. #2945 [don.park@gmail.com, Stefan Kaes]
Expand Down
Expand Up @@ -38,7 +38,17 @@ def self.mysql_connection(config) # :nodoc:


mysql = Mysql.init mysql = Mysql.init
mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslkey] 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
end end


Expand Down Expand Up @@ -87,9 +97,10 @@ class MysqlAdapter < AbstractAdapter
"MySQL server has gone away" "MySQL server has gone away"
] ]


def initialize(connection, logger, connection_options=nil) def initialize(connection, logger, connection_options=nil, mysql=Mysql)
super(connection, logger) super(connection, logger)
@connection_options = connection_options @connection_options = connection_options
@mysql = mysql
end end


def adapter_name #:nodoc: def adapter_name #:nodoc:
Expand Down Expand Up @@ -119,12 +130,21 @@ def native_database_types #:nodoc


# QUOTING ================================================== # 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: def quote_column_name(name) #:nodoc:
"`#{name}`" "`#{name}`"
end end


def quote_string(string) #:nodoc: def quote_string(string) #:nodoc:
Mysql::quote(string) @mysql.quote(string)
end end


def quoted_true def quoted_true
Expand Down

0 comments on commit f1a184f

Please sign in to comment.