Skip to content

Commit 3626638

Browse files
Fix Java String literal parsing in JQPL grammar.
Allow using java string literals using escaped double quotes next to single quoted values.
1 parent 0d9c1e6 commit 3626638

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Jpql.g4

+3
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ constructor_item
204204
| scalar_expression
205205
| aggregate_expression
206206
| identification_variable
207+
| literal
207208
;
208209

209210
aggregate_expression
@@ -620,6 +621,7 @@ constructor_name
620621

621622
literal
622623
: STRINGLITERAL
624+
| JAVASTRINGLITERAL
623625
| INTLITERAL
624626
| FLOATLITERAL
625627
| LONGLITERAL
@@ -855,6 +857,7 @@ NOT_EQUAL : '<>' | '!=' ;
855857
CHARACTER : '\'' (~ ('\'' | '\\')) '\'' ;
856858
IDENTIFICATION_VARIABLE : ('a' .. 'z' | 'A' .. 'Z' | '\u0080' .. '\ufffe' | '$' | '_') ('a' .. 'z' | 'A' .. 'Z' | '\u0080' .. '\ufffe' | '0' .. '9' | '$' | '_')* ;
857859
STRINGLITERAL : '\'' (~ ('\'' | '\\'))* '\'' ;
860+
JAVASTRINGLITERAL : '"' ( ('\\' [btnfr"']) | ~('"'))* '"';
858861
FLOATLITERAL : ('0' .. '9')* '.' ('0' .. '9')+ (E ('0' .. '9')+)* (F|D)?;
859862
INTLITERAL : ('0' .. '9')+ ;
860863
LONGLITERAL : ('0' .. '9')+L ;

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpqlQueryRenderer.java

+4
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ public List<JpaQueryParsingToken> visitConstructor_item(JpqlParser.Constructor_i
723723
tokens.addAll(visit(ctx.aggregate_expression()));
724724
} else if (ctx.identification_variable() != null) {
725725
tokens.addAll(visit(ctx.identification_variable()));
726+
} else if (ctx.literal() != null) {
727+
tokens.addAll(visit(ctx.literal()));
726728
}
727729

728730
return tokens;
@@ -2153,6 +2155,8 @@ public List<JpaQueryParsingToken> visitLiteral(JpqlParser.LiteralContext ctx) {
21532155

21542156
if (ctx.STRINGLITERAL() != null) {
21552157
tokens.add(new JpaQueryParsingToken(ctx.STRINGLITERAL()));
2158+
} else if (ctx.JAVASTRINGLITERAL() != null) {
2159+
tokens.add(new JpaQueryParsingToken(ctx.JAVASTRINGLITERAL()));
21562160
} else if (ctx.INTLITERAL() != null) {
21572161
tokens.add(new JpaQueryParsingToken(ctx.INTLITERAL()));
21582162
} else if (ctx.FLOATLITERAL() != null) {

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/JpqlComplianceTests.java

+5
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@ void numericLiterals() {
6666
assertQuery("SELECT s FROM Stat s WHERE s.ratio > 3.14e32D");
6767
}
6868

69+
@Test // GH-3308
70+
void newWithStrings() {
71+
assertQuery("select new com.example.demo.SampleObject(se.id, se.sampleValue, \"java\") from SampleEntity se");
72+
}
73+
6974
}

0 commit comments

Comments
 (0)