提交 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 { ...@@ -1172,10 +1172,22 @@ public class Parser {
command.setOnCondition(condition); command.setOnCondition(condition);
read(")"); read(")");
boolean matched = parseWhenMatched(command); if (readIf("WHEN")) {
if (parseWhenNotMatched(command) && !matched) { boolean matched = readIf("MATCHED");
if (matched) {
parseWhenMatched(command);
} else {
parseWhenNotMatched(command);
}
if (readIf("WHEN")) {
if (matched) {
parseWhenNotMatched(command);
} else {
read("MATCHED");
parseWhenMatched(command); parseWhenMatched(command);
} }
}
}
setSQL(command, "MERGE", start); setSQL(command, "MERGE", start);
...@@ -1192,10 +1204,8 @@ public class Parser { ...@@ -1192,10 +1204,8 @@ public class Parser {
return command; return command;
} }
private boolean parseWhenMatched(MergeUsing command) { private void parseWhenMatched(MergeUsing command) {
if (!readIfAll("WHEN", "MATCHED", "THEN")) { read("THEN");
return false;
}
int startMatched = lastParseIndex; int startMatched = lastParseIndex;
if (readIf("UPDATE")) { if (readIf("UPDATE")) {
Update updateCommand = new Update(session); Update updateCommand = new Update(session);
...@@ -1212,20 +1222,18 @@ public class Parser { ...@@ -1212,20 +1222,18 @@ public class Parser {
parseDeleteGivenTable(deleteCommand, null, startMatched); parseDeleteGivenTable(deleteCommand, null, startMatched);
command.setDeleteCommand(deleteCommand); command.setDeleteCommand(deleteCommand);
} }
return true;
} }
private boolean parseWhenNotMatched(MergeUsing command) { private void parseWhenNotMatched(MergeUsing command) {
if (!readIfAll("WHEN", "NOT", "MATCHED", "THEN")) { read("NOT");
return false; read("MATCHED");
} read("THEN");
if (readIf("INSERT")) { if (readIf("INSERT")) {
Insert insertCommand = new Insert(session); Insert insertCommand = new Insert(session);
insertCommand.setTable(command.getTargetTable()); insertCommand.setTable(command.getTargetTable());
parseInsertGivenTable(insertCommand, command.getTargetTable()); parseInsertGivenTable(insertCommand, command.getTargetTable());
command.setInsertCommand(insertCommand); command.setInsertCommand(insertCommand);
} }
return true;
} }
private static void appendTableWithSchemaAndAlias(StringBuilder buff, Table table, String alias) { private static void appendTableWithSchemaAndAlias(StringBuilder buff, Table table, String alias) {
...@@ -3554,26 +3562,6 @@ public class Parser { ...@@ -3554,26 +3562,6 @@ public class Parser {
return false; 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) { private boolean isToken(String token) {
boolean result = equalsToken(token, currentToken) && boolean result = equalsToken(token, currentToken) &&
!currentTokenQuoted; !currentTokenQuoted;
......
...@@ -149,3 +149,19 @@ SELECT * FROM TEST ORDER BY C1, C2; ...@@ -149,3 +149,19 @@ SELECT * FROM TEST ORDER BY C1, C2;
DROP TABLE TEST; DROP TABLE TEST;
> ok > 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论