Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8287,7 +8287,12 @@ public SQLCreateFunctionStatement parseCreateFunction() {
if (lexer.token() == Token.BEGIN) {
block = this.parseBlock();
} else {
block = this.parseStatement();
// Simple-form RETURN body is not a complete script; parseStatement()
// would reject leftover tokens via checkEndToken.
block = this.parseStatement0();
if (lexer.nextIf(Token.SEMI)) {
block.setAfterSemi(true);
}
}

stmt.setBlock(block);
Expand Down Expand Up @@ -8369,7 +8374,10 @@ public SQLCreateProcedureStatement parseCreateProcedure() {
if (lexer.token() == Token.BEGIN) {
block = this.parseBlock();
} else {
block = this.parseStatement();
block = this.parseStatement0();
if (lexer.nextIf(Token.SEMI)) {
block.setAfterSemi(true);
}
}

stmt.setBlock(block);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ public void parseStatementList(List<SQLStatement> statementList, int max, SQLObj
stmt.addBeforeComment(comments);
stmt.setParent(parent);
statementList.add(stmt);
if (createStatementConsumedSemi(stmt)) {
semi = true;
}
continue;
}
case INSERT: {
Expand Down Expand Up @@ -5328,6 +5331,23 @@ private void checkEndToken() {
}
}

/**
* Simple-form CREATE FUNCTION/PROCEDURE consumes the statement terminator on
* the body, not on the CREATE statement itself.
*/
private static boolean createStatementConsumedSemi(SQLStatement stmt) {
if (stmt.isAfterSemi()) {
return true;
}
SQLStatement block = null;
if (stmt instanceof SQLCreateFunctionStatement) {
block = ((SQLCreateFunctionStatement) stmt).getBlock();
} else if (stmt instanceof SQLCreateProcedureStatement) {
block = ((SQLCreateProcedureStatement) stmt).getBlock();
}
return block != null && block.isAfterSemi();
}

public void parseExplainFormatPartition(SQLExplainStatement explain) {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.mysql.create_function;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLCreateFunctionStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.util.JdbcConstants;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class MySqlCreateFunctionMultiStatementTest {
@Test
public void testSimpleFormFunctionThenSelect() {
String sql = "CREATE FUNCTION f() RETURNS INT RETURN 1;\nSELECT 1;";
List<SQLStatement> stmts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
assertEquals(2, stmts.size());
assertTrue(stmts.get(0) instanceof SQLCreateFunctionStatement);
assertTrue(stmts.get(1) instanceof SQLSelectStatement);
String functionSql = SQLUtils.toMySqlString(stmts.get(0));
assertTrue(functionSql.contains("CREATE FUNCTION"));
assertTrue(functionSql.contains("RETURN 1"));
assertFalse(functionSql.contains(";;"));
}

@Test
public void testSimpleFormFunctionThenFunction() {
String sql = "CREATE FUNCTION f() RETURNS INT RETURN 1;\nCREATE FUNCTION g() RETURNS INT RETURN 2;";
List<SQLStatement> stmts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
assertEquals(2, stmts.size());
assertTrue(stmts.get(0) instanceof SQLCreateFunctionStatement);
assertTrue(stmts.get(1) instanceof SQLCreateFunctionStatement);
}

@Test
public void testBeginEndFunctionThenSelect() {
String sql = "CREATE FUNCTION f() RETURNS INT BEGIN RETURN 1; END;\nSELECT 1;";
List<SQLStatement> stmts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
assertEquals(2, stmts.size());
assertTrue(stmts.get(0) instanceof SQLCreateFunctionStatement);
assertTrue(stmts.get(1) instanceof SQLSelectStatement);
}
}