1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

adding an offset node

This commit is contained in:
Aaron Patterson 2010-09-10 09:47:50 -07:00
parent 9efae8f92d
commit ac2bdd1dd3
6 changed files with 39 additions and 1 deletions

View file

@ -8,6 +8,7 @@ require 'arel/nodes/lock'
require 'arel/nodes/function' require 'arel/nodes/function'
require 'arel/nodes/count' require 'arel/nodes/count'
require 'arel/nodes/values' require 'arel/nodes/values'
require 'arel/nodes/offset'
require 'arel/nodes/sum' require 'arel/nodes/sum'
require 'arel/nodes/max' require 'arel/nodes/max'
require 'arel/nodes/avg' require 'arel/nodes/avg'

11
lib/arel/nodes/offset.rb Normal file
View file

@ -0,0 +1,11 @@
module Arel
module Nodes
class Offset
attr_accessor :value
def initialize value
@value = value
end
end
end
end

View file

@ -2,13 +2,14 @@ module Arel
module Nodes module Nodes
class SelectStatement class SelectStatement
attr_reader :cores attr_reader :cores
attr_accessor :limit, :orders, :lock attr_accessor :limit, :orders, :lock, :offset
def initialize cores = [SelectCore.new] def initialize cores = [SelectCore.new]
@cores = cores @cores = cores
@orders = [] @orders = []
@limit = nil @limit = nil
@lock = nil @lock = nil
@offset = nil
end end
def initialize_copy other def initialize_copy other

View file

@ -12,6 +12,11 @@ module Arel
@head.limit @head.limit
end end
def skip amount
@head.offset = Nodes::Offset.new(amount)
self
end
def where_clauses def where_clauses
warn "where_clauses is deprecated" if $VERBOSE warn "where_clauses is deprecated" if $VERBOSE
to_sql = Visitors::ToSql.new @engine to_sql = Visitors::ToSql.new @engine

View file

@ -52,6 +52,7 @@ module Arel
o.cores.map { |x| visit x }.join, o.cores.map { |x| visit x }.join,
("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?), ("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?),
("LIMIT #{o.limit}" if o.limit), ("LIMIT #{o.limit}" if o.limit),
(visit(o.offset) if o.offset),
(visit(o.lock) if o.lock), (visit(o.lock) if o.lock),
].compact.join ' ' ].compact.join ' '
end end
@ -70,6 +71,10 @@ module Arel
"HAVING #{visit o.expr}" "HAVING #{visit o.expr}"
end end
def visit_Arel_Nodes_Offset o
"OFFSET #{visit o.value}"
end
# FIXME: this does nothing on SQLLite3, but should do things on other # FIXME: this does nothing on SQLLite3, but should do things on other
# databases. # databases.
def visit_Arel_Nodes_Lock o def visit_Arel_Nodes_Lock o

View file

@ -26,6 +26,21 @@ module Arel
end end
describe 'select manager' do describe 'select manager' do
describe 'skip' do
it 'should add an offset' do
table = Table.new :users
mgr = table.from table
mgr.skip 10
mgr.to_sql.should be_like %{ SELECT FROM "users" OFFSET 10 }
end
it 'should chain' do
table = Table.new :users
mgr = table.from table
mgr.skip(10).to_sql.should be_like %{ SELECT FROM "users" OFFSET 10 }
end
end
describe 'taken' do describe 'taken' do
it 'should return limit' do it 'should return limit' do
table = Table.new :users table = Table.new :users