Skip to content

[6.6] HHH-19558: Fixed support of JDBC Escape Syntax #10451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,15 @@ protected String substituteBrackets(String sqlQuery) throws QueryException {
final char ch = sqlQuery.charAt( index );
switch (ch) {
case '\'':
if (!doubleQuoted && !escaped) {
singleQuoted = !singleQuoted;
if (escaped) {
token.append(ch);
}
else {
if (!doubleQuoted) {
singleQuoted = !singleQuoted;
}
result.append(ch);
}
result.append(ch);
break;
case '"':
if (!singleQuoted && !escaped) {
Expand Down Expand Up @@ -216,7 +221,7 @@ private String resolveCollectionProperties(
}
aliasesFound++;
return collectionPersister.selectFragment( aliasName, collectionSuffix )
+ ", " + resolveProperties( aliasName, propertyName );
+ ", " + resolveProperties( aliasName, propertyName );
case "element.*":
return resolveProperties( aliasName, "*" );
default:
Expand Down Expand Up @@ -266,7 +271,7 @@ private void validate(String aliasName, String propertyName, String[] columnAlia
// TODO: better error message since we actually support composites if names are explicitly listed
throw new QueryException(
"SQL queries only support properties mapped to a single column - property [" +
propertyName + "] is mapped to " + columnAliases.length + " columns.",
propertyName + "] is mapped to " + columnAliases.length + " columns.",
originalQueryString
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.sql;

import org.hibernate.dialect.H2Dialect;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.sql.internal.SQLQueryParser;

import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link SQLQueryParser}
*
* @author Steve Ebersole
*/
@SuppressWarnings("JUnitMalformedDeclaration")
public class SQLQueryParserUnitTests {

@ParameterizedTest
@DomainModel
@SessionFactory
@RequiresDialect(H2Dialect.class)
@ValueSource(strings = {
"{d '2025-06-18'}",
"{t '14:00'}",
"{t '14:00:00'}",
"{ts '2025-06-18T14:00'}",
"{ts '2025-06-18T14:00:00'}",
"{ts '2025-06-18T14:00:00.123'}",
"{ts '2025-06-18T14:00:00+01:00'}"})
void testJDBCEscapeSyntaxParsing(String variant, SessionFactoryScope scope) {
final SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
final String sqlQuery = "select id, name from {h-domain}the_table where date = " + variant;

final String full = processSqlString( sqlQuery, "my_catalog", "my_schema", sessionFactory );
assertThat( full ).contains( variant );

final String catalogOnly = processSqlString( sqlQuery, "my_catalog", null, sessionFactory );
assertThat( catalogOnly ).contains( variant );

final String schemaOnly = processSqlString( sqlQuery, null, "my_schema", sessionFactory );
assertThat( schemaOnly ).contains( variant );

final String none = processSqlString( sqlQuery, null, null, sessionFactory );
assertThat( none ).contains( variant );
}

private static String processSqlString(
String sqlQuery,
String catalogName,

Check notice

Code scanning / CodeQL

Useless parameter Note test

The parameter 'catalogName' is never used.
String schemaName,

Check notice

Code scanning / CodeQL

Useless parameter Note test

The parameter 'schemaName' is never used.
SessionFactoryImplementor sessionFactory) {
return new SQLQueryParser( sqlQuery, null, sessionFactory ).process();
}
}
Loading