Skip to content

Commit

Permalink
Revert "Revert "Merge pull request #300 from jpcody/master""
Browse files Browse the repository at this point in the history
This reverts commit 9b92af7.

beta2 is out, and we've fixed the issue that this caused in Rails
  • Loading branch information
sgrif committed Oct 31, 2014
1 parent 8ac47c4 commit 5437df4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
10 changes: 5 additions & 5 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down Expand Up @@ -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`:
Expand Down
9 changes: 5 additions & 4 deletions lib/arel/expressions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ def count distinct = false
end

def sum
Nodes::Sum.new [self], Nodes::SqlLiteral.new('sum_id')
Nodes::Sum.new [self]
end

def maximum
Nodes::Max.new [self], Nodes::SqlLiteral.new('max_id')
Nodes::Max.new [self]
end

def minimum
Nodes::Min.new [self], Nodes::SqlLiteral.new('min_id')
Nodes::Min.new [self]
end

def average
Nodes::Avg.new [self], Nodes::SqlLiteral.new('avg_id')
Nodes::Avg.new [self]
end

def extract field
Nodes::Extract.new [self], field
end

end
end
17 changes: 7 additions & 10 deletions test/attributes/test_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ module Attributes
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

Expand Down Expand Up @@ -313,12 +313,11 @@ module Attributes
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
Expand All @@ -330,12 +329,11 @@ module Attributes
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
Expand All @@ -354,12 +352,11 @@ module Attributes
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
Expand Down

0 comments on commit 5437df4

Please sign in to comment.