aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Gemfile4
-rw-r--r--History.txt23
-rw-r--r--Manifest.txt7
-rw-r--r--arel.gemspec22
-rw-r--r--lib/arel.rb2
-rw-r--r--lib/arel/nodes.rb3
-rw-r--r--lib/arel/nodes/ascending.rb23
-rw-r--r--lib/arel/nodes/descending.rb23
-rw-r--r--lib/arel/nodes/ordering.rb16
-rw-r--r--lib/arel/nodes/unary.rb1
-rw-r--r--lib/arel/order_predications.rb4
-rw-r--r--lib/arel/visitors.rb2
-rw-r--r--lib/arel/visitors/depth_first.rb4
-rw-r--r--lib/arel/visitors/dot.rb1
-rw-r--r--lib/arel/visitors/informix.rb33
-rw-r--r--lib/arel/visitors/mssql.rb68
-rw-r--r--lib/arel/visitors/mysql.rb22
-rw-r--r--lib/arel/visitors/to_sql.rb8
-rw-r--r--lib/arel/visitors/visitor.rb12
-rw-r--r--test/attributes/test_attribute.rb8
-rw-r--r--test/nodes/test_ascending.rb34
-rw-r--r--test/nodes/test_descending.rb34
-rw-r--r--test/nodes/test_infix_operation.rb4
-rw-r--r--test/test_select_manager.rb9
-rw-r--r--test/visitors/test_depth_first.rb2
-rw-r--r--test/visitors/test_dot.rb2
-rw-r--r--test/visitors/test_informix.rb42
-rw-r--r--test/visitors/test_mssql.rb55
-rw-r--r--test/visitors/test_mysql.rb10
-rw-r--r--test/visitors/test_to_sql.rb16
30 files changed, 431 insertions, 63 deletions
diff --git a/Gemfile b/Gemfile
new file mode 100644
index 0000000000..517921797a
--- /dev/null
+++ b/Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+gem 'hoe', '>= 2.1.0'
+gem 'minitest'
diff --git a/History.txt b/History.txt
index b5c37193ed..8b69413140 100644
--- a/History.txt
+++ b/History.txt
@@ -1,3 +1,26 @@
+== 2.1.4 / unreleased
+
+* Bug Fixes
+
+ * Fix depth-first traversal to understand ascending / descending nodes.
+ * Parentheis are suppressed with nested unions in MySQL. Thanks jhtwong!
+
+== 2.1.3 / 2011-06-27
+
+* Bug Fixues
+
+ * Fixed broken gem build.
+
+== 2.1.2 / 2011-06-27
+
+* Bug Fixes
+
+ * Visitors can define their own cache strategey so caches are not shared.
+ Fixes #57
+ * Informix support fixed. Thanks Khronos.
+ * Ordering nodes broken to subclasses. Thanks Ernie Miller!
+ * Reversal supported in ordering nodes. Thanks Ernie Miller!
+
== 2.1.1 / 2011/05/14
* Bug fixes
diff --git a/Manifest.txt b/Manifest.txt
index d7a57e0995..16333b7cf2 100644
--- a/Manifest.txt
+++ b/Manifest.txt
@@ -21,9 +21,11 @@ lib/arel/insert_manager.rb
lib/arel/math.rb
lib/arel/nodes.rb
lib/arel/nodes/and.rb
+lib/arel/nodes/ascending.rb
lib/arel/nodes/binary.rb
lib/arel/nodes/count.rb
lib/arel/nodes/delete_statement.rb
+lib/arel/nodes/descending.rb
lib/arel/nodes/equality.rb
lib/arel/nodes/function.rb
lib/arel/nodes/in.rb
@@ -59,6 +61,7 @@ lib/arel/visitors.rb
lib/arel/visitors/depth_first.rb
lib/arel/visitors/dot.rb
lib/arel/visitors/ibm_db.rb
+lib/arel/visitors/informix.rb
lib/arel/visitors/join_sql.rb
lib/arel/visitors/mssql.rb
lib/arel/visitors/mysql.rb
@@ -72,10 +75,13 @@ lib/arel/visitors/where_sql.rb
test/attributes/test_attribute.rb
test/helper.rb
test/nodes/test_as.rb
+test/nodes/test_ascending.rb
test/nodes/test_bin.rb
test/nodes/test_count.rb
test/nodes/test_delete_statement.rb
+test/nodes/test_descending.rb
test/nodes/test_equality.rb
+test/nodes/test_infix_operation.rb
test/nodes/test_insert_statement.rb
test/nodes/test_named_function.rb
test/nodes/test_node.rb
@@ -99,6 +105,7 @@ test/test_update_manager.rb
test/visitors/test_depth_first.rb
test/visitors/test_dot.rb
test/visitors/test_ibm_db.rb
+test/visitors/test_informix.rb
test/visitors/test_join_sql.rb
test/visitors/test_mssql.rb
test/visitors/test_mysql.rb
diff --git a/arel.gemspec b/arel.gemspec
index 85dc329eda..174992ee0e 100644
--- a/arel.gemspec
+++ b/arel.gemspec
@@ -2,11 +2,11 @@
Gem::Specification.new do |s|
s.name = %q{arel}
- s.version = "2.1.1.20110518143805"
+ s.version = "2.1.3.20110627095956"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = [%q{Aaron Patterson}, %q{Bryan Halmkamp}, %q{Emilio Tagua}, %q{Nick Kallen}]
- s.date = %q{2011-05-18}
+ s.date = %q{2011-06-27}
s.description = %q{Arel is a SQL AST manager for Ruby. It
1. Simplifies the generation complex of SQL queries
@@ -17,27 +17,27 @@ with it, focusing on innovative object and collection modeling as opposed to
database compatibility and query generation.}
s.email = [%q{aaron@tenderlovemaking.com}, %q{bryan@brynary.com}, %q{miloops@gmail.com}, %q{nick@example.org}]
s.extra_rdoc_files = [%q{History.txt}, %q{MIT-LICENSE.txt}, %q{Manifest.txt}, %q{README.markdown}]
- s.files = [%q{.autotest}, %q{.gemtest}, %q{History.txt}, %q{MIT-LICENSE.txt}, %q{Manifest.txt}, %q{README.markdown}, %q{Rakefile}, %q{arel.gemspec}, %q{lib/arel.rb}, %q{lib/arel/alias_predication.rb}, %q{lib/arel/attributes.rb}, %q{lib/arel/attributes/attribute.rb}, %q{lib/arel/compatibility/wheres.rb}, %q{lib/arel/crud.rb}, %q{lib/arel/delete_manager.rb}, %q{lib/arel/deprecated.rb}, %q{lib/arel/expression.rb}, %q{lib/arel/expressions.rb}, %q{lib/arel/factory_methods.rb}, %q{lib/arel/insert_manager.rb}, %q{lib/arel/math.rb}, %q{lib/arel/nodes.rb}, %q{lib/arel/nodes/and.rb}, %q{lib/arel/nodes/binary.rb}, %q{lib/arel/nodes/count.rb}, %q{lib/arel/nodes/delete_statement.rb}, %q{lib/arel/nodes/equality.rb}, %q{lib/arel/nodes/function.rb}, %q{lib/arel/nodes/in.rb}, %q{lib/arel/nodes/infix_operation.rb}, %q{lib/arel/nodes/inner_join.rb}, %q{lib/arel/nodes/insert_statement.rb}, %q{lib/arel/nodes/join_source.rb}, %q{lib/arel/nodes/named_function.rb}, %q{lib/arel/nodes/node.rb}, %q{lib/arel/nodes/ordering.rb}, %q{lib/arel/nodes/outer_join.rb}, %q{lib/arel/nodes/select_core.rb}, %q{lib/arel/nodes/select_statement.rb}, %q{lib/arel/nodes/sql_literal.rb}, %q{lib/arel/nodes/string_join.rb}, %q{lib/arel/nodes/table_alias.rb}, %q{lib/arel/nodes/terminal.rb}, %q{lib/arel/nodes/unary.rb}, %q{lib/arel/nodes/unqualified_column.rb}, %q{lib/arel/nodes/update_statement.rb}, %q{lib/arel/nodes/values.rb}, %q{lib/arel/nodes/with.rb}, %q{lib/arel/order_predications.rb}, %q{lib/arel/predications.rb}, %q{lib/arel/relation.rb}, %q{lib/arel/select_manager.rb}, %q{lib/arel/sql/engine.rb}, %q{lib/arel/sql_literal.rb}, %q{lib/arel/table.rb}, %q{lib/arel/tree_manager.rb}, %q{lib/arel/update_manager.rb}, %q{lib/arel/visitors.rb}, %q{lib/arel/visitors/depth_first.rb}, %q{lib/arel/visitors/dot.rb}, %q{lib/arel/visitors/ibm_db.rb}, %q{lib/arel/visitors/join_sql.rb}, %q{lib/arel/visitors/mssql.rb}, %q{lib/arel/visitors/mysql.rb}, %q{lib/arel/visitors/oracle.rb}, %q{lib/arel/visitors/order_clauses.rb}, %q{lib/arel/visitors/postgresql.rb}, %q{lib/arel/visitors/sqlite.rb}, %q{lib/arel/visitors/to_sql.rb}, %q{lib/arel/visitors/visitor.rb}, %q{lib/arel/visitors/where_sql.rb}, %q{test/attributes/test_attribute.rb}, %q{test/helper.rb}, %q{test/nodes/test_as.rb}, %q{test/nodes/test_bin.rb}, %q{test/nodes/test_count.rb}, %q{test/nodes/test_delete_statement.rb}, %q{test/nodes/test_equality.rb}, %q{test/nodes/test_insert_statement.rb}, %q{test/nodes/test_named_function.rb}, %q{test/nodes/test_node.rb}, %q{test/nodes/test_not.rb}, %q{test/nodes/test_or.rb}, %q{test/nodes/test_select_core.rb}, %q{test/nodes/test_select_statement.rb}, %q{test/nodes/test_sql_literal.rb}, %q{test/nodes/test_sum.rb}, %q{test/nodes/test_update_statement.rb}, %q{test/support/fake_record.rb}, %q{test/test_activerecord_compat.rb}, %q{test/test_attributes.rb}, %q{test/test_crud.rb}, %q{test/test_delete_manager.rb}, %q{test/test_factory_methods.rb}, %q{test/test_insert_manager.rb}, %q{test/test_select_manager.rb}, %q{test/test_table.rb}, %q{test/test_update_manager.rb}, %q{test/visitors/test_depth_first.rb}, %q{test/visitors/test_dot.rb}, %q{test/visitors/test_ibm_db.rb}, %q{test/visitors/test_join_sql.rb}, %q{test/visitors/test_mssql.rb}, %q{test/visitors/test_mysql.rb}, %q{test/visitors/test_oracle.rb}, %q{test/visitors/test_postgres.rb}, %q{test/visitors/test_sqlite.rb}, %q{test/visitors/test_to_sql.rb}, %q{test/nodes/test_infix_operation.rb}]
+ s.files = [%q{.autotest}, %q{.gemtest}, %q{History.txt}, %q{MIT-LICENSE.txt}, %q{Manifest.txt}, %q{README.markdown}, %q{Rakefile}, %q{arel.gemspec}, %q{lib/arel.rb}, %q{lib/arel/alias_predication.rb}, %q{lib/arel/attributes.rb}, %q{lib/arel/attributes/attribute.rb}, %q{lib/arel/compatibility/wheres.rb}, %q{lib/arel/crud.rb}, %q{lib/arel/delete_manager.rb}, %q{lib/arel/deprecated.rb}, %q{lib/arel/expression.rb}, %q{lib/arel/expressions.rb}, %q{lib/arel/factory_methods.rb}, %q{lib/arel/insert_manager.rb}, %q{lib/arel/math.rb}, %q{lib/arel/nodes.rb}, %q{lib/arel/nodes/and.rb}, %q{lib/arel/nodes/ascending.rb}, %q{lib/arel/nodes/binary.rb}, %q{lib/arel/nodes/count.rb}, %q{lib/arel/nodes/delete_statement.rb}, %q{lib/arel/nodes/descending.rb}, %q{lib/arel/nodes/equality.rb}, %q{lib/arel/nodes/function.rb}, %q{lib/arel/nodes/in.rb}, %q{lib/arel/nodes/infix_operation.rb}, %q{lib/arel/nodes/inner_join.rb}, %q{lib/arel/nodes/insert_statement.rb}, %q{lib/arel/nodes/join_source.rb}, %q{lib/arel/nodes/named_function.rb}, %q{lib/arel/nodes/node.rb}, %q{lib/arel/nodes/ordering.rb}, %q{lib/arel/nodes/outer_join.rb}, %q{lib/arel/nodes/select_core.rb}, %q{lib/arel/nodes/select_statement.rb}, %q{lib/arel/nodes/sql_literal.rb}, %q{lib/arel/nodes/string_join.rb}, %q{lib/arel/nodes/table_alias.rb}, %q{lib/arel/nodes/terminal.rb}, %q{lib/arel/nodes/unary.rb}, %q{lib/arel/nodes/unqualified_column.rb}, %q{lib/arel/nodes/update_statement.rb}, %q{lib/arel/nodes/values.rb}, %q{lib/arel/nodes/with.rb}, %q{lib/arel/order_predications.rb}, %q{lib/arel/predications.rb}, %q{lib/arel/relation.rb}, %q{lib/arel/select_manager.rb}, %q{lib/arel/sql/engine.rb}, %q{lib/arel/sql_literal.rb}, %q{lib/arel/table.rb}, %q{lib/arel/tree_manager.rb}, %q{lib/arel/update_manager.rb}, %q{lib/arel/visitors.rb}, %q{lib/arel/visitors/depth_first.rb}, %q{lib/arel/visitors/dot.rb}, %q{lib/arel/visitors/ibm_db.rb}, %q{lib/arel/visitors/informix.rb}, %q{lib/arel/visitors/join_sql.rb}, %q{lib/arel/visitors/mssql.rb}, %q{lib/arel/visitors/mysql.rb}, %q{lib/arel/visitors/oracle.rb}, %q{lib/arel/visitors/order_clauses.rb}, %q{lib/arel/visitors/postgresql.rb}, %q{lib/arel/visitors/sqlite.rb}, %q{lib/arel/visitors/to_sql.rb}, %q{lib/arel/visitors/visitor.rb}, %q{lib/arel/visitors/where_sql.rb}, %q{test/attributes/test_attribute.rb}, %q{test/helper.rb}, %q{test/nodes/test_as.rb}, %q{test/nodes/test_ascending.rb}, %q{test/nodes/test_bin.rb}, %q{test/nodes/test_count.rb}, %q{test/nodes/test_delete_statement.rb}, %q{test/nodes/test_descending.rb}, %q{test/nodes/test_equality.rb}, %q{test/nodes/test_infix_operation.rb}, %q{test/nodes/test_insert_statement.rb}, %q{test/nodes/test_named_function.rb}, %q{test/nodes/test_node.rb}, %q{test/nodes/test_not.rb}, %q{test/nodes/test_or.rb}, %q{test/nodes/test_select_core.rb}, %q{test/nodes/test_select_statement.rb}, %q{test/nodes/test_sql_literal.rb}, %q{test/nodes/test_sum.rb}, %q{test/nodes/test_update_statement.rb}, %q{test/support/fake_record.rb}, %q{test/test_activerecord_compat.rb}, %q{test/test_attributes.rb}, %q{test/test_crud.rb}, %q{test/test_delete_manager.rb}, %q{test/test_factory_methods.rb}, %q{test/test_insert_manager.rb}, %q{test/test_select_manager.rb}, %q{test/test_table.rb}, %q{test/test_update_manager.rb}, %q{test/visitors/test_depth_first.rb}, %q{test/visitors/test_dot.rb}, %q{test/visitors/test_ibm_db.rb}, %q{test/visitors/test_informix.rb}, %q{test/visitors/test_join_sql.rb}, %q{test/visitors/test_mssql.rb}, %q{test/visitors/test_mysql.rb}, %q{test/visitors/test_oracle.rb}, %q{test/visitors/test_postgres.rb}, %q{test/visitors/test_sqlite.rb}, %q{test/visitors/test_to_sql.rb}]
s.homepage = %q{http://github.com/rails/arel}
s.rdoc_options = [%q{--main}, %q{README.markdown}]
s.require_paths = [%q{lib}]
s.rubyforge_project = %q{arel}
- s.rubygems_version = %q{1.8.2}
+ s.rubygems_version = %q{1.8.5}
s.summary = %q{Arel is a SQL AST manager for Ruby}
- s.test_files = [%q{test/attributes/test_attribute.rb}, %q{test/nodes/test_as.rb}, %q{test/nodes/test_bin.rb}, %q{test/nodes/test_count.rb}, %q{test/nodes/test_delete_statement.rb}, %q{test/nodes/test_equality.rb}, %q{test/nodes/test_infix_operation.rb}, %q{test/nodes/test_insert_statement.rb}, %q{test/nodes/test_named_function.rb}, %q{test/nodes/test_node.rb}, %q{test/nodes/test_not.rb}, %q{test/nodes/test_or.rb}, %q{test/nodes/test_select_core.rb}, %q{test/nodes/test_select_statement.rb}, %q{test/nodes/test_sql_literal.rb}, %q{test/nodes/test_sum.rb}, %q{test/nodes/test_update_statement.rb}, %q{test/test_activerecord_compat.rb}, %q{test/test_attributes.rb}, %q{test/test_crud.rb}, %q{test/test_delete_manager.rb}, %q{test/test_factory_methods.rb}, %q{test/test_insert_manager.rb}, %q{test/test_select_manager.rb}, %q{test/test_table.rb}, %q{test/test_update_manager.rb}, %q{test/visitors/test_depth_first.rb}, %q{test/visitors/test_dot.rb}, %q{test/visitors/test_ibm_db.rb}, %q{test/visitors/test_join_sql.rb}, %q{test/visitors/test_mssql.rb}, %q{test/visitors/test_mysql.rb}, %q{test/visitors/test_oracle.rb}, %q{test/visitors/test_postgres.rb}, %q{test/visitors/test_sqlite.rb}, %q{test/visitors/test_to_sql.rb}]
+ s.test_files = [%q{test/attributes/test_attribute.rb}, %q{test/nodes/test_as.rb}, %q{test/nodes/test_ascending.rb}, %q{test/nodes/test_bin.rb}, %q{test/nodes/test_count.rb}, %q{test/nodes/test_delete_statement.rb}, %q{test/nodes/test_descending.rb}, %q{test/nodes/test_equality.rb}, %q{test/nodes/test_infix_operation.rb}, %q{test/nodes/test_insert_statement.rb}, %q{test/nodes/test_named_function.rb}, %q{test/nodes/test_node.rb}, %q{test/nodes/test_not.rb}, %q{test/nodes/test_or.rb}, %q{test/nodes/test_select_core.rb}, %q{test/nodes/test_select_statement.rb}, %q{test/nodes/test_sql_literal.rb}, %q{test/nodes/test_sum.rb}, %q{test/nodes/test_update_statement.rb}, %q{test/test_activerecord_compat.rb}, %q{test/test_attributes.rb}, %q{test/test_crud.rb}, %q{test/test_delete_manager.rb}, %q{test/test_factory_methods.rb}, %q{test/test_insert_manager.rb}, %q{test/test_select_manager.rb}, %q{test/test_table.rb}, %q{test/test_update_manager.rb}, %q{test/visitors/test_depth_first.rb}, %q{test/visitors/test_dot.rb}, %q{test/visitors/test_ibm_db.rb}, %q{test/visitors/test_informix.rb}, %q{test/visitors/test_join_sql.rb}, %q{test/visitors/test_mssql.rb}, %q{test/visitors/test_mysql.rb}, %q{test/visitors/test_oracle.rb}, %q{test/visitors/test_postgres.rb}, %q{test/visitors/test_sqlite.rb}, %q{test/visitors/test_to_sql.rb}]
if s.respond_to? :specification_version then
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
- s.add_development_dependency(%q<minitest>, [">= 2.0.2"])
- s.add_development_dependency(%q<hoe>, [">= 2.9.1"])
+ s.add_development_dependency(%q<minitest>, [">= 2.2.2"])
+ s.add_development_dependency(%q<hoe>, [">= 2.9.4"])
else
- s.add_dependency(%q<minitest>, [">= 2.0.2"])
- s.add_dependency(%q<hoe>, [">= 2.9.1"])
+ s.add_dependency(%q<minitest>, [">= 2.2.2"])
+ s.add_dependency(%q<hoe>, [">= 2.9.4"])
end
else
- s.add_dependency(%q<minitest>, [">= 2.0.2"])
- s.add_dependency(%q<hoe>, [">= 2.9.1"])
+ s.add_dependency(%q<minitest>, [">= 2.2.2"])
+ s.add_dependency(%q<hoe>, [">= 2.9.4"])
end
end
diff --git a/lib/arel.rb b/lib/arel.rb
index 6ef2f52a21..58fa2ea77f 100644
--- a/lib/arel.rb
+++ b/lib/arel.rb
@@ -33,7 +33,7 @@ require 'arel/sql_literal'
####
module Arel
- VERSION = '2.1.1'
+ VERSION = '2.1.3'
def self.sql raw_sql
Arel::Nodes::SqlLiteral.new raw_sql
diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb
index 9576930a54..9edf3a9a95 100644
--- a/lib/arel/nodes.rb
+++ b/lib/arel/nodes.rb
@@ -11,6 +11,8 @@ require 'arel/nodes/terminal'
# unary
require 'arel/nodes/unary'
+require 'arel/nodes/ascending'
+require 'arel/nodes/descending'
require 'arel/nodes/unqualified_column'
require 'arel/nodes/with'
@@ -19,7 +21,6 @@ require 'arel/nodes/binary'
require 'arel/nodes/equality'
require 'arel/nodes/in' # Why is this subclassed from equality?
require 'arel/nodes/join_source'
-require 'arel/nodes/ordering'
require 'arel/nodes/delete_statement'
require 'arel/nodes/table_alias'
require 'arel/nodes/infix_operation'
diff --git a/lib/arel/nodes/ascending.rb b/lib/arel/nodes/ascending.rb
new file mode 100644
index 0000000000..bca00a8339
--- /dev/null
+++ b/lib/arel/nodes/ascending.rb
@@ -0,0 +1,23 @@
+module Arel
+ module Nodes
+ class Ascending < Ordering
+
+ def reverse
+ Descending.new(expr)
+ end
+
+ def direction
+ :asc
+ end
+
+ def ascending?
+ true
+ end
+
+ def descending?
+ false
+ end
+
+ end
+ end
+end
diff --git a/lib/arel/nodes/descending.rb b/lib/arel/nodes/descending.rb
new file mode 100644
index 0000000000..d886bdcb5f
--- /dev/null
+++ b/lib/arel/nodes/descending.rb
@@ -0,0 +1,23 @@
+module Arel
+ module Nodes
+ class Descending < Ordering
+
+ def reverse
+ Ascending.new(expr)
+ end
+
+ def direction
+ :desc
+ end
+
+ def ascending?
+ false
+ end
+
+ def descending?
+ true
+ end
+
+ end
+ end
+end
diff --git a/lib/arel/nodes/ordering.rb b/lib/arel/nodes/ordering.rb
index 0a3621cf54..efb4d18ae4 100644
--- a/lib/arel/nodes/ordering.rb
+++ b/lib/arel/nodes/ordering.rb
@@ -1,20 +1,6 @@
module Arel
module Nodes
- class Ordering < Arel::Nodes::Binary
- alias :expr :left
- alias :direction :right
-
- def initialize expr, direction = :asc
- super
- end
-
- def ascending?
- direction == :asc
- end
-
- def descending?
- direction == :desc
- end
+ class Ordering < Unary
end
end
end
diff --git a/lib/arel/nodes/unary.rb b/lib/arel/nodes/unary.rb
index 5c4add4792..4688fff623 100644
--- a/lib/arel/nodes/unary.rb
+++ b/lib/arel/nodes/unary.rb
@@ -18,6 +18,7 @@ module Arel
Not
Offset
On
+ Ordering
Top
Lock
DistinctOn
diff --git a/lib/arel/order_predications.rb b/lib/arel/order_predications.rb
index af163c9454..153fcffb41 100644
--- a/lib/arel/order_predications.rb
+++ b/lib/arel/order_predications.rb
@@ -2,11 +2,11 @@ module Arel
module OrderPredications
def asc
- Nodes::Ordering.new self, :asc
+ Nodes::Ascending.new self
end
def desc
- Nodes::Ordering.new self, :desc
+ Nodes::Descending.new self
end
end
diff --git a/lib/arel/visitors.rb b/lib/arel/visitors.rb
index f2644d7205..8276eace2b 100644
--- a/lib/arel/visitors.rb
+++ b/lib/arel/visitors.rb
@@ -11,6 +11,7 @@ require 'arel/visitors/where_sql'
require 'arel/visitors/order_clauses'
require 'arel/visitors/dot'
require 'arel/visitors/ibm_db'
+require 'arel/visitors/informix'
module Arel
module Visitors
@@ -24,6 +25,7 @@ module Arel
'sqlite' => Arel::Visitors::SQLite,
'sqlite3' => Arel::Visitors::SQLite,
'ibm_db' => Arel::Visitors::IBM_DB,
+ 'informix' => Arel::Visitors::Informix,
}
ENGINE_VISITORS = Hash.new do |hash, engine|
diff --git a/lib/arel/visitors/depth_first.rb b/lib/arel/visitors/depth_first.rb
index 914b2d1999..43d186cc1a 100644
--- a/lib/arel/visitors/depth_first.rb
+++ b/lib/arel/visitors/depth_first.rb
@@ -22,6 +22,9 @@ module Arel
alias :visit_Arel_Nodes_Not :unary
alias :visit_Arel_Nodes_Offset :unary
alias :visit_Arel_Nodes_On :unary
+ alias :visit_Arel_Nodes_Ordering :unary
+ alias :visit_Arel_Nodes_Ascending :unary
+ alias :visit_Arel_Nodes_Descending :unary
alias :visit_Arel_Nodes_Top :unary
alias :visit_Arel_Nodes_UnqualifiedColumn :unary
@@ -75,7 +78,6 @@ module Arel
alias :visit_Arel_Nodes_NotEqual :binary
alias :visit_Arel_Nodes_NotIn :binary
alias :visit_Arel_Nodes_Or :binary
- alias :visit_Arel_Nodes_Ordering :binary
alias :visit_Arel_Nodes_OuterJoin :binary
alias :visit_Arel_Nodes_TableAlias :binary
alias :visit_Arel_Nodes_Values :binary
diff --git a/lib/arel/visitors/dot.rb b/lib/arel/visitors/dot.rb
index 92d05c7b67..8303279211 100644
--- a/lib/arel/visitors/dot.rb
+++ b/lib/arel/visitors/dot.rb
@@ -30,7 +30,6 @@ module Arel
private
def visit_Arel_Nodes_Ordering o
visit_edge o, "expr"
- visit_edge o, "direction"
end
def visit_Arel_Nodes_TableAlias o
diff --git a/lib/arel/visitors/informix.rb b/lib/arel/visitors/informix.rb
new file mode 100644
index 0000000000..25eef1e6de
--- /dev/null
+++ b/lib/arel/visitors/informix.rb
@@ -0,0 +1,33 @@
+module Arel
+ module Visitors
+ class Informix < Arel::Visitors::ToSql
+ private
+ def visit_Arel_Nodes_SelectStatement o
+ [
+ "SELECT",
+ (visit(o.offset) if o.offset),
+ (visit(o.limit) if o.limit),
+ o.cores.map { |x| visit_Arel_Nodes_SelectCore x }.join,
+ ("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?),
+ (visit(o.lock) if o.lock),
+ ].compact.join ' '
+ end
+ def visit_Arel_Nodes_SelectCore o
+ [
+ "#{o.projections.map { |x| visit x }.join ', '}",
+ ("FROM #{visit o.froms}" if o.froms),
+ ("WHERE #{o.wheres.map { |x| visit x }.join ' AND ' }" unless o.wheres.empty?),
+ ("GROUP BY #{o.groups.map { |x| visit x }.join ', ' }" unless o.groups.empty?),
+ (visit(o.having) if o.having),
+ ].compact.join ' '
+ end
+ def visit_Arel_Nodes_Offset o
+ "SKIP #{visit o.expr}"
+ end
+ def visit_Arel_Nodes_Limit o
+ "LIMIT #{visit o.expr}"
+ end
+ end
+ end
+end
+
diff --git a/lib/arel/visitors/mssql.rb b/lib/arel/visitors/mssql.rb
index ea7ab6394c..23dc06a936 100644
--- a/lib/arel/visitors/mssql.rb
+++ b/lib/arel/visitors/mssql.rb
@@ -3,21 +3,71 @@ module Arel
class MSSQL < Arel::Visitors::ToSql
private
- def build_subselect key, o
- stmt = super
- core = stmt.cores.first
- core.top = Nodes::Top.new(o.limit.expr) if o.limit
- stmt
+ # `top` wouldn't really work here. I.e. User.select("distinct first_name").limit(10) would generate
+ # "select top 10 distinct first_name from users", which is invalid query! it should be
+ # "select distinct top 10 first_name from users"
+ def visit_Arel_Nodes_Top o
+ ""
end
- def visit_Arel_Nodes_Limit o
- ""
+ def visit_Arel_Nodes_SelectStatement o
+ if !o.limit && !o.offset
+ return super o
+ end
+
+ select_order_by = "ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?
+
+ is_select_count = false
+ sql = o.cores.map { |x|
+ core_order_by = select_order_by || determine_order_by(x)
+ if select_count? x
+ x.projections = [row_num_literal(core_order_by)]
+ is_select_count = true
+ else
+ x.projections << row_num_literal(core_order_by)
+ end
+
+ visit_Arel_Nodes_SelectCore x
+ }.join
+
+ sql = "SELECT _t.* FROM (#{sql}) as _t WHERE #{get_offset_limit_clause(o)}"
+ # fixme count distinct wouldn't work with limit or offset
+ sql = "SELECT COUNT(1) as count_id FROM (#{sql}) AS subquery" if is_select_count
+ sql
end
- def visit_Arel_Nodes_Top o
- "TOP #{visit o.expr}"
+ def get_offset_limit_clause o
+ first_row = o.offset ? o.offset.expr.to_i + 1 : 1
+ last_row = o.limit ? o.limit.expr.to_i - 1 + first_row : nil
+ if last_row
+ " _row_num BETWEEN #{first_row} AND #{last_row}"
+ else
+ " _row_num >= #{first_row}"
+ end
end
+ def determine_order_by x
+ unless x.groups.empty?
+ "ORDER BY #{x.groups.map { |g| visit g }.join ', ' }"
+ else
+ "ORDER BY #{find_left_table_pk(x.froms)}"
+ end
+ end
+
+ def row_num_literal order_by
+ Nodes::SqlLiteral.new("ROW_NUMBER() OVER (#{order_by}) as _row_num")
+ end
+
+ def select_count? x
+ x.projections.length == 1 && Arel::Nodes::Count === x.projections.first
+ end
+
+ # fixme raise exception of there is no pk?
+ # fixme!! Table.primary_key will be depricated. What is the replacement??
+ def find_left_table_pk o
+ return visit o.primary_key if o.instance_of? Arel::Table
+ find_left_table_pk o.left if o.kind_of? Arel::Nodes::Join
+ end
end
end
end
diff --git a/lib/arel/visitors/mysql.rb b/lib/arel/visitors/mysql.rb
index 763cf11aad..70166a15c5 100644
--- a/lib/arel/visitors/mysql.rb
+++ b/lib/arel/visitors/mysql.rb
@@ -2,6 +2,28 @@ module Arel
module Visitors
class MySQL < Arel::Visitors::ToSql
private
+ def visit_Arel_Nodes_Union o, suppress_parens = false
+ left_result = case o.left
+ when Arel::Nodes::Union
+ visit_Arel_Nodes_Union o.left, true
+ else
+ visit o.left
+ end
+
+ right_result = case o.right
+ when Arel::Nodes::Union
+ visit_Arel_Nodes_Union o.right, true
+ else
+ visit o.right
+ end
+
+ if suppress_parens
+ "#{left_result} UNION #{right_result}"
+ else
+ "( #{left_result} UNION #{right_result} )"
+ end
+ end
+
def visit_Arel_Nodes_Bin o
"BINARY #{visit o.expr}"
end
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb
index 29e965bef8..933edc15f2 100644
--- a/lib/arel/visitors/to_sql.rb
+++ b/lib/arel/visitors/to_sql.rb
@@ -203,8 +203,12 @@ key on UpdateManager using UpdateManager#key=
"(#{visit o.expr})"
end
- def visit_Arel_Nodes_Ordering o
- "#{visit o.expr} #{o.descending? ? 'DESC' : 'ASC'}"
+ def visit_Arel_Nodes_Ascending o
+ "#{visit o.expr} ASC"
+ end
+
+ def visit_Arel_Nodes_Descending o
+ "#{visit o.expr} DESC"
end
def visit_Arel_Nodes_Group o
diff --git a/lib/arel/visitors/visitor.rb b/lib/arel/visitors/visitor.rb
index c9cdf34adb..8f9dd929e1 100644
--- a/lib/arel/visitors/visitor.rb
+++ b/lib/arel/visitors/visitor.rb
@@ -11,15 +11,19 @@ module Arel
hash[klass] = "visit_#{(klass.name || '').gsub('::', '_')}"
end
+ def dispatch
+ DISPATCH
+ end
+
def visit object
- send DISPATCH[object.class], object
+ send dispatch[object.class], object
rescue NoMethodError => e
- raise e if respond_to?(DISPATCH[object.class], true)
+ raise e if respond_to?(dispatch[object.class], true)
superklass = object.class.ancestors.find { |klass|
- respond_to?(DISPATCH[klass], true)
+ respond_to?(dispatch[klass], true)
}
raise(TypeError, "Cannot visit #{object.class}") unless superklass
- DISPATCH[object.class] = DISPATCH[superklass]
+ dispatch[object.class] = dispatch[superklass]
retry
end
end
diff --git a/test/attributes/test_attribute.rb b/test/attributes/test_attribute.rb
index 352774071a..901850ff4b 100644
--- a/test/attributes/test_attribute.rb
+++ b/test/attributes/test_attribute.rb
@@ -619,9 +619,9 @@ module Arel
end
describe '#asc' do
- it 'should create an Ordering node' do
+ it 'should create an Ascending node' do
relation = Table.new(:users)
- relation[:id].asc.must_be_kind_of Nodes::Ordering
+ relation[:id].asc.must_be_kind_of Nodes::Ascending
end
it 'should generate ASC in sql' do
@@ -635,9 +635,9 @@ module Arel
end
describe '#desc' do
- it 'should create an Ordering node' do
+ it 'should create a Descending node' do
relation = Table.new(:users)
- relation[:id].desc.must_be_kind_of Nodes::Ordering
+ relation[:id].desc.must_be_kind_of Nodes::Descending
end
it 'should generate DESC in sql' do
diff --git a/test/nodes/test_ascending.rb b/test/nodes/test_ascending.rb
new file mode 100644
index 0000000000..0e2c4810c6
--- /dev/null
+++ b/test/nodes/test_ascending.rb
@@ -0,0 +1,34 @@
+require 'helper'
+
+module Arel
+ module Nodes
+ class TestAscending < MiniTest::Unit::TestCase
+ def test_construct
+ ascending = Ascending.new 'zomg'
+ assert_equal 'zomg', ascending.expr
+ end
+
+ def test_reverse
+ ascending = Ascending.new 'zomg'
+ descending = ascending.reverse
+ assert_kind_of Descending, descending
+ assert_equal ascending.expr, descending.expr
+ end
+
+ def test_direction
+ ascending = Ascending.new 'zomg'
+ assert_equal :asc, ascending.direction
+ end
+
+ def test_ascending?
+ ascending = Ascending.new 'zomg'
+ assert ascending.ascending?
+ end
+
+ def test_descending?
+ ascending = Ascending.new 'zomg'
+ assert !ascending.descending?
+ end
+ end
+ end
+end
diff --git a/test/nodes/test_descending.rb b/test/nodes/test_descending.rb
new file mode 100644
index 0000000000..424f8298cd
--- /dev/null
+++ b/test/nodes/test_descending.rb
@@ -0,0 +1,34 @@
+require 'helper'
+
+module Arel
+ module Nodes
+ class TestDescending < MiniTest::Unit::TestCase
+ def test_construct
+ descending = Descending.new 'zomg'
+ assert_equal 'zomg', descending.expr
+ end
+
+ def test_reverse
+ descending = Descending.new 'zomg'
+ ascending = descending.reverse
+ assert_kind_of Ascending, ascending
+ assert_equal descending.expr, ascending.expr
+ end
+
+ def test_direction
+ descending = Descending.new 'zomg'
+ assert_equal :desc, descending.direction
+ end
+
+ def test_ascending?
+ descending = Descending.new 'zomg'
+ assert !descending.ascending?
+ end
+
+ def test_descending?
+ descending = Descending.new 'zomg'
+ assert descending.descending?
+ end
+ end
+ end
+end
diff --git a/test/nodes/test_infix_operation.rb b/test/nodes/test_infix_operation.rb
index db3216eeee..3d2eb0d9c6 100644
--- a/test/nodes/test_infix_operation.rb
+++ b/test/nodes/test_infix_operation.rb
@@ -21,9 +21,9 @@ module Arel
def test_opertaion_ordering
operation = InfixOperation.new :+, 1, 2
ordering = operation.desc
- assert_kind_of Ordering, ordering
+ assert_kind_of Descending, ordering
assert_equal operation, ordering.expr
- assert_equal :desc, ordering.direction
+ assert ordering.descending?
end
end
end
diff --git a/test/test_select_manager.rb b/test/test_select_manager.rb
index e948aec131..119ad3ec4f 100644
--- a/test/test_select_manager.rb
+++ b/test/test_select_manager.rb
@@ -412,6 +412,15 @@ module Arel
ast = mgr.ast
mgr.visitor.accept(ast).must_equal mgr.to_sql
end
+ it 'should allow orders to work when the ast is grepped' do
+ table = Table.new :users
+ mgr = table.from table
+ mgr.project Arel.sql '*'
+ mgr.from table
+ mgr.orders << Arel::Nodes::Ascending.new(Arel.sql('foo'))
+ mgr.ast.grep(Arel::Nodes::OuterJoin)
+ mgr.to_sql.must_be_like %{ SELECT * FROM "users" ORDER BY foo ASC }
+ end
end
describe 'taken' do
diff --git a/test/visitors/test_depth_first.rb b/test/visitors/test_depth_first.rb
index 5bbdf57697..e62ce5266f 100644
--- a/test/visitors/test_depth_first.rb
+++ b/test/visitors/test_depth_first.rb
@@ -28,6 +28,7 @@ module Arel
Arel::Nodes::On,
Arel::Nodes::Grouping,
Arel::Nodes::Offset,
+ Arel::Nodes::Ordering,
Arel::Nodes::Having,
Arel::Nodes::StringJoin,
Arel::Nodes::UnqualifiedColumn,
@@ -104,7 +105,6 @@ module Arel
Arel::Nodes::Values,
Arel::Nodes::As,
Arel::Nodes::DeleteStatement,
- Arel::Nodes::Ordering,
Arel::Nodes::JoinSource,
].each do |klass|
define_method("test_#{klass.name.gsub('::', '_')}") do
diff --git a/test/visitors/test_dot.rb b/test/visitors/test_dot.rb
index b311246436..362e39339c 100644
--- a/test/visitors/test_dot.rb
+++ b/test/visitors/test_dot.rb
@@ -33,6 +33,7 @@ module Arel
Arel::Nodes::On,
Arel::Nodes::Grouping,
Arel::Nodes::Offset,
+ Arel::Nodes::Ordering,
Arel::Nodes::Having,
Arel::Nodes::UnqualifiedColumn,
Arel::Nodes::Top,
@@ -63,7 +64,6 @@ module Arel
Arel::Nodes::Values,
Arel::Nodes::As,
Arel::Nodes::DeleteStatement,
- Arel::Nodes::Ordering,
Arel::Nodes::JoinSource,
].each do |klass|
define_method("test_#{klass.name.gsub('::', '_')}") do
diff --git a/test/visitors/test_informix.rb b/test/visitors/test_informix.rb
new file mode 100644
index 0000000000..a8a52a0160
--- /dev/null
+++ b/test/visitors/test_informix.rb
@@ -0,0 +1,42 @@
+require 'helper'
+
+module Arel
+ module Visitors
+ describe 'the informix visitor' do
+ before do
+ @visitor = Informix.new Table.engine
+ end
+
+ it 'uses LIMIT n to limit results' do
+ stmt = Nodes::SelectStatement.new
+ stmt.limit = Nodes::Limit.new(1)
+ sql = @visitor.accept(stmt)
+ sql.must_be_like "SELECT LIMIT 1"
+ end
+
+ it 'uses LIMIT n 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 LIMIT 1 'id')"
+ end
+
+ it 'uses SKIP n to jump results' do
+ stmt = Nodes::SelectStatement.new
+ stmt.offset = Nodes::Offset.new(10)
+ sql = @visitor.accept(stmt)
+ sql.must_be_like "SELECT SKIP 10"
+ end
+
+ it 'uses SKIP before LIMIT' do
+ stmt = Nodes::SelectStatement.new
+ stmt.limit = Nodes::Limit.new(1)
+ stmt.offset = Nodes::Offset.new(1)
+ sql = @visitor.accept(stmt)
+ sql.must_be_like "SELECT SKIP 1 LIMIT 1"
+ end
+
+ end
+ end
+end
diff --git a/test/visitors/test_mssql.rb b/test/visitors/test_mssql.rb
index ccaea395fe..8b2b756900 100644
--- a/test/visitors/test_mssql.rb
+++ b/test/visitors/test_mssql.rb
@@ -5,21 +5,60 @@ module Arel
describe 'the mssql visitor' do
before do
@visitor = MSSQL.new Table.engine
+ @table = Arel::Table.new "users"
end
- it 'uses TOP to limit results' do
+ it 'should not modify query if no offset or limit' do
stmt = Nodes::SelectStatement.new
- stmt.cores.last.top = Nodes::Top.new(1)
sql = @visitor.accept(stmt)
- sql.must_be_like "SELECT TOP 1"
+ sql.must_be_like "SELECT"
end
- it 'uses TOP in updates with a limit' do
- stmt = Nodes::UpdateStatement.new
- stmt.limit = Nodes::Limit.new(1)
- stmt.key = 'id'
+ it 'should go over table PK if no .order() or .group()' do
+ stmt = Nodes::SelectStatement.new
+ stmt.cores.first.from = @table
+ stmt.limit = Nodes::Limit.new(10)
+ sql = @visitor.accept(stmt)
+ sql.must_be_like "SELECT _t.* FROM (SELECT ROW_NUMBER() OVER (ORDER BY \"users\".\"id\") as _row_num FROM \"users\" ) as _t WHERE _row_num BETWEEN 1 AND 10"
+ end
+
+ it 'should go over query ORDER BY if .order()' do
+ stmt = Nodes::SelectStatement.new
+ stmt.limit = Nodes::Limit.new(10)
+ stmt.orders << Nodes::SqlLiteral.new('order_by')
+ sql = @visitor.accept(stmt)
+ sql.must_be_like "SELECT _t.* FROM (SELECT ROW_NUMBER() OVER (ORDER BY order_by) as _row_num) as _t WHERE _row_num BETWEEN 1 AND 10"
+ end
+
+ it 'should go over query GROUP BY if no .order() and there is .group()' do
+ stmt = Nodes::SelectStatement.new
+ stmt.cores.first.groups << Nodes::SqlLiteral.new('group_by')
+ stmt.limit = Nodes::Limit.new(10)
+ sql = @visitor.accept(stmt)
+ sql.must_be_like "SELECT _t.* FROM (SELECT ROW_NUMBER() OVER (ORDER BY group_by) as _row_num GROUP BY group_by) as _t WHERE _row_num BETWEEN 1 AND 10"
+ end
+
+ it 'should use BETWEEN if both .limit() and .offset' do
+ stmt = Nodes::SelectStatement.new
+ stmt.limit = Nodes::Limit.new(10)
+ stmt.offset = Nodes::Offset.new(20)
+ sql = @visitor.accept(stmt)
+ sql.must_be_like "SELECT _t.* FROM (SELECT ROW_NUMBER() OVER (ORDER BY ) as _row_num) as _t WHERE _row_num BETWEEN 21 AND 30"
+ end
+
+ it 'should use >= if only .offset' do
+ stmt = Nodes::SelectStatement.new
+ stmt.offset = Nodes::Offset.new(20)
+ sql = @visitor.accept(stmt)
+ sql.must_be_like "SELECT _t.* FROM (SELECT ROW_NUMBER() OVER (ORDER BY ) as _row_num) as _t WHERE _row_num >= 21"
+ end
+
+ it 'should generate subquery for .count' do
+ stmt = Nodes::SelectStatement.new
+ stmt.limit = Nodes::Limit.new(10)
+ stmt.cores.first.projections << Nodes::Count.new('*')
sql = @visitor.accept(stmt)
- sql.must_be_like "UPDATE NULL WHERE 'id' IN (SELECT TOP 1 'id' )"
+ sql.must_be_like "SELECT COUNT(1) as count_id FROM (SELECT _t.* FROM (SELECT ROW_NUMBER() OVER (ORDER BY ) as _row_num) as _t WHERE _row_num BETWEEN 1 AND 10) AS subquery"
end
end
diff --git a/test/visitors/test_mysql.rb b/test/visitors/test_mysql.rb
index 3c15c218b2..8d220ac04b 100644
--- a/test/visitors/test_mysql.rb
+++ b/test/visitors/test_mysql.rb
@@ -7,6 +7,16 @@ module Arel
@visitor = MySQL.new Table.engine
end
+ it 'squashes parenthesis on multiple unions' do
+ subnode = Nodes::Union.new 'left', 'right'
+ node = Nodes::Union.new subnode, 'topright'
+ assert_equal 1, @visitor.accept(node).scan('(').length
+
+ subnode = Nodes::Union.new 'left', 'right'
+ node = Nodes::Union.new 'topleft', subnode
+ assert_equal 1, @visitor.accept(node).scan('(').length
+ end
+
###
# :'(
# http://dev.mysql.com/doc/refman/5.0/en/select.html#id3482214
diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb
index d046e543e1..b52722ddd6 100644
--- a/test/visitors/test_to_sql.rb
+++ b/test/visitors/test_to_sql.rb
@@ -13,6 +13,22 @@ module Arel
@attr = @table[:id]
end
+ it 'can define a dispatch method' do
+ visited = false
+ viz = Class.new(Arel::Visitors::Visitor) {
+ define_method(:hello) do |node|
+ visited = true
+ end
+
+ def dispatch
+ { Arel::Table => 'hello' }
+ end
+ }.new
+
+ viz.accept(@table)
+ assert visited, 'hello method was called'
+ end
+
it "should be thread safe around usage of last_column" do
visit_integer_column = Thread.new do
Thread.stop