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

adding default limits when there is an offset for sqlite and mysql [#5316 state:resolved]

This commit is contained in:
Aaron Patterson 2010-10-20 17:16:18 -07:00
parent 4041d7d33a
commit e42506f9ea
9 changed files with 75 additions and 1 deletions

View file

@ -73,6 +73,7 @@ lib/arel/visitors/mysql.rb
lib/arel/visitors/oracle.rb
lib/arel/visitors/order_clauses.rb
lib/arel/visitors/postgresql.rb
lib/arel/visitors/sqlite.rb
lib/arel/visitors/to_sql.rb
lib/arel/visitors/visitor.rb
lib/arel/visitors/where_sql.rb
@ -98,6 +99,8 @@ test/test_select_manager.rb
test/test_table.rb
test/test_update_manager.rb
test/visitors/test_join_sql.rb
test/visitors/test_mysql.rb
test/visitors/test_oracle.rb
test/visitors/test_postgres.rb
test/visitors/test_sqlite.rb
test/visitors/test_to_sql.rb

View file

@ -80,6 +80,10 @@ module Arel
from(self).take amount
end
def skip amount
from(self).skip amount
end
def having expr
from(self).having expr
end

View file

@ -1,5 +1,6 @@
require 'arel/visitors/visitor'
require 'arel/visitors/to_sql'
require 'arel/visitors/sqlite'
require 'arel/visitors/postgresql'
require 'arel/visitors/mysql'
require 'arel/visitors/oracle'
@ -15,6 +16,8 @@ module Arel
'mysql' => Arel::Visitors::MySQL,
'mysql2' => Arel::Visitors::MySQL,
'oracle_enhanced' => Arel::Visitors::Oracle,
'sqlite' => Arel::Visitors::SQLite,
'sqlite3' => Arel::Visitors::SQLite,
}
ENGINE_VISITORS = Hash.new do |hash, engine|

View file

@ -2,6 +2,13 @@ module Arel
module Visitors
class MySQL < Arel::Visitors::ToSql
private
###
# :'(
# http://dev.mysql.com/doc/refman/5.0/en/select.html#id3482214
def visit_Arel_Nodes_SelectStatement o
o.limit = 18446744073709551615 if o.offset && !o.limit
super
end
def visit_Arel_Nodes_UpdateStatement o
[

View file

@ -0,0 +1,11 @@
module Arel
module Visitors
class SQLite < Arel::Visitors::ToSql
private
def visit_Arel_Nodes_SelectStatement o
o.limit = -1 if o.offset && !o.limit
super
end
end
end
end

View file

@ -66,7 +66,7 @@ module FakeRecord
attr_reader :spec, :connection
def initialize
@spec = Spec.new(:adapter => 'sqlite3')
@spec = Spec.new(:adapter => 'america')
@connection = Connection.new
end

View file

@ -6,6 +6,13 @@ module Arel
@relation = Table.new(:users)
end
describe 'skip' do
it 'should add an offset' do
sm = @relation.skip 2
sm.to_sql.must_be_like "SELECT FROM \"users\" OFFSET 2"
end
end
describe 'primary_key' do
it 'should return an attribute' do
@relation.primary_key.name.must_equal :id

View file

@ -0,0 +1,21 @@
require 'helper'
module Arel
module Visitors
describe 'the mysql visitor' do
before do
@visitor = MySQL.new Table.engine
end
###
# :'(
# http://dev.mysql.com/doc/refman/5.0/en/select.html#id3482214
it 'defaults limit to 18446744073709551615' do
stmt = Nodes::SelectStatement.new
stmt.offset = Nodes::Offset.new(1)
sql = @visitor.accept(stmt)
sql.must_be_like "SELECT LIMIT 18446744073709551615 OFFSET 1"
end
end
end
end

View file

@ -0,0 +1,18 @@
require 'helper'
module Arel
module Visitors
describe 'the sqlite visitor' do
before do
@visitor = SQLite.new Table.engine
end
it 'defaults limit to -1' do
stmt = Nodes::SelectStatement.new
stmt.offset = Nodes::Offset.new(1)
sql = @visitor.accept(stmt)
sql.must_be_like "SELECT LIMIT -1 OFFSET 1"
end
end
end
end