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)
| 3159 | self.options = options or {} |
| 3160 | |
| 3161 | def as_cql_query(self, formatted=False): |
| 3162 | """ |
| 3163 | Returns a CQL query that can be used to recreate this function. |
| 3164 | If `formatted` is set to :const:`True`, extra whitespace will |
| 3165 | be added to make the query more readable. |
| 3166 | """ |
| 3167 | sep = '\n ' if formatted else ' ' |
| 3168 | keyspace = protect_name(self.keyspace_name) |
| 3169 | name = protect_name(self.name) |
| 3170 | |
| 3171 | selected_cols = '*' if self.include_all_columns else ', '.join(protect_name(col.name) for col in self.columns.values()) |
| 3172 | base_table = protect_name(self.base_table_name) |
| 3173 | where_clause = self.where_clause |
| 3174 | |
| 3175 | part_key = ', '.join(protect_name(col.name) for col in self.partition_key) |
| 3176 | if len(self.partition_key) > 1: |
| 3177 | pk = "((%s)" % part_key |
| 3178 | else: |
| 3179 | pk = "(%s" % part_key |
| 3180 | if self.clustering_key: |
| 3181 | pk += ", %s" % ', '.join(protect_name(col.name) for col in self.clustering_key) |
| 3182 | pk += ")" |
| 3183 | |
| 3184 | properties = TableMetadataV3._property_string(formatted, self.clustering_key, self.options) |
| 3185 | |
| 3186 | ret = ("CREATE MATERIALIZED VIEW %(keyspace)s.%(name)s AS%(sep)s" |
| 3187 | "SELECT %(selected_cols)s%(sep)s" |
| 3188 | "FROM %(keyspace)s.%(base_table)s%(sep)s" |
| 3189 | "WHERE %(where_clause)s%(sep)s" |
| 3190 | "PRIMARY KEY %(pk)s%(sep)s" |
| 3191 | "WITH %(properties)s") % locals() |
| 3192 | |
| 3193 | if self.extensions: |
| 3194 | registry = _RegisteredExtensionType._extension_registry |
| 3195 | for k in registry.keys() & self.extensions: # no viewkeys on OrderedMapSerializeKey |
| 3196 | ext = registry[k] |
| 3197 | cql = ext.after_table_cql(self, k, self.extensions[k]) |
| 3198 | if cql: |
| 3199 | ret += "\n\n%s" % (cql,) |
| 3200 | return ret |
| 3201 | |
| 3202 | def export_as_string(self): |
| 3203 | return self.as_cql_query(formatted=True) + ";" |
no test coverage detected