Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
351e39c2
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 搜索 >>
提交
351e39c2
编写于
2月 01, 2016
作者:
R
Ryuta Kamizono
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Extract `ExplainPrettyPrinter` to appropriate files
上级
43dff0a7
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
137 addition
and
119 deletion
+137
-119
activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
...tive_record/connection_adapters/abstract_mysql_adapter.rb
+2
-66
activerecord/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb
...ecord/connection_adapters/mysql/explain_pretty_printer.rb
+70
-0
activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb
...ord/connection_adapters/postgresql/database_statements.rb
+1
-38
activerecord/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
.../connection_adapters/postgresql/explain_pretty_printer.rb
+42
-0
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
...b/active_record/connection_adapters/postgresql_adapter.rb
+1
-0
activerecord/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb
...ord/connection_adapters/sqlite3/explain_pretty_printer.rb
+19
-0
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
.../lib/active_record/connection_adapters/sqlite3_adapter.rb
+2
-15
未找到文件。
activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
浏览文件 @
351e39c2
require
'active_record/connection_adapters/abstract_adapter'
require
'active_record/connection_adapters/mysql/column'
require
'active_record/connection_adapters/mysql/explain_pretty_printer'
require
'active_record/connection_adapters/mysql/schema_creation'
require
'active_record/connection_adapters/mysql/schema_definitions'
require
'active_record/connection_adapters/mysql/schema_dumper'
...
...
@@ -232,72 +233,7 @@ def explain(arel, binds = [])
result
=
exec_query
(
sql
,
'EXPLAIN'
,
binds
)
elapsed
=
Time
.
now
-
start
ExplainPrettyPrinter
.
new
.
pp
(
result
,
elapsed
)
end
class
ExplainPrettyPrinter
# :nodoc:
# Pretty prints the result of an EXPLAIN in a way that resembles the output of the
# MySQL shell:
#
# +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
# | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
# +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
# | 1 | SIMPLE | users | const | PRIMARY | PRIMARY | 4 | const | 1 | |
# | 1 | SIMPLE | posts | ALL | NULL | NULL | NULL | NULL | 1 | Using where |
# +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
# 2 rows in set (0.00 sec)
#
# This is an exercise in Ruby hyperrealism :).
def
pp
(
result
,
elapsed
)
widths
=
compute_column_widths
(
result
)
separator
=
build_separator
(
widths
)
pp
=
[]
pp
<<
separator
pp
<<
build_cells
(
result
.
columns
,
widths
)
pp
<<
separator
result
.
rows
.
each
do
|
row
|
pp
<<
build_cells
(
row
,
widths
)
end
pp
<<
separator
pp
<<
build_footer
(
result
.
rows
.
length
,
elapsed
)
pp
.
join
(
"
\n
"
)
+
"
\n
"
end
private
def
compute_column_widths
(
result
)
[].
tap
do
|
widths
|
result
.
columns
.
each_with_index
do
|
column
,
i
|
cells_in_column
=
[
column
]
+
result
.
rows
.
map
{
|
r
|
r
[
i
].
nil?
?
'NULL'
:
r
[
i
].
to_s
}
widths
<<
cells_in_column
.
map
(
&
:length
).
max
end
end
end
def
build_separator
(
widths
)
padding
=
1
'+'
+
widths
.
map
{
|
w
|
'-'
*
(
w
+
(
padding
*
2
))}.
join
(
'+'
)
+
'+'
end
def
build_cells
(
items
,
widths
)
cells
=
[]
items
.
each_with_index
do
|
item
,
i
|
item
=
'NULL'
if
item
.
nil?
justifier
=
item
.
is_a?
(
Numeric
)
?
'rjust'
:
'ljust'
cells
<<
item
.
to_s
.
send
(
justifier
,
widths
[
i
])
end
'| '
+
cells
.
join
(
' | '
)
+
' |'
end
def
build_footer
(
nrows
,
elapsed
)
rows_label
=
nrows
==
1
?
'row'
:
'rows'
"
#{
nrows
}
#{
rows_label
}
in set (%.2f sec)"
%
elapsed
end
MySQL
::
ExplainPrettyPrinter
.
new
.
pp
(
result
,
elapsed
)
end
def
clear_cache!
...
...
activerecord/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb
0 → 100644
浏览文件 @
351e39c2
module
ActiveRecord
module
ConnectionAdapters
module
MySQL
class
ExplainPrettyPrinter
# :nodoc:
# Pretty prints the result of an EXPLAIN in a way that resembles the output of the
# MySQL shell:
#
# +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
# | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
# +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
# | 1 | SIMPLE | users | const | PRIMARY | PRIMARY | 4 | const | 1 | |
# | 1 | SIMPLE | posts | ALL | NULL | NULL | NULL | NULL | 1 | Using where |
# +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
# 2 rows in set (0.00 sec)
#
# This is an exercise in Ruby hyperrealism :).
def
pp
(
result
,
elapsed
)
widths
=
compute_column_widths
(
result
)
separator
=
build_separator
(
widths
)
pp
=
[]
pp
<<
separator
pp
<<
build_cells
(
result
.
columns
,
widths
)
pp
<<
separator
result
.
rows
.
each
do
|
row
|
pp
<<
build_cells
(
row
,
widths
)
end
pp
<<
separator
pp
<<
build_footer
(
result
.
rows
.
length
,
elapsed
)
pp
.
join
(
"
\n
"
)
+
"
\n
"
end
private
def
compute_column_widths
(
result
)
[].
tap
do
|
widths
|
result
.
columns
.
each_with_index
do
|
column
,
i
|
cells_in_column
=
[
column
]
+
result
.
rows
.
map
{
|
r
|
r
[
i
].
nil?
?
'NULL'
:
r
[
i
].
to_s
}
widths
<<
cells_in_column
.
map
(
&
:length
).
max
end
end
end
def
build_separator
(
widths
)
padding
=
1
'+'
+
widths
.
map
{
|
w
|
'-'
*
(
w
+
(
padding
*
2
))}.
join
(
'+'
)
+
'+'
end
def
build_cells
(
items
,
widths
)
cells
=
[]
items
.
each_with_index
do
|
item
,
i
|
item
=
'NULL'
if
item
.
nil?
justifier
=
item
.
is_a?
(
Numeric
)
?
'rjust'
:
'ljust'
cells
<<
item
.
to_s
.
send
(
justifier
,
widths
[
i
])
end
'| '
+
cells
.
join
(
' | '
)
+
' |'
end
def
build_footer
(
nrows
,
elapsed
)
rows_label
=
nrows
==
1
?
'row'
:
'rows'
"
#{
nrows
}
#{
rows_label
}
in set (%.2f sec)"
%
elapsed
end
end
end
end
end
activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb
浏览文件 @
351e39c2
...
...
@@ -4,44 +4,7 @@ module PostgreSQL
module
DatabaseStatements
def
explain
(
arel
,
binds
=
[])
sql
=
"EXPLAIN
#{
to_sql
(
arel
,
binds
)
}
"
ExplainPrettyPrinter
.
new
.
pp
(
exec_query
(
sql
,
'EXPLAIN'
,
binds
))
end
class
ExplainPrettyPrinter
# :nodoc:
# Pretty prints the result of an EXPLAIN in a way that resembles the output of the
# PostgreSQL shell:
#
# QUERY PLAN
# ------------------------------------------------------------------------------
# Nested Loop Left Join (cost=0.00..37.24 rows=8 width=0)
# Join Filter: (posts.user_id = users.id)
# -> Index Scan using users_pkey on users (cost=0.00..8.27 rows=1 width=4)
# Index Cond: (id = 1)
# -> Seq Scan on posts (cost=0.00..28.88 rows=8 width=4)
# Filter: (posts.user_id = 1)
# (6 rows)
#
def
pp
(
result
)
header
=
result
.
columns
.
first
lines
=
result
.
rows
.
map
(
&
:first
)
# We add 2 because there's one char of padding at both sides, note
# the extra hyphens in the example above.
width
=
[
header
,
*
lines
].
map
(
&
:length
).
max
+
2
pp
=
[]
pp
<<
header
.
center
(
width
).
rstrip
pp
<<
'-'
*
width
pp
+=
lines
.
map
{
|
line
|
"
#{
line
}
"
}
nrows
=
result
.
rows
.
length
rows_label
=
nrows
==
1
?
'row'
:
'rows'
pp
<<
"(
#{
nrows
}
#{
rows_label
}
)"
pp
.
join
(
"
\n
"
)
+
"
\n
"
end
PostgreSQL
::
ExplainPrettyPrinter
.
new
.
pp
(
exec_query
(
sql
,
'EXPLAIN'
,
binds
))
end
def
select_value
(
arel
,
name
=
nil
,
binds
=
[])
...
...
activerecord/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb
0 → 100644
浏览文件 @
351e39c2
module
ActiveRecord
module
ConnectionAdapters
module
PostgreSQL
class
ExplainPrettyPrinter
# :nodoc:
# Pretty prints the result of an EXPLAIN in a way that resembles the output of the
# PostgreSQL shell:
#
# QUERY PLAN
# ------------------------------------------------------------------------------
# Nested Loop Left Join (cost=0.00..37.24 rows=8 width=0)
# Join Filter: (posts.user_id = users.id)
# -> Index Scan using users_pkey on users (cost=0.00..8.27 rows=1 width=4)
# Index Cond: (id = 1)
# -> Seq Scan on posts (cost=0.00..28.88 rows=8 width=4)
# Filter: (posts.user_id = 1)
# (6 rows)
#
def
pp
(
result
)
header
=
result
.
columns
.
first
lines
=
result
.
rows
.
map
(
&
:first
)
# We add 2 because there's one char of padding at both sides, note
# the extra hyphens in the example above.
width
=
[
header
,
*
lines
].
map
(
&
:length
).
max
+
2
pp
=
[]
pp
<<
header
.
center
(
width
).
rstrip
pp
<<
'-'
*
width
pp
+=
lines
.
map
{
|
line
|
"
#{
line
}
"
}
nrows
=
result
.
rows
.
length
rows_label
=
nrows
==
1
?
'row'
:
'rows'
pp
<<
"(
#{
nrows
}
#{
rows_label
}
)"
pp
.
join
(
"
\n
"
)
+
"
\n
"
end
end
end
end
end
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
浏览文件 @
351e39c2
...
...
@@ -5,6 +5,7 @@
require
"active_record/connection_adapters/abstract_adapter"
require
"active_record/connection_adapters/postgresql/column"
require
"active_record/connection_adapters/postgresql/database_statements"
require
"active_record/connection_adapters/postgresql/explain_pretty_printer"
require
"active_record/connection_adapters/postgresql/oid"
require
"active_record/connection_adapters/postgresql/quoting"
require
"active_record/connection_adapters/postgresql/referential_integrity"
...
...
activerecord/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb
0 → 100644
浏览文件 @
351e39c2
module
ActiveRecord
module
ConnectionAdapters
module
SQLite3
class
ExplainPrettyPrinter
# :nodoc:
# Pretty prints the result of an EXPLAIN QUERY PLAN in a way that resembles
# the output of the SQLite shell:
#
# 0|0|0|SEARCH TABLE users USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)
# 0|1|1|SCAN TABLE posts (~100000 rows)
#
def
pp
(
result
)
result
.
rows
.
map
do
|
row
|
row
.
join
(
'|'
)
end
.
join
(
"
\n
"
)
+
"
\n
"
end
end
end
end
end
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
浏览文件 @
351e39c2
require
'active_record/connection_adapters/abstract_adapter'
require
'active_record/connection_adapters/statement_pool'
require
'active_record/connection_adapters/sqlite3/explain_pretty_printer'
require
'active_record/connection_adapters/sqlite3/schema_creation'
gem
'sqlite3'
,
'~> 1.3.6'
...
...
@@ -218,21 +219,7 @@ def quote_column_name(name) #:nodoc:
def
explain
(
arel
,
binds
=
[])
sql
=
"EXPLAIN QUERY PLAN
#{
to_sql
(
arel
,
binds
)
}
"
ExplainPrettyPrinter
.
new
.
pp
(
exec_query
(
sql
,
'EXPLAIN'
,
[]))
end
class
ExplainPrettyPrinter
# Pretty prints the result of an EXPLAIN QUERY PLAN in a way that resembles
# the output of the SQLite shell:
#
# 0|0|0|SEARCH TABLE users USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)
# 0|1|1|SCAN TABLE posts (~100000 rows)
#
def
pp
(
result
)
# :nodoc:
result
.
rows
.
map
do
|
row
|
row
.
join
(
'|'
)
end
.
join
(
"
\n
"
)
+
"
\n
"
end
SQLite3
::
ExplainPrettyPrinter
.
new
.
pp
(
exec_query
(
sql
,
'EXPLAIN'
,
[]))
end
def
exec_query
(
sql
,
name
=
nil
,
binds
=
[],
prepare:
false
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录