2019-06-09 08:56:37 +08:00
|
|
|
from django.contrib.postgres.fields import ArrayField
|
2020-07-19 16:08:44 +08:00
|
|
|
from django.db.models import Aggregate, BooleanField, JSONField, Value
|
2015-02-08 23:21:48 +08:00
|
|
|
|
2016-07-05 17:47:24 +08:00
|
|
|
from .mixins import OrderableAggMixin
|
|
|
|
|
2015-02-08 23:21:48 +08:00
|
|
|
__all__ = [
|
2016-11-13 04:42:20 +08:00
|
|
|
'ArrayAgg', 'BitAnd', 'BitOr', 'BoolAnd', 'BoolOr', 'JSONBAgg', 'StringAgg',
|
2015-02-08 23:21:48 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2016-07-05 17:47:24 +08:00
|
|
|
class ArrayAgg(OrderableAggMixin, Aggregate):
|
2015-02-08 23:21:48 +08:00
|
|
|
function = 'ARRAY_AGG'
|
2016-07-05 17:47:24 +08:00
|
|
|
template = '%(function)s(%(distinct)s%(expressions)s %(ordering)s)'
|
2019-01-10 06:52:36 +08:00
|
|
|
allow_distinct = True
|
2017-01-23 23:34:42 +08:00
|
|
|
|
2017-12-31 03:46:52 +08:00
|
|
|
@property
|
|
|
|
def output_field(self):
|
|
|
|
return ArrayField(self.source_expressions[0].output_field)
|
|
|
|
|
2017-07-07 01:18:05 +08:00
|
|
|
def convert_value(self, value, expression, connection):
|
2015-02-08 23:21:48 +08:00
|
|
|
if not value:
|
|
|
|
return []
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
class BitAnd(Aggregate):
|
|
|
|
function = 'BIT_AND'
|
|
|
|
|
|
|
|
|
|
|
|
class BitOr(Aggregate):
|
|
|
|
function = 'BIT_OR'
|
|
|
|
|
|
|
|
|
|
|
|
class BoolAnd(Aggregate):
|
|
|
|
function = 'BOOL_AND'
|
2020-07-19 16:08:44 +08:00
|
|
|
output_field = BooleanField()
|
2015-02-08 23:21:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BoolOr(Aggregate):
|
|
|
|
function = 'BOOL_OR'
|
2020-07-19 16:08:44 +08:00
|
|
|
output_field = BooleanField()
|
2015-02-08 23:21:48 +08:00
|
|
|
|
|
|
|
|
2020-06-12 22:55:22 +08:00
|
|
|
class JSONBAgg(OrderableAggMixin, Aggregate):
|
2016-09-26 19:16:03 +08:00
|
|
|
function = 'JSONB_AGG'
|
2020-11-04 19:30:47 +08:00
|
|
|
template = '%(function)s(%(distinct)s%(expressions)s %(ordering)s)'
|
|
|
|
allow_distinct = True
|
2017-07-15 09:56:01 +08:00
|
|
|
output_field = JSONField()
|
2016-09-26 19:16:03 +08:00
|
|
|
|
2017-07-07 01:18:05 +08:00
|
|
|
def convert_value(self, value, expression, connection):
|
2016-09-26 19:16:03 +08:00
|
|
|
if not value:
|
2020-08-28 13:56:04 +08:00
|
|
|
return '[]'
|
2016-09-26 19:16:03 +08:00
|
|
|
return value
|
|
|
|
|
|
|
|
|
2016-07-05 17:47:24 +08:00
|
|
|
class StringAgg(OrderableAggMixin, Aggregate):
|
2015-02-08 23:21:48 +08:00
|
|
|
function = 'STRING_AGG'
|
2020-01-01 01:46:06 +08:00
|
|
|
template = '%(function)s(%(distinct)s%(expressions)s %(ordering)s)'
|
2019-01-10 06:52:36 +08:00
|
|
|
allow_distinct = True
|
2015-02-08 23:21:48 +08:00
|
|
|
|
2019-01-10 06:52:36 +08:00
|
|
|
def __init__(self, expression, delimiter, **extra):
|
2020-01-01 01:46:06 +08:00
|
|
|
delimiter_expr = Value(str(delimiter))
|
|
|
|
super().__init__(expression, delimiter_expr, **extra)
|
2015-02-08 23:21:48 +08:00
|
|
|
|
2017-07-07 01:18:05 +08:00
|
|
|
def convert_value(self, value, expression, connection):
|
2015-02-08 23:21:48 +08:00
|
|
|
if not value:
|
|
|
|
return ''
|
|
|
|
return value
|