mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Remove default aliases from aggregate functions
This commit is contained in:
parent
bb8416d094
commit
712c002af5
4 changed files with 20 additions and 58 deletions
|
@ -120,10 +120,10 @@ Aggregate functions `AVG`, `SUM`, `COUNT`, `MIN`, `MAX`, `HAVING`:
|
|||
|
||||
```ruby
|
||||
photos.group(photos[:user_id]).having(photos[:id].count.gt(5)) # => SELECT FROM photos GROUP BY photos.user_id HAVING COUNT(photos.id) > 5
|
||||
users.project(users[:age].sum) # => SELECT SUM(users.age) AS sum_id FROM users
|
||||
users.project(users[:age].average) # => SELECT AVG(users.age) AS avg_id FROM users
|
||||
users.project(users[:age].maximum) # => SELECT MAX(users.age) AS max_id FROM users
|
||||
users.project(users[:age].minimum) # => SELECT MIN(users.age) AS min_id FROM users
|
||||
users.project(users[:age].sum) # => SELECT SUM(users.age) FROM users
|
||||
users.project(users[:age].average) # => SELECT AVG(users.age) FROM users
|
||||
users.project(users[:age].maximum) # => SELECT MAX(users.age) FROM users
|
||||
users.project(users[:age].minimum) # => SELECT MIN(users.age) FROM users
|
||||
users.project(users[:age].count) # => SELECT COUNT(users.age) FROM users
|
||||
```
|
||||
|
||||
|
@ -201,7 +201,7 @@ users.
|
|||
project(users[:id], cte_table[:click].sum).
|
||||
with(composed_cte)
|
||||
|
||||
# => WITH cte_table AS (SELECT FROM photos WHERE photos.created_at > '2014-05-02') SELECT users.id, SUM(cte_table.click) AS sum_id FROM users INNER JOIN cte_table ON users.id = cte_table.user_id
|
||||
# => WITH cte_table AS (SELECT FROM photos WHERE photos.created_at > '2014-05-02') SELECT users.id, SUM(cte_table.click) FROM users INNER JOIN cte_table ON users.id = cte_table.user_id
|
||||
```
|
||||
|
||||
When your query is too complex for `Arel`, you can use `Arel::SqlLiteral`:
|
||||
|
|
|
@ -4,31 +4,25 @@ module Arel
|
|||
Nodes::Count.new [self], distinct
|
||||
end
|
||||
|
||||
def sum(alias_as = "sum_id")
|
||||
Nodes::Sum.new [self], node_alias(alias_as)
|
||||
def sum
|
||||
Nodes::Sum.new [self]
|
||||
end
|
||||
|
||||
def maximum(alias_as = "max_id")
|
||||
Nodes::Max.new [self], node_alias(alias_as)
|
||||
def maximum
|
||||
Nodes::Max.new [self]
|
||||
end
|
||||
|
||||
def minimum(alias_as = "min_id")
|
||||
Nodes::Min.new [self], node_alias(alias_as)
|
||||
def minimum
|
||||
Nodes::Min.new [self]
|
||||
end
|
||||
|
||||
def average(alias_as = "avg_id")
|
||||
Nodes::Avg.new [self], node_alias(alias_as)
|
||||
def average
|
||||
Nodes::Avg.new [self]
|
||||
end
|
||||
|
||||
def extract field
|
||||
Nodes::Extract.new [self], field
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def node_alias(alias_as)
|
||||
alias_as.nil? ? nil : Nodes::SqlLiteral.new(alias_as)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -82,7 +82,7 @@ module Arel
|
|||
mgr = users.project(Arel.star).where(users[:karma].gt(avg))
|
||||
|
||||
mgr.to_sql.must_be_like %{
|
||||
SELECT * FROM "users" WHERE "users"."karma" > (SELECT AVG("users"."karma") AS avg_id FROM "users")
|
||||
SELECT * FROM "users" WHERE "users"."karma" > (SELECT AVG("users"."karma") FROM "users")
|
||||
}
|
||||
end
|
||||
end
|
||||
|
@ -269,12 +269,11 @@ module Arel
|
|||
relation[:id].average.must_be_kind_of Nodes::Avg
|
||||
end
|
||||
|
||||
# FIXME: backwards compat. Is this really necessary?
|
||||
it 'should set the alias to "avg_id"' do
|
||||
it 'should generate the proper SQL' do
|
||||
relation = Table.new(:users)
|
||||
mgr = relation.project relation[:id].average
|
||||
mgr.to_sql.must_be_like %{
|
||||
SELECT AVG("users"."id") AS avg_id
|
||||
SELECT AVG("users"."id")
|
||||
FROM "users"
|
||||
}
|
||||
end
|
||||
|
@ -286,12 +285,11 @@ module Arel
|
|||
relation[:id].maximum.must_be_kind_of Nodes::Max
|
||||
end
|
||||
|
||||
# FIXME: backwards compat. Is this really necessary?
|
||||
it 'should set the alias to "max_id"' do
|
||||
it 'should generate the proper SQL' do
|
||||
relation = Table.new(:users)
|
||||
mgr = relation.project relation[:id].maximum
|
||||
mgr.to_sql.must_be_like %{
|
||||
SELECT MAX("users"."id") AS max_id
|
||||
SELECT MAX("users"."id")
|
||||
FROM "users"
|
||||
}
|
||||
end
|
||||
|
@ -310,12 +308,11 @@ module Arel
|
|||
relation[:id].sum.must_be_kind_of Nodes::Sum
|
||||
end
|
||||
|
||||
# FIXME: backwards compat. Is this really necessary?
|
||||
it 'should set the alias to "sum_id"' do
|
||||
it 'should generate the proper SQL' do
|
||||
relation = Table.new(:users)
|
||||
mgr = relation.project relation[:id].sum
|
||||
mgr.to_sql.must_be_like %{
|
||||
SELECT SUM("users"."id") AS sum_id
|
||||
SELECT SUM("users"."id")
|
||||
FROM "users"
|
||||
}
|
||||
end
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
require 'helper'
|
||||
|
||||
module Arel
|
||||
describe "Expressions" do
|
||||
before do
|
||||
@table = Table.new(:users)
|
||||
end
|
||||
|
||||
describe "average" do
|
||||
it "aliases the average as avg_id by default" do
|
||||
@table[:score].average.to_sql.must_be_like %{
|
||||
AVG("users"."score") AS avg_id
|
||||
}
|
||||
end
|
||||
|
||||
it "aliases the average as another string" do
|
||||
@table[:score].average("my_alias").to_sql.must_be_like %{
|
||||
AVG("users"."score") AS my_alias
|
||||
}
|
||||
end
|
||||
|
||||
it "omits the alias if nil" do
|
||||
@table[:score].average(nil).to_sql.must_be_like %{
|
||||
AVG("users"."score")
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue