aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2011-04-29 10:05:54 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2011-04-29 10:05:54 -0700
commit9cd009b3728b07921cbfbfcf820748735c1de38c (patch)
treecabf9c424841a423a7290739e9369c611703692d
parentb4d6e5debc4f8293eb15cffed56d8a4dbf74378c (diff)
parent490d6f98f8974c717ddca29df162f5f1ad48e094 (diff)
downloadrails-9cd009b3728b07921cbfbfcf820748735c1de38c.tar.gz
rails-9cd009b3728b07921cbfbfcf820748735c1de38c.tar.bz2
rails-9cd009b3728b07921cbfbfcf820748735c1de38c.zip
Merge branch 'master' of github.com:rails/arel
* 'master' of github.com:rails/arel: replace 'LIMIT n' with 'FETCH FIRST n ROWS ONLY' when using ibm_db Generate more sqlish queue.
-rw-r--r--lib/arel/predications.rb12
-rw-r--r--lib/arel/visitors.rb2
-rw-r--r--lib/arel/visitors/ibm_db.rb12
-rw-r--r--test/visitors/test_ibm_db.rb27
4 files changed, 52 insertions, 1 deletions
diff --git a/lib/arel/predications.rb b/lib/arel/predications.rb
index 08cbf16d9d..36eae074a8 100644
--- a/lib/arel/predications.rb
+++ b/lib/arel/predications.rb
@@ -42,7 +42,17 @@ module Arel
Nodes::Between.new(self, Nodes::And.new([other.begin, other.end]))
end
else
- Nodes::In.new self, other
+ if other.include?(nil)
+ if other.size > 1
+ set = Nodes::In.new self, other.compact
+ null = Nodes::Equality.new self, nil
+ Nodes::Or.new set, null
+ else
+ Nodes::Equality.new self, nil
+ end
+ else
+ Nodes::In.new self, other
+ end
end
end
diff --git a/lib/arel/visitors.rb b/lib/arel/visitors.rb
index 8410b2b912..f2644d7205 100644
--- a/lib/arel/visitors.rb
+++ b/lib/arel/visitors.rb
@@ -10,6 +10,7 @@ require 'arel/visitors/join_sql'
require 'arel/visitors/where_sql'
require 'arel/visitors/order_clauses'
require 'arel/visitors/dot'
+require 'arel/visitors/ibm_db'
module Arel
module Visitors
@@ -22,6 +23,7 @@ module Arel
'oracle_enhanced' => Arel::Visitors::Oracle,
'sqlite' => Arel::Visitors::SQLite,
'sqlite3' => Arel::Visitors::SQLite,
+ 'ibm_db' => Arel::Visitors::IBM_DB,
}
ENGINE_VISITORS = Hash.new do |hash, engine|
diff --git a/lib/arel/visitors/ibm_db.rb b/lib/arel/visitors/ibm_db.rb
new file mode 100644
index 0000000000..0c26a3ae9e
--- /dev/null
+++ b/lib/arel/visitors/ibm_db.rb
@@ -0,0 +1,12 @@
+module Arel
+ module Visitors
+ class IBM_DB < Arel::Visitors::ToSql
+ private
+
+ def visit_Arel_Nodes_Limit o
+ "FETCH FIRST #{visit o.expr} ROWS ONLY"
+ end
+
+ end
+ end
+end
diff --git a/test/visitors/test_ibm_db.rb b/test/visitors/test_ibm_db.rb
new file mode 100644
index 0000000000..cc35bb768d
--- /dev/null
+++ b/test/visitors/test_ibm_db.rb
@@ -0,0 +1,27 @@
+require 'helper'
+
+module Arel
+ module Visitors
+ describe 'the ibm_db visitor' do
+ before do
+ @visitor = IBM_DB.new Table.engine
+ end
+
+ it 'uses FETCH FIRST n ROWS to limit results' do
+ stmt = Nodes::SelectStatement.new
+ stmt.limit = Nodes::Limit.new(1)
+ sql = @visitor.accept(stmt)
+ sql.must_be_like "SELECT FETCH FIRST 1 ROWS ONLY"
+ end
+
+ it 'uses FETCH FIRST n ROWS in updates with a limit' do
+ stmt = Nodes::UpdateStatement.new
+ stmt.limit = Nodes::Limit.new(1)
+ stmt.key = 'id'
+ sql = @visitor.accept(stmt)
+ sql.must_be_like "UPDATE NULL WHERE 'id' IN (SELECT 'id' FETCH FIRST 1 ROWS ONLY)"
+ end
+
+ end
+ end
+end