-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathto_sql.rb
More file actions
74 lines (64 loc) · 1.52 KB
/
to_sql.rb
File metadata and controls
74 lines (64 loc) · 1.52 KB
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
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true
require File.expand_path('../to_sql/expression_factory', __FILE__)
require File.expand_path('../to_sql/container', __FILE__)
class Interpreter
class ToSql
def call(ast)
visit(ast)
end
# @api private
def visit(node)
return ToSql::ExpressionFactory.new.call(node) if node.node_with_attr_name? &&
Container::VISITORS_MAPPER.key?(node.left.value)
send(
:"visit_#{node.type}",
node.children[0],
node.children[1],
node.children[2]
)
end
# @api private
def visit_root(_value, left, _right)
visit(left)
end
# @api private
def visit_statement(value, left, right)
return visit(left) if right.nil?
case value
when :and
"#{visit(left)} AND #{visit(right)}"
when :or
"(#{visit(left)} OR #{visit(right)})"
else
''
end
end
# @api private
def visit_expression(value, left, right)
case value
when :op_equal
"#{visit(left)}=#{visit(right)}"
when :op_in
"#{visit(left)} IN(#{visit(right).join(',')})"
else
''
end
end
# @api private
def visit_attr_name(value, _left, _right)
value
end
# @api private
def visit_attr_value(value, _left, _right)
"'#{value}'"
end
# @api private
def visit_attr_array(value, left, right)
if right
["'#{value}'", visit(left), visit(right)].flatten
else
"'#{value}'"
end
end
end
end