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

support having #171

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/infi/clickhouse_orm/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ def __init__(self, model_cls, database):
self._model_cls = model_cls
self._database = database
self._order_by = []
self._having = ''
self._where_q = Q()
self._prewhere_q = Q()
self._grouping_fields = []
Expand Down Expand Up @@ -392,6 +393,9 @@ def as_sql(self):
if self._grouping_with_totals:
sql += ' WITH TOTALS'

if self._having:
sql += '\nhaving ' + self.having_as_sql()
Copy link
Contributor

Choose a reason for hiding this comment

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

it makes sense to use caps i.e. HAVING in order to keep the style

Copy link
Author

Choose a reason for hiding this comment

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

ok,, I change it.


if self._order_by:
sql += '\nORDER BY ' + self.order_by_as_sql()

Expand All @@ -413,6 +417,12 @@ def order_by_as_sql(self):
for field in self._order_by
])

def having_as_sql(self):
"""
Returns the contents of the query's `Having` clause as a string.
"""
return self._having

def conditions_as_sql(self, prewhere=False):
"""
Returns the contents of the query's `WHERE` or `PREWHERE` clause as a string.
Expand Down Expand Up @@ -442,6 +452,14 @@ def order_by(self, *field_names):
qs._order_by = field_names
return qs

def having(self, having_str):
Copy link
Contributor

Choose a reason for hiding this comment

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

what is having_str ?
are you proposing to provide a condition just as a string?
It's a bit changing the concept of ORM, unlike when we do filter there is kwargs approach

Copy link
Author

Choose a reason for hiding this comment

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

I use Q function to support HAVING. Thanks to check it

"""
Returns a copy of this queryset with the having changed.
"""
qs = copy(self)
qs._having = having_str
return qs

def only(self, *field_names):
"""
Returns a copy of this queryset limited to the specified field names.
Expand Down