diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-12-24 07:49:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-12-24 07:49:59 +0000 |
commit | 43a52bccf5bf17217a2734f4680c00a102f82cac (patch) | |
tree | b745cdd5b462b8f0fbd9b54390f4676a48aae33a /sqlglot/generator.py | |
parent | Releasing debian version 20.3.0-1. (diff) | |
download | sqlglot-43a52bccf5bf17217a2734f4680c00a102f82cac.tar.xz sqlglot-43a52bccf5bf17217a2734f4680c00a102f82cac.zip |
Merging upstream version 20.4.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
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("\\", "\\\\")) |