Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
1b022990
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,发现更多精彩内容 >>
提交
1b022990
编写于
5月 10, 2013
作者:
J
Jon Leighton
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #6792 from Empact/postgres-distinct
Fix that #exists? can produce invalid SQL: "SELECT DISTINCT DISTINCT"
上级
7d74a318
15d6e4dc
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
61 addition
and
12 deletion
+61
-12
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
..._record/connection_adapters/abstract/schema_statements.rb
+11
-3
activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
...ecord/connection_adapters/postgresql/schema_statements.rb
+2
-7
activerecord/lib/active_record/relation/finder_methods.rb
activerecord/lib/active_record/relation/finder_methods.rb
+2
-2
activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
...test/cases/adapters/postgresql/postgresql_adapter_test.rb
+34
-0
activerecord/test/cases/finder_test.rb
activerecord/test/cases/finder_test.rb
+12
-0
未找到文件。
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
浏览文件 @
1b022990
...
...
@@ -706,12 +706,20 @@ def add_column_options!(sql, options) #:nodoc:
end
# SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
# Both PostgreSQL and Oracle overrides this for custom DISTINCT syntax.
#
# distinct("posts.id",
"posts.created_at desc"
)
# distinct("posts.id",
["posts.created_at desc"]
)
#
def
distinct
(
columns
,
order_by
)
"DISTINCT
#{
columns
}
"
"DISTINCT
#{
columns_for_distinct
(
columns
,
order_by
)
}
"
end
# Given a set of columns and an ORDER BY clause, returns the columns for a SELECT DISTINCT.
# Both PostgreSQL and Oracle overrides this for custom DISTINCT syntax - they
# require the order columns appear in the SELECT.
#
# columns_for_distinct("posts.id", ["posts.created_at desc"])
def
columns_for_distinct
(
columns
,
orders
)
columns
end
# Adds timestamps (+created_at+ and +updated_at+) columns to the named table.
...
...
activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
浏览文件 @
1b022990
...
...
@@ -467,14 +467,9 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil)
end
end
# Returns a SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
#
# PostgreSQL requires the ORDER BY columns in the select list for distinct queries, and
# requires that the ORDER BY include the distinct column.
#
# distinct("posts.id", ["posts.created_at desc"])
# # => "DISTINCT posts.id, posts.created_at AS alias_0"
def
distinct
(
columns
,
orders
)
#:nodoc:
def
columns_for_distinct
(
columns
,
orders
)
#:nodoc:
order_columns
=
orders
.
map
{
|
s
|
# Convert Arel node to string
s
=
s
.
to_sql
unless
s
.
is_a?
(
String
)
...
...
@@ -482,7 +477,7 @@ def distinct(columns, orders) #:nodoc:
s
.
gsub
(
/\s+(ASC|DESC)\s*(NULLS\s+(FIRST|LAST)\s*)?/i
,
''
)
}.
reject
(
&
:blank?
).
map
.
with_index
{
|
column
,
i
|
"
#{
column
}
AS alias_
#{
i
}
"
}
[
super
].
concat
(
order_columns
)
.
join
(
', '
)
[
super
,
*
order_columns
]
.
join
(
', '
)
end
end
end
...
...
activerecord/lib/active_record/relation/finder_methods.rb
浏览文件 @
1b022990
...
...
@@ -245,9 +245,9 @@ def apply_join_dependency(relation, join_dependency)
def
construct_limited_ids_condition
(
relation
)
orders
=
relation
.
order_values
.
map
{
|
val
|
val
.
presence
}.
compact
values
=
@klass
.
connection
.
distinct
(
"
#{
quoted_table_name
}
.
#{
primary_key
}
"
,
orders
)
values
=
@klass
.
connection
.
columns_for_distinct
(
"
#{
quoted_table_name
}
.
#{
quoted_
primary_key
}
"
,
orders
)
relation
=
relation
.
dup
.
select
(
values
)
relation
=
relation
.
dup
.
select
(
values
)
.
distinct!
id_rows
=
@klass
.
connection
.
select_all
(
relation
.
arel
,
'SQL'
,
relation
.
bind_values
)
ids_array
=
id_rows
.
map
{
|
row
|
row
[
primary_key
]}
...
...
activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
浏览文件 @
1b022990
...
...
@@ -230,21 +230,41 @@ def test_distinct_zero_orders
@connection
.
distinct
(
"posts.id"
,
[])
end
def
test_columns_for_distinct_zero_orders
assert_equal
"posts.id"
,
@connection
.
columns_for_distinct
(
"posts.id"
,
[])
end
def
test_distinct_one_order
assert_equal
"DISTINCT posts.id, posts.created_at AS alias_0"
,
@connection
.
distinct
(
"posts.id"
,
[
"posts.created_at desc"
])
end
def
test_columns_for_distinct_one_order
assert_equal
"posts.id, posts.created_at AS alias_0"
,
@connection
.
columns_for_distinct
(
"posts.id"
,
[
"posts.created_at desc"
])
end
def
test_distinct_few_orders
assert_equal
"DISTINCT posts.id, posts.created_at AS alias_0, posts.position AS alias_1"
,
@connection
.
distinct
(
"posts.id"
,
[
"posts.created_at desc"
,
"posts.position asc"
])
end
def
test_columns_for_distinct_few_orders
assert_equal
"posts.id, posts.created_at AS alias_0, posts.position AS alias_1"
,
@connection
.
columns_for_distinct
(
"posts.id"
,
[
"posts.created_at desc"
,
"posts.position asc"
])
end
def
test_distinct_blank_not_nil_orders
assert_equal
"DISTINCT posts.id, posts.created_at AS alias_0"
,
@connection
.
distinct
(
"posts.id"
,
[
"posts.created_at desc"
,
""
,
" "
])
end
def
test_columns_for_distinct_blank_not_nil_orders
assert_equal
"posts.id, posts.created_at AS alias_0"
,
@connection
.
columns_for_distinct
(
"posts.id"
,
[
"posts.created_at desc"
,
""
,
" "
])
end
def
test_distinct_with_arel_order
order
=
Object
.
new
def
order
.
to_sql
...
...
@@ -254,11 +274,25 @@ def order.to_sql
@connection
.
distinct
(
"posts.id"
,
[
order
])
end
def
test_columns_for_distinct_with_arel_order
order
=
Object
.
new
def
order
.
to_sql
"posts.created_at desc"
end
assert_equal
"posts.id, posts.created_at AS alias_0"
,
@connection
.
columns_for_distinct
(
"posts.id"
,
[
order
])
end
def
test_distinct_with_nulls
assert_equal
"DISTINCT posts.title, posts.updater_id AS alias_0"
,
@connection
.
distinct
(
"posts.title"
,
[
"posts.updater_id desc nulls first"
])
assert_equal
"DISTINCT posts.title, posts.updater_id AS alias_0"
,
@connection
.
distinct
(
"posts.title"
,
[
"posts.updater_id desc nulls last"
])
end
def
test_columns_for_distinct_with_nulls
assert_equal
"posts.title, posts.updater_id AS alias_0"
,
@connection
.
columns_for_distinct
(
"posts.title"
,
[
"posts.updater_id desc nulls first"
])
assert_equal
"posts.title, posts.updater_id AS alias_0"
,
@connection
.
columns_for_distinct
(
"posts.title"
,
[
"posts.updater_id desc nulls last"
])
end
def
test_raise_error_when_cannot_translate_exception
assert_raise
TypeError
do
@connection
.
send
(
:log
,
nil
)
{
@connection
.
execute
(
nil
)
}
...
...
activerecord/test/cases/finder_test.rb
浏览文件 @
1b022990
...
...
@@ -98,6 +98,18 @@ def test_exists_with_includes_limit_and_empty_result
assert
!
Topic
.
includes
(
:replies
).
limit
(
1
).
where
(
'0 = 1'
).
exists?
end
def
test_exists_with_distinct_association_includes_and_limit
author
=
Author
.
first
assert
!
author
.
unique_categorized_posts
.
includes
(
:special_comments
).
limit
(
0
).
exists?
assert
author
.
unique_categorized_posts
.
includes
(
:special_comments
).
limit
(
1
).
exists?
end
def
test_exists_with_distinct_association_includes_limit_and_order
author
=
Author
.
first
assert
!
author
.
unique_categorized_posts
.
includes
(
:special_comments
).
order
(
'comments.taggings_count DESC'
).
limit
(
0
).
exists?
assert
author
.
unique_categorized_posts
.
includes
(
:special_comments
).
order
(
'comments.taggings_count DESC'
).
limit
(
1
).
exists?
end
def
test_exists_with_empty_table_and_no_args_given
Topic
.
delete_all
assert
!
Topic
.
exists?
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录