Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in GROUP BY querying #234

Merged
merged 7 commits into from Sep 14, 2019
Merged

Add in GROUP BY querying #234

merged 7 commits into from Sep 14, 2019

Conversation

jwoertink
Copy link
Member

@jwoertink jwoertink commented Sep 13, 2019

Fixes #193

  • - Low level group_by SQL generation
  • - High level Query access to low level group
  • - type-safe group_by access
  • - A simple way to get counts from a group like rails User.group(:status).count #=> {"active" => 100, "inactive" => 4}

We have select_count currently, but maybe that needs to become aware if there's some grouping?

@jwoertink jwoertink marked this pull request as ready for review September 13, 2019 19:38
@jwoertink jwoertink changed the title WIP: Add in GROUP BY querying Add in GROUP BY querying Sep 13, 2019
@paulcsmith
Copy link
Member

Very small comments. Otherwise looks awesome!

For the count I think we should have a different method otherwise the return type will be weird. How about “select_counts_by_group” or “select_group_counts”?

@paulcsmith
Copy link
Member

Can be added in a separate PR though. This is useful even without a count method!

spec/query_spec.cr Outdated Show resolved Hide resolved
spec/query_spec.cr Outdated Show resolved Hide resolved
src/avram/queryable.cr Show resolved Hide resolved
Copy link
Member

@paulcsmith paulcsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specs look good and overall LGTM. Just retying to understand the result of group_by because it looks the same as without group.

@@ -746,14 +746,10 @@ describe Avram::Query do

users = UserQuery.new.group(&.age).group(&.id)
users.query.statement.should eq "SELECT #{User::COLUMN_SQL} FROM users GROUP BY users.age, users.id"
users.map(&.name).should contain "Dwight"
users.map(&.name).should eq ["Dwight", "Michael", "Jim"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh so does it just return an array? What does group by do in this case. I thought it'd be a hash or something

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It returns an array here of .map(), but UserQuery.new.group(&.age).group(&.id) just returns the BaseQuery object like normal. It is confusing though because technically in this query, the group is useless.

I see this more as building blocks for the next parts to come as well as the underlying access. But if you have suggestions, or whatever, I'm all ears. Postgres grouping hurts my head lol

@paulcsmith
Copy link
Member

Yeah let’s merge it! I think this is a good starting point

@jwoertink jwoertink merged commit bbb127c into master Sep 14, 2019
@jwoertink jwoertink deleted the features/193 branch September 14, 2019 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add missing GROUP BY stuff
2 participants