Skip to content

Commit b144c06

Browse files
Update jpql grammar to support F & D for numeric values.
1 parent a2e8ea0 commit b144c06

File tree

2 files changed

+66
-2
lines changed
  • spring-data-jpa/src

2 files changed

+66
-2
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -852,10 +852,9 @@ WHERE : W H E R E;
852852
EQUAL : '=' ;
853853
NOT_EQUAL : '<>' | '!=' ;
854854

855-
856855
CHARACTER : '\'' (~ ('\'' | '\\')) '\'' ;
857856
IDENTIFICATION_VARIABLE : ('a' .. 'z' | 'A' .. 'Z' | '\u0080' .. '\ufffe' | '$' | '_') ('a' .. 'z' | 'A' .. 'Z' | '\u0080' .. '\ufffe' | '0' .. '9' | '$' | '_')* ;
858857
STRINGLITERAL : '\'' (~ ('\'' | '\\'))* '\'' ;
859-
FLOATLITERAL : ('0' .. '9')* '.' ('0' .. '9')+ (E '0' .. '9')* ;
858+
FLOATLITERAL : ('0' .. '9')* '.' ('0' .. '9')+ (E ('0' .. '9')+)* (F|D)?;
860859
INTLITERAL : ('0' .. '9')+ ;
861860
LONGLITERAL : ('0' .. '9')+L ;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.repository.query;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.springframework.data.jpa.repository.query.JpaQueryParsingToken.*;
20+
21+
import org.antlr.v4.runtime.CharStreams;
22+
import org.antlr.v4.runtime.CommonTokenStream;
23+
import org.junit.jupiter.api.Test;
24+
25+
/**
26+
* @author Christoph Strobl
27+
*/
28+
public class JpqlComplianceTests {
29+
30+
private static String parseWithoutChanges(String query) {
31+
32+
JpqlLexer lexer = new JpqlLexer(CharStreams.fromString(query));
33+
JpqlParser parser = new JpqlParser(new CommonTokenStream(lexer));
34+
35+
parser.addErrorListener(new BadJpqlGrammarErrorListener(query));
36+
37+
JpqlParser.StartContext parsedQuery = parser.start();
38+
39+
return render(new JpqlQueryRenderer().visit(parsedQuery));
40+
}
41+
42+
private void assertQuery(String query) {
43+
44+
String slimmedDownQuery = reduceWhitespace(query);
45+
assertThat(parseWithoutChanges(slimmedDownQuery)).isEqualTo(slimmedDownQuery);
46+
}
47+
48+
private String reduceWhitespace(String original) {
49+
50+
return original //
51+
.replaceAll("[ \\t\\n]{1,}", " ") //
52+
.trim();
53+
}
54+
55+
@Test
56+
void numericLiterals() {
57+
58+
assertQuery("SELECT e FROM Employee e WHERE e.id = 1234");
59+
assertQuery("SELECT e FROM Employee e WHERE e.id = 1234L");
60+
assertQuery("SELECT s FROM Stat s WHERE s.ratio > 3.14");
61+
assertQuery("SELECT s FROM Stat s WHERE s.ratio > 3.14F");
62+
assertQuery("SELECT s FROM Stat s WHERE s.ratio > 3.14e32D");
63+
}
64+
65+
}

0 commit comments

Comments
 (0)