Skip to content

Commit

Permalink
refactor parseIf
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Oct 30, 2024
1 parent 790ac68 commit ea773cc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8676,42 +8676,19 @@ public MySqlChecksumTableStatement parseChecksum() {
return stmt;
}

/**
* parse if statement
*
* @return MySqlIfStatement
*/
public SQLIfStatement parseIf() {
accept(Token.IF);

SQLIfStatement stmt = new SQLIfStatement();

stmt.setCondition(this.exprParser.expr());

accept(Token.THEN);

this.parseStatementList(stmt.getStatements(), -1, stmt);

while (lexer.token() == Token.ELSEIF) {
lexer.nextToken();

@Override
protected void parseIfElse(SQLIfStatement stmt) {
while (lexer.nextIf(Token.ELSEIF)) {
SQLIfStatement.ElseIf elseIf = new SQLIfStatement.ElseIf();

elseIf.setCondition(this.exprParser.expr());
elseIf.setParent(stmt);

accept(Token.THEN);
this.parseStatementList(elseIf.getStatements(), -1, stmt);

this.parseStatementList(elseIf.getStatements(), -1, elseIf);
stmt.getElseIfList().add(elseIf);
}

while (lexer.token() == Token.ELSE) {
lexer.nextToken();

if (lexer.token() == Token.IF) {
lexer.nextToken();

while (lexer.nextIf(Token.ELSE)) {
if (lexer.nextIf(Token.IF)) {
SQLIfStatement.ElseIf elseIf = new SQLIfStatement.ElseIf();

elseIf.setCondition(this.exprParser.expr());
Expand All @@ -8728,13 +8705,6 @@ public SQLIfStatement parseIf() {
break;
}
}

accept(Token.END);
accept(Token.IF);
accept(SEMI);
stmt.setAfterSemi(true);

return stmt;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1177,52 +1177,22 @@ public SQLStatement parseCase() {
return caseStmt;
}

public SQLStatement parseIf() {
accept(Token.IF);

SQLIfStatement stmt = new SQLIfStatement();
stmt.setDbType(dbType);

stmt.setCondition(this.exprParser.expr());

accept(Token.THEN);

this.parseStatementList(stmt.getStatements(), -1, stmt);

while (lexer.token() == Token.ELSIF) {
lexer.nextToken();

@Override
protected void parseIfElse(SQLIfStatement stmt) {
while (lexer.nextIf(Token.ELSIF)) {
SQLIfStatement.ElseIf elseIf = new SQLIfStatement.ElseIf();

elseIf.setCondition(this.exprParser.expr());
elseIf.setParent(stmt);

accept(Token.THEN);
this.parseStatementList(elseIf.getStatements(), -1, elseIf);

stmt.getElseIfList().add(elseIf);
}

if (lexer.token() == Token.ELSE) {
lexer.nextToken();

if (lexer.nextIf(Token.ELSE)) {
SQLIfStatement.Else elseItem = new SQLIfStatement.Else();
this.parseStatementList(elseItem.getStatements(), -1, elseItem);
stmt.setElseItem(elseItem);
}

accept(Token.END);
// if (lexer.token() != Token.SEMI) {
// accept(Token.IF);
// }
accept(Token.IF);
// if (lexer.token() == Token.SEMI) {
// lexer.nextToken();
// }
accept(Token.SEMI);
stmt.setAfterSemi(true);

return stmt;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,37 +579,6 @@ private void parseParameters(List<SQLParameter> parameters, SQLObject parent) {
}
}

@Override
public SQLStatement parseIf() {
accept(Token.IF);
SQLIfStatement stmt = new SQLIfStatement();
stmt.setCondition(this.exprParser.expr());
accept(Token.THEN);
this.parseStatementList(stmt.getStatements(), -1, stmt);
while (lexer.token() == Token.ELSE) {
lexer.nextToken();
if (lexer.token() == Token.IF) {
lexer.nextToken();
SQLIfStatement.ElseIf elseIf = new SQLIfStatement.ElseIf();
elseIf.setCondition(this.exprParser.expr());
elseIf.setParent(stmt);
accept(Token.THEN);
this.parseStatementList(elseIf.getStatements(), -1, elseIf);
stmt.getElseIfList().add(elseIf);
} else {
SQLIfStatement.Else elseItem = new SQLIfStatement.Else();
this.parseStatementList(elseItem.getStatements(), -1, elseItem);
stmt.setElseItem(elseItem);
break;
}
}
accept(Token.END);
accept(Token.IF);
accept(Token.SEMI);
stmt.setAfterSemi(true);
return stmt;
}

protected PGStartTransactionStatement parseBegin() {
PGStartTransactionStatement stmt = new PGStartTransactionStatement();
if (lexer.token() == Token.START) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1257,11 +1257,21 @@ public SQLStatement parseIf() {
SQLIfStatement stmt = new SQLIfStatement();
stmt.setCondition(this.exprParser.expr());
accept(Token.THEN);

this.parseStatementList(stmt.getStatements(), -1, stmt);
while (lexer.token() == Token.ELSE) {
lexer.nextToken();
if (lexer.token() == Token.IF) {
lexer.nextToken();

parseIfElse(stmt);

accept(Token.END);
accept(Token.IF);
accept(Token.SEMI);
stmt.setAfterSemi(true);
return stmt;
}

protected void parseIfElse(SQLIfStatement stmt) {
while (lexer.nextIf(Token.ELSE)) {
if (lexer.nextIf(Token.IF)) {
SQLIfStatement.ElseIf elseIf = new SQLIfStatement.ElseIf();
elseIf.setCondition(this.exprParser.expr());
elseIf.setParent(stmt);
Expand All @@ -1275,11 +1285,6 @@ public SQLStatement parseIf() {
break;
}
}
accept(Token.END);
accept(Token.IF);
accept(Token.SEMI);
stmt.setAfterSemi(true);
return stmt;
}

public SQLWhileStatement parseWhile() {
Expand Down

0 comments on commit ea773cc

Please sign in to comment.