Returns a CQL query that can be used to recreate this function. If `formatted` is set to :const:`True`, extra whitespace will be added to make the query more readable.
(self, formatted=False)
| 1137 | self.monotonic_on = monotonic_on |
| 1138 | |
| 1139 | def as_cql_query(self, formatted=False): |
| 1140 | """ |
| 1141 | Returns a CQL query that can be used to recreate this function. |
| 1142 | If `formatted` is set to :const:`True`, extra whitespace will |
| 1143 | be added to make the query more readable. |
| 1144 | """ |
| 1145 | sep = '\n ' if formatted else ' ' |
| 1146 | keyspace = protect_name(self.keyspace) |
| 1147 | name = protect_name(self.name) |
| 1148 | arg_list = ', '.join(["%s %s" % (protect_name(n), types.strip_frozen(t)) |
| 1149 | for n, t in zip(self.argument_names, self.argument_types)]) |
| 1150 | typ = self.return_type |
| 1151 | lang = self.language |
| 1152 | body = self.body |
| 1153 | on_null = "CALLED" if self.called_on_null_input else "RETURNS NULL" |
| 1154 | deterministic_token = ('DETERMINISTIC{}'.format(sep) |
| 1155 | if self.deterministic else |
| 1156 | '') |
| 1157 | monotonic_tokens = '' # default for nonmonotonic function |
| 1158 | if self.monotonic: |
| 1159 | # monotonic on all arguments; ignore self.monotonic_on |
| 1160 | monotonic_tokens = 'MONOTONIC{}'.format(sep) |
| 1161 | elif self.monotonic_on: |
| 1162 | # if monotonic == False and monotonic_on is nonempty, we know that |
| 1163 | # monotonicity was specified with MONOTONIC ON <arg>, so there's |
| 1164 | # exactly 1 value there |
| 1165 | monotonic_tokens = 'MONOTONIC ON {}{}'.format(self.monotonic_on[0], |
| 1166 | sep) |
| 1167 | |
| 1168 | return "CREATE FUNCTION %(keyspace)s.%(name)s(%(arg_list)s)%(sep)s" \ |
| 1169 | "%(on_null)s ON NULL INPUT%(sep)s" \ |
| 1170 | "RETURNS %(typ)s%(sep)s" \ |
| 1171 | "%(deterministic_token)s" \ |
| 1172 | "%(monotonic_tokens)s" \ |
| 1173 | "LANGUAGE %(lang)s%(sep)s" \ |
| 1174 | "AS $$%(body)s$$" % locals() |
| 1175 | |
| 1176 | def export_as_string(self): |
| 1177 | return self.as_cql_query(formatted=True) + ';' |