class SqlQuery

Attributes

config[W]
connection[R]

Public Class Methods

config() click to toggle source
# File lib/sql_query.rb, line 59
def self.config
  @config ||= Config.new
end
configure() { |config| ... } click to toggle source
# File lib/sql_query.rb, line 63
def self.configure
  yield(config)
end
new(file_name, options = {}) click to toggle source
# File lib/sql_query.rb, line 8
def initialize(file_name, options = {})
  unless file_name.is_a?(String) || file_name.is_a?(Symbol)
    raise ArgumentError, 'SQL file name should be String or Symbol'
  end

  @sql_filename = file_name
  @options = options
  @connection = options.try(:delete, :db_connection) ||
                self.class.config.adapter.connection
  prepare_variables
end

Public Instance Methods

exec_query(prepare = true) click to toggle source
# File lib/sql_query.rb, line 31
def exec_query(prepare = true)
  to_execute = prepare ? prepared_for_logs : sql
  connection.exec_query(to_execute).to_a
end
execute(prepare = true) click to toggle source
# File lib/sql_query.rb, line 26
def execute(prepare = true)
  to_execute = prepare ? prepared_for_logs : sql
  connection.execute(to_execute).entries
end
explain() click to toggle source
# File lib/sql_query.rb, line 20
def explain
  msg = "EXPLAIN for: \n#{sql}\n"
  msg += connection.explain(sql)
  pretty(msg)
end
partial(partial_name, partial_options = {}) click to toggle source
# File lib/sql_query.rb, line 52
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
prepared_for_logs() click to toggle source
# File lib/sql_query.rb, line 48
def prepared_for_logs
  sql.gsub(/(\n|\s)+/, ' ')
end
pretty_sql() click to toggle source
# File lib/sql_query.rb, line 40
def pretty_sql
  pretty(sql.dup)
end
quote(value) click to toggle source
# File lib/sql_query.rb, line 44
def quote(value)
  connection.quote(value)
end
sql() click to toggle source
# File lib/sql_query.rb, line 36
def sql
  @sql ||= ERB.new(File.read(file_path)).result(binding)
end

Private Instance Methods

file_path() click to toggle source
# File lib/sql_query.rb, line 105
def file_path
  files = Dir.glob(path)
  if files.empty?
    raise "File not found: #{@sql_filename}"
  elsif files.size > 1
    raise "More than one file found: #{files.join(', ')}"
  else
    files.first
  end
end
path() click to toggle source
# File lib/sql_query.rb, line 116
def path
  if @sql_file_path.present?
    tmp_path = @sql_file_path
  else
    root = defined?(Rails) ? Rails.root.to_s : Dir.pwd
    tmp_path = "#{root}#{self.class.config.path}"
  end
  tmp_path += "/#{@sql_filename}.{sql.erb,erb.sql}"
  tmp_path
end
prepare_variables() click to toggle source
# File lib/sql_query.rb, line 97
def prepare_variables
  return if @options.blank?

  @options.each do |k, v|
    instance_variable_set("@#{k}", v)
  end
end
pretty(value) click to toggle source
# File lib/sql_query.rb, line 87
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
split_to_path_and_name(file) click to toggle source
# File lib/sql_query.rb, line 78
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