An SQL function call.
| 1052 | |
| 1053 | @deconstructible(path="django.db.models.Func") |
| 1054 | class Func(SQLiteNumericMixin, Expression): |
| 1055 | """An SQL function call.""" |
| 1056 | |
| 1057 | function = None |
| 1058 | template = "%(function)s(%(expressions)s)" |
| 1059 | arg_joiner = ", " |
| 1060 | arity = None # The number of arguments the function accepts. |
| 1061 | |
| 1062 | def __init__(self, *expressions, output_field=None, **extra): |
| 1063 | if self.arity is not None and len(expressions) != self.arity: |
| 1064 | raise TypeError( |
| 1065 | "'%s' takes exactly %s %s (%s given)" |
| 1066 | % ( |
| 1067 | self.__class__.__name__, |
| 1068 | self.arity, |
| 1069 | "argument" if self.arity == 1 else "arguments", |
| 1070 | len(expressions), |
| 1071 | ) |
| 1072 | ) |
| 1073 | super().__init__(output_field=output_field) |
| 1074 | self.source_expressions = self._parse_expressions(*expressions) |
| 1075 | self.extra = extra |
| 1076 | |
| 1077 | def __repr__(self): |
| 1078 | args = self.arg_joiner.join(str(arg) for arg in self.source_expressions) |
| 1079 | extra = {**self.extra, **self._get_repr_options()} |
| 1080 | if extra: |
| 1081 | extra = ", ".join( |
| 1082 | str(key) + "=" + str(val) for key, val in sorted(extra.items()) |
| 1083 | ) |
| 1084 | return "{}({}, {})".format(self.__class__.__name__, args, extra) |
| 1085 | return "{}({})".format(self.__class__.__name__, args) |
| 1086 | |
| 1087 | def _get_repr_options(self): |
| 1088 | """Return a dict of extra __init__() options to include in the repr.""" |
| 1089 | return {} |
| 1090 | |
| 1091 | def get_source_expressions(self): |
| 1092 | return self.source_expressions |
| 1093 | |
| 1094 | def set_source_expressions(self, exprs): |
| 1095 | self.source_expressions = exprs |
| 1096 | |
| 1097 | def as_sql( |
| 1098 | self, |
| 1099 | compiler, |
| 1100 | connection, |
| 1101 | function=None, |
| 1102 | template=None, |
| 1103 | arg_joiner=None, |
| 1104 | **extra_context, |
| 1105 | ): |
| 1106 | connection.ops.check_expression_support(self) |
| 1107 | sql_parts = [] |
| 1108 | params = [] |
| 1109 | for arg in self.source_expressions: |
| 1110 | try: |
| 1111 | arg_sql, arg_params = compiler.compile(arg) |
no outgoing calls