Skip to content

Commit bb2bc00

Browse files
committed
[WIP][OPENJPA-2940] Adding JPQL syntax support for ID and VERSION
1 parent 4fc3260 commit bb2bc00

2 files changed

Lines changed: 81 additions & 2 deletions

File tree

openjpa-kernel/src/main/jjtree/org/apache/openjpa/kernel/jpql/JPQL.jjt

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ TOKEN [ IGNORE_CASE ]: /* basics */
144144
| < DIV: "/" >
145145

146146
| < NEW: "NEW" >
147+
148+
| < ID: "ID" >
149+
| < VERSION: "VERSION" >
147150

148151
| < ALL: "ALL" >
149152
| < ANY: "ANY" >
@@ -886,14 +889,15 @@ void all_expression() #ALL : { }
886889

887890
void comparison_expression() : { }
888891
{
889-
// comparison_expression ::= string_value comparison_operator {string_expression | all_or_any_expression} | boolean_value { =|<>} {boolean_expression | all_or_any_expression} | datetime_primary comparison_operator {datetime_expression | all_or_any_expression} | entity_bean_value { = | <> } {entity_bean_expression | all_or_any_expression} | arithmetic_value comparison_operator {arithmetic_expression | all_or_any_expression
892+
// comparison_expression ::= string_value comparison_operator {string_expression | all_or_any_expression} | boolean_value { =|<>} {boolean_expression | all_or_any_expression} | datetime_primary comparison_operator {datetime_expression | all_or_any_expression} | entity_bean_value { = | <> } {entity_bean_expression | all_or_any_expression} | arithmetic_value comparison_operator {arithmetic_expression | all_or_any_expression | entity_id_or_version_function {= | <>} input_parameter | entity_type_expression {= | <>} entity_type_expression}
890893

891894
LOOKAHEAD(arithmetic_comp()) arithmetic_comp() |
892895
LOOKAHEAD(string_comp()) string_comp() |
893896
LOOKAHEAD(boolean_comp()) boolean_comp() |
894897
LOOKAHEAD(enum_comp()) enum_comp() |
895898
LOOKAHEAD(datetime_comp()) datetime_comp() |
896899
LOOKAHEAD(entity_comp()) entity_comp() |
900+
LOOKAHEAD(entity_id_or_version_comp()) entity_id_or_version_comp() |
897901
LOOKAHEAD(entity_type_comp()) entity_type_comp()
898902
}
899903

@@ -1040,6 +1044,11 @@ void entity_type_comp() : { }
10401044
)
10411045
}
10421046

1047+
void entity_id_or_version_comp() : { }
1048+
{
1049+
entity_id_or_version_function() ( <EQ> input_parameter() #EQUALS(2) | <NE> input_parameter() #NOTEQUALS(2))
1050+
}
1051+
10431052
void type_discriminator() #TYPE : { }
10441053
{
10451054
<TYPE> "(" (LOOKAHEAD(path()) path()
@@ -1064,7 +1073,24 @@ void scalar_expression() #SCALAREXPRESSION : { }
10641073
LOOKAHEAD(datetime_primary()) datetime_primary() |
10651074
LOOKAHEAD(enum_primary()) enum_primary() |
10661075
LOOKAHEAD(boolean_primary()) boolean_primary() |
1067-
LOOKAHEAD(entity_type_expression()) entity_type_expression()
1076+
LOOKAHEAD(entity_type_expression()) entity_type_expression() |
1077+
LOOKAHEAD(entity_id_or_version_function()) entity_id_or_version_function()
1078+
}
1079+
1080+
void entity_id_or_version_function(): { }
1081+
{
1082+
id_function() |
1083+
version_function()
1084+
}
1085+
1086+
void id_function() #IDFUNCTION : { }
1087+
{
1088+
<ID> "(" ( general_identification_variable() | simple_entity_expression() ) ")"
1089+
}
1090+
1091+
void version_function() #VERSIONFUNCTION : { }
1092+
{
1093+
<VERSION> "(" ( general_identification_variable() | simple_entity_expression() ) ")"
10681094
}
10691095

10701096
void arithmetic_cast_function() #CASTTONUMBER : { }
@@ -1567,6 +1593,8 @@ void path_component() #IDENTIFICATIONVARIABLE :
15671593
| t = <INDEX>
15681594
| t = <TYPE>
15691595
| t = <CAST>
1596+
| t = <ID>
1597+
| t = <VERSION>
15701598
| t = <STRING>
15711599
| t = <CLASS>
15721600
) { jjtThis.setToken (t); }

openjpa-kernel/src/test/java/org/apache/openjpa/kernel/jpql/TestJPQLParser.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,5 +232,56 @@ public void testReplaceStringFunction() {
232232
}
233233
fail();
234234
}
235+
236+
@Test
237+
public void testIdFunctionSimple() {
238+
try {
239+
String query = "SELECT u FROM User AS u WHERE ID(u) = :id";
240+
JPQLNode node = (JPQLNode) new JPQL(query).parseQuery();
241+
assertNotNull(node);
242+
return;
243+
} catch (ParseException ex) {
244+
ex.printStackTrace();
245+
}
246+
fail();
247+
}
235248

249+
@Test
250+
public void testIdFunctionOnSelect() {
251+
try {
252+
String query = "SELECT ID(u) FROM User AS u WHERE u.name = :name";
253+
JPQLNode node = (JPQLNode) new JPQL(query).parseQuery();
254+
assertNotNull(node);
255+
return;
256+
} catch (ParseException ex) {
257+
ex.printStackTrace();
258+
}
259+
fail();
260+
}
261+
262+
@Test
263+
public void testVersionFunctionSimple() {
264+
try {
265+
String query = "SELECT u FROM User AS u WHERE VERSION(u) <> :version";
266+
JPQLNode node = (JPQLNode) new JPQL(query).parseQuery();
267+
assertNotNull(node);
268+
return;
269+
} catch (ParseException ex) {
270+
ex.printStackTrace();
271+
}
272+
fail();
273+
}
274+
275+
@Test
276+
public void testDeleteUsingIdAndVersion() {
277+
try {
278+
String query = "DELETE from Employee WHERE id(this) = :id AND version(this) = :version";
279+
JPQLNode node = (JPQLNode) new JPQL(query).parseQuery();
280+
assertNotNull(node);
281+
return;
282+
} catch (ParseException ex) {
283+
ex.printStackTrace();
284+
}
285+
fail();
286+
}
236287
}

0 commit comments

Comments
 (0)