Class: ActiveRecord::ConnectionAdapters::MysqlAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/active_record/connection_adapters/mysql_adapter.rb

Overview

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 www.tmtm.org/en/mysql/ruby/).

Options:

  • :host – Defaults to localhost

  • :port – Defaults to 3306

  • :socket – Defaults to /tmp/mysql.sock

  • :username – Defaults to root

  • :password – Defaults to nothing

  • :database – The name of the database. No default, must be provided.

  • :sslkey – Necessary to use MySQL with an SSL connection

  • :sslcert – Necessary to use MySQL with an SSL connection

  • :sslcapath – Necessary to use MySQL with an SSL connection

  • :sslcipher – Necessary to use MySQL with an SSL connection

Constant Summary collapse

LOST_CONNECTION_ERROR_MESSAGES =
[ 
  "Server shutdown in progress",
  "Broken pipe", 
  "Lost connection to MySQL server during query", 
  "MySQL server has gone away"
]

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#add_column, #add_limit!, #create_table, #drop_table, #initialize_schema_information, #quote, #remove_column, #reset_runtime, #transaction

Constructor Details

#initialize(connection, logger, connection_options = nil) ⇒ MysqlAdapter

Returns a new instance of MysqlAdapter.



83
84
85
86
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 83

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

Instance Method Details

#adapter_nameObject



88
89
90
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 88

def adapter_name
  'MySQL'
end

#begin_db_transactionObject



136
137
138
139
140
141
142
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 136

def begin_db_transaction
  begin
    execute "BEGIN"
  rescue Exception
    # Transactions aren't supported
  end
end

#columns(table_name, name = nil) ⇒ Object



102
103
104
105
106
107
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 102

def columns(table_name, name = nil)
  sql = "SHOW FIELDS FROM #{table_name}" 
  columns = []
  execute(sql, name).each { |field| columns << Column.new(field[0], field[4], field[1]) }
  columns
end

#commit_db_transactionObject



144
145
146
147
148
149
150
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 144

def commit_db_transaction
  begin
    execute "COMMIT"
  rescue Exception
    # Transactions aren't supported
  end
end

#create_database(name) ⇒ Object



185
186
187
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 185

def create_database(name)
  execute "CREATE DATABASE #{name}"
end

#drop_database(name) ⇒ Object



181
182
183
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 181

def drop_database(name)
  execute "DROP DATABASE IF EXISTS #{name}"
end

#execute(sql, name = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 114

def execute(sql, name = nil)
  begin
    return log(sql, name, @connection) { |connection| connection.query(sql) }
  rescue ActiveRecord::StatementInvalid => exception
    if LOST_CONNECTION_ERROR_MESSAGES.any? { |msg| exception.message.split(":").first =~ /^#{msg}/ }
      @connection.real_connect(*@connection_options)
      @logger.info("Retrying invalid statement with reopened connection") if @logger
      return log(sql, name, @connection) { |connection| connection.query(sql) }
    else
      raise
    end
  end
end

#insert(sql, name = nil, pk = nil, id_value = nil) ⇒ Object



109
110
111
112
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 109

def insert(sql, name = nil, pk = nil, id_value = nil)
  execute(sql, name = nil)
  return id_value || @connection.insert_id
end

#native_database_typesObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 67

def native_database_types
  {
    :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY",
    :string      => "varchar(255)",
    :text        => "text",
    :integer     => "int(11)",
    :float       => "float",
    :datetime    => "datetime",
    :timestamp   => "datetime",
    :time        => "datetime",
    :date        => "date",
    :binary      => "blob",
    :boolean     => "tinyint(1)"
  }
end

#quote_column_name(name) ⇒ Object



161
162
163
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 161

def quote_column_name(name)
  return "`#{name}`"
end

#quote_string(s) ⇒ Object



165
166
167
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 165

def quote_string(s)
  Mysql::quote(s)
end

#recreate_database(name) ⇒ Object



176
177
178
179
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 176

def recreate_database(name)
  drop_database(name)
  create_database(name)
end

#rollback_db_transactionObject



152
153
154
155
156
157
158
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 152

def rollback_db_transaction
  begin
    execute "ROLLBACK"
  rescue Exception
    # Transactions aren't supported
  end
end

#select_all(sql, name = nil) ⇒ Object



93
94
95
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 93

def select_all(sql, name = nil)
  select(sql, name)
end

#select_one(sql, name = nil) ⇒ Object



97
98
99
100
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 97

def select_one(sql, name = nil)
  result = select(sql, name)
  result.nil? ? nil : result.first
end

#structure_dumpObject



170
171
172
173
174
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 170

def structure_dump
  select_all("SHOW TABLES").inject("") do |structure, table|
    structure += select_one("SHOW CREATE TABLE #{table.to_a.first.last}")["Create Table"] + ";\n\n"
  end
end

#update(sql, name = nil) ⇒ Object Also known as: delete



128
129
130
131
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 128

def update(sql, name = nil)
  execute(sql, name)
  @connection.affected_rows
end