Skip to content

Commit

Permalink
Replace 1.3 _compile_context to 1.4 _statement_20
Browse files Browse the repository at this point in the history
  • Loading branch information
xzkostyan committed Oct 18, 2024
1 parent 1ad3bb3 commit 423c32e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
31 changes: 19 additions & 12 deletions clickhouse_sqlalchemy/orm/query.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from functools import partial

from sqlalchemy import exc
from sqlalchemy.sql.base import _generative
from sqlalchemy.orm.query import Query as BaseQuery
Expand All @@ -10,6 +12,19 @@
)


def _compile_state_factory(orig_compile_state_factory, query, statement, *args, **kwargs):
rv = orig_compile_state_factory(statement, *args, **kwargs)
new_stmt = rv.statement
new_stmt._with_cube = query._with_cube
new_stmt._with_rollup = query._with_rollup
new_stmt._with_totals = query._with_totals
new_stmt._final_clause = query._final
new_stmt._sample_clause = sample_clause(query._sample)
new_stmt._limit_by_clause = query._limit_by
new_stmt._array_join = query._array_join
return rv


class Query(BaseQuery):
_with_cube = False
_with_rollup = False
Expand All @@ -19,19 +34,11 @@ class Query(BaseQuery):
_limit_by = None
_array_join = None

def _compile_context(self, *args, **kwargs):
context = super(Query, self)._compile_context(*args, **kwargs)
query = context.query

query._with_cube = self._with_cube
query._with_rollup = self._with_rollup
query._with_totals = self._with_totals
query._final_clause = self._final
query._sample_clause = sample_clause(self._sample)
query._limit_by_clause = self._limit_by
query._array_join = self._array_join
def _statement_20(self, *args, **kwargs):
statement = super(Query, self)._statement_20(*args, **kwargs)
statement._compile_state_factory = partial(_compile_state_factory, statement._compile_state_factory, self)

return context
return statement

@_generative
def with_cube(self):
Expand Down
3 changes: 1 addition & 2 deletions tests/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ def _compile(self, clause, bind=None, literal_binds=False,
if bind is None:
bind = self.session.bind
if isinstance(clause, Query):
context = clause._compile_context()
clause = context.query
clause = clause._statement_20()

kw = {}
compile_kwargs = {}
Expand Down

0 comments on commit 423c32e

Please sign in to comment.