Skip to content

Commit

Permalink
fix: Fix SQL WHENEVER for NOT FOUND condition (#2374)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nurkambay authored Jul 9, 2024
1 parent 57bd79b commit d71dbe3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -474,17 +474,24 @@ private Pair<ExecSqlWheneverNode.WheneverType, String> getWheneverType(Db2SqlPar
Pair<ExecSqlWheneverNode.WheneverType, String> result = Pair.of(ExecSqlWheneverNode.WheneverType.CONTINUE, null);

if (ruleContext.getChildCount() > 3) {
ParseTree pt = ruleContext.getChild(2);

int index = 2;
ParseTree pt = ruleContext.getChild(index);
String value = pt.getText().trim().toUpperCase();
if (Objects.equals(value, "FOUND")) {
index = 3;
pt = ruleContext.getChild(index);
value = pt.getText().trim().toUpperCase();
}

if (Objects.equals(value, "DO")) {
result = Pair.of(ExecSqlWheneverNode.WheneverType.DO, ruleContext.getChild(3).getText());
result = Pair.of(ExecSqlWheneverNode.WheneverType.DO, ruleContext.getChild(index + 1).getText());
} else if (Objects.equals(value, "GO")) {
if (ruleContext.getChildCount() > 4) {
result = Pair.of(ExecSqlWheneverNode.WheneverType.GOTO, ruleContext.getChild(4).getText());
if (ruleContext.getChildCount() > index + 2) {
result = Pair.of(ExecSqlWheneverNode.WheneverType.GOTO, ruleContext.getChild(index + 2).getText());
}
} else if (Objects.equals(value, "GOTO")) {
result = Pair.of(ExecSqlWheneverNode.WheneverType.GOTO, ruleContext.getChild(3).getText());
result = Pair.of(ExecSqlWheneverNode.WheneverType.GOTO, ruleContext.getChild(index + 1).getText());
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
PROGRAM-ID. CBACT01C.
PROCEDURE DIVISION.
EXEC SQL WHENEVER SQLERROR GOTO HANDLER END-EXEC.
EXEC SQL WHENEVER SQLERROR GO TO HANDLER END-EXEC.
EXEC SQL WHENEVER SQLWARNING GO TO HANDLER END-EXEC.
EXEC SQL WHENEVER SQLERROR CONTINUE END-EXEC.
EXEC SQL WHENEVER NOT FOUND GO TO HANDLER END-EXEC.

EXEC SQL
SELECT ABC FROM XYZ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"character": 9
},
"end": {
"line": 25,
"line": 26,
"character": 31
}
},
Expand Down Expand Up @@ -46,7 +46,7 @@
}
},
{
"wheneverCondition": "SQLERROR",
"wheneverCondition": "SQLWARNING",
"wheneverType": "GOTO",
"value": "HANDLER",
"type": "execwhenever",
Expand All @@ -58,7 +58,7 @@
},
"end": {
"line": 17,
"character": 49
"character": 51
}
}
},
Expand All @@ -72,7 +72,7 @@
},
"end": {
"line": 17,
"character": 49
"character": 51
}
}
},
Expand Down Expand Up @@ -106,16 +106,47 @@
}
}
},
{
"wheneverCondition": "NOT_FOUND",
"wheneverType": "GOTO",
"value": "HANDLER",
"type": "execwhenever",
"location": {
"uri": "fake/path",
"start": {
"line": 19,
"character": 22
},
"end": {
"line": 19,
"character": 49
}
}
},
{
"type": "endexec",
"location": {
"uri": "fake/path",
"start": {
"line": 19,
"character": 22
},
"end": {
"line": 19,
"character": 49
}
}
},
{
"type": "execsql",
"location": {
"uri": "fake/path",
"start": {
"line": 20,
"line": 21,
"character": 12
},
"end": {
"line": 22,
"line": 23,
"character": 45
}
}
Expand All @@ -125,11 +156,11 @@
"location": {
"uri": "fake/path",
"start": {
"line": 20,
"line": 21,
"character": 12
},
"end": {
"line": 22,
"line": 23,
"character": 45
}
}
Expand All @@ -141,11 +172,11 @@
"location": {
"uri": "fake/path",
"start": {
"line": 24,
"line": 25,
"character": 9
},
"end": {
"line": 25,
"line": 26,
"character": 31
}
}
Expand Down

0 comments on commit d71dbe3

Please sign in to comment.