| 1 |
from django.contrib.postgres.fields import ArrayField |
| 2 |
from django.db.models import Aggregate, BooleanField, JSONField, TextField, Value |
| 3 |
|
| 4 |
from .mixins import OrderableAggMixin |
| 5 |
|
| 6 |
__all__ = [ |
| 7 |
"ArrayAgg", |
| 8 |
"BitAnd", |
| 9 |
"BitOr", |
| 10 |
"BitXor", |
| 11 |
"BoolAnd", |
| 12 |
"BoolOr", |
| 13 |
"JSONBAgg", |
| 14 |
"StringAgg", |
| 15 |
] |
| 16 |
|
| 17 |
|
| 18 |
class ArrayAgg(OrderableAggMixin, Aggregate): |
| 19 |
function = "ARRAY_AGG" |
| 20 |
template = "%(function)s(%(distinct)s%(expressions)s %(order_by)s)" |
| 21 |
allow_distinct = True |
| 22 |
|
| 23 |
@property |
| 24 |
def output_field(self): |
| 25 |
return ArrayField(self.source_expressions[0].output_field) |
| 26 |
|
| 27 |
|
| 28 |
class BitAnd(Aggregate): |
| 29 |
function = "BIT_AND" |
| 30 |
|
| 31 |
|
| 32 |
class BitOr(Aggregate): |
| 33 |
function = "BIT_OR" |
| 34 |
|
| 35 |
|
| 36 |
class BitXor(Aggregate): |
| 37 |
function = "BIT_XOR" |
| 38 |
|
| 39 |
|
| 40 |
class BoolAnd(Aggregate): |
| 41 |
function = "BOOL_AND" |
| 42 |
output_field = BooleanField() |
| 43 |
|
| 44 |
|
| 45 |
class BoolOr(Aggregate): |
| 46 |
function = "BOOL_OR" |
| 47 |
output_field = BooleanField() |
| 48 |
|
| 49 |
|
| 50 |
class JSONBAgg(OrderableAggMixin, Aggregate): |
| 51 |
function = "JSONB_AGG" |
| 52 |
template = "%(function)s(%(distinct)s%(expressions)s %(order_by)s)" |
| 53 |
allow_distinct = True |
| 54 |
output_field = JSONField() |
| 55 |
|
| 56 |
|
| 57 |
class StringAgg(OrderableAggMixin, Aggregate): |
| 58 |
function = "STRING_AGG" |
| 59 |
template = "%(function)s(%(distinct)s%(expressions)s %(order_by)s)" |
| 60 |
allow_distinct = True |
| 61 |
output_field = TextField() |
| 62 |
|
| 63 |
def __init__(self, expression, delimiter, **extra): |
| 64 |
delimiter_expr = Value(str(delimiter)) |
| 65 |
super().__init__(expression, delimiter_expr, **extra) |