Class: MiniSql::Postgres::PreparedBinds

Inherits:
Abstract::PreparedBinds show all
Defined in:
lib/mini_sql/postgres/prepared_binds.rb

Instance Method Summary collapse

Methods inherited from Abstract::PreparedBinds

#array_wrap, #bind, #bind_array

Instance Method Details

#bind_hash(sql, hash) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mini_sql/postgres/prepared_binds.rb', line 9

def bind_hash(sql, hash)
  sql = sql.dup
  binds = []
  bind_names = []
  i = 0

  hash.each do |k, v|
    bind_outputs =
      array_wrap(v).map { |vv|
        binds << vv
        bind_names << [BindName.new(k)]
        bind_output(i += 1)
      }.join(', ')

    sql.gsub!(":#{k}") do
      # ignore ::int and stuff like that
      # $` is previous to match
      if $` && $`[-1] != ":"
        bind_outputs
      else
        ":#{k}"
      end
    end
  end
  [sql, binds, bind_names]
end

#bind_output(i) ⇒ Object



36
37
38
# File 'lib/mini_sql/postgres/prepared_binds.rb', line 36

def bind_output(i)
  "$#{i}"
end