require 'erb' class SqlQuery attr_reader :connection def initialize(file_name, options = {}) unless file_name.is_a?(String) || file_name.is_a?(Symbol) fail ArgumentError, 'SQL file name should be String or Symbol' end @sql_filename = file_name @options = options prepare_variables @connection = ActiveRecord::Base.connection end def explain msg = "EXPLAIN for: \n#{ sql }\n" msg += connection.explain(sql) pretty(msg) end def execute(prepare = true) to_execute = prepare ? prepared_for_logs : sql connection.execute(to_execute).entries end def sql @sql ||= ERB.new(File.read(file_path)).result(binding) end def pretty_sql pretty(sql.dup) end def quote(value) connection.quote(value) end def prepared_for_logs sql.gsub(/(\n|\s)+/,' ') end def partial(partial_name, partial_options = {}) path, file_name = split_to_path_and_name(partial_name) self.class.new("#{path}/_#{file_name}", @options.merge(partial_options), ).sql end def self.config=(value) @config = value end def self.config @config ||= Config.new end def self.configure yield(config) end class Config attr_accessor :path def initialize @path = '/app/sql_queries' end end private def split_to_path_and_name(file) if file.is_a?(Symbol) ['', file.to_s] else parts = file.rpartition('/') [parts.first, parts.last] end end def pretty(value) # override inspect to be more human readable from console # code copy from ActiveRecord # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/explain.rb#L30 def value.inspect; self; end value end def prepare_variables @options.each do |k, v| instance_variable_set("@#{k}", v) end end def file_path files = Dir.glob(path) if files.size == 0 raise "File not found: #{@sql_filename}" elsif files.size > 1 raise "More than one file found: #{ files.join(', ')}" else files.first end end def path root = defined?(Rails) ? Rails.root.to_s : Dir.pwd tmp_path = "#{root}#{self.class.config.path}" tmp_path += "/#{@sql_filename}.{sql.erb,erb.sql}" tmp_path end end