Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
ddab9d7f
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,发现更多精彩内容 >>
提交
ddab9d7f
编写于
6月 11, 2008
作者:
B
Ben Munat
提交者:
Jeremy Kemper
6月 11, 2008
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add :from option to calculations. [#397 state:resolved]
上级
e170d34a
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
57 addition
and
3 deletion
+57
-3
activerecord/CHANGELOG
activerecord/CHANGELOG
+2
-0
activerecord/lib/active_record/calculations.rb
activerecord/lib/active_record/calculations.rb
+9
-3
activerecord/test/cases/calculations_test.rb
activerecord/test/cases/calculations_test.rb
+46
-0
未找到文件。
activerecord/CHANGELOG
浏览文件 @
ddab9d7f
*Edge*
* Add :from option to calculations. #397 [Ben Munat]
* Add :validate option to associations to enable/disable the automatic validation of associated models. Resolves #301. [Jan De Poorter]
* PostgreSQL: use 'INSERT ... RETURNING id' for 8.2 and later. [Jeremy Kemper]
...
...
activerecord/lib/active_record/calculations.rb
浏览文件 @
ddab9d7f
module
ActiveRecord
module
Calculations
#:nodoc:
CALCULATIONS_OPTIONS
=
[
:conditions
,
:joins
,
:order
,
:select
,
:group
,
:having
,
:distinct
,
:limit
,
:offset
,
:include
]
CALCULATIONS_OPTIONS
=
[
:conditions
,
:joins
,
:order
,
:select
,
:group
,
:having
,
:distinct
,
:limit
,
:offset
,
:include
,
:from
]
def
self
.
included
(
base
)
base
.
extend
(
ClassMethods
)
end
...
...
@@ -27,6 +27,8 @@ module ClassMethods
# * <tt>:select</tt>: By default, this is * as in SELECT * FROM, but can be changed if you, for example, want to do a join but not
# include the joined columns.
# * <tt>:distinct</tt>: Set this to true to make this a distinct calculation, such as SELECT COUNT(DISTINCT posts.id) ...
# * <tt>:from</tt> - By default, this is the table name of the class, but can be changed to an alternate table name (or even the name
# of a database view).
#
# Examples for counting all:
# Person.count # returns the total count of all people
...
...
@@ -178,8 +180,12 @@ def construct_calculation_sql(operation, column_name, options) #:nodoc:
sql
=
"SELECT COUNT(*) AS
#{
aggregate_alias
}
"
if
use_workaround
sql
<<
",
#{
options
[
:group_field
]
}
AS
#{
options
[
:group_alias
]
}
"
if
options
[
:group
]
sql
<<
" FROM (SELECT
#{
distinct
}#{
column_name
}
"
if
use_workaround
sql
<<
" FROM
#{
connection
.
quote_table_name
(
table_name
)
}
"
if
options
[
:from
]
sql
<<
" FROM
#{
options
[
:from
]
}
"
else
sql
<<
" FROM (SELECT
#{
distinct
}#{
column_name
}
"
if
use_workaround
sql
<<
" FROM
#{
connection
.
quote_table_name
(
table_name
)
}
"
end
if
merged_includes
.
any?
join_dependency
=
ActiveRecord
::
Associations
::
ClassMethods
::
JoinDependency
.
new
(
self
,
merged_includes
,
options
[
:joins
])
sql
<<
join_dependency
.
join_associations
.
collect
{
|
join
|
join
.
association_join
}.
join
...
...
activerecord/test/cases/calculations_test.rb
浏览文件 @
ddab9d7f
require
"cases/helper"
require
'models/company'
require
'models/topic'
require
'models/edge'
Company
.
has_many
:accounts
...
...
@@ -274,4 +275,49 @@ def test_count_with_too_many_parameters_raises
def
test_should_sum_expression
assert_equal
636
,
Account
.
sum
(
"2 * credit_limit"
)
end
def
test_count_with_from_option
assert_equal
Company
.
count
(
:all
),
Company
.
count
(
:all
,
:from
=>
'companies'
)
assert_equal
Account
.
count
(
:all
,
:conditions
=>
"credit_limit = 50"
),
Account
.
count
(
:all
,
:from
=>
'accounts'
,
:conditions
=>
"credit_limit = 50"
)
assert_equal
Company
.
count
(
:type
,
:conditions
=>
{
:type
=>
"Firm"
}),
Company
.
count
(
:type
,
:conditions
=>
{
:type
=>
"Firm"
},
:from
=>
'companies'
)
end
def
test_sum_with_from_option
assert_equal
Account
.
sum
(
:credit_limit
),
Account
.
sum
(
:credit_limit
,
:from
=>
'accounts'
)
assert_equal
Account
.
sum
(
:credit_limit
,
:conditions
=>
"credit_limit > 50"
),
Account
.
sum
(
:credit_limit
,
:from
=>
'accounts'
,
:conditions
=>
"credit_limit > 50"
)
end
def
test_average_with_from_option
assert_equal
Account
.
average
(
:credit_limit
),
Account
.
average
(
:credit_limit
,
:from
=>
'accounts'
)
assert_equal
Account
.
average
(
:credit_limit
,
:conditions
=>
"credit_limit > 50"
),
Account
.
average
(
:credit_limit
,
:from
=>
'accounts'
,
:conditions
=>
"credit_limit > 50"
)
end
def
test_minimum_with_from_option
assert_equal
Account
.
minimum
(
:credit_limit
),
Account
.
minimum
(
:credit_limit
,
:from
=>
'accounts'
)
assert_equal
Account
.
minimum
(
:credit_limit
,
:conditions
=>
"credit_limit > 50"
),
Account
.
minimum
(
:credit_limit
,
:from
=>
'accounts'
,
:conditions
=>
"credit_limit > 50"
)
end
def
test_maximum_with_from_option
assert_equal
Account
.
maximum
(
:credit_limit
),
Account
.
maximum
(
:credit_limit
,
:from
=>
'accounts'
)
assert_equal
Account
.
maximum
(
:credit_limit
,
:conditions
=>
"credit_limit > 50"
),
Account
.
maximum
(
:credit_limit
,
:from
=>
'accounts'
,
:conditions
=>
"credit_limit > 50"
)
end
def
test_from_option_with_specified_index
if
Edge
.
connection
.
adapter_name
==
'MySQL'
assert_equal
Edge
.
count
(
:all
),
Edge
.
count
(
:all
,
:from
=>
'edges USE INDEX(unique_edge_index)'
)
assert_equal
Edge
.
count
(
:all
,
:conditions
=>
'sink_id < 5'
),
Edge
.
count
(
:all
,
:from
=>
'edges USE INDEX(unique_edge_index)'
,
:conditions
=>
'sink_id < 5'
)
end
end
def
test_from_option_with_table_different_than_class
assert_equal
Account
.
count
(
:all
),
Company
.
count
(
:all
,
:from
=>
'accounts'
)
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录