Skip to content

Commit 5659546

Browse files
author
linzhiwen
committed
1. add toYYYYMM[DD[hhmmss]] function 2. fix str(queryset.query) when default database is not clickhouse
1 parent b1cba8b commit 5659546

File tree

5 files changed

+59
-15
lines changed

5 files changed

+59
-15
lines changed
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
from .base import ClickhouseModel
22
from .engines import *
3-
from .engines import __all__ as engine_all # NOQA
3+
from .engines import __all__ as engines_all # NOQA
44
from .fields import *
55
from .fields import __all__ as fields_all # NOQA
6+
from .functions import *
7+
from .functions import __all__ as fucntions_all # NOQA
68
from .indexes import *
7-
from .indexes import __all__ as index_all # NOQA
9+
from .indexes import __all__ as indexes_all # NOQA
810

911
__all__ = [
1012
'ClickhouseModel',
13+
*engines_all,
1114
*fields_all,
12-
*engine_all,
13-
*index_all,
15+
*fucntions_all,
16+
*indexes_all,
1417
]

clickhouse_backend/models/base.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ def _do_update(self, base_qs, using, pk_val, values, update_fields, forced_updat
5858
# exists.
5959
return update_fields is not None or filtered.exists()
6060
return (
61-
filtered.exists()
62-
and
63-
# It may happen that the object is deleted from the DB right after
64-
# this check, causing the subsequent UPDATE to return zero matching
65-
# rows. The same result can occur in some rare cases when the
66-
# database returns zero despite the UPDATE being executed
67-
# successfully (a row is matched and updated). In order to
68-
# distinguish these two cases, the object's existence in the
69-
# database is again checked for if the UPDATE query returns 0.
70-
(filtered._update(values) > 0 or filtered.exists())
61+
filtered.exists()
62+
and
63+
# It may happen that the object is deleted from the DB right after
64+
# this check, causing the subsequent UPDATE to return zero matching
65+
# rows. The same result can occur in some rare cases when the
66+
# database returns zero despite the UPDATE being executed
67+
# successfully (a row is matched and updated). In order to
68+
# distinguish these two cases, the object's existence in the
69+
# database is again checked for if the UPDATE query returns 0.
70+
(filtered._update(values) > 0 or filtered.exists())
7171
)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from django.conf import settings
2+
from django.db.models import Func, Value
3+
4+
__all__ = [
5+
'toYYYYMM',
6+
'toYYYYMMDD',
7+
'toYYYYMMDDhhmmss',
8+
]
9+
10+
11+
class toYYYYMM(Func):
12+
function = 'toYYYYMM'
13+
14+
def __init__(self, *expressions, output_field=None, **extra):
15+
arity = len(expressions)
16+
if arity < 1 or arity > 2:
17+
raise TypeError(
18+
"'%s' takes 1 or 2 arguments (%s given)"
19+
% (
20+
self.__class__.__name__,
21+
len(expressions),
22+
)
23+
)
24+
if arity == 2 and isinstance(expressions[1], str):
25+
expressions = (expressions[0], Value(expressions[1]))
26+
elif settings.USE_TZ:
27+
expressions = (expressions[0], Value(settings.TIME_ZONE))
28+
super().__init__(*expressions, output_field=output_field, **extra)
29+
30+
31+
class toYYYYMMDD(toYYYYMM):
32+
function = 'toYYYYMMDD'
33+
34+
35+
class toYYYYMMDDhhmmss(toYYYYMM):
36+
function = 'toYYYYMMDDhhmmss'

clickhouse_backend/models/sql/query.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections import namedtuple
22

3+
from django.db import router
34
from django.db.models.sql import query
45
from django.db.models.sql import subqueries
56

@@ -16,6 +17,10 @@ def __init__(self, model, where=query.WhereNode, alias_cols=True):
1617
super().__init__(model, where, alias_cols)
1718
self.setting_info = {}
1819

20+
def sql_with_params(self):
21+
"""Choose the right db when database router is used."""
22+
return self.get_compiler(router.db_for_read(self.model)).as_sql()
23+
1924
def clone(self):
2025
obj = super().clone()
2126
obj.setting_info = self.setting_info.copy()

example/testapp/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Meta:
2525
db_table = 'event'
2626
engine = models.ReplacingMergeTree(
2727
order_by=['id'],
28-
partition_by=Func('timestamp', function='toYYYYMMDD')
28+
partition_by=models.toYYYYMMDD('timestamp')
2929
)
3030
indexes = [
3131
models.Index(

0 commit comments

Comments
 (0)