class MiniSql::Abstract::PreparedBinds

Constants

BindName

For compatibility with Active Record

Public Instance Methods

array_wrap(object) click to toggle source
# File lib/mini_sql/abstract/prepared_binds.rb, line 60
def array_wrap(object)
  if object.respond_to?(:to_ary)
    object.to_ary || [object]
  else
    [object]
  end
end
bind(sql, *params) click to toggle source
# File lib/mini_sql/abstract/prepared_binds.rb, line 10
def bind(sql, *params)
  if Hash === (hash = params[0])
    bind_hash(sql, hash)
  else
    bind_array(sql, params)
  end
end
bind_array(sql, array) click to toggle source
# File lib/mini_sql/abstract/prepared_binds.rb, line 42
def bind_array(sql, array)
  sql = sql.dup
  param_i = 0
  i = 0
  binds = []
  bind_names = []
  sql.gsub!("?") do
    param_i += 1
    array_wrap(array[param_i - 1]).map do |vv|
      binds << vv
      i += 1
      bind_names << [BindName.new("$#{i}")]
      bind_output(i)
    end.join(', ')
  end
  [sql, binds, bind_names]
end
bind_hash(sql, hash) click to toggle source
# File lib/mini_sql/abstract/prepared_binds.rb, line 18
def bind_hash(sql, hash)
  sql = sql.dup
  binds = []
  bind_names = []
  i = 0

  hash.each do |k, v|
    sql.gsub!(":#{k}") do
      # ignore ::int and stuff like that
      # $` is previous to match
      if $` && $`[-1] != ":"
        array_wrap(v).map do |vv|
          binds << vv
          bind_names << [BindName.new(k)]
          bind_output(i += 1)
        end.join(', ')
      else
        ":#{k}"
      end
    end
  end
  [sql, binds, bind_names]
end
bind_output(_) click to toggle source
# File lib/mini_sql/abstract/prepared_binds.rb, line 68
def bind_output(_)
  raise NotImplementedError, "must be implemented by specific database driver"
end