class MiniSql::Builder

Public Class Methods

new(connection, template) click to toggle source
# File lib/mini_sql/builder.rb, line 5
def initialize(connection, template)
  @args = {}
  @sql = template
  @sections = {}
  @connection = connection
  @count_variables = 1
  @is_prepared = false
end

Public Instance Methods

prepared(condition = true) click to toggle source
# File lib/mini_sql/builder.rb, line 54
def prepared(condition = true)
  @is_prepared = condition

  self
end
query_decorator(decorator, hash_args = nil) click to toggle source
# File lib/mini_sql/builder.rb, line 50
def query_decorator(decorator, hash_args = nil)
  connection_switcher.query_decorator(decorator, parametrized_sql, union_parameters(hash_args))
end
to_sql(hash_args = nil) click to toggle source
# File lib/mini_sql/builder.rb, line 60
def to_sql(hash_args = nil)
  @connection.param_encoder.encode(parametrized_sql, union_parameters(hash_args))
end

Private Instance Methods

connection_switcher() click to toggle source
# File lib/mini_sql/builder.rb, line 64
        def connection_switcher
  if @is_prepared
    @connection.prepared
  else
    @connection
  end
end
parametrized_sql() click to toggle source
# File lib/mini_sql/builder.rb, line 72
        def parametrized_sql
  sql = @sql.dup

  @sections.each do |k, v|
    joined = nil
    case k
    when :select
      joined = (+"SELECT ") << v.join(" , ")
    when :where, :where2
      joined = (+"WHERE ") << v.map { |c| (+"(") << c << ")" }.join(" AND ")
    when :join
      joined = v.map { |item| (+"JOIN ") << item }.join("\n")
    when :left_join
      joined = v.map { |item| (+"LEFT JOIN ") << item }.join("\n")
    when :limit
      joined = (+"LIMIT :mq_auto_limit")
    when :offset
      joined = (+"OFFSET :mq_auto_offset")
    when :order_by
      joined = (+"ORDER BY ") << v.join(" , ")
    when :group_by
      joined = (+"GROUP BY ") << v.join(" , ")
    when :set
      joined = (+"SET ") << v.join(" , ")
    end

    sql.sub!("/*#{k}*/", joined)
  end

  sql
end
union_parameters(hash_args) click to toggle source
# File lib/mini_sql/builder.rb, line 104
        def union_parameters(hash_args)
  if hash_args
    @args.merge(hash_args)
  else
    @args
  end
end