Permalink
Browse files

Merge branch 'master' into Khronos/master

* master:
  visitors can define their own cache strategy for dispatch. fixes #57
  Break Ordering into Ascending/Descending nodes, allow reversal
  remove unnecessary guarding agains literal
  LIMIT and OFFSET support for MS SQL
  Include Arel::Predicates to Arel::Nodes::Function so you can do table[:id].count.eq(2)
  updating spec
  zomg prep release
  make sure thread runs
  do not cache sql literal values
  no longer use this instance variable
  • Loading branch information...
2 parents 0d39cbe + 0c8723a commit d193a52dedc060e62c63fbc0c57a16ba8795f599 @tenderlove tenderlove committed Jun 27, 2011
View
@@ -1,4 +1,11 @@
-== 2.1.1 (unreleased)
+== 2.1.2 / Unreleased
+
+* Bug Fixes
+
+ * Visitors can define their own cache strategey so caches are not shared.
+ Fixes #57
+
+== 2.1.1 / 2011/05/14
* Bug fixes
View
@@ -2,11 +2,11 @@
Gem::Specification.new do |s|
s.name = %q{arel}
- s.version = "2.1.0.20110430172428"
+ s.version = "2.1.1.20110518143805"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
- s.authors = ["Aaron Patterson", "Bryan Halmkamp", "Emilio Tagua", "Nick Kallen"]
- s.date = %q{2011-04-30}
+ s.authors = [%q{Aaron Patterson}, %q{Bryan Halmkamp}, %q{Emilio Tagua}, %q{Nick Kallen}]
+ s.date = %q{2011-05-18}
s.description = %q{Arel is a SQL AST manager for Ruby. It
1. Simplifies the generation complex of SQL queries
@@ -15,16 +15,16 @@ Gem::Specification.new do |s|
It is intended to be a framework framework; that is, you can build your own ORM
with it, focusing on innovative object and collection modeling as opposed to
database compatibility and query generation.}
- s.email = ["aaron@tenderlovemaking.com", "bryan@brynary.com", "miloops@gmail.com", "nick@example.org"]
- s.extra_rdoc_files = ["History.txt", "MIT-LICENSE.txt", "Manifest.txt", "README.markdown"]
- s.files = [".autotest", ".gemtest", "History.txt", "MIT-LICENSE.txt", "Manifest.txt", "README.markdown", "Rakefile", "arel.gemspec", "lib/arel.rb", "lib/arel/alias_predication.rb", "lib/arel/attributes.rb", "lib/arel/attributes/attribute.rb", "lib/arel/compatibility/wheres.rb", "lib/arel/crud.rb", "lib/arel/delete_manager.rb", "lib/arel/deprecated.rb", "lib/arel/expression.rb", "lib/arel/expressions.rb", "lib/arel/factory_methods.rb", "lib/arel/insert_manager.rb", "lib/arel/math.rb", "lib/arel/nodes.rb", "lib/arel/nodes/and.rb", "lib/arel/nodes/binary.rb", "lib/arel/nodes/count.rb", "lib/arel/nodes/delete_statement.rb", "lib/arel/nodes/equality.rb", "lib/arel/nodes/function.rb", "lib/arel/nodes/in.rb", "lib/arel/nodes/infix_operation.rb", "lib/arel/nodes/inner_join.rb", "lib/arel/nodes/insert_statement.rb", "lib/arel/nodes/join_source.rb", "lib/arel/nodes/named_function.rb", "lib/arel/nodes/node.rb", "lib/arel/nodes/ordering.rb", "lib/arel/nodes/outer_join.rb", "lib/arel/nodes/select_core.rb", "lib/arel/nodes/select_statement.rb", "lib/arel/nodes/sql_literal.rb", "lib/arel/nodes/string_join.rb", "lib/arel/nodes/table_alias.rb", "lib/arel/nodes/terminal.rb", "lib/arel/nodes/unary.rb", "lib/arel/nodes/unqualified_column.rb", "lib/arel/nodes/update_statement.rb", "lib/arel/nodes/values.rb", "lib/arel/nodes/with.rb", "lib/arel/order_predications.rb", "lib/arel/predications.rb", "lib/arel/relation.rb", "lib/arel/select_manager.rb", "lib/arel/sql/engine.rb", "lib/arel/sql_literal.rb", "lib/arel/table.rb", "lib/arel/tree_manager.rb", "lib/arel/update_manager.rb", "lib/arel/visitors.rb", "lib/arel/visitors/depth_first.rb", "lib/arel/visitors/dot.rb", "lib/arel/visitors/ibm_db.rb", "lib/arel/visitors/join_sql.rb", "lib/arel/visitors/mssql.rb", "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", "test/attributes/test_attribute.rb", "test/helper.rb", "test/nodes/test_as.rb", "test/nodes/test_bin.rb", "test/nodes/test_count.rb", "test/nodes/test_delete_statement.rb", "test/nodes/test_equality.rb", "test/nodes/test_insert_statement.rb", "test/nodes/test_named_function.rb", "test/nodes/test_node.rb", "test/nodes/test_not.rb", "test/nodes/test_or.rb", "test/nodes/test_select_core.rb", "test/nodes/test_select_statement.rb", "test/nodes/test_sql_literal.rb", "test/nodes/test_sum.rb", "test/nodes/test_update_statement.rb", "test/support/fake_record.rb", "test/test_activerecord_compat.rb", "test/test_attributes.rb", "test/test_crud.rb", "test/test_delete_manager.rb", "test/test_factory_methods.rb", "test/test_insert_manager.rb", "test/test_select_manager.rb", "test/test_table.rb", "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_join_sql.rb", "test/visitors/test_mssql.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"]
+ 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.homepage = %q{http://github.com/rails/arel}
- s.rdoc_options = ["--main", "README.markdown"]
- s.require_paths = ["lib"]
+ s.rdoc_options = [%q{--main}, %q{README.markdown}]
+ s.require_paths = [%q{lib}]
s.rubyforge_project = %q{arel}
- s.rubygems_version = %q{1.6.1}
+ s.rubygems_version = %q{1.8.2}
s.summary = %q{Arel is a SQL AST manager for Ruby}
- s.test_files = ["test/attributes/test_attribute.rb", "test/nodes/test_as.rb", "test/nodes/test_bin.rb", "test/nodes/test_count.rb", "test/nodes/test_delete_statement.rb", "test/nodes/test_equality.rb", "test/nodes/test_insert_statement.rb", "test/nodes/test_named_function.rb", "test/nodes/test_node.rb", "test/nodes/test_not.rb", "test/nodes/test_or.rb", "test/nodes/test_select_core.rb", "test/nodes/test_select_statement.rb", "test/nodes/test_sql_literal.rb", "test/nodes/test_sum.rb", "test/nodes/test_update_statement.rb", "test/test_activerecord_compat.rb", "test/test_attributes.rb", "test/test_crud.rb", "test/test_delete_manager.rb", "test/test_factory_methods.rb", "test/test_insert_manager.rb", "test/test_select_manager.rb", "test/test_table.rb", "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_join_sql.rb", "test/visitors/test_mssql.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"]
+ 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}]
if s.respond_to? :specification_version then
s.specification_version = 3
View
@@ -33,7 +33,7 @@
####
module Arel
- VERSION = '2.1.0'
+ VERSION = '2.1.1'
def self.sql raw_sql
Arel::Nodes::SqlLiteral.new raw_sql
View
@@ -11,6 +11,8 @@
# 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/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'
@@ -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
@@ -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
@@ -2,6 +2,7 @@ module Arel
module Nodes
class Function < Arel::Nodes::Node
include Arel::Expression
+ include Arel::Predications
attr_accessor :expressions, :alias, :distinct
def initialize expr, aliaz = nil
@@ -3,8 +3,6 @@ module Nodes
class NamedFunction < Arel::Nodes::Function
attr_accessor :name
- include Arel::Predications
-
def initialize name, expr, aliaz = nil
super(expr, aliaz)
@name = name
View
@@ -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
View
@@ -18,6 +18,7 @@ def initialize expr
Not
Offset
On
+ Ordering
Top
Lock
DistinctOn
@@ -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
@@ -22,6 +22,7 @@ def unary o
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_Top :unary
alias :visit_Arel_Nodes_UnqualifiedColumn :unary
@@ -75,7 +76,6 @@ def binary o
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
View
@@ -30,7 +30,6 @@ def accept object
private
def visit_Arel_Nodes_Ordering o
visit_edge o, "expr"
- visit_edge o, "direction"
end
def visit_Arel_Nodes_TableAlias o
View
@@ -3,21 +3,71 @@ module Visitors
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
@@ -203,8 +203,12 @@ def visit_Arel_Nodes_Grouping o
"(#{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
@@ -416,7 +420,8 @@ def quote value, column = nil
end
def quote_table_name name
- @quoted_tables[name] ||= Arel::Nodes::SqlLiteral === name ? name : @connection.quote_table_name(name)
+ return name if Arel::Nodes::SqlLiteral === name
+ @quoted_tables[name] ||= @connection.quote_table_name(name)
end
def quote_column_name name
@@ -11,15 +11,19 @@ def accept object
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
@@ -619,9 +619,9 @@ module Attributes
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 Attributes
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
@@ -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
Oops, something went wrong.

0 comments on commit d193a52

Please sign in to comment.