A representation of a secondary index on a column.
| 1644 | |
| 1645 | |
| 1646 | class IndexMetadata(object): |
| 1647 | """ |
| 1648 | A representation of a secondary index on a column. |
| 1649 | """ |
| 1650 | keyspace_name = None |
| 1651 | """ A string name of the keyspace. """ |
| 1652 | |
| 1653 | table_name = None |
| 1654 | """ A string name of the table this index is on. """ |
| 1655 | |
| 1656 | name = None |
| 1657 | """ A string name for the index. """ |
| 1658 | |
| 1659 | kind = None |
| 1660 | """ A string representing the kind of index (COMPOSITE, CUSTOM,...). """ |
| 1661 | |
| 1662 | index_options = {} |
| 1663 | """ A dict of index options. """ |
| 1664 | |
| 1665 | def __init__(self, keyspace_name, table_name, index_name, kind, index_options): |
| 1666 | self.keyspace_name = keyspace_name |
| 1667 | self.table_name = table_name |
| 1668 | self.name = index_name |
| 1669 | self.kind = kind |
| 1670 | self.index_options = index_options |
| 1671 | |
| 1672 | def as_cql_query(self): |
| 1673 | """ |
| 1674 | Returns a CQL query that can be used to recreate this index. |
| 1675 | """ |
| 1676 | options = dict(self.index_options) |
| 1677 | index_target = options.pop("target") |
| 1678 | if self.kind != "CUSTOM": |
| 1679 | return "CREATE INDEX %s ON %s.%s (%s)" % ( |
| 1680 | protect_name(self.name), |
| 1681 | protect_name(self.keyspace_name), |
| 1682 | protect_name(self.table_name), |
| 1683 | index_target) |
| 1684 | else: |
| 1685 | class_name = options.pop("class_name") |
| 1686 | ret = "CREATE CUSTOM INDEX %s ON %s.%s (%s) USING '%s'" % ( |
| 1687 | protect_name(self.name), |
| 1688 | protect_name(self.keyspace_name), |
| 1689 | protect_name(self.table_name), |
| 1690 | index_target, |
| 1691 | class_name) |
| 1692 | if options: |
| 1693 | # PYTHON-1008: `ret` will always be a unicode |
| 1694 | opts_cql_encoded = _encoder.cql_encode_all_types(options, as_text_type=True) |
| 1695 | ret += " WITH OPTIONS = %s" % opts_cql_encoded |
| 1696 | return ret |
| 1697 | |
| 1698 | def export_as_string(self): |
| 1699 | """ |
| 1700 | Returns a CQL query string that can be used to recreate this index. |
| 1701 | """ |
| 1702 | return self.as_cql_query() + ';' |
| 1703 |
no outgoing calls