Skip to content

Commit

Permalink
HHH-9794 - Replace string with preceding comma is not replacing strin…
Browse files Browse the repository at this point in the history
…g as required

(cherry picked from commit 4218f36)
  • Loading branch information
sebersole committed Nov 4, 2015
1 parent 4125467 commit fa09055
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
Expand Up @@ -74,6 +74,8 @@ public static String[] concreteQueries(String query, SessionFactoryImplementor f
String next;
String last = tokens[start - 1].toLowerCase(Locale.ROOT);

boolean inQuote = false;

for ( int i = start; i < tokens.length; i++ ) {

String token = tokens[i];
Expand All @@ -82,11 +84,19 @@ public static String[] concreteQueries(String query, SessionFactoryImplementor f
templateQuery.append( token );
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);

boolean process = isJavaIdentifier( token ) &&
isPossiblyClassName( last, next );
boolean process = isJavaIdentifier( token )
&& isPossiblyClassName( last, next );

last = token.toLowerCase(Locale.ROOT);

Expand Down Expand Up @@ -116,6 +126,10 @@ public static String[] concreteQueries(String query, SessionFactoryImplementor f
return results;
}

private static boolean isQuoteCharacter(String token) {
return "'".equals( token ) || "\"".equals( token );
}

private static String nextNonWhite(String[] tokens, int start) {
for ( int i = start + 1; i < tokens.length; i++ ) {
if ( !ParserHelper.isWhitespace( tokens[i] ) ) {
Expand Down
@@ -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;
}

}
}

0 comments on commit fa09055

Please sign in to comment.