jumpserver/apps/common/db/aggregates.py

28 lines
809 B
Python
Raw Normal View History

2020-07-06 03:14:20 +00:00
from django.db.models import Aggregate
class GroupConcat(Aggregate):
function = 'GROUP_CONCAT'
2020-07-20 02:42:22 +00:00
template = '%(function)s(%(expressions)s %(order_by)s %(separator)s)'
2020-07-06 03:14:20 +00:00
allow_distinct = False
2020-07-20 02:42:22 +00:00
def __init__(self, expression, order_by=None, separator=',', **extra):
2020-07-06 03:14:20 +00:00
order_by_clause = ''
if order_by is not None:
order = 'ASC'
prefix, body = order_by[1], order_by[1:]
if prefix == '-':
order = 'DESC'
elif prefix == '+':
pass
else:
body = order_by
order_by_clause = f'ORDER BY {body} {order}'
super().__init__(
expression,
order_by=order_by_clause,
2020-07-20 02:42:22 +00:00
separator=f"SEPARATOR '{separator}'",
2020-07-06 03:14:20 +00:00
**extra
)