|
39 | 39 | flags=re.IGNORECASE | re.DOTALL | re.VERBOSE, |
40 | 40 | ) |
41 | 41 |
|
| 42 | +CREATE_VIEW_RE = re.compile( |
| 43 | + r""" |
| 44 | + ^\s*CREATE\s+ |
| 45 | + (?P<replace>OR\s+REPLACE\s+)? |
| 46 | + VIEW\s+ |
| 47 | + (?P<target>[^\s(]+) |
| 48 | + \s+AS\s+ |
| 49 | + (?P<definition>.+?) |
| 50 | + \s*$ |
| 51 | + """, |
| 52 | + flags=re.IGNORECASE | re.DOTALL | re.VERBOSE, |
| 53 | +) |
| 54 | + |
| 55 | +DROP_VIEW_RE = re.compile( |
| 56 | + r""" |
| 57 | + ^\s*DROP\s+VIEW\s+ |
| 58 | + (?P<target>[^\s;]+) |
| 59 | + \s*$ |
| 60 | + """, |
| 61 | + flags=re.IGNORECASE | re.DOTALL | re.VERBOSE, |
| 62 | +) |
| 63 | + |
42 | 64 |
|
43 | 65 | def set_logging_config( |
44 | 66 | log_level: Union[str, int], log_file: Optional[str] = None |
@@ -427,6 +449,27 @@ def parse_create_table_as_select_statement(self, statement: str) -> Optional[Dic |
427 | 449 | } |
428 | 450 | } |
429 | 451 |
|
| 452 | + def parse_create_view_statement(self, statement: str) -> Optional[Dict]: |
| 453 | + match = CREATE_VIEW_RE.match(statement) |
| 454 | + if not match: |
| 455 | + return None |
| 456 | + |
| 457 | + schema, view_name = self.split_table_identifier(match.group("target")) |
| 458 | + return { |
| 459 | + "schema": schema, |
| 460 | + "view_name": view_name, |
| 461 | + "definition": match.group("definition").strip(), |
| 462 | + "replace": bool(match.group("replace")), |
| 463 | + } |
| 464 | + |
| 465 | + def parse_drop_view_statement(self, statement: str) -> Optional[Dict]: |
| 466 | + match = DROP_VIEW_RE.match(statement) |
| 467 | + if not match: |
| 468 | + return None |
| 469 | + |
| 470 | + schema, view_name = self.split_table_identifier(match.group("target")) |
| 471 | + return {"schema": schema, "drop_view_name": view_name} |
| 472 | + |
430 | 473 | @staticmethod |
431 | 474 | def clone_create_table_as_select_columns( |
432 | 475 | source_table: Dict, select_columns: Union[str, List[Dict[str, str]]] |
@@ -585,6 +628,14 @@ def parse_statement(self) -> None: |
585 | 628 | if create_table_as_select_statement: |
586 | 629 | self.tables.append(create_table_as_select_statement) |
587 | 630 | return |
| 631 | + create_view_statement = self.parse_create_view_statement(self.statement) |
| 632 | + if create_view_statement: |
| 633 | + self.tables.append(create_view_statement) |
| 634 | + return |
| 635 | + drop_view_statement = self.parse_drop_view_statement(self.statement) |
| 636 | + if drop_view_statement: |
| 637 | + self.tables.append(drop_view_statement) |
| 638 | + return |
588 | 639 | _parse_result = yacc.parse(self.statement) |
589 | 640 | if _parse_result: |
590 | 641 | if ( |
|
0 commit comments