From a59ab3ea4e7f8fce88c6d9a8e49b1d1cb9c3ce53 Mon Sep 17 00:00:00 2001 From: Noam Bloom Date: Wed, 4 Nov 2020 16:29:18 +0200 Subject: [PATCH] Fix OracleQuery Dialect Quote Char Oracle SQL query breaks when double quoted A quoted identifier begins and ends with double quotation marks ("). If you name a schema object using a quoted identifier, then you must use the double quotation marks whenever you refer to that object. Thus it makes sense that the user will double quote the identifier him/herself. A nonquoted identifier is not surrounded by any punctuation. See https://docs.oracle.com/database/121/SQLRF/sql_elements008.htm --- pypika/dialects.py | 2 ++ pypika/tests/test_selects.py | 25 +++++++++++++++---------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/pypika/dialects.py b/pypika/dialects.py index dbc72f42..84b56d9d 100644 --- a/pypika/dialects.py +++ b/pypika/dialects.py @@ -270,6 +270,8 @@ def create_table(cls, table: Union[str, Table]) -> VerticaCreateQueryBuilder: class OracleQueryBuilder(QueryBuilder): + QUOTE_CHAR = None + def __init__(self, **kwargs: Any) -> None: super().__init__(dialect=Dialects.ORACLE, **kwargs) diff --git a/pypika/tests/test_selects.py b/pypika/tests/test_selects.py index c9551997..7f155d80 100644 --- a/pypika/tests/test_selects.py +++ b/pypika/tests/test_selects.py @@ -272,10 +272,10 @@ def test_mssql_query_uses_double_quote_chars(self): self.assertEqual('SELECT "foo","bar" FROM "abc"', str(q)) - def test_oracle_query_uses_double_quote_chars(self): + def test_oracle_query_uses_no_quote_chars(self): q = OracleQuery.from_("abc").select("foo", "bar") - self.assertEqual('SELECT "foo","bar" FROM "abc"', str(q)) + self.assertEqual('SELECT foo,bar FROM abc', str(q)) def test_postgresql_query_uses_double_quote_chars(self): q = PostgreSQLQuery.from_("abc").select("foo", "bar") @@ -530,12 +530,17 @@ def test_groupby__no_alias(self): q.get_sql(groupby_alias=False), ) - def test_groupby__no_alias_platforms(self): + def test_groupby__no_alias_mssql(self): bar = self.t.bar.as_("bar01") - for query_cls in [MSSQLQuery, OracleQuery]: - q = query_cls.from_(self.t).select(fn.Sum(self.t.foo), bar).groupby(bar) + q = MSSQLQuery.from_(self.t).select(fn.Sum(self.t.foo), bar).groupby(bar) - self.assertEqual('SELECT SUM("foo"),"bar" "bar01" FROM "abc" GROUP BY "bar"', str(q)) + self.assertEqual('SELECT SUM("foo"),"bar" "bar01" FROM "abc" GROUP BY "bar"', str(q)) + + def test_groupby__no_alias_oracle(self): + bar = self.t.bar.as_("bar01") + q = OracleQuery.from_(self.t).select(fn.Sum(self.t.foo), bar).groupby(bar) + + self.assertEqual('SELECT SUM(foo),bar bar01 FROM abc GROUP BY bar', str(q)) def test_groupby__alias_platforms(self): bar = self.t.bar.as_("bar01") @@ -609,10 +614,10 @@ def test_mssql_query_uses_double_quote_chars(self): self.assertEqual('SELECT "foo" FROM "abc" GROUP BY "foo"', str(q)) - def test_oracle_query_uses_double_quote_chars(self): + def test_oracle_query_uses_no_quote_chars(self): q = OracleQuery.from_(self.t).groupby(self.t.foo).select(self.t.foo) - self.assertEqual('SELECT "foo" FROM "abc" GROUP BY "foo"', str(q)) + self.assertEqual('SELECT foo FROM abc GROUP BY foo', str(q)) def test_postgres_query_uses_double_quote_chars(self): q = PostgreSQLQuery.from_(self.t).groupby(self.t.foo).select(self.t.foo) @@ -710,14 +715,14 @@ def test_mssql_query_uses_double_quote_chars(self): ) self.assertEqual('SELECT "foo" FROM "abc" GROUP BY "foo" HAVING "buz"=\'fiz\'', str(q)) - def test_oracle_query_uses_double_quote_chars(self): + def test_oracle_query_uses_no_quote_chars(self): q = ( OracleQuery.from_(self.table_abc) .select(self.table_abc.foo) .groupby(self.table_abc.foo) .having(self.table_abc.buz == "fiz") ) - self.assertEqual('SELECT "foo" FROM "abc" GROUP BY "foo" HAVING "buz"=\'fiz\'', str(q)) + self.assertEqual('SELECT foo FROM abc GROUP BY foo HAVING buz=\'fiz\'', str(q)) def test_postgres_query_uses_double_quote_chars(self): q = (