From 67adc0b5b3a0ba5b91866d6b24f4487bf625ea2b Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 3 Jul 2005 08:32:57 +0000 Subject: 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 --- .../connection_adapters/mysql_adapter.rb | 56 +++++++++++----------- 1 file changed, 27 insertions(+), 29 deletions(-) (limited to 'activerecord/lib/active_record/connection_adapters/mysql_adapter.rb') diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index b026a72093..ad0e490194 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/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,7 +11,7 @@ module ActiveRecord 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 @@ -19,27 +19,27 @@ module ActiveRecord 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 ActiveRecord # * :sslcapath -- Necessary to use MySQL with an SSL connection # * :sslcipher -- 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 @@ module ActiveRecord super(connection, logger) @connection_options = connection_options end - + def adapter_name 'MySQL' end @@ -93,24 +93,24 @@ module ActiveRecord 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 @@ module ActiveRecord 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 @@ module ActiveRecord 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 @@ module ActiveRecord 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 -- cgit v1.2.3