Returns a CQL query that can be used to recreate this table (index creations are not included). If `formatted` is set to :const:`True`, extra whitespace will be added to make the query human readable.
(self, formatted=False)
| 1349 | return ret |
| 1350 | |
| 1351 | def as_cql_query(self, formatted=False): |
| 1352 | """ |
| 1353 | Returns a CQL query that can be used to recreate this table (index |
| 1354 | creations are not included). If `formatted` is set to :const:`True`, |
| 1355 | extra whitespace will be added to make the query human readable. |
| 1356 | """ |
| 1357 | ret = "%s TABLE %s.%s (%s" % ( |
| 1358 | ('VIRTUAL' if self.virtual else 'CREATE'), |
| 1359 | protect_name(self.keyspace_name), |
| 1360 | protect_name(self.name), |
| 1361 | "\n" if formatted else "") |
| 1362 | |
| 1363 | if formatted: |
| 1364 | column_join = ",\n" |
| 1365 | padding = " " |
| 1366 | else: |
| 1367 | column_join = ", " |
| 1368 | padding = "" |
| 1369 | |
| 1370 | columns = [] |
| 1371 | for col in self.columns.values(): |
| 1372 | columns.append("%s %s%s" % (protect_name(col.name), col.cql_type, ' static' if col.is_static else '')) |
| 1373 | |
| 1374 | if len(self.partition_key) == 1 and not self.clustering_key: |
| 1375 | columns[0] += " PRIMARY KEY" |
| 1376 | |
| 1377 | ret += column_join.join("%s%s" % (padding, col) for col in columns) |
| 1378 | |
| 1379 | # primary key |
| 1380 | if len(self.partition_key) > 1 or self.clustering_key: |
| 1381 | ret += "%s%sPRIMARY KEY (" % (column_join, padding) |
| 1382 | |
| 1383 | if len(self.partition_key) > 1: |
| 1384 | ret += "(%s)" % ", ".join(protect_name(col.name) for col in self.partition_key) |
| 1385 | else: |
| 1386 | ret += protect_name(self.partition_key[0].name) |
| 1387 | |
| 1388 | if self.clustering_key: |
| 1389 | ret += ", %s" % ", ".join(protect_name(col.name) for col in self.clustering_key) |
| 1390 | |
| 1391 | ret += ")" |
| 1392 | |
| 1393 | # properties |
| 1394 | ret += "%s) WITH " % ("\n" if formatted else "") |
| 1395 | ret += self._property_string(formatted, self.clustering_key, self.options, self.is_compact_storage) |
| 1396 | |
| 1397 | return ret |
| 1398 | |
| 1399 | @classmethod |
| 1400 | def _property_string(cls, formatted, clustering_key, options_map, is_compact_storage=False): |
no test coverage detected