Sha256: 9a277d1d665862575889cd6c8280d777bd7e33165c2151e84fb4f07b0dc55f3e

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

require 'active_record/connection_adapters/mysql2_adapter'

module ActiveRecord
  module ConnectionHandling
    def pedant_mysql2_connection(config)
      config = config.symbolize_keys

      config[:username] = 'root' if config[:username].nil?

      if Mysql2::Client.const_defined? :FOUND_ROWS
        config[:flags] = Mysql2::Client::FOUND_ROWS
      end

      client = Mysql2::Client.new(config)
      options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0]
      ActiveRecord::ConnectionAdapters::PedantMysql2Adapter.new(client, logger, options, config)
    rescue Mysql2::Error => error
      if error.message.include?("Unknown database") && defined?(ActiveRecord::NoDatabaseError)
        raise ActiveRecord::NoDatabaseError.new(error.message)
      else
        raise
      end
    end
  end
end

class MysqlWarning < StandardError

  attr_reader :code, :level, :query

  def initialize(message, code, level, query)
    super(message)
    @code = code
    @level = level
    @query = query
  end

end

class ActiveRecord::ConnectionAdapters::PedantMysql2Adapter < ActiveRecord::ConnectionAdapters::Mysql2Adapter

  alias_method :original_execute, :execute

  def execute(sql, name = nil)
    value = super
    log_warnings(sql)
    value
  end

  def exec_delete(sql, name, binds)
    original_execute to_sql(sql, binds), name
    affected_rows = @connection.affected_rows
    log_warnings(sql)
    affected_rows
  end

  alias :exec_update :exec_delete

  private

  def log_warnings(sql)
    return unless @connection.warning_count > 0

    result = @connection.query('SHOW WARNINGS')
    result.each do |level, code, message|
      warning = MysqlWarning.new(message, code, level, sql)
      ::PedantMysql2.on_warning.call(warning) unless PedantMysql2.ignored?(warning)
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord-pedantmysql2-adapter-1.0.0 lib/active_record/connection_adapters/pedant_mysql2_adapter.rb