Class representing index.
| 14 | |
| 15 | |
| 16 | class Index(SQLObject, DBMLObject): |
| 17 | '''Class representing index.''' |
| 18 | required_attributes = ('subjects', 'table') |
| 19 | dont_compare_fields = ('table',) |
| 20 | |
| 21 | def __init__(self, |
| 22 | subjects: List[Union[str, Column, Expression]], |
| 23 | name: Optional[str] = None, |
| 24 | unique: bool = False, |
| 25 | type: Optional[ |
| 26 | Literal[ |
| 27 | # https://www.postgresql.org/docs/current/indexes-types.html |
| 28 | "brin", |
| 29 | "btree", |
| 30 | "gin", |
| 31 | "gist", |
| 32 | "hash", |
| 33 | "spgist", |
| 34 | ] |
| 35 | ] = None, |
| 36 | pk: bool = False, |
| 37 | note: Optional[Union[Note, str]] = None, |
| 38 | comment: Optional[str] = None): |
| 39 | self.subjects = subjects |
| 40 | self.table: Optional[Table] = None |
| 41 | |
| 42 | self.name = name if name else None |
| 43 | self.unique = unique |
| 44 | self.type = type |
| 45 | self.pk = pk |
| 46 | self.note = Note(note) |
| 47 | self.comment = comment |
| 48 | |
| 49 | @property |
| 50 | def note(self): |
| 51 | return self._note |
| 52 | |
| 53 | @note.setter |
| 54 | def note(self, val: Note) -> None: |
| 55 | self._note = val |
| 56 | val.parent = self |
| 57 | |
| 58 | @property |
| 59 | def subject_names(self): |
| 60 | ''' |
| 61 | Returns updated list of subject names. |
| 62 | ''' |
| 63 | return [s.name if isinstance(s, Column) else str(s) for s in self.subjects] |
| 64 | |
| 65 | def __repr__(self): |
| 66 | ''' |
| 67 | <Index 'test', ['col', '(c*2)']> |
| 68 | ''' |
| 69 | |
| 70 | table_name = self.table.name if self.table else None |
| 71 | return f"<Index {table_name!r}, {self.subject_names!r}>" |
| 72 | |
| 73 | def __str__(self): |
no outgoing calls