mysql_adapter.rb 17.2 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1
require 'active_record/connection_adapters/abstract_adapter'
2
require 'set'
3

4
module MysqlCompat #:nodoc:
5 6
  # add all_hashes method to standard mysql-c bindings or pure ruby version
  def self.define_all_hashes_method!
7 8
    raise 'Mysql not loaded' unless defined?(::Mysql)

9 10
    target = defined?(Mysql::Result) ? Mysql::Result : MysqlRes
    return if target.instance_methods.include?('all_hashes')
11 12 13

    # Ruby driver has a version string and returns null values in each_hash
    # C driver >= 2.7 returns null values in each_hash
14 15 16 17 18 19
    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
20
      end
21
      end_eval
22 23 24 25

    # adapters before 2.7 don't have a version constant
    # and don't return null values in each_hash
    else
26
      target.class_eval <<-'end_eval'
27 28 29 30 31 32 33 34
      def all_hashes
        rows = []
        all_fields = fetch_fields.inject({}) { |fields, f| fields[f.name] = nil; fields }
        each_hash { |row| rows << all_fields.dup.update(row) }
        rows
      end
      end_eval
    end
35

36 37
    unless target.instance_methods.include?('all_hashes') ||
           target.instance_methods.include?(:all_hashes)
38 39
      raise "Failed to defined #{target.name}#all_hashes method. Mysql::VERSION = #{Mysql::VERSION.inspect}"
    end
40 41 42
  end
end

D
Initial  
David Heinemeier Hansson 已提交
43 44
module ActiveRecord
  class Base
45 46
    # Establishes a connection to the database that's used by all Active Record objects.
    def self.mysql_connection(config) # :nodoc:
47
      config = config.symbolize_keys
D
Initial  
David Heinemeier Hansson 已提交
48 49 50 51 52
      host     = config[:host]
      port     = config[:port]
      socket   = config[:socket]
      username = config[:username] ? config[:username].to_s : 'root'
      password = config[:password].to_s
53

D
Initial  
David Heinemeier Hansson 已提交
54 55 56 57 58
      if config.has_key?(:database)
        database = config[:database]
      else
        raise ArgumentError, "No database specified. Missing argument: database."
      end
59

60
      # Require the MySQL driver and define Mysql::Result.all_hashes
61 62 63 64 65 66 67 68
      unless defined? Mysql
        begin
          require_library_or_gem('mysql')
        rescue LoadError
          $stderr.puts '!!! The bundled mysql.rb driver has been removed from Rails 2.2. Please install the mysql gem and try again: gem install mysql.'
          raise
        end
      end
69 70
      MysqlCompat.define_all_hashes_method!

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

74
      ConnectionAdapters::MysqlAdapter.new(mysql, logger, [host, username, password, database, port, socket], config)
D
Initial  
David Heinemeier Hansson 已提交
75 76
    end
  end
77

D
Initial  
David Heinemeier Hansson 已提交
78
  module ConnectionAdapters
79
    class MysqlColumn < Column #:nodoc:
80 81 82
      def extract_default(default)
        if type == :binary || type == :text
          if default.blank?
83
            nil
84 85 86 87 88 89 90 91
          else
            raise ArgumentError, "#{type} columns cannot have a default value: #{default.inspect}"
          end
        elsif missing_default_forged_as_empty_string?(default)
          nil
        else
          super
        end
92 93
      end

94 95
      private
        def simplified_type(field_type)
96
          return :boolean if MysqlAdapter.emulate_booleans && field_type.downcase.index("tinyint(1)")
97
          return :string  if field_type =~ /enum/i
98 99
          super
        end
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        def extract_limit(sql_type)
          if sql_type =~ /blob|text/i
            case sql_type
            when /tiny/i
              255
            when /medium/i
              16777215
            when /long/i
              2147483647 # mysql only allows 2^31-1, not 2^32-1, somewhat inconsistently with the tiny/medium/normal cases
            else
              super # we could return 65535 here, but we leave it undecorated by default
            end
          else
            super
          end
        end

118 119
        # MySQL misreports NOT NULL column default when none is given.
        # We can't detect this for columns which may have a legitimate ''
120 121
        # default (string) but we can for others (integer, datetime, boolean,
        # and the rest).
122 123 124
        #
        # Test whether the column has default '', is not null, and is not
        # a type allowing default ''.
125 126
        def missing_default_forged_as_empty_string?(default)
          type != :string && !null && default == ''
127
        end
128 129
    end

130 131 132 133 134
    # 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/).
    #
    # Options:
    #
P
Pratik Naik 已提交
135 136 137 138 139 140 141 142 143 144 145
    # * <tt>:host</tt> - Defaults to "localhost".
    # * <tt>:port</tt> - Defaults to 3306.
    # * <tt>:socket</tt> - Defaults to "/tmp/mysql.sock".
    # * <tt>:username</tt> - Defaults to "root"
    # * <tt>:password</tt> - Defaults to nothing.
    # * <tt>:database</tt> - The name of the database. No default, must be provided.
    # * <tt>:encoding</tt> - (Optional) Sets the client encoding by executing "SET NAMES <encoding>" after connection.
    # * <tt>:sslkey</tt> - Necessary to use MySQL with an SSL connection.
    # * <tt>:sslcert</tt> - Necessary to use MySQL with an SSL connection.
    # * <tt>:sslcapath</tt> - Necessary to use MySQL with an SSL connection.
    # * <tt>:sslcipher</tt> - Necessary to use MySQL with an SSL connection.
146
    #
P
Pratik Naik 已提交
147
    # By default, the MysqlAdapter will consider all columns of type <tt>tinyint(1)</tt>
148 149 150 151 152
    # as boolean. If you wish to disable this emulation (which was the default
    # behavior in versions 0.13.1 and earlier) you can add the following line
    # to your environment.rb file:
    #
    #   ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans = false
153
    class MysqlAdapter < AbstractAdapter
154 155 156
      @@emulate_booleans = true
      cattr_accessor :emulate_booleans

157 158
      ADAPTER_NAME = 'MySQL'.freeze

159
      LOST_CONNECTION_ERROR_MESSAGES = [
160
        "Server shutdown in progress",
161 162
        "Broken pipe",
        "Lost connection to MySQL server during query",
163 164 165
        "MySQL server has gone away" ]

      QUOTED_TRUE, QUOTED_FALSE = '1', '0'
166

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
      NATIVE_DATABASE_TYPES = {
        :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY".freeze,
        :string      => { :name => "varchar", :limit => 255 },
        :text        => { :name => "text" },
        :integer     => { :name => "int"},
        :float       => { :name => "float" },
        :decimal     => { :name => "decimal" },
        :datetime    => { :name => "datetime" },
        :timestamp   => { :name => "datetime" },
        :time        => { :name => "time" },
        :date        => { :name => "date" },
        :binary      => { :name => "blob" },
        :boolean     => { :name => "tinyint", :limit => 1 }
      }

J
Jeremy Kemper 已提交
182
      def initialize(connection, logger, connection_options, config)
183
        super(connection, logger)
J
Jeremy Kemper 已提交
184
        @connection_options, @config = connection_options, config
185
        @quoted_column_names, @quoted_table_names = {}, {}
186
        connect
187 188 189
      end

      def adapter_name #:nodoc:
190
        ADAPTER_NAME
191 192 193
      end

      def supports_migrations? #:nodoc:
194 195
        true
      end
196

197
      def native_database_types #:nodoc:
198
        NATIVE_DATABASE_TYPES
199 200
      end

201

202 203
      # QUOTING ==================================================

204
      def quote(value, column = nil)
205
        if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
206 207
          s = column.class.string_to_binary(value).unpack("H*")[0]
          "x'#{s}'"
208 209
        elsif value.kind_of?(BigDecimal)
          "'#{value.to_s("F")}'"
210 211 212 213 214
        else
          super
        end
      end

215
      def quote_column_name(name) #:nodoc:
216
        @quoted_column_names[name] ||= "`#{name}`"
217 218
      end

219
      def quote_table_name(name) #:nodoc:
220
        @quoted_table_names[name] ||= quote_column_name(name).gsub('.', '`.`')
221 222
      end

223
      def quote_string(string) #:nodoc:
224
        @connection.quote(string)
D
Initial  
David Heinemeier Hansson 已提交
225
      end
226

227
      def quoted_true
228
        QUOTED_TRUE
229
      end
230

231
      def quoted_false
232
        QUOTED_FALSE
D
Initial  
David Heinemeier Hansson 已提交
233
      end
234

235 236 237 238 239 240 241 242 243 244 245 246
      # REFERENTIAL INTEGRITY ====================================

      def disable_referential_integrity(&block) #:nodoc:
        old = select_value("SELECT @@FOREIGN_KEY_CHECKS")

        begin
          update("SET FOREIGN_KEY_CHECKS = 0")
          yield
        ensure
          update("SET FOREIGN_KEY_CHECKS = #{old}")
        end
      end
247

248 249 250
      # CONNECTION MANAGEMENT ====================================

      def active?
251 252 253 254 255
        if @connection.respond_to?(:stat)
          @connection.stat
        else
          @connection.query 'select 1'
        end
256 257 258 259 260 261 262

        # mysql-ruby doesn't raise an exception when stat fails.
        if @connection.respond_to?(:errno)
          @connection.errno.zero?
        else
          true
        end
263 264 265 266 267
      rescue Mysql::Error
        false
      end

      def reconnect!
268
        disconnect!
269
        connect
270
      end
271

272 273 274
      def disconnect!
        @connection.close rescue nil
      end
275 276


277
      # DATABASE STATEMENTS ======================================
278

279 280 281 282 283 284 285 286 287
      def select_rows(sql, name = nil)
        @connection.query_with_result = true
        result = execute(sql, name)
        rows = []
        result.each { |row| rows << row }
        result.free
        rows
      end

288
      def execute(sql, name = nil) #:nodoc:
289
        log(sql, name) { @connection.query(sql) }
290
      rescue ActiveRecord::StatementInvalid => exception
291 292
        if exception.message.split(":").first =~ /Packets out of order/
          raise ActiveRecord::StatementInvalid, "'Packets out of order' error was received from the database. Please update your mysql bindings (gem install mysql) and read http://dev.mysql.com/doc/mysql/en/password-hashing.html for more information.  If you're on Windows, use the Instant Rails installer to get the updated mysql bindings."
293 294
        else
          raise
295
        end
D
Initial  
David Heinemeier Hansson 已提交
296
      end
297

298 299
      def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
        super sql, name
300 301 302
        id_value || @connection.insert_id
      end

303 304
      def update_sql(sql, name = nil) #:nodoc:
        super
305 306
        @connection.affected_rows
      end
307

308
      def begin_db_transaction #:nodoc:
309 310 311
        execute "BEGIN"
      rescue Exception
        # Transactions aren't supported
D
Initial  
David Heinemeier Hansson 已提交
312
      end
313

314
      def commit_db_transaction #:nodoc:
315 316 317
        execute "COMMIT"
      rescue Exception
        # Transactions aren't supported
D
Initial  
David Heinemeier Hansson 已提交
318
      end
319

320
      def rollback_db_transaction #:nodoc:
321 322 323
        execute "ROLLBACK"
      rescue Exception
        # Transactions aren't supported
D
Initial  
David Heinemeier Hansson 已提交
324
      end
325

326

327
      def add_limit_offset!(sql, options) #:nodoc:
328
        if limit = options[:limit]
329
          limit = sanitize_limit(limit)
330 331
          unless offset = options[:offset]
            sql << " LIMIT #{limit}"
332
          else
333
            sql << " LIMIT #{offset.to_i}, #{limit}"
334
          end
335
        end
336
      end
337

338 339 340 341

      # SCHEMA STATEMENTS ========================================

      def structure_dump #:nodoc:
342 343 344 345 346
        if supports_views?
          sql = "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'"
        else
          sql = "SHOW TABLES"
        end
347

348 349
        select_all(sql).inject("") do |structure, table|
          table.delete('Table_type')
350
          structure += select_one("SHOW CREATE TABLE #{quote_table_name(table.to_a.first.last)}")["Create Table"] + ";\n\n"
351 352 353 354
        end
      end

      def recreate_database(name) #:nodoc:
D
Initial  
David Heinemeier Hansson 已提交
355 356 357
        drop_database(name)
        create_database(name)
      end
358

359
      # Create a new MySQL database with optional <tt>:charset</tt> and <tt>:collation</tt>.
360 361 362 363 364 365 366 367 368 369 370 371
      # Charset defaults to utf8.
      #
      # Example:
      #   create_database 'charset_test', :charset => 'latin1', :collation => 'latin1_bin'
      #   create_database 'matt_development'
      #   create_database 'matt_development', :charset => :big5
      def create_database(name, options = {})
        if options[:collation]
          execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
        else
          execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
        end
372
      end
373

374
      def drop_database(name) #:nodoc:
375
        execute "DROP DATABASE IF EXISTS `#{name}`"
D
Initial  
David Heinemeier Hansson 已提交
376
      end
377

378
      def current_database
379 380 381 382 383 384 385 386 387 388 389
        select_value 'SELECT DATABASE() as db'
      end

      # Returns the database character set.
      def charset
        show_variable 'character_set_database'
      end

      # Returns the database collation strategy.
      def collation
        show_variable 'collation_database'
390
      end
391 392 393 394 395

      def tables(name = nil) #:nodoc:
        tables = []
        execute("SHOW TABLES", name).each { |field| tables << field[0] }
        tables
D
Initial  
David Heinemeier Hansson 已提交
396
      end
397

398 399 400 401
      def drop_table(table_name, options = {})
        super(table_name, options)
      end

402 403 404
      def indexes(table_name, name = nil)#:nodoc:
        indexes = []
        current_index = nil
405
        execute("SHOW KEYS FROM #{quote_table_name(table_name)}", name).each do |row|
406 407 408 409 410 411 412 413 414 415 416 417
          if current_index != row[2]
            next if row[2] == "PRIMARY" # skip the primary key
            current_index = row[2]
            indexes << IndexDefinition.new(row[0], row[2], row[1] == "0", [])
          end

          indexes.last.columns << row[4]
        end
        indexes
      end

      def columns(table_name, name = nil)#:nodoc:
418
        sql = "SHOW FIELDS FROM #{quote_table_name(table_name)}"
419 420 421 422 423
        columns = []
        execute(sql, name).each { |field| columns << MysqlColumn.new(field[0], field[4], field[1], field[2] == "YES") }
        columns
      end

424 425
      def create_table(table_name, options = {}) #:nodoc:
        super(table_name, options.reverse_merge(:options => "ENGINE=InnoDB"))
426
      end
427

428 429
      def rename_table(table_name, new_name)
        execute "RENAME TABLE #{quote_table_name(table_name)} TO #{quote_table_name(new_name)}"
430
      end
431 432

      def change_column_default(table_name, column_name, default) #:nodoc:
433
        current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"]
434

435
        execute("ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{current_type} DEFAULT #{quote(default)}")
436
      end
437

438
      def change_column(table_name, column_name, type, options = {}) #:nodoc:
439
        unless options_include_default?(options)
440 441
          if column = columns(table_name).find { |c| c.name == column_name.to_s }
            options[:default] = column.default
442 443 444
          else
            raise "No such column: #{table_name}.#{column_name}"
          end
445
        end
446

447
        change_column_sql = "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
448 449 450 451
        add_column_options!(change_column_sql, options)
        execute(change_column_sql)
      end

452
      def rename_column(table_name, column_name, new_column_name) #:nodoc:
453 454
        current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"]
        execute "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}"
455 456
      end

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
      # Maps logical Rails types to MySQL-specific data types.
      def type_to_sql(type, limit = nil, precision = nil, scale = nil)
        return super unless type.to_s == 'integer'

        case limit
        when 0..3
          "smallint(#{limit})"
        when 4..8
          "int(#{limit})"
        when 9..20
          "bigint(#{limit})"
        else
          'int(11)'
        end
      end

473

474 475
      # SHOW VARIABLES LIKE 'name'
      def show_variable(name)
476 477
        variables = select_all("SHOW VARIABLES LIKE '#{name}'")
        variables.first['Value'] unless variables.empty?
478 479
      end

480 481 482
      # Returns a table's primary key and belonging sequence.
      def pk_and_sequence_for(table) #:nodoc:
        keys = []
483
        execute("describe #{quote_table_name(table)}").each_hash do |h|
484 485 486 487 488
          keys << h["Field"]if h["Key"] == "PRI"
        end
        keys.length == 1 ? [keys.first, nil] : nil
      end

D
Initial  
David Heinemeier Hansson 已提交
489
      private
490 491 492
        def connect
          encoding = @config[:encoding]
          if encoding
493
            @connection.options(Mysql::SET_CHARSET_NAME, encoding) rescue nil
494
          end
495
          @connection.ssl_set(@config[:sslkey], @config[:sslcert], @config[:sslca], @config[:sslcapath], @config[:sslcipher]) if @config[:sslkey]
496 497
          @connection.real_connect(*@connection_options)
          execute("SET NAMES '#{encoding}'") if encoding
498 499 500 501

          # By default, MySQL 'where id is null' selects the last inserted id.
          # Turn this off. http://dev.rubyonrails.org/ticket/6778
          execute("SET SQL_AUTO_IS_NULL=0")
502 503
        end

D
Initial  
David Heinemeier Hansson 已提交
504
        def select(sql, name = nil)
505 506
          @connection.query_with_result = true
          result = execute(sql, name)
507
          rows = result.all_hashes
508
          result.free
D
Initial  
David Heinemeier Hansson 已提交
509 510
          rows
        end
511

512 513 514
        def supports_views?
          version[0] >= 5
        end
515

516 517 518
        def version
          @version ||= @connection.server_info.scan(/^(\d+)\.(\d+)\.(\d+)/).flatten.map { |v| v.to_i }
        end
D
Initial  
David Heinemeier Hansson 已提交
519 520
    end
  end
521
end