Class: MiniSql::Abstract::PreparedBinds

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_sql/abstract/prepared_binds.rb

Defined Under Namespace

Classes: BindName

Instance Method Summary collapse

Instance Method Details

#array_wrap(object) ⇒ Object



60
61
62
63
64
65
66
# 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) ⇒ Object



10
11
12
13
14
15
16
# 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) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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(_) ⇒ Object

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/mini_sql/abstract/prepared_binds.rb', line 68

def bind_output(_)
  raise NotImplementedError, "must be implemented by specific database driver"
end