提交 40f3269b authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Require MATCHED THEN | NOT MATCHED THEN after WHERE in MERGE USING

上级 39c36dd2
......@@ -1172,10 +1172,22 @@ public class Parser {
command.setOnCondition(condition);
read(")");
boolean matched = parseWhenMatched(command);
if (parseWhenNotMatched(command) && !matched) {
if (readIf("WHEN")) {
boolean matched = readIf("MATCHED");
if (matched) {
parseWhenMatched(command);
} else {
parseWhenNotMatched(command);
}
if (readIf("WHEN")) {
if (matched) {
parseWhenNotMatched(command);
} else {
read("MATCHED");
parseWhenMatched(command);
}
}
}
setSQL(command, "MERGE", start);
......@@ -1192,10 +1204,8 @@ public class Parser {
return command;
}
private boolean parseWhenMatched(MergeUsing command) {
if (!readIfAll("WHEN", "MATCHED", "THEN")) {
return false;
}
private void parseWhenMatched(MergeUsing command) {
read("THEN");
int startMatched = lastParseIndex;
if (readIf("UPDATE")) {
Update updateCommand = new Update(session);
......@@ -1212,20 +1222,18 @@ public class Parser {
parseDeleteGivenTable(deleteCommand, null, startMatched);
command.setDeleteCommand(deleteCommand);
}
return true;
}
private boolean parseWhenNotMatched(MergeUsing command) {
if (!readIfAll("WHEN", "NOT", "MATCHED", "THEN")) {
return false;
}
private void parseWhenNotMatched(MergeUsing command) {
read("NOT");
read("MATCHED");
read("THEN");
if (readIf("INSERT")) {
Insert insertCommand = new Insert(session);
insertCommand.setTable(command.getTargetTable());
parseInsertGivenTable(insertCommand, command.getTargetTable());
command.setInsertCommand(insertCommand);
}
return true;
}
private static void appendTableWithSchemaAndAlias(StringBuilder buff, Table table, String alias) {
......@@ -3554,26 +3562,6 @@ public class Parser {
return false;
}
/*
* Reads every token in list, in order - returns true if all are found.
* If any are not found, returns false - AND resets parsing back to state when called.
*/
private boolean readIfAll(String... tokens) {
// save parse location in case we have to fail this test
int start = lastParseIndex;
for (String token: tokens) {
if (!currentTokenQuoted && equalsToken(token, currentToken)) {
read();
} else {
// read failed - revert parse location to before when called
parseIndex = start;
read();
return false;
}
}
return true;
}
private boolean isToken(String token) {
boolean result = equalsToken(token, currentToken) &&
!currentTokenQuoted;
......
......@@ -149,3 +149,19 @@ SELECT * FROM TEST ORDER BY C1, C2;
DROP TABLE TEST;
> ok
CREATE TABLE TEST (ID INT, VALUE INT);
> ok
MERGE INTO TEST USING DUAL ON (ID = 1)
WHEN MATCHED THEN UPDATE SET VALUE = 1
WHEN;
> exception SYNTAX_ERROR_2
MERGE INTO TEST USING DUAL ON (ID = 1)
WHEN NOT MATCHED THEN INSERT (ID, VALUE) VALUES (1, 1)
WHEN;
> exception SYNTAX_ERROR_2
DROP TABLE TEST;
> ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论