diff --git a/pypika/terms.py b/pypika/terms.py index b25af5c5..d6344387 100644 --- a/pypika/terms.py +++ b/pypika/terms.py @@ -375,7 +375,6 @@ def get_formatted_value(cls, value: Any, **kwargs): if isinstance(value, date): return cls.get_formatted_value(value.isoformat(), **kwargs) if isinstance(value, str): - value = value.replace(quote_char, quote_char * 2) return format_quotes(value, quote_char) if isinstance(value, bool): return str.lower(str(value)) diff --git a/pypika/tests/test_terms.py b/pypika/tests/test_terms.py index 562e0d28..de5e94f7 100644 --- a/pypika/tests/test_terms.py +++ b/pypika/tests/test_terms.py @@ -73,3 +73,25 @@ def test_passes_kwargs_to_field_get_sql(self): 'FROM "customers" JOIN "accounts" ON "customers"."account_id"="accounts"."account_id"', query.get_sql(with_namespace=True), ) + + +class IdentifierEscapingTests(TestCase): + def test_escape_identifier_quotes(self): + customers = Table('customers"') + customer_id = getattr(customers, '"id') + email = getattr(customers, 'email"').as_('customer_email"') + + query = ( + Query.from_(customers) + .select(customer_id, email) + .where(customer_id == "abc") + .where(email == "abc@abc.com") + .orderby(email, customer_id) + ) + + self.assertEqual( + 'SELECT """id","email""" "customer_email""" ' + 'FROM "customers""" WHERE """id"=\'abc\' AND "email"""=\'abc@abc.com\' ' + 'ORDER BY "customer_email""","""id"', + query.get_sql(), + ) diff --git a/pypika/utils.py b/pypika/utils.py index ca3e9c4c..32e69cc9 100644 --- a/pypika/utils.py +++ b/pypika/utils.py @@ -103,6 +103,9 @@ def resolve_is_aggregate(values: List[Optional[bool]]) -> Optional[bool]: def format_quotes(value: Any, quote_char: Optional[str]) -> str: + if quote_char: + value = value.replace(quote_char, quote_char * 2) + return "{quote}{value}{quote}".format(value=value, quote=quote_char or "")