-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
bound_sql_literal.rb
65 lines (55 loc) · 2.24 KB
/
bound_sql_literal.rb
1
2
3
4
5
6
7
8
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true
module Arel # :nodoc: all
module Nodes
class BoundSqlLiteral < NodeExpression
attr_reader :sql_with_placeholders, :positional_binds, :named_binds
def initialize(sql_with_placeholders, positional_binds, named_binds)
has_positional = !(positional_binds.nil? || positional_binds.empty?)
has_named = !(named_binds.nil? || named_binds.empty?)
if has_positional
if has_named
raise BindError.new("cannot mix positional and named binds", sql_with_placeholders)
end
if positional_binds.size != (expected = sql_with_placeholders.count("?"))
raise BindError.new("wrong number of bind variables (#{positional_binds.size} for #{expected})", sql_with_placeholders)
end
elsif has_named
tokens_in_string = sql_with_placeholders.scan(/:(?<!::)([a-zA-Z]\w*)/).flatten.map(&:to_sym).uniq
tokens_in_hash = named_binds.keys.map(&:to_sym).uniq
if !(missing = (tokens_in_string - tokens_in_hash)).empty?
if missing.size == 1
raise BindError.new("missing value for #{missing.first.inspect}", sql_with_placeholders)
else
raise BindError.new("missing values for #{missing.inspect}", sql_with_placeholders)
end
end
end
@sql_with_placeholders = sql_with_placeholders
if !positional_binds.empty?
@positional_binds = positional_binds
@named_binds = nil
else
@positional_binds = nil
@named_binds = named_binds
end
end
def hash
[self.class, sql_with_placeholders, positional_binds, named_binds].hash
end
def eql?(other)
self.class == other.class &&
sql_with_placeholders == other.sql_with_placeholders &&
positional_binds == other.positional_binds &&
named_binds == other.named_binds
end
alias :== :eql?
def +(other)
raise ArgumentError, "Expected Arel node" unless Arel.arel_node?(other)
Fragments.new([self, other])
end
def inspect
"#<#{self.class.name} #{sql_with_placeholders.inspect} #{(named_binds || positional_binds).inspect}>"
end
end
end
end