Skip to content

Commit

Permalink
adding default limits when there is an offset for sqlite and mysql [#…
Browse files Browse the repository at this point in the history
…5316 state:resolved]
  • Loading branch information
tenderlove committed Oct 21, 2010
1 parent 4041d7d commit e42506f
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Manifest.txt
Expand Up @@ -73,6 +73,7 @@ lib/arel/visitors/mysql.rb
lib/arel/visitors/oracle.rb lib/arel/visitors/oracle.rb
lib/arel/visitors/order_clauses.rb lib/arel/visitors/order_clauses.rb
lib/arel/visitors/postgresql.rb lib/arel/visitors/postgresql.rb
lib/arel/visitors/sqlite.rb
lib/arel/visitors/to_sql.rb lib/arel/visitors/to_sql.rb
lib/arel/visitors/visitor.rb lib/arel/visitors/visitor.rb
lib/arel/visitors/where_sql.rb lib/arel/visitors/where_sql.rb
Expand All @@ -98,6 +99,8 @@ test/test_select_manager.rb
test/test_table.rb test/test_table.rb
test/test_update_manager.rb test/test_update_manager.rb
test/visitors/test_join_sql.rb test/visitors/test_join_sql.rb
test/visitors/test_mysql.rb
test/visitors/test_oracle.rb test/visitors/test_oracle.rb
test/visitors/test_postgres.rb test/visitors/test_postgres.rb
test/visitors/test_sqlite.rb
test/visitors/test_to_sql.rb test/visitors/test_to_sql.rb
4 changes: 4 additions & 0 deletions lib/arel/table.rb
Expand Up @@ -80,6 +80,10 @@ def take amount
from(self).take amount from(self).take amount
end end


def skip amount
from(self).skip amount
end

def having expr def having expr
from(self).having expr from(self).having expr
end end
Expand Down
3 changes: 3 additions & 0 deletions lib/arel/visitors.rb
@@ -1,5 +1,6 @@
require 'arel/visitors/visitor' require 'arel/visitors/visitor'
require 'arel/visitors/to_sql' require 'arel/visitors/to_sql'
require 'arel/visitors/sqlite'
require 'arel/visitors/postgresql' require 'arel/visitors/postgresql'
require 'arel/visitors/mysql' require 'arel/visitors/mysql'
require 'arel/visitors/oracle' require 'arel/visitors/oracle'
Expand All @@ -15,6 +16,8 @@ module Visitors
'mysql' => Arel::Visitors::MySQL, 'mysql' => Arel::Visitors::MySQL,
'mysql2' => Arel::Visitors::MySQL, 'mysql2' => Arel::Visitors::MySQL,
'oracle_enhanced' => Arel::Visitors::Oracle, 'oracle_enhanced' => Arel::Visitors::Oracle,
'sqlite' => Arel::Visitors::SQLite,
'sqlite3' => Arel::Visitors::SQLite,
} }


ENGINE_VISITORS = Hash.new do |hash, engine| ENGINE_VISITORS = Hash.new do |hash, engine|
Expand Down
7 changes: 7 additions & 0 deletions lib/arel/visitors/mysql.rb
Expand Up @@ -2,6 +2,13 @@ module Arel
module Visitors module Visitors
class MySQL < Arel::Visitors::ToSql class MySQL < Arel::Visitors::ToSql
private 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 def visit_Arel_Nodes_UpdateStatement o
[ [
Expand Down
11 changes: 11 additions & 0 deletions lib/arel/visitors/sqlite.rb
@@ -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
2 changes: 1 addition & 1 deletion test/support/fake_record.rb
Expand Up @@ -66,7 +66,7 @@ class Spec < Struct.new(:config)
attr_reader :spec, :connection attr_reader :spec, :connection


def initialize def initialize
@spec = Spec.new(:adapter => 'sqlite3') @spec = Spec.new(:adapter => 'america')
@connection = Connection.new @connection = Connection.new
end end


Expand Down
7 changes: 7 additions & 0 deletions test/test_table.rb
Expand Up @@ -6,6 +6,13 @@ module Arel
@relation = Table.new(:users) @relation = Table.new(:users)
end 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 describe 'primary_key' do
it 'should return an attribute' do it 'should return an attribute' do
@relation.primary_key.name.must_equal :id @relation.primary_key.name.must_equal :id
Expand Down
21 changes: 21 additions & 0 deletions test/visitors/test_mysql.rb
@@ -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
18 changes: 18 additions & 0 deletions test/visitors/test_sqlite.rb
@@ -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

0 comments on commit e42506f

Please sign in to comment.