Skip to content
Browse files

r1617@asus: jeremy | 2005-07-03 08:23:31 -0700

 settle on the 'slow' MysqlAdapter#select since it means fewer comparisons later.  strip trailing whitespace.


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1635 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 94cd3fd commit 67adc0b5b3a0ba5b91866d6b24f4487bf625ea2b @jeremy jeremy committed Jul 3, 2005
Showing with 27 additions and 29 deletions.
  1. +27 −29 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
View
56 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -1,6 +1,6 @@
require 'active_record/connection_adapters/abstract_adapter'
require 'parsedate'
-
+
module ActiveRecord
class Base
# Establishes a connection to the database that's used by all Active Record objects.
@@ -11,35 +11,35 @@ def self.mysql_connection(config) # :nodoc:
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
- begin
+ begin
require 'active_record/vendor/mysql'
require 'active_record/vendor/mysql411'
rescue LoadError
raise cannot_require_mysql
end
end
end
-
+
symbolize_strings_in_hash(config)
-
+
host = config[:host]
port = config[:port]
socket = config[:socket]
username = config[:username] ? config[:username].to_s : 'root'
password = config[:password].to_s
-
+
if config.has_key?(:database)
database = config[:database]
else
raise ArgumentError, "No database specified. Missing argument: database."
end
-
+
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])
end
end
-
+
module ConnectionAdapters
# The MySQL adapter will work with both Ruby/MySQL, which is a Ruby-based MySQL adapter that comes bundled with Active Record, and with
# the faster C-based MySQL/Ruby adapter (available both as a gem and from http://www.tmtm.org/en/mysql/ruby/).
@@ -57,10 +57,10 @@ module ConnectionAdapters
# * <tt>:sslcapath</tt> -- Necessary to use MySQL with an SSL connection
# * <tt>:sslcipher</tt> -- Necessary to use MySQL with an SSL connection
class MysqlAdapter < AbstractAdapter
- LOST_CONNECTION_ERROR_MESSAGES = [
+ LOST_CONNECTION_ERROR_MESSAGES = [
"Server shutdown in progress",
- "Broken pipe",
- "Lost connection to MySQL server during query",
+ "Broken pipe",
+ "Lost connection to MySQL server during query",
"MySQL server has gone away"
]
@@ -84,7 +84,7 @@ def initialize(connection, logger, connection_options=nil)
super(connection, logger)
@connection_options = connection_options
end
-
+
def adapter_name
'MySQL'
end
@@ -93,24 +93,24 @@ def adapter_name
def select_all(sql, name = nil)
select(sql, name)
end
-
+
def select_one(sql, name = nil)
result = select(sql, name)
result.nil? ? nil : result.first
end
-
+
def columns(table_name, name = nil)
- sql = "SHOW FIELDS FROM #{table_name}"
+ sql = "SHOW FIELDS FROM #{table_name}"
columns = []
execute(sql, name).each { |field| columns << Column.new(field[0], field[4], field[1]) }
columns
end
-
+
def insert(sql, name = nil, pk = nil, id_value = nil)
execute(sql, name = nil)
id_value || @connection.insert_id
end
-
+
def execute(sql, name = nil, retries = 2)
unless @logger
@connection.query(sql)
@@ -130,38 +130,38 @@ def execute(sql, name = nil, retries = 2)
raise
end
end
-
+
def update(sql, name = nil)
execute(sql, name)
@connection.affected_rows
end
-
+
alias_method :delete, :update
-
-
+
+
def begin_db_transaction
execute "BEGIN"
rescue Exception
# Transactions aren't supported
end
-
+
def commit_db_transaction
execute "COMMIT"
rescue Exception
# Transactions aren't supported
end
-
+
def rollback_db_transaction
execute "ROLLBACK"
rescue Exception
# Transactions aren't supported
end
-
+
def quote_column_name(name)
"`#{name}`"
end
-
+
def quote_string(string)
Mysql::quote(string)
end
@@ -187,16 +187,15 @@ def recreate_database(name)
drop_database(name)
create_database(name)
end
-
+
def drop_database(name)
execute "DROP DATABASE IF EXISTS #{name}"
end
-
+
def create_database(name)
execute "CREATE DATABASE #{name}"
end
-
def create_table(name)
super(name, "ENGINE=InnoDB")
end
@@ -207,8 +206,7 @@ def select(sql, name = nil)
result = execute(sql, name)
rows = []
all_fields_initialized = result.fetch_fields.inject({}) { |all_fields, f| all_fields[f.name] = nil; all_fields }
- result.each_hash { |row| rows << all_fields_initialized.merge(row) }
- #result.each_hash { |row| rows << row }
+ result.each_hash { |row| rows << all_fields_initialized.dup.update(row) }
result.free
rows
end

0 comments on commit 67adc0b

Please sign in to comment.
Something went wrong with that request. Please try again.