@@ -143,7 +143,9 @@ def visit_Arel_Nodes_SelectStatementWithOutOffset(o, windowed=false)
143143 [ ( "SELECT" if !windowed ) ,
144144 ( visit ( o . limit ) if o . limit && !windowed ) ,
145145 ( projections . map { |x | visit ( x ) } . join ( ', ' ) ) ,
146- visit ( core . source ) ,
146+ # TODO: [ARel 2.2] Use #from/#source vs. #froms
147+ # visit(core.source),
148+ ( "FROM #{ visit core . froms } " if core . froms ) ,
147149 ( visit ( o . lock ) if o . lock ) ,
148150 ( "WHERE #{ core . wheres . map { |x | visit ( x ) } . join ' AND ' } " unless core . wheres . empty? ) ,
149151 ( "GROUP BY #{ groups . map { |x | visit x } . join ', ' } " unless groups . empty? ) ,
@@ -175,7 +177,9 @@ def visit_Arel_Nodes_SelectStatementForComplexCount(o)
175177 ( visit ( o . limit ) if o . limit ) ,
176178 "ROW_NUMBER() OVER (ORDER BY #{ orders . map { |x | visit ( x ) } . join ( ', ' ) } ) AS [__rn]," ,
177179 "1 AS [count]" ,
178- visit ( core . source ) ,
180+ # TODO: [ARel 2.2] Use #from/#source vs. #froms
181+ # visit(core.source),
182+ ( "FROM #{ visit core . froms } " if core . froms ) ,
179183 ( visit ( o . lock ) if o . lock ) ,
180184 ( "WHERE #{ core . wheres . map { |x | visit ( x ) } . join ' AND ' } " unless core . wheres . empty? ) ,
181185 ( "GROUP BY #{ core . groups . map { |x | visit x } . join ', ' } " unless core . groups . empty? ) ,
@@ -191,20 +195,36 @@ def visit_Arel_Nodes_SelectStatementForComplexCount(o)
191195
192196 def table_from_select_statement ( o )
193197 core = o . cores . first
194- if Arel ::Table === core . from
195- core . from
196- elsif Arel ::Nodes ::SqlLiteral === core . from
197- Arel ::Table . new ( core . from , @engine )
198- elsif Arel ::Nodes ::JoinSource === core . source
199- Arel ::Nodes ::SqlLiteral === core . source . left ? Arel ::Table . new ( core . source . left , @engine ) : core . source . left
200- end
198+ # TODO: [ARel 2.2] Use #from/#source vs. #froms
199+ # if Arel::Table === core.from
200+ # core.from
201+ # elsif Arel::Nodes::SqlLiteral === core.from
202+ # Arel::Table.new(core.from, @engine)
203+ # elsif Arel::Nodes::JoinSource === core.source
204+ # Arel::Nodes::SqlLiteral === core.source.left ? Arel::Table.new(core.source.left, @engine) : core.source.left
205+ # end
206+ if Arel ::Table === core . froms
207+ core . froms
208+ elsif Arel ::Nodes ::SqlLiteral === core . froms
209+ Arel ::Table . new ( core . froms , @engine )
210+ elsif Arel ::Nodes ::Join === core . froms
211+ case x = core . froms . left
212+ when Arel ::Table
213+ x
214+ when Arel ::Nodes ::SqlLiteral
215+ Arel ::Table . new ( x , @engine )
216+ when Arel ::Nodes ::OuterJoin
217+ x . left
218+ end
219+ end
201220 end
202221
203222 def single_distinct_select_statement? ( o )
204223 projections = o . cores . first . projections
205- first_prjn = projections . first
224+ p1 = projections . first
206225 projections . size == 1 &&
207- ( ( first_prjn . respond_to? ( :distinct ) && first_prjn . distinct ) || first_prjn . include? ( 'DISTINCT' ) )
226+ ( ( p1 . respond_to? ( :distinct ) && p1 . distinct ) ||
227+ p1 . respond_to? ( :include? ) && p1 . include? ( 'DISTINCT' ) )
208228 end
209229
210230 def all_projections_aliased_in_select_statement? ( o )
@@ -226,7 +246,9 @@ def eager_limiting_select_statement?(o)
226246
227247 def join_in_select_statement? ( o )
228248 core = o . cores . first
229- core . source . right . any? { |x | Arel ::Nodes ::Join === x }
249+ # TODO: [ARel 2.2] Use #from/#source vs. #froms
250+ # core.source.right.any? { |x| Arel::Nodes::Join === x }
251+ Arel ::Nodes ::Join === core . froms
230252 end
231253
232254 def complex_count_sql? ( o )
@@ -239,9 +261,12 @@ def complex_count_sql?(o)
239261
240262 def find_and_fix_uncorrelated_joins_in_select_statement ( o )
241263 core = o . cores . first
242- return if !join_in_select_statement? ( o ) || core . source . right . size != 2
243- j1 = core . source . right . first
244- j2 = core . source . right . second
264+ # TODO: [ARel 2.2] Use #from/#source vs. #froms
265+ # return if !join_in_select_statement?(o) || core.source.right.size != 2
266+ # j1 = core.source.right.first
267+ # j2 = core.source.right.second
268+ return
269+
245270 return unless Arel ::Nodes ::OuterJoin === j1 && Arel ::Nodes ::StringJoin === j2
246271 j1_tn = j1 . left . name
247272 j2_tn = j2 . left . match ( /JOIN \[ (.*)\] .*ON/ ) . try ( :[] , 1 )
@@ -267,7 +292,8 @@ def rowtable_projections(o)
267292 Arel ::Nodes ::SqlLiteral . new x . split ( ',' ) . map { |y | y . split ( ' AS ' ) . last . strip } . join ( ', ' )
268293 end
269294 elsif function_select_statement? ( o )
270- [ Arel . star ]
295+ # TODO: [ARel 2.2] Use Arel.star
296+ [ Arel ::Nodes ::SqlLiteral . new '*' ]
271297 else
272298 tn = table_from_select_statement ( o ) . name
273299 core . projections . map { |x | x . gsub /\[ #{ tn } \] \. / , '[__rnt].' }
@@ -279,7 +305,7 @@ def rowtable_orders(o)
279305 if !o . orders . empty?
280306 o . orders
281307 elsif join_in_select_statement? ( o )
282- [ core . from . primary_key . asc ]
308+ [ table_from_select_statement ( o ) . primary_key . asc ]
283309 else
284310 [ table_from_select_statement ( o ) . primary_key . asc ]
285311 end . reverse . uniq . reverse
0 commit comments