From 9dbe7a7987ff66fecdef17fbebc1a3a0397d861a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Thu, 1 Feb 2024 14:23:41 +0100 Subject: [PATCH] Revert "Merge pull request #37282 from ifeelgood/hql-escape-columns-in-order-by-with-backtick" This reverts commit 5929d492799cec06d0cef666f67c3be857d664d3, reversing changes made to 670b43cac8587d27d7810af41c22ca3cf161df23. --- .../test/JpaOperationsSortTest.java | 25 ++++--------------- .../common/runtime/PanacheJpaUtil.java | 18 +------------ 2 files changed, 6 insertions(+), 37 deletions(-) diff --git a/extensions/panache/hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test/JpaOperationsSortTest.java b/extensions/panache/hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test/JpaOperationsSortTest.java index 39e14f149d201..30e13c7a45c5f 100644 --- a/extensions/panache/hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test/JpaOperationsSortTest.java +++ b/extensions/panache/hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test/JpaOperationsSortTest.java @@ -2,11 +2,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import io.quarkus.panache.common.Sort; -import io.quarkus.panache.common.exception.PanacheQueryException; import io.quarkus.panache.hibernate.common.runtime.PanacheJpaUtil; public class JpaOperationsSortTest { @@ -20,7 +18,7 @@ public void testEmptySortByYieldsEmptyString() { @Test public void testSortBy() { Sort sort = Sort.by("foo", "bar"); - assertEquals(" ORDER BY `foo` , `bar`", PanacheJpaUtil.toOrderBy(sort)); + assertEquals(" ORDER BY foo , bar", PanacheJpaUtil.toOrderBy(sort)); } @Test @@ -31,27 +29,14 @@ public void testEmptySortEmptyYieldsEmptyString() { @Test public void testSortByNullsFirst() { - Sort sort = Sort.by("foo", Sort.Direction.Ascending, Sort.NullPrecedence.NULLS_FIRST); - assertEquals(" ORDER BY `foo` NULLS FIRST", PanacheJpaUtil.toOrderBy(sort)); + Sort emptySort = Sort.by("foo", Sort.Direction.Ascending, Sort.NullPrecedence.NULLS_FIRST); + assertEquals(" ORDER BY foo NULLS FIRST", PanacheJpaUtil.toOrderBy(emptySort)); } @Test public void testSortByNullsLast() { - Sort sort = Sort.by("foo", Sort.Direction.Descending, Sort.NullPrecedence.NULLS_LAST); - assertEquals(" ORDER BY `foo` DESC NULLS LAST", PanacheJpaUtil.toOrderBy(sort)); - } - - @Test - public void testSortByColumnWithBacktick() { - Sort sort = Sort.by("jeanne", "d`arc"); - Assertions.assertThrowsExactly(PanacheQueryException.class, () -> PanacheJpaUtil.toOrderBy(sort), - "Sort column name cannot have backticks"); - } - - @Test - public void testSortByQuotedColumn() { - Sort sort = Sort.by("`foo`", "bar"); - assertEquals(" ORDER BY `foo` , `bar`", PanacheJpaUtil.toOrderBy(sort)); + Sort emptySort = Sort.by("foo", Sort.Direction.Descending, Sort.NullPrecedence.NULLS_LAST); + assertEquals(" ORDER BY foo DESC NULLS LAST", PanacheJpaUtil.toOrderBy(emptySort)); } } diff --git a/extensions/panache/panache-hibernate-common/runtime/src/main/java/io/quarkus/panache/hibernate/common/runtime/PanacheJpaUtil.java b/extensions/panache/panache-hibernate-common/runtime/src/main/java/io/quarkus/panache/hibernate/common/runtime/PanacheJpaUtil.java index 14edc447ad802..3029a33398242 100644 --- a/extensions/panache/panache-hibernate-common/runtime/src/main/java/io/quarkus/panache/hibernate/common/runtime/PanacheJpaUtil.java +++ b/extensions/panache/panache-hibernate-common/runtime/src/main/java/io/quarkus/panache/hibernate/common/runtime/PanacheJpaUtil.java @@ -175,7 +175,7 @@ public static String toOrderBy(Sort sort) { Sort.Column column = sort.getColumns().get(i); if (i > 0) sb.append(" , "); - sb.append('`').append(unquoteColumnName(column)).append('`'); + sb.append(column.getName()); if (column.getDirection() != Sort.Direction.Ascending) { sb.append(" DESC"); } @@ -191,20 +191,4 @@ public static String toOrderBy(Sort sort) { } return sb.toString(); } - - private static String unquoteColumnName(Sort.Column column) { - String columnName = column.getName(); - String unquotedColumnName; - //Note HQL uses backticks to escape/quote special words that are used as identifiers - if (columnName.charAt(0) == '`' && columnName.charAt(columnName.length() - 1) == '`') { - unquotedColumnName = columnName.substring(1, columnName.length() - 1); - } else { - unquotedColumnName = columnName; - } - // Note we're not dealing with columns but with entity attributes so no backticks expected in unquoted column name - if (unquotedColumnName.indexOf('`') >= 0) { - throw new PanacheQueryException("Sort column name cannot have backticks"); - } - return unquotedColumnName; - } }