Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
128d0062
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
128d0062
编写于
8月 09, 2011
作者:
J
Jon Leighton
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support updates with joins. Fixes #522.
上级
9482554f
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
34 addition
and
4 deletion
+34
-4
activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
...ecord/connection_adapters/abstract/database_statements.rb
+9
-0
activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
...d/lib/active_record/connection_adapters/mysql2_adapter.rb
+4
-0
activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
...rd/lib/active_record/connection_adapters/mysql_adapter.rb
+4
-0
activerecord/lib/active_record/relation.rb
activerecord/lib/active_record/relation.rb
+9
-4
activerecord/test/cases/relations_test.rb
activerecord/test/cases/relations_test.rb
+8
-0
未找到文件。
activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
浏览文件 @
128d0062
...
...
@@ -306,6 +306,15 @@ def sanitize_limit(limit)
end
end
# The default strategy for an UPDATE with joins is to use a subquery. This doesn't work
# on mysql (even when aliasing the tables), but mysql allows using JOIN directly in
# an UPDATE statement, so in the mysql adapters we redefine this to do that.
def
join_to_update
(
update
,
select
)
#:nodoc:
subselect
=
select
.
clone
subselect
.
ast
.
cores
.
last
.
projections
=
[
update
.
ast
.
key
]
update
.
wheres
=
[
update
.
ast
.
key
.
in
(
subselect
)]
end
protected
# Returns an array of record hashes with the column names as keys and
# column values as values.
...
...
activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
浏览文件 @
128d0062
...
...
@@ -577,6 +577,10 @@ def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
where_sql
end
def
join_to_update
(
update
,
select
)
#:nodoc:
update
.
table
select
.
ast
.
cores
.
last
.
source
end
protected
def
quoted_columns_for_index
(
column_names
,
options
=
{})
length
=
options
[
:length
]
if
options
.
is_a?
(
Hash
)
...
...
activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
浏览文件 @
128d0062
...
...
@@ -491,6 +491,10 @@ def release_savepoint
execute
(
"RELEASE SAVEPOINT
#{
current_savepoint_name
}
"
)
end
def
join_to_update
(
update
,
select
)
#:nodoc:
update
.
table
select
.
ast
.
cores
.
last
.
source
end
# SCHEMA STATEMENTS ========================================
def
structure_dump
#:nodoc:
...
...
activerecord/lib/active_record/relation.rb
浏览文件 @
128d0062
...
...
@@ -217,13 +217,18 @@ def update_all(updates, conditions = nil, options = {})
where
(
conditions
).
apply_finder_options
(
options
.
slice
(
:limit
,
:order
)).
update_all
(
updates
)
else
stmt
=
arel
.
compile_update
(
Arel
.
sql
(
@klass
.
send
(
:sanitize_sql_for_assignment
,
updates
)))
stmt
.
key
=
table
[
primary_key
]
if
limit
=
arel
.
limit
stmt
.
take
limit
if
joins_values
.
any?
@klass
.
connection
.
join_to_update
(
stmt
,
arel
)
else
if
limit
=
arel
.
limit
stmt
.
take
limit
end
stmt
.
order
(
*
arel
.
orders
)
end
stmt
.
order
(
*
arel
.
orders
)
stmt
.
key
=
table
[
primary_key
]
@klass
.
connection
.
update
stmt
,
'SQL'
,
bind_values
end
end
...
...
activerecord/test/cases/relations_test.rb
浏览文件 @
128d0062
...
...
@@ -965,4 +965,12 @@ def test_eager_loading_with_conditions_on_joins
def
test_ordering_with_extra_spaces
assert_equal
authors
(
:david
),
Author
.
order
(
'id DESC , name DESC'
).
last
end
def
test_update_all_with_joins
comments
=
Comment
.
joins
(
:post
).
where
(
'posts.id'
=>
posts
(
:welcome
).
id
)
count
=
comments
.
count
assert_equal
count
,
comments
.
update_all
(
:post_id
=>
posts
(
:thinking
).
id
)
assert_equal
posts
(
:thinking
),
comments
(
:greetings
).
post
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录