Skip to content

Commit f6de8f8

Browse files
committed
HHH-1706 - Named parameters ignored when single apostrophe encountered within an SQL comment
(cherry picked from commit e4d102c)
1 parent 943acc7 commit f6de8f8

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

hibernate-core/src/main/java/org/hibernate/engine/query/spi/ParameterParser.java

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,71 @@ public static void parse(String sqlString, Recognizer recognizer) throws QueryEx
8484
boolean foundMainOutputParam = false;
8585

8686
final int stringLength = sqlString.length();
87-
boolean inQuote = false;
87+
88+
boolean inSingleQuotes = false;
89+
boolean inDoubleQuotes = false;
90+
boolean inLineComment = false;
91+
boolean inDelimitedComment = false;
92+
8893
for ( int indx = 0; indx < stringLength; indx++ ) {
8994
final char c = sqlString.charAt( indx );
90-
if ( inQuote ) {
95+
final boolean lastCharacter = indx == stringLength-1;
96+
97+
if ( inLineComment ) {
98+
// see if the character ends the line
99+
if ( '\n' == c ) {
100+
inLineComment = false;
101+
recognizer.other( c );
102+
}
103+
else if ( '\r' == c ) {
104+
inLineComment = false;
105+
recognizer.other( c );
106+
if ( !lastCharacter && '\n' == sqlString.charAt( indx+1 ) ) {
107+
recognizer.other( sqlString.charAt( indx+1 ) );
108+
indx++;
109+
}
110+
}
111+
}
112+
else if ( '-' == c ) {
113+
recognizer.other( c );
114+
if ( !lastCharacter && '-' == sqlString.charAt( indx+1 ) ) {
115+
inLineComment = true;
116+
recognizer.other( sqlString.charAt( indx+1 ) );
117+
indx++;
118+
}
119+
}
120+
else if ( inDelimitedComment ) {
121+
recognizer.other( c );
122+
if ( !lastCharacter && '*' == c && '/' == sqlString.charAt( indx+1 ) ) {
123+
inDelimitedComment = true;
124+
recognizer.other( sqlString.charAt( indx+1 ) );
125+
indx++;
126+
}
127+
}
128+
else if ( !lastCharacter && '/' == c && '*' == sqlString.charAt( indx+1 ) ) {
129+
inDelimitedComment = true;
130+
recognizer.other( c );
131+
recognizer.other( sqlString.charAt( indx+1 ) );
132+
indx++;
133+
}
134+
else if ( inDoubleQuotes ) {
135+
if ( '\"' == c ) {
136+
inDoubleQuotes = false;
137+
}
138+
recognizer.other( c );
139+
}
140+
else if ( '\"' == c ) {
141+
inDoubleQuotes = true;
142+
recognizer.other( c );
143+
}
144+
else if ( inSingleQuotes ) {
91145
if ( '\'' == c ) {
92-
inQuote = false;
146+
inSingleQuotes = false;
93147
}
94148
recognizer.other( c );
95149
}
96150
else if ( '\'' == c ) {
97-
inQuote = true;
151+
inSingleQuotes = true;
98152
recognizer.other( c );
99153
}
100154
else if ( '\\' == c ) {

hibernate-core/src/test/java/org/hibernate/engine/query/ParameterParserTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import org.junit.Test;
1010

11+
import org.hibernate.engine.query.spi.ParamLocationRecognizer;
1112
import org.hibernate.engine.query.spi.ParameterParser;
1213
import org.hibernate.testing.junit4.BaseUnitTestCase;
1314

@@ -27,4 +28,32 @@ public void testEscapeCallRecognition() {
2728
"from User u where u.userName = ? and u.userType = 'call'"
2829
) );
2930
}
31+
@Test
32+
public void testQuotedTextInComment() {
33+
ParamLocationRecognizer recognizer = new ParamLocationRecognizer();
34+
35+
ParameterParser.parse("-- 'This' should not fail the test.\n"
36+
+ "SELECT column FROM Table WHERE column <> :param", recognizer);
37+
38+
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("param"));
39+
}
40+
41+
@Test
42+
public void testContractionInComment() {
43+
ParamLocationRecognizer recognizer = new ParamLocationRecognizer();
44+
45+
ParameterParser.parse("-- This shouldn't fail the test.\n" + "SELECT column FROM Table WHERE column <> :param",
46+
recognizer);
47+
48+
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("param"));
49+
}
50+
51+
@Test
52+
public void testApostropheInOracleAlias() {
53+
ParamLocationRecognizer recognizer = new ParamLocationRecognizer();
54+
55+
ParameterParser.parse("SELECT column as \"Table's column\" FROM Table WHERE column <> :param", recognizer);
56+
57+
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("param"));
58+
}
3059
}

0 commit comments

Comments
 (0)