Skip to content

Commit 4218f36

Browse files
committed
HHH-9794 - Replace string with preceding comma is not replacing string as required
1 parent 30d7c80 commit 4218f36

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

hibernate-core/src/main/java/org/hibernate/hql/internal/QuerySplitter.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public static String[] concreteQueries(String query, SessionFactoryImplementor f
7474
String next;
7575
String last = tokens[start - 1].toLowerCase(Locale.ROOT);
7676

77+
boolean inQuote = false;
78+
7779
for ( int i = start; i < tokens.length; i++ ) {
7880

7981
String token = tokens[i];
@@ -82,11 +84,19 @@ public static String[] concreteQueries(String query, SessionFactoryImplementor f
8284
templateQuery.append( token );
8385
continue;
8486
}
85-
87+
else if ( isQuoteCharacter( token) ) {
88+
inQuote = !inQuote;
89+
templateQuery.append( token );
90+
continue;
91+
}
92+
else if ( inQuote ) {
93+
templateQuery.append( token );
94+
continue;
95+
}
8696
next = nextNonWhite( tokens, i ).toLowerCase(Locale.ROOT);
8797

88-
boolean process = isJavaIdentifier( token ) &&
89-
isPossiblyClassName( last, next );
98+
boolean process = isJavaIdentifier( token )
99+
&& isPossiblyClassName( last, next );
90100

91101
last = token.toLowerCase(Locale.ROOT);
92102

@@ -116,6 +126,10 @@ public static String[] concreteQueries(String query, SessionFactoryImplementor f
116126
return results;
117127
}
118128

129+
private static boolean isQuoteCharacter(String token) {
130+
return "'".equals( token ) || "\"".equals( token );
131+
}
132+
119133
private static String nextNonWhite(String[] tokens, int start) {
120134
for ( int i = start + 1; i < tokens.length; i++ ) {
121135
if ( !ParserHelper.isWhitespace( tokens[i] ) ) {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.hql;
8+
9+
import javax.persistence.Entity;
10+
import javax.persistence.Id;
11+
import javax.persistence.Table;
12+
13+
import org.hibernate.hql.internal.QuerySplitter;
14+
15+
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
16+
import org.junit.Test;
17+
18+
import static org.junit.Assert.assertEquals;
19+
20+
/**
21+
* @author Steve Ebersole
22+
*/
23+
public class QuerySplitterTest extends BaseNonConfigCoreFunctionalTestCase {
24+
@Test
25+
public void testQueryWithEntityNameAsStringLiteral() {
26+
final String qry = "select e from Employee a where e.name = ', Employee Number 1'";
27+
28+
String[] results = QuerySplitter.concreteQueries( qry, sessionFactory() );
29+
assertEquals( 1, results.length );
30+
assertEquals(
31+
"select e from org.hibernate.test.hql.QuerySplitterTest$Employee a where e.name = ', Employee Number 1'",
32+
results[0]
33+
);
34+
}
35+
36+
@Override
37+
protected Class[] getAnnotatedClasses() {
38+
return new Class[] { Employee.class };
39+
}
40+
41+
@Entity( name = "Employee" )
42+
@Table( name= "tabEmployees" )
43+
public class Employee {
44+
@Id
45+
private long id;
46+
private String name;
47+
48+
public Employee() {
49+
50+
}
51+
52+
public Employee(long id, String strName) {
53+
this();
54+
this.name = strName;
55+
}
56+
57+
public long getId() {
58+
return id;
59+
}
60+
61+
public String getName() {
62+
return name;
63+
}
64+
65+
public void setName(String strName) {
66+
this.name = strName;
67+
}
68+
69+
}
70+
}

0 commit comments

Comments
 (0)