Sha256: 7f8128b56e0dba95ced6ef2b95b03d1ec4587991f30e6d7e504abed6ff717c75
Contents?: true
Size: 1.86 KB
Versions: 2
Compression:
Stored size: 1.86 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 def execute(sql, name = nil) value = super log_warnings(sql) value end def exec_delete(sql, name, binds) @affected_rows_before_logging = nil value = super @affected_rows_before_logging || value end alias :exec_update :exec_delete private def log_warnings(sql) return unless @connection.warning_count > 0 @affected_rows_before_logging = @connection.affected_rows result = @connection.query('SHOW WARNINGS') result.each do |level, code, message| warning = MysqlWarning.new(message, code, level, sql) ::PedantMysql2.warn(warning) end end end if ActiveRecord::VERSION::MAJOR == 3 ActiveRecord::Base.extend(ActiveRecord::ConnectionHandling) end
Version data entries
2 entries across 2 versions & 1 rubygems