Class: Mysql2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql2-cs-bind.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pseudo_bind(sql, values) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mysql2-cs-bind.rb', line 15

def self.pseudo_bind(sql, values)
  sql = sql.dup

  placeholders = []
  search_pos = 0
  while pos = sql.index('?', search_pos)
    placeholders.push(pos)
    search_pos = pos + 1
  end

  if placeholders.length != values.length &&
     placeholders.length != (values = values.flatten(1)).length
    raise ArgumentError, "mismatch between placeholders number and values arguments"
  end

  while pos = placeholders.pop()
    rawvalue = values.pop()
    if rawvalue.is_a?(Array)
      sql[pos] = rawvalue.map{|v| quote(v) }.join(",")
    else
      sql[pos] = quote(rawvalue)
    end
  end
  sql
end

Instance Method Details

#xquery(sql, *args, **options) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/mysql2-cs-bind.rb', line 7

def xquery(sql, *args, **options)
  if args.empty?
    query(sql, options)
  else
    query(Mysql2::Client.pseudo_bind(sql, args), options)
  end
end