提交 6e43a207 编写于 作者: R Ryuta Kamizono

Revert unused code and re-using query annotation for `update_all` and `delete_all`

This partly reverts #35617.

#35617 includes unused code (for `InsertStatement`) and re-using query
annotation for `update_all` and `delete_all`, which has not been
discussed yet.

If a relation has any annotation, I think it is mostly for SELECT query,
so re-using annotation by default is not always desired behavior for me.

We should discuss about desired behavior before publishing the
implementation.
上级 43866b2c
...@@ -389,8 +389,6 @@ def update_all(updates) ...@@ -389,8 +389,6 @@ def update_all(updates)
stmt.set Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name)) stmt.set Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name))
end end
stmt.comment(*arel.comment_node.values) if arel.comment_node
@klass.connection.update stmt, "#{@klass} Update All" @klass.connection.update stmt, "#{@klass} Update All"
end end
...@@ -506,7 +504,6 @@ def delete_all ...@@ -506,7 +504,6 @@ def delete_all
stmt.offset(arel.offset) stmt.offset(arel.offset)
stmt.order(*arel.orders) stmt.order(*arel.orders)
stmt.wheres = arel.constraints stmt.wheres = arel.constraints
stmt.comment(*arel.comment_node.values) if arel.comment_node
affected = @klass.connection.delete(stmt, "#{@klass} Destroy") affected = @klass.connection.delete(stmt, "#{@klass} Destroy")
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
module Arel # :nodoc: all module Arel # :nodoc: all
module Nodes module Nodes
class DeleteStatement < Arel::Nodes::Node class DeleteStatement < Arel::Nodes::Node
attr_accessor :left, :right, :orders, :limit, :offset, :key, :comment attr_accessor :left, :right, :orders, :limit, :offset, :key
alias :relation :left alias :relation :left
alias :relation= :left= alias :relation= :left=
...@@ -18,18 +18,16 @@ def initialize(relation = nil, wheres = []) ...@@ -18,18 +18,16 @@ def initialize(relation = nil, wheres = [])
@limit = nil @limit = nil
@offset = nil @offset = nil
@key = nil @key = nil
@comment = nil
end end
def initialize_copy(other) def initialize_copy(other)
super super
@left = @left.clone if @left @left = @left.clone if @left
@right = @right.clone if @right @right = @right.clone if @right
@comment = @comment.clone if @comment
end end
def hash def hash
[self.class, @left, @right, @orders, @limit, @offset, @key, @comment].hash [self.class, @left, @right, @orders, @limit, @offset, @key].hash
end end
def eql?(other) def eql?(other)
...@@ -39,8 +37,7 @@ def eql?(other) ...@@ -39,8 +37,7 @@ def eql?(other)
self.orders == other.orders && self.orders == other.orders &&
self.limit == other.limit && self.limit == other.limit &&
self.offset == other.offset && self.offset == other.offset &&
self.key == other.key && self.key == other.key
self.comment == other.comment
end end
alias :== :eql? alias :== :eql?
end end
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
module Arel # :nodoc: all module Arel # :nodoc: all
module Nodes module Nodes
class InsertStatement < Arel::Nodes::Node class InsertStatement < Arel::Nodes::Node
attr_accessor :relation, :columns, :values, :select, :comment attr_accessor :relation, :columns, :values, :select
def initialize def initialize
super() super()
...@@ -11,7 +11,6 @@ def initialize ...@@ -11,7 +11,6 @@ def initialize
@columns = [] @columns = []
@values = nil @values = nil
@select = nil @select = nil
@comment = nil
end end
def initialize_copy(other) def initialize_copy(other)
...@@ -19,11 +18,10 @@ def initialize_copy(other) ...@@ -19,11 +18,10 @@ def initialize_copy(other)
@columns = @columns.clone @columns = @columns.clone
@values = @values.clone if @values @values = @values.clone if @values
@select = @select.clone if @select @select = @select.clone if @select
@comment = @comment.clone if @comment
end end
def hash def hash
[@relation, @columns, @values, @select, @comment].hash [@relation, @columns, @values, @select].hash
end end
def eql?(other) def eql?(other)
...@@ -31,8 +29,7 @@ def eql?(other) ...@@ -31,8 +29,7 @@ def eql?(other)
self.relation == other.relation && self.relation == other.relation &&
self.columns == other.columns && self.columns == other.columns &&
self.select == other.select && self.select == other.select &&
self.values == other.values && self.values == other.values
self.comment == other.comment
end end
alias :== :eql? alias :== :eql?
end end
......
...@@ -40,7 +40,6 @@ def initialize_copy(other) ...@@ -40,7 +40,6 @@ def initialize_copy(other)
@groups = @groups.clone @groups = @groups.clone
@havings = @havings.clone @havings = @havings.clone
@windows = @windows.clone @windows = @windows.clone
@comment = @comment.clone if @comment
end end
def hash def hash
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
module Arel # :nodoc: all module Arel # :nodoc: all
module Nodes module Nodes
class UpdateStatement < Arel::Nodes::Node class UpdateStatement < Arel::Nodes::Node
attr_accessor :relation, :wheres, :values, :orders, :limit, :offset, :key, :comment attr_accessor :relation, :wheres, :values, :orders, :limit, :offset, :key
def initialize def initialize
@relation = nil @relation = nil
...@@ -13,18 +13,16 @@ def initialize ...@@ -13,18 +13,16 @@ def initialize
@limit = nil @limit = nil
@offset = nil @offset = nil
@key = nil @key = nil
@comment = nil
end end
def initialize_copy(other) def initialize_copy(other)
super super
@wheres = @wheres.clone @wheres = @wheres.clone
@values = @values.clone @values = @values.clone
@comment = @comment.clone if @comment
end end
def hash def hash
[@relation, @wheres, @values, @orders, @limit, @offset, @key, @comment].hash [@relation, @wheres, @values, @orders, @limit, @offset, @key].hash
end end
def eql?(other) def eql?(other)
...@@ -35,8 +33,7 @@ def eql?(other) ...@@ -35,8 +33,7 @@ def eql?(other)
self.orders == other.orders && self.orders == other.orders &&
self.limit == other.limit && self.limit == other.limit &&
self.offset == other.offset && self.offset == other.offset &&
self.key == other.key && self.key == other.key
self.comment == other.comment
end end
alias :== :eql? alias :== :eql?
end end
......
...@@ -249,10 +249,6 @@ def comment(*values) ...@@ -249,10 +249,6 @@ def comment(*values)
self self
end end
def comment_node
@ctx.comment
end
private private
def collapse(exprs) def collapse(exprs)
exprs = exprs.compact exprs = exprs.compact
......
...@@ -36,11 +36,6 @@ def where(expr) ...@@ -36,11 +36,6 @@ def where(expr)
@ast.wheres << expr @ast.wheres << expr
self self
end end
def comment(*values)
@ast.comment = Nodes::Comment.new(values)
self
end
end end
attr_reader :ast attr_reader :ast
......
...@@ -35,7 +35,6 @@ def visit_Arel_Nodes_DeleteStatement(o, collector) ...@@ -35,7 +35,6 @@ def visit_Arel_Nodes_DeleteStatement(o, collector)
collect_nodes_for o.wheres, collector, " WHERE ", " AND " collect_nodes_for o.wheres, collector, " WHERE ", " AND "
collect_nodes_for o.orders, collector, " ORDER BY " collect_nodes_for o.orders, collector, " ORDER BY "
maybe_visit o.limit, collector maybe_visit o.limit, collector
maybe_visit o.comment, collector
end end
def visit_Arel_Nodes_UpdateStatement(o, collector) def visit_Arel_Nodes_UpdateStatement(o, collector)
...@@ -48,7 +47,6 @@ def visit_Arel_Nodes_UpdateStatement(o, collector) ...@@ -48,7 +47,6 @@ def visit_Arel_Nodes_UpdateStatement(o, collector)
collect_nodes_for o.wheres, collector, " WHERE ", " AND " collect_nodes_for o.wheres, collector, " WHERE ", " AND "
collect_nodes_for o.orders, collector, " ORDER BY " collect_nodes_for o.orders, collector, " ORDER BY "
maybe_visit o.limit, collector maybe_visit o.limit, collector
maybe_visit o.comment, collector
end end
def visit_Arel_Nodes_InsertStatement(o, collector) def visit_Arel_Nodes_InsertStatement(o, collector)
...@@ -64,9 +62,9 @@ def visit_Arel_Nodes_InsertStatement(o, collector) ...@@ -64,9 +62,9 @@ def visit_Arel_Nodes_InsertStatement(o, collector)
maybe_visit o.values, collector maybe_visit o.values, collector
elsif o.select elsif o.select
maybe_visit o.select, collector maybe_visit o.select, collector
else
collector
end end
maybe_visit o.comment, collector
end end
def visit_Arel_Nodes_Exists(o, collector) def visit_Arel_Nodes_Exists(o, collector)
......
...@@ -49,23 +49,5 @@ class DeleteManagerTest < Arel::Spec ...@@ -49,23 +49,5 @@ class DeleteManagerTest < Arel::Spec
dm.where(table[:id].eq(10)).must_equal dm dm.where(table[:id].eq(10)).must_equal dm
end end
end end
describe "comment" do
it "chains" do
manager = Arel::DeleteManager.new
manager.comment("deleting").must_equal manager
end
it "appends a comment to the generated query" do
table = Table.new(:users)
dm = Arel::DeleteManager.new
dm.from table
dm.comment("deletion")
assert_match(%r{DELETE FROM "users" /\* deletion \*/}, dm.to_sql)
dm.comment("deletion", "with", "comment")
assert_match(%r{DELETE FROM "users" /\* deletion \*/ /\* with \*/ /\* comment \*/}, dm.to_sql)
end
end
end end
end end
...@@ -18,10 +18,8 @@ ...@@ -18,10 +18,8 @@
it "is equal with equal ivars" do it "is equal with equal ivars" do
statement1 = Arel::Nodes::DeleteStatement.new statement1 = Arel::Nodes::DeleteStatement.new
statement1.wheres = %w[a b c] statement1.wheres = %w[a b c]
statement1.comment = Arel::Nodes::Comment.new(["comment"])
statement2 = Arel::Nodes::DeleteStatement.new statement2 = Arel::Nodes::DeleteStatement.new
statement2.wheres = %w[a b c] statement2.wheres = %w[a b c]
statement2.comment = Arel::Nodes::Comment.new(["comment"])
array = [statement1, statement2] array = [statement1, statement2]
assert_equal 1, array.uniq.size assert_equal 1, array.uniq.size
end end
...@@ -29,14 +27,8 @@ ...@@ -29,14 +27,8 @@
it "is not equal with different ivars" do it "is not equal with different ivars" do
statement1 = Arel::Nodes::DeleteStatement.new statement1 = Arel::Nodes::DeleteStatement.new
statement1.wheres = %w[a b c] statement1.wheres = %w[a b c]
statement1.comment = Arel::Nodes::Comment.new(["comment"])
statement2 = Arel::Nodes::DeleteStatement.new statement2 = Arel::Nodes::DeleteStatement.new
statement2.wheres = %w[1 2 3] statement2.wheres = %w[1 2 3]
statement2.comment = Arel::Nodes::Comment.new(["comment"])
array = [statement1, statement2]
assert_equal 2, array.uniq.size
statement2.wheres = %w[a b c]
statement2.comment = Arel::Nodes::Comment.new(["other"])
array = [statement1, statement2] array = [statement1, statement2]
assert_equal 2, array.uniq.size assert_equal 2, array.uniq.size
end end
......
...@@ -23,11 +23,9 @@ ...@@ -23,11 +23,9 @@
statement1 = Arel::Nodes::InsertStatement.new statement1 = Arel::Nodes::InsertStatement.new
statement1.columns = %w[a b c] statement1.columns = %w[a b c]
statement1.values = %w[x y z] statement1.values = %w[x y z]
statement1.comment = Arel::Nodes::Comment.new(["comment"])
statement2 = Arel::Nodes::InsertStatement.new statement2 = Arel::Nodes::InsertStatement.new
statement2.columns = %w[a b c] statement2.columns = %w[a b c]
statement2.values = %w[x y z] statement2.values = %w[x y z]
statement2.comment = Arel::Nodes::Comment.new(["comment"])
array = [statement1, statement2] array = [statement1, statement2]
assert_equal 1, array.uniq.size assert_equal 1, array.uniq.size
end end
...@@ -36,15 +34,9 @@ ...@@ -36,15 +34,9 @@
statement1 = Arel::Nodes::InsertStatement.new statement1 = Arel::Nodes::InsertStatement.new
statement1.columns = %w[a b c] statement1.columns = %w[a b c]
statement1.values = %w[x y z] statement1.values = %w[x y z]
statement1.comment = Arel::Nodes::Comment.new(["comment"])
statement2 = Arel::Nodes::InsertStatement.new statement2 = Arel::Nodes::InsertStatement.new
statement2.columns = %w[a b c] statement2.columns = %w[a b c]
statement2.values = %w[1 2 3] statement2.values = %w[1 2 3]
statement2.comment = Arel::Nodes::Comment.new(["comment"])
array = [statement1, statement2]
assert_equal 2, array.uniq.size
statement2.values = %w[x y z]
statement2.comment = Arel::Nodes::Comment.new("other")
array = [statement1, statement2] array = [statement1, statement2]
assert_equal 2, array.uniq.size assert_equal 2, array.uniq.size
end end
......
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
statement1.orders = %w[x y z] statement1.orders = %w[x y z]
statement1.limit = 42 statement1.limit = 42
statement1.key = "zomg" statement1.key = "zomg"
statement1.comment = Arel::Nodes::Comment.new(["comment"])
statement2 = Arel::Nodes::UpdateStatement.new statement2 = Arel::Nodes::UpdateStatement.new
statement2.relation = "zomg" statement2.relation = "zomg"
statement2.wheres = 2 statement2.wheres = 2
...@@ -35,7 +34,6 @@ ...@@ -35,7 +34,6 @@
statement2.orders = %w[x y z] statement2.orders = %w[x y z]
statement2.limit = 42 statement2.limit = 42
statement2.key = "zomg" statement2.key = "zomg"
statement2.comment = Arel::Nodes::Comment.new(["comment"])
array = [statement1, statement2] array = [statement1, statement2]
assert_equal 1, array.uniq.size assert_equal 1, array.uniq.size
end end
...@@ -48,7 +46,6 @@ ...@@ -48,7 +46,6 @@
statement1.orders = %w[x y z] statement1.orders = %w[x y z]
statement1.limit = 42 statement1.limit = 42
statement1.key = "zomg" statement1.key = "zomg"
statement1.comment = Arel::Nodes::Comment.new(["comment"])
statement2 = Arel::Nodes::UpdateStatement.new statement2 = Arel::Nodes::UpdateStatement.new
statement2.relation = "zomg" statement2.relation = "zomg"
statement2.wheres = 2 statement2.wheres = 2
...@@ -56,11 +53,6 @@ ...@@ -56,11 +53,6 @@
statement2.orders = %w[x y z] statement2.orders = %w[x y z]
statement2.limit = 42 statement2.limit = 42
statement2.key = "wth" statement2.key = "wth"
statement2.comment = Arel::Nodes::Comment.new(["comment"])
array = [statement1, statement2]
assert_equal 2, array.uniq.size
statement2.key = "zomg"
statement2.comment = Arel::Nodes::Comment.new(["other"])
array = [statement1, statement2] array = [statement1, statement2]
assert_equal 2, array.uniq.size assert_equal 2, array.uniq.size
end end
......
...@@ -122,29 +122,5 @@ class UpdateManagerTest < Arel::Spec ...@@ -122,29 +122,5 @@ class UpdateManagerTest < Arel::Spec
@um.key.must_equal @table[:foo] @um.key.must_equal @table[:foo]
end end
end end
describe "comment" do
it "chains" do
manager = Arel::UpdateManager.new
manager.comment("updating").must_equal manager
end
it "appends a comment to the generated query" do
table = Table.new :users
manager = Arel::UpdateManager.new
manager.table table
manager.comment("updating")
manager.to_sql.must_be_like %{
UPDATE "users" /* updating */
}
manager.comment("updating", "with", "comment")
manager.to_sql.must_be_like %{
UPDATE "users" /* updating */ /* with */ /* comment */
}
end
end
end end
end end
...@@ -99,23 +99,4 @@ def test_delete_all_with_order_and_limit_and_offset_deletes_subset_only ...@@ -99,23 +99,4 @@ def test_delete_all_with_order_and_limit_and_offset_deletes_subset_only
assert_raise(ActiveRecord::RecordNotFound) { posts(:thinking) } assert_raise(ActiveRecord::RecordNotFound) { posts(:thinking) }
assert posts(:welcome) assert posts(:welcome)
end end
def test_delete_all_with_annotation_includes_a_query_comment
davids = Author.where(name: "David").annotate("deleting all")
assert_sql(%r{/\* deleting all \*/}) do
assert_difference("Author.count", -1) { davids.delete_all }
end
end
def test_delete_all_without_annotation_does_not_include_an_empty_comment
davids = Author.where(name: "David")
log = capture_sql do
assert_difference("Author.count", -1) { davids.delete_all }
end
assert_not_predicate log, :empty?
assert_predicate log.select { |query| query.match?(%r{/\*}) }, :empty?
end
end end
...@@ -241,33 +241,6 @@ def test_touch_all_cares_about_optimistic_locking ...@@ -241,33 +241,6 @@ def test_touch_all_cares_about_optimistic_locking
end end
end end
def test_update_all_with_annotation_includes_a_query_comment
tag = Tag.first
assert_sql(%r{/\* updating all \*/}) do
Post.tagged_with(tag.id).annotate("updating all").update_all(title: "rofl")
end
posts = Post.tagged_with(tag.id).all.to_a
assert_operator posts.length, :>, 0
posts.each { |post| assert_equal "rofl", post.title }
end
def test_update_all_without_annotation_does_not_include_an_empty_comment
tag = Tag.first
log = capture_sql do
Post.tagged_with(tag.id).update_all(title: "rofl")
end
assert_not_predicate log, :empty?
assert_predicate log.select { |query| query.match?(%r{/\*}) }, :empty?
posts = Post.tagged_with(tag.id).all.to_a
assert_operator posts.length, :>, 0
posts.each { |post| assert_equal "rofl", post.title }
end
# Oracle UPDATE does not support ORDER BY # Oracle UPDATE does not support ORDER BY
unless current_adapter?(:OracleAdapter) unless current_adapter?(:OracleAdapter)
def test_update_all_ignores_order_without_limit_from_association def test_update_all_ignores_order_without_limit_from_association
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册