Skip to content

Commit 36f860e

Browse files
committed
Make sure limit/offset are cast to integers. Also make sure we return SqlLiteral objects when removing expressions from projections.
1 parent 4d63805 commit 36f860e

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

lib/arel/visitors/sqlserver.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
module Arel
22

33
module Nodes
4+
45
class LockWithSQLServer < Arel::Nodes::Unary
56
end
7+
8+
class Limit < Arel::Nodes::Unary
9+
def initialize expr
10+
@expr = expr.to_i
11+
end
12+
end
13+
14+
class Offset < Arel::Nodes::Unary
15+
def initialize expr
16+
@expr = expr.to_i
17+
end
18+
end
19+
620
end
721

822
class SelectManager < Arel::TreeManager
@@ -101,7 +115,7 @@ def visit_Arel_Nodes_SelectStatementForComplexCount(o)
101115
# joins = correlated_safe_joins
102116
core = o.cores.first
103117
orders = rowtable_orders(o)
104-
o.limit.expr = o.limit.expr.to_i + o.offset.expr.to_i if o.limit
118+
o.limit.expr = o.limit.expr + o.offset.expr if o.limit
105119
[ "SELECT COUNT([count]) AS [count_id]",
106120
"FROM (",
107121
"SELECT",
@@ -186,13 +200,13 @@ def rowtable_orders(o)
186200
end
187201

188202
def projection_without_expression(projection)
189-
projection.to_s.split(',').map do |x|
203+
Arel::SqlLiteral.new(projection.split(',').map do |x|
190204
x.strip!
191205
x.sub!(/^(COUNT|SUM|MAX|MIN|AVG)\s*(\((.*)\))?/,'\3')
192206
x.sub!(/^DISTINCT\s*/,'')
193207
x.sub!(/TOP\s*\(\d+\)\s*/i,'')
194208
x.strip
195-
end.join(', ')
209+
end.join(', '))
196210
end
197211

198212
end

0 commit comments

Comments
 (0)