From 9e53118c4ef6e68d7236ab85b80381ae92bcc54e Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Tue, 10 Apr 2012 15:49:19 +0800 Subject: [PATCH] HHH-2697 - Can't use := for variable assignment within a SQL-Statement --- .../engine/query/spi/ParameterParser.java | 4 ++++ .../test/sql/hand/query/NativeSQLQueriesTest.java | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/query/spi/ParameterParser.java b/hibernate-core/src/main/java/org/hibernate/engine/query/spi/ParameterParser.java index d3616414db..1c6b94babe 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/query/spi/ParameterParser.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/query/spi/ParameterParser.java @@ -81,6 +81,10 @@ public class ParameterParser { inQuote = true; recognizer.other( c ); } + else if ( '\\' == c ) { + // skip sending the backslash and instead send then next character, treating is as a literal + recognizer.other( sqlString.charAt( ++indx ) ); + } else { if ( c == ':' ) { // named parameter diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/sql/hand/query/NativeSQLQueriesTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/sql/hand/query/NativeSQLQueriesTest.java index 29910bd56a..344749567a 100644 --- a/hibernate-core/src/matrix/java/org/hibernate/test/sql/hand/query/NativeSQLQueriesTest.java +++ b/hibernate-core/src/matrix/java/org/hibernate/test/sql/hand/query/NativeSQLQueriesTest.java @@ -13,12 +13,14 @@ import org.junit.Test; import org.hibernate.Hibernate; import org.hibernate.HibernateException; import org.hibernate.Query; +import org.hibernate.QueryException; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.dialect.H2Dialect; +import org.hibernate.dialect.MySQL5Dialect; import org.hibernate.internal.util.collections.ArrayHelper; import org.hibernate.test.sql.hand.Dimension; import org.hibernate.test.sql.hand.Employment; @@ -32,6 +34,7 @@ import org.hibernate.test.sql.hand.SpaceShip; import org.hibernate.test.sql.hand.Speech; import org.hibernate.test.sql.hand.TextHolder; import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.SkipForDialect; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.transform.BasicTransformerAdapter; @@ -833,6 +836,17 @@ public class NativeSQLQueriesTest extends BaseCoreFunctionalTestCase { t.commit(); s.close(); } + @Test + @RequiresDialect(MySQL5Dialect.class) + public void testEscapeColonInSQL() throws QueryException { + Session s = openSession(); + Transaction t = s.beginTransaction(); + SQLQuery query = s.createSQLQuery( "SELECT @row \\:= 1" ); + List list = query.list(); + assertTrue( list.get( 0 ).toString().equals( "1" ) ); + t.commit(); + s.close(); + } private String buildLongString(int size, char baseChar) { StringBuilder buff = new StringBuilder();