HHH-9794 - Replace string with preceding comma is not replacing string as required

(cherry picked from commit 4218f365e5)
This commit is contained in:
Steve Ebersole 2015-11-04 16:10:39 -06:00
parent 412546753f
commit fa09055a8c
2 changed files with 87 additions and 3 deletions

View File

@ -74,6 +74,8 @@ public final class QuerySplitter {
String next; String next;
String last = tokens[start - 1].toLowerCase(Locale.ROOT); String last = tokens[start - 1].toLowerCase(Locale.ROOT);
boolean inQuote = false;
for ( int i = start; i < tokens.length; i++ ) { for ( int i = start; i < tokens.length; i++ ) {
String token = tokens[i]; String token = tokens[i];
@ -82,11 +84,19 @@ public final class QuerySplitter {
templateQuery.append( token ); templateQuery.append( token );
continue; continue;
} }
else if ( isQuoteCharacter( token) ) {
inQuote = !inQuote;
templateQuery.append( token );
continue;
}
else if ( inQuote ) {
templateQuery.append( token );
continue;
}
next = nextNonWhite( tokens, i ).toLowerCase(Locale.ROOT); next = nextNonWhite( tokens, i ).toLowerCase(Locale.ROOT);
boolean process = isJavaIdentifier( token ) && boolean process = isJavaIdentifier( token )
isPossiblyClassName( last, next ); && isPossiblyClassName( last, next );
last = token.toLowerCase(Locale.ROOT); last = token.toLowerCase(Locale.ROOT);
@ -116,6 +126,10 @@ public final class QuerySplitter {
return results; return results;
} }
private static boolean isQuoteCharacter(String token) {
return "'".equals( token ) || "\"".equals( token );
}
private static String nextNonWhite(String[] tokens, int start) { private static String nextNonWhite(String[] tokens, int start) {
for ( int i = start + 1; i < tokens.length; i++ ) { for ( int i = start + 1; i < tokens.length; i++ ) {
if ( !ParserHelper.isWhitespace( tokens[i] ) ) { if ( !ParserHelper.isWhitespace( tokens[i] ) ) {

View File

@ -0,0 +1,70 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.hql;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.hql.internal.QuerySplitter;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* @author Steve Ebersole
*/
public class QuerySplitterTest extends BaseNonConfigCoreFunctionalTestCase {
@Test
public void testQueryWithEntityNameAsStringLiteral() {
final String qry = "select e from Employee a where e.name = ', Employee Number 1'";
String[] results = QuerySplitter.concreteQueries( qry, sessionFactory() );
assertEquals( 1, results.length );
assertEquals(
"select e from org.hibernate.test.hql.QuerySplitterTest$Employee a where e.name = ', Employee Number 1'",
results[0]
);
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] { Employee.class };
}
@Entity( name = "Employee" )
@Table( name= "tabEmployees" )
public class Employee {
@Id
private long id;
private String name;
public Employee() {
}
public Employee(long id, String strName) {
this();
this.name = strName;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String strName) {
this.name = strName;
}
}
}