diff options
Diffstat (limited to '')
-rw-r--r-- | sqlglot/generator.py | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/sqlglot/generator.py b/sqlglot/generator.py index c571e8f..b0e83d2 100644 --- a/sqlglot/generator.py +++ b/sqlglot/generator.py @@ -1,6 +1,7 @@ from __future__ import annotations import logging +import re import typing as t from collections import defaultdict from functools import reduce @@ -17,6 +18,8 @@ if t.TYPE_CHECKING: logger = logging.getLogger("sqlglot") +ESCAPED_UNICODE_RE = re.compile(r"\\(\d+)") + class Generator: """ @@ -917,11 +920,19 @@ class Generator: def unicodestring_sql(self, expression: exp.UnicodeString) -> str: this = self.sql(expression, "this") + escape = expression.args.get("escape") + if self.dialect.UNICODE_START: - escape = self.sql(expression, "escape") - escape = f" UESCAPE {escape}" if escape else "" + escape = f" UESCAPE {self.sql(escape)}" if escape else "" return f"{self.dialect.UNICODE_START}{this}{self.dialect.UNICODE_END}{escape}" - return this + + if escape: + pattern = re.compile(rf"{escape.name}(\d+)") + else: + pattern = ESCAPED_UNICODE_RE + + this = pattern.sub(r"\\u\1", this) + return f"{self.dialect.QUOTE_START}{this}{self.dialect.QUOTE_END}" def rawstring_sql(self, expression: exp.RawString) -> str: string = self.escape_str(expression.this.replace("\\", "\\\\")) |