-
Notifications
You must be signed in to change notification settings - Fork 300
/
query_builder.rb
40 lines (34 loc) · 1.07 KB
/
query_builder.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
require 'uri'
module Quickbooks
module Util
class QueryBuilder
VALUE_QUOTE = "'"
def initialize
end
def clause(field, operator, value)
# replace with an escaped backslash
escape_single_quotes = -> field { field.to_s.gsub("'", "\\\\'") }
value = case value
when DateTime, Time
value.iso8601
when Date
value.strftime('%Y-%m-%d')
when Array
value = value.map(&escape_single_quotes)
when true, false
value
else
value = escape_single_quotes.call(value)
end
if operator.downcase == 'in' && value.is_a?(Array)
value = value.map { |v| "#{VALUE_QUOTE}#{v}#{VALUE_QUOTE}" }
"#{field} #{operator} (#{value.join(', ')})"
elsif value == true || value == false
"#{field} #{operator} #{value}"
else
"#{field} #{operator} #{VALUE_QUOTE}#{value}#{VALUE_QUOTE}"
end
end
end
end
end