我真的需要另一双眼睛来观察这个,所以我想我会把它贴在这里。
如果我跳过Railtie,把它作为initializers文件夹中的传统monkeypatch来做,就可以了。使用铁轨。。。没有什么。
从表面上看,我的铁轨从来没有被处决,因此似乎没有其他事情发生。
有人看到这里有什么问题吗?
项目文件:
gem 'sql_explain', :path => "/home/mike/projects/sql_explain/"
gemspec公司:
...
spec.files = %w(README.rdoc sql_explain.rb lib/sql_explain.rb lib/railtie.rb sql_explain.gemspec)
...
require 'lib/railtie.rb'
铁路.rb
require 'active_record'
require 'sql_explain'
module SqlExplain
class Railtie < Rails::Railtie
railtie_name :sql_explain
initializer 'sql_explain.extend.activerecord' do
if defined?(ActiveRecord)
ActiveRecord::ConnectionAdapters::MysqlAdapter.include SqlExplain::AR
end
end
end
end
sql语句_解释.rb
module SqlExplain
module AR
def self.included(base_klass)
base_klass.send :alias_method_chain, :select, :explain
end
def select_with_explain(sql, name = nil)
@connection.query_with_result = true
result = execute('explain ' + sql, :skip_logging)
rows = []
result.each_hash { |row| rows << row }
result.free
@connection.more_results && @connection.next_result # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped
exp_string = ""
rows.each{|row| row.each_pair{|k,v| exp_string += " #{k}: #{v} |"}}
log(exp_string, "Explanation") {}
select_without_explain(sql, name)
end
end
end