This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 214
/
compat.rb
209 lines (169 loc) · 4.22 KB
/
compat.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
module Arel
class SelectManager
def as other
Nodes::TableAlias.new Nodes::SqlLiteral.new(other), Nodes::Grouping.new(@ast)
end
end
class Table
alias :table_name :name
def [] name
if table_exists?
columns.find { |column| column.name == name.to_sym }
else
::Arel::Attribute.new self, name.to_sym
end
end
def hash
[name, engine].hash
end
def eql?(other)
self.class == other.class &&
self.name == other.name &&
self.engine == other.engine
end
alias :== :eql?
end
module Attributes
class Attribute < Attribute.superclass
def hash
[relation, name].hash
end
def eql?(other)
self.class == other.class &&
self.relation == other.relation &&
self.name == other.name
end
alias :== :eql?
end
end
module Nodes
class Node
def not
Nodes::Not.new self
end
end
remove_const :And
class And < Arel::Nodes::Node
attr_reader :children
def initialize children, right = nil
unless Array === children
children = [children, right]
end
@children = children
end
def left
children.first
end
def right
children[1]
end
end
class Function < Arel::Nodes::Node
include Arel::Predications
def as aliaz
self.alias = SqlLiteral.new(aliaz)
self
end
end
class NamedFunction < Arel::Nodes::Function
attr_accessor :name, :distinct
def initialize name, expr, aliaz = nil
super(expr, aliaz)
@name = name
@distinct = false
end
end
class InfixOperation < Binary
include Arel::Expressions
include Arel::Predications
attr_reader :operator
def initialize operator, left, right
super(left, right)
@operator = operator
end
end
class Multiplication < InfixOperation
def initialize left, right
super(:*, left, right)
end
end
class Division < InfixOperation
def initialize left, right
super(:/, left, right)
end
end
class Addition < InfixOperation
def initialize left, right
super(:+, left, right)
end
end
class Subtraction < InfixOperation
def initialize left, right
super(:-, left, right)
end
end
class Grouping < Unary
include Arel::Predications
end unless Grouping.include?(Arel::Predications)
end
module Visitors
class ToSql
def column_for attr
name = attr.name.to_s
table = attr.relation.table_name
column_cache[table][name]
end
def column_cache
@column_cache ||= Hash.new do |hash, key|
hash[key] = Hash[
@engine.connection.columns(key, "#{key} Columns").map do |c|
[c.name, c]
end
]
end
end
def visit_Arel_Nodes_InfixOperation o
"#{visit o.left} #{o.operator} #{visit o.right}"
end
def visit_Arel_Nodes_NamedFunction o
"#{o.name}(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x|
visit x
}.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
end
def visit_Arel_Nodes_And o
o.children.map { |x| visit x }.join ' AND '
end
def visit_Arel_Nodes_Not o
"NOT (#{visit o.expr})"
end
def visit_Arel_Nodes_Values o
"VALUES (#{o.expressions.zip(o.columns).map { |value, attr|
if Nodes::SqlLiteral === value
visit_Arel_Nodes_SqlLiteral value
else
quote(value, attr && column_for(attr))
end
}.join ', '})"
end
def quote_table_name name
return name if Arel::Nodes::SqlLiteral === name
@quoted_tables[name] ||= @connection.quote_table_name(name)
end
end
end
module Predications
def as other
Nodes::As.new self, Nodes::SqlLiteral.new(other)
end
end
end
module ActiveRecord
module Reflection
class AssociationReflection < MacroReflection
alias :foreign_key :primary_key_name
def foreign_type
options[:foreign_type]
end
end
end
end