martenframework / marten

The pragmatic web framework.
https://martenframework.com
MIT License
405 stars 23 forks source link

Add the ability to count records on specific column #134

Closed treagod closed 8 months ago

treagod commented 8 months ago

This adds the ability to count records by a specific column name.

Closes #95

treagod commented 8 months ago

Thanks for looking into that! 👍

This is definitely a good start! 🚀 I left a few comments below that we will have to address before merging this PR. The most important thing is that we need to add some logic in order to verify that the targeted field exists. Moreover, we should also update the Marten::DB::Query::Set#count so that it's possible to call the #count query set method with a field name (updating the Marten::DB::Query::SQL::Query#count method only isn't sufficient!).

One more question: There is also a #count method for the raw_set.cr. Should I adapt the API so that the method calls are the same? Even though the method is not calling #count on the sql internally?

ellmetha commented 8 months ago

One more question: There is also a #count method for the raw_set.cr. Should I adapt the API so that the method calls are the same? Even though the method is not calling #count on the sql internally?

@treagod no need! For raw query sets we simply execute the specified raw SQL query and determine the number of records based on the obtained results. We wouldn't be able to do a count on a specific column in that situation.