diff options
-rw-r--r-- | Manifest.txt | 3 | ||||
-rw-r--r-- | lib/arel/table.rb | 4 | ||||
-rw-r--r-- | lib/arel/visitors.rb | 3 | ||||
-rw-r--r-- | lib/arel/visitors/mysql.rb | 7 | ||||
-rw-r--r-- | lib/arel/visitors/sqlite.rb | 11 | ||||
-rw-r--r-- | test/support/fake_record.rb | 2 | ||||
-rw-r--r-- | test/test_table.rb | 7 | ||||
-rw-r--r-- | test/visitors/test_mysql.rb | 21 | ||||
-rw-r--r-- | test/visitors/test_sqlite.rb | 18 |
9 files changed, 75 insertions, 1 deletions
diff --git a/Manifest.txt b/Manifest.txt index 200bb5f5c0..1ea5768f06 100644 --- a/Manifest.txt +++ b/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 diff --git a/lib/arel/table.rb b/lib/arel/table.rb index 171d165bc2..61210dae76 100644 --- a/lib/arel/table.rb +++ b/lib/arel/table.rb @@ -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 diff --git a/lib/arel/visitors.rb b/lib/arel/visitors.rb index 01c6647039..5d4c6084b2 100644 --- a/lib/arel/visitors.rb +++ b/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 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| diff --git a/lib/arel/visitors/mysql.rb b/lib/arel/visitors/mysql.rb index 0c94ee1b27..594fd5504a 100644 --- a/lib/arel/visitors/mysql.rb +++ b/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 [ diff --git a/lib/arel/visitors/sqlite.rb b/lib/arel/visitors/sqlite.rb new file mode 100644 index 0000000000..c45160851d --- /dev/null +++ b/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 diff --git a/test/support/fake_record.rb b/test/support/fake_record.rb index ef3cc6a291..e0f9685c05 100644 --- a/test/support/fake_record.rb +++ b/test/support/fake_record.rb @@ -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 diff --git a/test/test_table.rb b/test/test_table.rb index 9a275402cb..d34383922c 100644 --- a/test/test_table.rb +++ b/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 diff --git a/test/visitors/test_mysql.rb b/test/visitors/test_mysql.rb new file mode 100644 index 0000000000..1d38161caf --- /dev/null +++ b/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 diff --git a/test/visitors/test_sqlite.rb b/test/visitors/test_sqlite.rb new file mode 100644 index 0000000000..fb8392c504 --- /dev/null +++ b/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 |