Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get MSSQL working with ARel 2, refactor limit helper code into shared…
… module
- Loading branch information
1 parent
73857db
commit 8e824d0
Showing
3 changed files
with
69 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,44 @@ | ||
module Arel | ||
module Visitors | ||
class SQLServer < Arel::Visitors::ToSql | ||
include ArJdbc::MsSQL::LimitHelpers::SqlServerReplaceLimitOffset | ||
|
||
def select_count? o | ||
sel = o.cores.length == 1 && o.cores.first | ||
projections = sel.projections.length == 1 && sel.projections | ||
Arel::Nodes::Count === projections.first | ||
end | ||
|
||
# Need to mimic the subquery logic in ARel 1.x for select count with limit | ||
# See arel/engines/sql/compilers/mssql_compiler.rb for details | ||
def visit_Arel_Nodes_SelectStatement o | ||
order = "ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty? | ||
add_limit_offset([o.cores.map { |x| visit_Arel_Nodes_SelectCore x }.join, order].compact.join(' '), o, order) | ||
if o.limit | ||
if select_count?(o) | ||
subquery = true | ||
sql = o.cores.map do |x| | ||
x = x.dup | ||
x.projections = [Arel::Nodes::SqlLiteral.new("*")] | ||
visit_Arel_Nodes_SelectCore x | ||
end.join | ||
else | ||
sql = o.cores.map { |x| visit_Arel_Nodes_SelectCore x }.join | ||
end | ||
|
||
order ||= "ORDER BY #{@connection.determine_order_clause(sql)}" | ||
replace_limit_offset!(sql, o.limit.to_i, o.offset && o.offset.value.to_i, order) | ||
sql = "SELECT COUNT(*) AS count_id FROM (#{sql}) AS subquery" if subquery | ||
elsif order | ||
sql << " #{order}" | ||
else | ||
sql = super | ||
end | ||
sql | ||
end | ||
end | ||
|
||
class SQLServer2000 < SQLServer | ||
include ArJdbc::MsSQL::LimitHelpers::SqlServer2000ReplaceLimitOffset | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters