| 198 | |
| 199 | @dataclass |
| 200 | class TableBlueprint(Blueprint): |
| 201 | name: str |
| 202 | schema: str = 'public' |
| 203 | columns: Optional[List[ColumnBlueprint]] = None |
| 204 | indexes: Optional[List[IndexBlueprint]] = None |
| 205 | alias: Optional[str] = None |
| 206 | note: Optional[NoteBlueprint] = None |
| 207 | header_color: Optional[str] = None |
| 208 | comment: Optional[str] = None |
| 209 | properties: Optional[Dict[str, str]] = None |
| 210 | |
| 211 | def build(self) -> 'Table': |
| 212 | result = Table( |
| 213 | name=self.name, |
| 214 | schema=self.schema, |
| 215 | alias=self.alias, |
| 216 | note=self.note.build() if self.note else None, |
| 217 | header_color=self.header_color, |
| 218 | comment=self.comment, |
| 219 | properties=self.properties |
| 220 | ) |
| 221 | columns = self.columns or [] |
| 222 | indexes = self.indexes or [] |
| 223 | for col_bp in columns: |
| 224 | result.add_column(col_bp.build()) |
| 225 | for index_bp in indexes: |
| 226 | index = index_bp.build() |
| 227 | new_subjects: List[Union[str, Column, Expression]] = [] |
| 228 | for subj in index_bp.subject_names: |
| 229 | if isinstance(subj, ExpressionBlueprint): |
| 230 | new_subjects.append(subj.build()) |
| 231 | else: |
| 232 | for col in result.columns: |
| 233 | if col.name == subj: |
| 234 | new_subjects.append(col) |
| 235 | break |
| 236 | else: |
| 237 | raise ColumnNotFoundError( |
| 238 | f'Cannot add index, column "{subj}" not defined in' |
| 239 | ' table "{self.name}".' |
| 240 | ) |
| 241 | index.subjects = new_subjects |
| 242 | result.add_index(index) |
| 243 | return result |
| 244 | |
| 245 | def get_reference_blueprints(self): |
| 246 | ''' the inline ones ''' |
| 247 | result = [] |
| 248 | for col in self.columns: |
| 249 | for ref_bp in col.ref_blueprints or []: |
| 250 | ref_bp.schema1 = self.schema |
| 251 | ref_bp.table1 = self.name |
| 252 | ref_bp.col1 = col.name |
| 253 | result.append(ref_bp) |
| 254 | return result |
| 255 | |
| 256 | |
| 257 | @dataclass |
no outgoing calls