Skip to content

Commit

Permalink
JPQL query with cast function is transformed with errors #1710
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Subbotin committed Dec 24, 2018
1 parent 034fbd9 commit 9a22d13
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,17 @@ public void getResult_noChangesMade_Decimal() throws RecognitionException {
assertTransformsToSame(model, "select p.createTs as hours, p.name as name from Player p where p.version * 1.12 = 5");
}

@Test
public void getResult_noChangesMade_Cast() throws RecognitionException {
EntityBuilder builder = new EntityBuilder();
builder.startNewEntity("Player");
builder.addSingleValueAttribute(Date.class, "createTs");
builder.addStringAttribute("createTs");
DomainModel model = new DomainModel(builder.produce());
assertTransformsToSame(model, "select p.createTs as hours, cast(p.version float) as version from Player p where cast( p.version float) = 5");
assertTransformsToSame(model, "select p.createTs as hours, cast(p.version number(4)) as version from Player p where cast( p.version number ( 4)) = 5");
}

@Test
public void addJoinAsId() throws RecognitionException {
DomainModel model = prepareDomainModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ public Object pre(Object t) {
node.getType() == JPA2Lexer.THEN ||
node.getType() == JPA2Lexer.ELSE ||
node.getType() == JPA2Lexer.END ||
isExtractFromNode(node)
) {
isExtractFromNode(node) ||
isCastTypeNode(node)
) {
sb.appendSpace();
}

Expand Down Expand Up @@ -184,6 +185,19 @@ private boolean isExtractEnd(CommonTree node) {
return false;
}

private boolean isCastTypeNode(CommonTree node) {
if (node.parent != null) {
//third part of CAST expression
if (node.childIndex >= 2) {
Tree extractNode = node.parent.getChild(node.childIndex - 2);
if ("cast(".equalsIgnoreCase(extractNode.getText())) {
return true;
}
}
}
return false;
}

@Override
public Object post(Object t) {
if (!(t instanceof CommonTree))
Expand Down

0 comments on commit 9a22d13

Please sign in to comment.