mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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
This commit is contained in:
commit
d193a52ded
26 changed files with 300 additions and 69 deletions
|
@ -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
|
||||
|
||||
|
|
20
arel.gemspec
20
arel.gemspec
|
@ -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
|
||||
|
|
|
@ -33,7 +33,7 @@ require 'arel/sql_literal'
|
|||
####
|
||||
|
||||
module Arel
|
||||
VERSION = '2.1.0'
|
||||
VERSION = '2.1.1'
|
||||
|
||||
def self.sql raw_sql
|
||||
Arel::Nodes::SqlLiteral.new raw_sql
|
||||
|
|
|
@ -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'
|
||||
|
|
23
lib/arel/nodes/ascending.rb
Normal file
23
lib/arel/nodes/ascending.rb
Normal file
|
@ -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
|
23
lib/arel/nodes/descending.rb
Normal file
23
lib/arel/nodes/descending.rb
Normal file
|
@ -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 Arel
|
|||
class NamedFunction < Arel::Nodes::Function
|
||||
attr_accessor :name
|
||||
|
||||
include Arel::Predications
|
||||
|
||||
def initialize name, expr, aliaz = nil
|
||||
super(expr, aliaz)
|
||||
@name = name
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -18,6 +18,7 @@ module Arel
|
|||
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 @@ 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_Top :unary
|
||||
alias :visit_Arel_Nodes_UnqualifiedColumn :unary
|
||||
|
||||
|
@ -75,7 +76,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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
end
|
||||
|
||||
def visit_Arel_Nodes_Limit o
|
||||
# `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_Top o
|
||||
"TOP #{visit o.expr}"
|
||||
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 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 @@ 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
|
||||
|
@ -416,7 +420,8 @@ key on UpdateManager using UpdateManager#key=
|
|||
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 @@ 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
|
||||
|
|
|
@ -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
|
||||
|
|
34
test/nodes/test_ascending.rb
Normal file
34
test/nodes/test_ascending.rb
Normal file
|
@ -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
|
|
@ -15,4 +15,13 @@ describe Arel::Nodes::Count do
|
|||
}
|
||||
end
|
||||
end
|
||||
|
||||
describe "eq" do
|
||||
it "should compare the count" do
|
||||
table = Arel::Table.new :users
|
||||
table[:id].count.eq(2).to_sql.must_be_like %{
|
||||
COUNT("users"."id") = 2
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
34
test/nodes/test_descending.rb
Normal file
34
test/nodes/test_descending.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -216,7 +216,7 @@ module Arel
|
|||
table = Table.new :users, :engine => Table.engine, :as => 'foo'
|
||||
mgr = table.from table
|
||||
mgr.skip 10
|
||||
mgr.to_sql.must_be_like %{ SELECT FROM "users" foo OFFSET 10 }
|
||||
mgr.to_sql.must_be_like %{ SELECT FROM "users" "foo" OFFSET 10 }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 "UPDATE NULL WHERE 'id' IN (SELECT TOP 1 'id' )"
|
||||
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 "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
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
require 'helper'
|
||||
|
||||
class Arel::Visitors::ToSql
|
||||
def last_column; Thread.current[:arel_visitors_to_sql_last_column] || @last_column; end
|
||||
def last_column; Thread.current[:arel_visitors_to_sql_last_column]; end
|
||||
end
|
||||
|
||||
module Arel
|
||||
|
@ -13,12 +13,29 @@ 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
|
||||
@visitor.send(:visit_Arel_Attributes_Attribute, @attr)
|
||||
end
|
||||
|
||||
sleep 0.2
|
||||
@visitor.accept(@table[:name])
|
||||
assert_equal(:string, @visitor.last_column.type)
|
||||
visit_integer_column.run
|
||||
|
|
Loading…
Reference in a new issue