Skip to content

Commit

Permalink
Merge [5533] from trunk.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-pre-release@5534 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Nov 16, 2006
1 parent dd1dc5e commit 629b2ac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion activerecord/CHANGELOG
Expand Up @@ -24,7 +24,7 @@

* Document other options available to migration's add_column. #6419 [grg]

* MySQL: all_hashes compatibility with old MysqlRes class. #6429 [Jeremy Kemper]
* MySQL: all_hashes compatibility with old MysqlRes class. #6429, #6601 [Jeremy Kemper]

* Fix has_many :through to add the appropriate conditions when going through an association using STI. Closes #5783. [Jonathan Viney]

Expand Down
42 changes: 22 additions & 20 deletions activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
Expand Up @@ -5,30 +5,24 @@ module MysqlCompat
def self.define_all_hashes_method!
raise 'Mysql not loaded' unless defined?(::Mysql)

# for compatibility
Object.const_set(:MysqlRes, Mysql::Result) unless defined?(::MysqlRes)
Object.const_set(:MysqlField, Mysql::Field) unless defined?(::MysqlField)
Object.const_set(:MysqlError, Mysql::Error) unless defined?(::MysqlError)

return if ::MysqlRes.instance_methods.include?('all_hashes')
target = defined?(Mysql::Result) ? Mysql::Result : MysqlRes
return if target.instance_methods.include?('all_hashes')

# Ruby driver has a version string and returns null values in each_hash
# C driver >= 2.7 returns null values in each_hash
if Mysql.const_defined?(:VERSION)
if Mysql::VERSION.is_a?(String) || Mysql::VERSION >= 20700
::MysqlRes.class_eval <<-'end_eval'
def all_hashes
rows = []
each_hash { |row| rows << row }
rows
end
end_eval
if Mysql.const_defined?(:VERSION) && (Mysql::VERSION.is_a?(String) || Mysql::VERSION >= 20700)
target.class_eval <<-'end_eval'
def all_hashes
rows = []
each_hash { |row| rows << row }
rows
end
end_eval

# adapters before 2.7 don't have a version constant
# and don't return null values in each_hash
else
::MysqlRes.class_eval <<-'end_eval'
target.class_eval <<-'end_eval'
def all_hashes
rows = []
all_fields = fetch_fields.inject({}) { |fields, f| fields[f.name] = nil; fields }
Expand All @@ -37,19 +31,22 @@ def all_hashes
end
end_eval
end

unless target.instance_methods.include?('all_hashes')
raise "Failed to defined #{target.name}#all_hashes method. Mysql::VERSION = #{Mysql::VERSION.inspect}"
end
end
end

module ActiveRecord
class Base
# Establishes a connection to the database that's used by all Active Record objects.
def self.mysql_connection(config) # :nodoc:
# Only include the MySQL driver if one hasn't already been loaded
def self.require_mysql
# Include the MySQL driver if one hasn't already been loaded
unless defined? Mysql
begin
require_library_or_gem 'mysql'
rescue LoadError => cannot_require_mysql
# Only use the supplied backup Ruby/MySQL driver if no driver is already in place
# Use the bundled Ruby/MySQL driver if no driver is already in place
begin
require 'active_record/vendor/mysql'
rescue LoadError
Expand All @@ -60,7 +57,10 @@ def self.mysql_connection(config) # :nodoc:

# Define Mysql::Result.all_hashes
MysqlCompat.define_all_hashes_method!
end

# Establishes a connection to the database that's used by all Active Record objects.
def self.mysql_connection(config) # :nodoc:
config = config.symbolize_keys
host = config[:host]
port = config[:port]
Expand All @@ -74,8 +74,10 @@ def self.mysql_connection(config) # :nodoc:
raise ArgumentError, "No database specified. Missing argument: database."
end

require_mysql
mysql = Mysql.init
mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslkey]

ConnectionAdapters::MysqlAdapter.new(mysql, logger, [host, username, password, database, port, socket], config)
end
end
Expand Down

0 comments on commit 629b2ac

Please sign in to comment.