summaryrefslogtreecommitdiffstats
path: root/sqlglot/optimizer/quote_identities.py
blob: 17623cc1b676a0ca211c9ff5abc344ea3d2dbd2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from sqlglot import exp


def quote_identities(expression):
    """
    Rewrite sqlglot AST to ensure all identities are quoted.

    Example:
        >>> import sqlglot
        >>> expression = sqlglot.parse_one("SELECT x.a AS a FROM db.x")
        >>> quote_identities(expression).sql()
        'SELECT "x"."a" AS "a" FROM "db"."x"'

    Args:
        expression (sqlglot.Expression): expression to quote
    Returns:
        sqlglot.Expression: quoted expression
    """

    def qualify(node):
        if isinstance(node, exp.Identifier):
            node.set("quoted", True)
        return node

    return expression.transform(qualify, copy=False)