Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
bf1494a1
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 搜索 >>
提交
bf1494a1
编写于
4月 08, 2019
作者:
R
Ryuta Kamizono
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix GROUP BY with calculate longer name field to respect `table_alias_length`
Follow up of
c9e4c848
.
上级
57c7cbb1
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
30 addition
and
11 deletion
+30
-11
activerecord/lib/active_record/connection_adapters/abstract/database_limits.rb
...ve_record/connection_adapters/abstract/database_limits.rb
+8
-4
activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb
...ive_record/connection_adapters/mysql/schema_statements.rb
+4
-0
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
...b/active_record/connection_adapters/postgresql_adapter.rb
+0
-2
activerecord/lib/active_record/relation/calculations.rb
activerecord/lib/active_record/relation/calculations.rb
+6
-5
activerecord/test/cases/calculations_test.rb
activerecord/test/cases/calculations_test.rb
+11
-0
activerecord/test/schema/schema.rb
activerecord/test/schema/schema.rb
+1
-0
未找到文件。
activerecord/lib/active_record/connection_adapters/abstract/database_limits.rb
浏览文件 @
bf1494a1
...
...
@@ -5,20 +5,24 @@
module
ActiveRecord
module
ConnectionAdapters
# :nodoc:
module
DatabaseLimits
def
max_identifier_length
# :nodoc:
64
end
# Returns the maximum length of a table alias.
def
table_alias_length
255
max_identifier_length
end
# Returns the maximum length of a column name.
def
column_name_length
64
max_identifier_length
end
deprecate
:column_name_length
# Returns the maximum length of a table name.
def
table_name_length
64
max_identifier_length
end
deprecate
:table_name_length
...
...
@@ -33,7 +37,7 @@ def allowed_index_name_length
# Returns the maximum length of an index name.
def
index_name_length
64
max_identifier_length
end
# Returns the maximum number of columns per table.
...
...
activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb
浏览文件 @
bf1494a1
...
...
@@ -121,6 +121,10 @@ def type_to_sql(type, limit: nil, precision: nil, scale: nil, size: limit_to_siz
sql
end
def
table_alias_length
256
# https://dev.mysql.com/doc/refman/8.0/en/identifiers.html
end
private
CHARSETS_OF_4BYTES_MAXLEN
=
[
"utf8mb4"
,
"utf16"
,
"utf16le"
,
"utf32"
]
...
...
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
浏览文件 @
bf1494a1
...
...
@@ -400,8 +400,6 @@ def extensions
def
max_identifier_length
@max_identifier_length
||=
query_value
(
"SHOW max_identifier_length"
,
"SCHEMA"
).
to_i
end
alias
table_alias_length
max_identifier_length
alias
index_name_length
max_identifier_length
# Set the authorized user for this session
def
session_auth
=
(
user
)
...
...
activerecord/lib/active_record/relation/calculations.rb
浏览文件 @
bf1494a1
...
...
@@ -319,12 +319,11 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
group_aliases
=
group_fields
.
map
{
|
field
|
field
=
connection
.
visitor
.
compile
(
field
)
if
Arel
.
arel_node?
(
field
)
column_alias_for
(
field
)
column_alias_for
(
field
.
to_s
.
downcase
)
}
group_columns
=
group_aliases
.
zip
(
group_fields
)
aggregate_alias
=
"
#{
operation
}
_
#{
column_name
.
to_s
.
downcase
}
"
aggregate_alias
=
column_alias_for
(
aggregate_alias
)
unless
aggregate_alias
.
match?
(
/\A\w+\z/
)
aggregate_alias
=
column_alias_for
(
"
#{
operation
}
_
#{
column_name
.
to_s
.
downcase
}
"
)
select_values
=
[
operation_over_aggregate_column
(
...
...
@@ -369,7 +368,7 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
end
]
end
# Converts the given
keys
to the value that the database adapter returns as
# Converts the given
field
to the value that the database adapter returns as
# a usable column name:
#
# column_alias_for("users.id") # => "users_id"
...
...
@@ -377,7 +376,9 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
# column_alias_for("count(distinct users.id)") # => "count_distinct_users_id"
# column_alias_for("count(*)") # => "count_all"
def
column_alias_for
(
field
)
column_alias
=
field
.
to_s
.
downcase
return
field
if
field
.
match?
(
/\A\w{,
#{
connection
.
table_alias_length
}
}\z/
)
column_alias
=
+
field
column_alias
.
gsub!
(
/\*/
,
"all"
)
column_alias
.
gsub!
(
/\W+/
,
" "
)
column_alias
.
strip!
...
...
activerecord/test/cases/calculations_test.rb
浏览文件 @
bf1494a1
...
...
@@ -363,6 +363,17 @@ def test_should_group_by_fields_with_table_alias
assert_equal
60
,
c
[
2
]
end
def
test_should_calculate_grouped_with_longer_field
field
=
"a"
*
Account
.
connection
.
max_identifier_length
Account
.
update_all
(
"
#{
field
}
= credit_limit"
)
c
=
Account
.
group
(
:firm_id
).
sum
(
field
)
assert_equal
50
,
c
[
1
]
assert_equal
105
,
c
[
6
]
assert_equal
60
,
c
[
2
]
end
def
test_should_calculate_with_invalid_field
assert_equal
6
,
Account
.
calculate
(
:count
,
"*"
)
assert_equal
6
,
Account
.
calculate
(
:count
,
:all
)
...
...
activerecord/test/schema/schema.rb
浏览文件 @
bf1494a1
...
...
@@ -19,6 +19,7 @@
t
.
references
:firm
,
index:
false
t
.
string
:firm_name
t
.
integer
:credit_limit
t
.
integer
"a"
*
max_identifier_length
end
create_table
:admin_accounts
,
force:
true
do
|
t
|
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录