Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding default limits when there is an offset for sqlite and mysql [#…

…5316 state:resolved]
  • Loading branch information...
commit e42506f9ea58511246f638e81002947ee3c36b18 1 parent 4041d7d
@tenderlove tenderlove authored
View
3  Manifest.txt
@@ -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
4 lib/arel/table.rb
@@ -80,6 +80,10 @@ def take amount
from(self).take amount
end
+ def skip amount
+ from(self).skip amount
+ end
+
def having expr
from(self).having expr
end
View
3  lib/arel/visitors.rb
@@ -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 Visitors
'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
7 lib/arel/visitors/mysql.rb
@@ -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
11 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
View
2  test/support/fake_record.rb
@@ -66,7 +66,7 @@ class Spec < Struct.new(:config)
attr_reader :spec, :connection
def initialize
- @spec = Spec.new(:adapter => 'sqlite3')
+ @spec = Spec.new(:adapter => 'america')
@connection = Connection.new
end
View
7 test/test_table.rb
@@ -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
21 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
View
18 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
Please sign in to comment.
Something went wrong with that request. Please try again.