forked from DefectDojo/django-DefectDojo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_group_concat.py
35 lines (30 loc) · 1.07 KB
/
sql_group_concat.py
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
from django.db.models import Aggregate, CharField
class Sql_GroupConcat(Aggregate):
function = "GROUP_CONCAT"
allow_distinct = True
def __init__(
self, expression, separator, distinct=False, ordering=None, **extra
):
self.separator = separator
super().__init__(
expression,
distinct="DISTINCT " if distinct else "",
ordering=f" ORDER BY {ordering}" if ordering is not None else "",
separator=f' SEPARATOR "{separator}"',
output_field=CharField(),
**extra
)
def as_mysql(self, compiler, connection):
return super().as_sql(
compiler,
connection,
template="%(function)s(%(distinct)s%(expressions)s%(ordering)s%(separator)s)",
separator=f" SEPARATOR '{self.separator}'",
)
def as_sql(self, compiler, connection, **extra):
return super().as_sql(
compiler,
connection,
template="%(function)s(%(distinct)s%(expressions)s%(ordering)s)",
**extra
)