-
-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathaggregates.py
More file actions
82 lines (61 loc) · 2.12 KB
/
Copy pathaggregates.py
File metadata and controls
82 lines (61 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from __future__ import annotations
from typing import Any
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.models import Aggregate, CharField
from django.db.models.sql.compiler import SQLCompiler
class BitAnd(Aggregate):
function = "BIT_AND"
name = "bitand"
class BitOr(Aggregate):
function = "BIT_OR"
name = "bitor"
class BitXor(Aggregate):
function = "BIT_XOR"
name = "bitxor"
class GroupConcat(Aggregate):
function = "GROUP_CONCAT"
def __init__(
self,
expression: Any,
distinct: bool = False,
separator: str | None = None,
ordering: str | None = None,
**extra: Any,
) -> None:
if "output_field" not in extra:
# This can/will be improved to SetTextField or ListTextField
extra["output_field"] = CharField()
super().__init__(expression, **extra)
self.distinct = distinct
self.separator = separator
if ordering not in ("asc", "desc", None):
raise ValueError("'ordering' must be one of 'asc', 'desc', or None")
self.ordering = ordering
def as_sql(
self,
compiler: SQLCompiler,
connection: BaseDatabaseWrapper,
**extra_context: Any,
) -> tuple[str, tuple[Any, ...]]:
connection.ops.check_expression_support(self)
sql = ["GROUP_CONCAT("]
if self.distinct:
sql.append("DISTINCT ")
expr_parts = []
params = []
for arg in self.source_expressions:
arg_sql, arg_params = compiler.compile(arg)
expr_parts.append(arg_sql)
params.extend(arg_params)
expr_sql = self.arg_joiner.join(expr_parts)
sql.append(expr_sql)
if self.ordering is not None:
sql.append(" ORDER BY ")
sql.append(expr_sql)
params.extend(params[:])
sql.append(" ")
sql.append(self.ordering.upper())
if self.separator is not None:
sql.append(f" SEPARATOR '{self.separator}'") # noqa: B028
sql.append(")")
return "".join(sql), tuple(params)