Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
bd3b28f7
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,发现更多精彩内容 >>
提交
bd3b28f7
编写于
4月 14, 2014
作者:
A
Aaron Patterson
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cache scope building on associations
SQL statements for querying associations are now cached
上级
316ee25c
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
78 addition
and
12 deletion
+78
-12
activerecord/lib/active_record/associations/association_scope.rb
...ecord/lib/active_record/associations/association_scope.rb
+49
-6
activerecord/lib/active_record/associations/collection_association.rb
.../lib/active_record/associations/collection_association.rb
+15
-1
activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
...ecord/connection_adapters/abstract/database_statements.rb
+5
-1
activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
...d/lib/active_record/connection_adapters/mysql2_adapter.rb
+0
-4
activerecord/lib/active_record/reflection.rb
activerecord/lib/active_record/reflection.rb
+9
-0
未找到文件。
activerecord/lib/active_record/associations/association_scope.rb
浏览文件 @
bd3b28f7
module
ActiveRecord
module
Associations
class
AssociationScope
#:nodoc:
INSTANCE
=
new
def
self
.
scope
(
association
,
connection
)
INSTANCE
.
scope
association
,
connection
end
class
BindSubstitution
def
initialize
(
block
)
@block
=
block
end
def
bind_value
(
scope
,
column
,
value
,
alias_tracker
)
substitute
=
alias_tracker
.
connection
.
substitute_at
(
column
,
scope
.
bind_values
.
length
)
scope
.
bind_values
+=
[[
column
,
@block
.
call
(
value
)]]
substitute
end
end
def
self
.
create
(
&
block
)
block
=
block
?
block
:
lambda
{
|
val
|
val
}
new
BindSubstitution
.
new
(
block
)
end
def
initialize
(
bind_substitution
)
@bind_substitution
=
bind_substitution
end
INSTANCE
=
create
def
scope
(
association
,
connection
)
klass
=
association
.
klass
reflection
=
association
.
reflection
...
...
@@ -22,6 +44,30 @@ def join_type
Arel
::
Nodes
::
InnerJoin
end
def
self
.
get_bind_values
(
owner
,
chain
)
bvs
=
[]
chain
.
each_with_index
do
|
reflection
,
i
|
if
reflection
.
source_macro
==
:belongs_to
foreign_key
=
reflection
.
foreign_key
else
foreign_key
=
reflection
.
active_record_primary_key
end
if
reflection
==
chain
.
last
bvs
<<
owner
[
foreign_key
]
if
reflection
.
type
bvs
<<
owner
.
class
.
base_class
.
name
end
else
if
reflection
.
type
bvs
<<
chain
[
i
+
1
].
klass
.
base_class
.
name
end
end
end
bvs
end
private
def
construct_tables
(
chain
,
klass
,
refl
,
alias_tracker
)
...
...
@@ -49,10 +95,7 @@ def column_for(table_name, column_name, alias_tracker)
end
def
bind_value
(
scope
,
column
,
value
,
alias_tracker
)
substitute
=
alias_tracker
.
connection
.
substitute_at
(
column
,
scope
.
bind_values
.
length
)
scope
.
bind_values
+=
[[
column
,
value
]]
substitute
@bind_substitution
.
bind_value
scope
,
column
,
value
,
alias_tracker
end
def
bind
(
scope
,
table_name
,
column_name
,
value
,
tracker
)
...
...
activerecord/lib/active_record/associations/collection_association.rb
浏览文件 @
bd3b28f7
...
...
@@ -412,9 +412,23 @@ def null_scope?
end
private
def
get_records
return
scope
.
to_a
if
reflection
.
scope_chain
.
any?
(
&
:any?
)
conn
=
klass
.
connection
sc
=
reflection
.
association_scope_cache
(
conn
)
do
StatementCache
.
create
(
conn
)
{
|
params
|
as
=
AssociationScope
.
create
{
params
.
bind
}
target_scope
.
merge
as
.
scope
(
self
,
conn
)
}
end
binds
=
AssociationScope
.
get_bind_values
(
owner
,
reflection
.
chain
)
sc
.
execute
binds
,
klass
,
klass
.
connection
end
def
find_target
records
=
scope
.
to_a
records
=
get_records
records
.
each
{
|
record
|
set_inverse_instance
(
record
)
}
records
end
...
...
activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
浏览文件 @
bd3b28f7
...
...
@@ -19,7 +19,11 @@ def to_sql(arel, binds = [])
# This is used in the StatementCache object. It returns an object that
# can be used to query the database repeatedly.
def
cacheable_query
(
arel
)
# :nodoc:
ActiveRecord
::
StatementCache
.
query
visitor
,
arel
.
ast
if
prepared_statements
ActiveRecord
::
StatementCache
.
query
visitor
,
arel
.
ast
else
ActiveRecord
::
StatementCache
.
partial_query
visitor
,
arel
.
ast
,
collector
end
end
# Returns an ActiveRecord::Result instance.
...
...
activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
浏览文件 @
bd3b28f7
...
...
@@ -44,10 +44,6 @@ def initialize(connection, logger, connection_options, config)
configure_connection
end
def
cacheable_query
(
arel
)
ActiveRecord
::
StatementCache
.
partial_query
visitor
,
arel
.
ast
,
collector
end
MAX_INDEX_LENGTH_FOR_UTF8MB4
=
191
def
initialize_schema_migrations_table
if
@config
[
:encoding
]
==
'utf8mb4'
...
...
activerecord/lib/active_record/reflection.rb
浏览文件 @
bd3b28f7
require
'thread'
module
ActiveRecord
# = Active Record Reflection
module
Reflection
# :nodoc:
...
...
@@ -199,6 +201,13 @@ def initialize(macro, name, scope, options, active_record)
@type
=
options
[
:as
]
&&
"
#{
options
[
:as
]
}
_type"
@foreign_type
=
options
[
:foreign_type
]
||
"
#{
name
}
_type"
@constructable
=
calculate_constructable
(
macro
,
options
)
@association_scope_cache
=
{}
@scope_lock
=
Mutex
.
new
end
def
association_scope_cache
(
conn
)
key
=
conn
.
prepared_statements
@association_scope_cache
[
key
]
||=
@scope_lock
.
synchronize
{
yield
}
end
# Returns a new, unsaved instance of the associated class. +attributes+ will
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录