From dfc16e8e3a4738d0afde39168fb1744c86f976b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20PELEGRIN?= Date: Mon, 13 Feb 2023 16:56:58 +0100 Subject: [PATCH] Implementation of StaticValue class (with modification of format_alias_sql) --- pypika/terms.py | 9 +++++++++ pypika/utils.py | 6 ++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pypika/terms.py b/pypika/terms.py index c522550a..9d8d74a1 100644 --- a/pypika/terms.py +++ b/pypika/terms.py @@ -465,6 +465,15 @@ def get_sql(self, quote_char: Optional[str] = None, **kwargs: Any) -> str: return "VALUES({value})".format(value=self.field.get_sql(quote_char=quote_char, **kwargs)) +class StaticValue(Term): + def __init__(self, value, alias: Optional[str] = None) -> None: + super().__init__(alias) + self._value = value + + def get_sql(self, **kwargs: Any) -> str: + return format_alias_sql(self._value, self.alias, static_quote_char="'", **kwargs) + + class LiteralValue(Term): def __init__(self, value, alias: Optional[str] = None) -> None: super().__init__(alias) diff --git a/pypika/utils.py b/pypika/utils.py index 1506704b..8541b968 100644 --- a/pypika/utils.py +++ b/pypika/utils.py @@ -108,13 +108,15 @@ def format_alias_sql( alias: Optional[str], quote_char: Optional[str] = None, alias_quote_char: Optional[str] = None, + static_quote_char: Optional[str] = None, as_keyword: bool = False, **kwargs: Any, ) -> str: if alias is None: - return sql + return format_quotes(value=sql, quote_char=static_quote_char) return "{sql}{_as}{alias}".format( - sql=sql, _as=' AS ' if as_keyword else ' ', alias=format_quotes(alias, alias_quote_char or quote_char) + sql=format_quotes(value=sql, quote_char=static_quote_char), _as=' AS ' if as_keyword else ' ', + alias=format_quotes(alias, alias_quote_char or quote_char) )