|
4 | 4 | parameter binding and validation. |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import contextlib |
7 | 8 | from collections.abc import Mapping, Sequence |
8 | 9 | from datetime import datetime |
9 | 10 | from decimal import Decimal |
|
12 | 13 |
|
13 | 14 | from mypy_extensions import trait |
14 | 15 | from sqlglot import exp |
| 16 | +from sqlglot.errors import ParseError |
15 | 17 | from typing_extensions import Self |
16 | 18 |
|
17 | 19 | from sqlspec.builder._base import QueryBuilder |
@@ -45,32 +47,34 @@ def _is_column_reference(self, value: str) -> bool: |
45 | 47 | if not isinstance(value, str): |
46 | 48 | return False |
47 | 49 |
|
48 | | - parsed: exp.Expression | None = exp.maybe_parse(value.strip()) |
49 | | - if parsed is None: |
50 | | - return False |
51 | | - |
52 | | - if isinstance(parsed, exp.Column): |
53 | | - return parsed.table is not None and bool(parsed.table) |
54 | | - |
55 | | - return isinstance( |
56 | | - parsed, |
57 | | - ( |
58 | | - exp.Dot, |
59 | | - exp.Add, |
60 | | - exp.Sub, |
61 | | - exp.Mul, |
62 | | - exp.Div, |
63 | | - exp.Mod, |
64 | | - exp.Func, |
65 | | - exp.Anonymous, |
66 | | - exp.Null, |
67 | | - exp.CurrentTimestamp, |
68 | | - exp.CurrentDate, |
69 | | - exp.CurrentTime, |
70 | | - exp.Paren, |
71 | | - exp.Case, |
72 | | - ), |
73 | | - ) |
| 50 | + with contextlib.suppress(ParseError): |
| 51 | + parsed: exp.Expression | None = exp.maybe_parse(value.strip()) |
| 52 | + if parsed is None: |
| 53 | + return False |
| 54 | + |
| 55 | + if isinstance(parsed, exp.Column): |
| 56 | + return parsed.table is not None and bool(parsed.table) |
| 57 | + |
| 58 | + return isinstance( |
| 59 | + parsed, |
| 60 | + ( |
| 61 | + exp.Dot, |
| 62 | + exp.Add, |
| 63 | + exp.Sub, |
| 64 | + exp.Mul, |
| 65 | + exp.Div, |
| 66 | + exp.Mod, |
| 67 | + exp.Func, |
| 68 | + exp.Anonymous, |
| 69 | + exp.Null, |
| 70 | + exp.CurrentTimestamp, |
| 71 | + exp.CurrentDate, |
| 72 | + exp.CurrentTime, |
| 73 | + exp.Paren, |
| 74 | + exp.Case, |
| 75 | + ), |
| 76 | + ) |
| 77 | + return False |
74 | 78 |
|
75 | 79 | def _process_assignment(self, target_column: str, value: Any) -> exp.Expression: |
76 | 80 | column_identifier = exp.column(target_column) if isinstance(target_column, str) else target_column |
@@ -571,7 +575,9 @@ def when_not_matched_then_insert( |
571 | 575 | if values is None: |
572 | 576 | using_alias = None |
573 | 577 | using_expr = current_expr.args.get("using") |
574 | | - if using_expr is not None and (isinstance(using_expr, (exp.Subquery, exp.Table)) or hasattr(using_expr, "alias")): |
| 578 | + if using_expr is not None and ( |
| 579 | + isinstance(using_expr, (exp.Subquery, exp.Table)) or hasattr(using_expr, "alias") |
| 580 | + ): |
575 | 581 | using_alias = using_expr.alias |
576 | 582 | column_values = [f"{using_alias}.{col}" for col in column_names] if using_alias else column_names |
577 | 583 | else: |
|
0 commit comments