This commit is contained in:
ZHENGGUAN LI 2025-07-10 18:24:35 +08:00 committed by GitHub
commit 1dca9ac877
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 84 additions and 1 deletions

View File

@ -820,6 +820,45 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
return false;
}
@Override
public boolean visit(SQLCreateFunctionStatement x) {
boolean create = x.isCreate();
if (create) {
print0(ucase ? "CREATE " : "create ");
if (x.isOrReplace()) {
print0(ucase ? "OR REPLACE " : "or replace ");
}
}
if (x.isTemporary()) {
print0(ucase ? "TEMPORARY " : "temporary ");
}
print0(ucase ? "FUNCTION " : "function ");
x.getName().accept(this);
int paramSize = x.getParameters().size();
print0(" (");
this.indentCount++;
for (int i = 0; i < paramSize; ++i) {
if (i != 0) {
print0(","); // change: remove extra space
println();
}
SQLParameter param = x.getParameters().get(i);
param.accept(this);
}
this.indentCount--;
print(')');
printCreateFunctionBody(x);
return false;
}
@Override
public boolean visit(SQLCreateUserStatement x) {
print0(ucase ? "CREATE USER " : "create user ");

View File

@ -12513,7 +12513,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
return;
}
println();
println(ucase ? "AS" : "as");
print(ucase ? "AS " : "as ");
block.accept(this);
}

View File

@ -0,0 +1,44 @@
package com.alibaba.druid.bvt.sql.postgresql.create_function;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLNullExpr;
import com.alibaba.druid.sql.ast.statement.SQLBlockStatement;
import com.alibaba.druid.sql.ast.statement.SQLCreateFunctionStatement;
import com.alibaba.druid.sql.ast.statement.SQLExprStatement;
import junit.framework.TestCase;
public class PGCreateFunctionTest extends TestCase {
public void test_0() throws Exception {
SQLCreateFunctionStatement sqlCreateFunctionStatement = new SQLCreateFunctionStatement();
sqlCreateFunctionStatement.setDbType(DbType.postgresql);
sqlCreateFunctionStatement.setCreate(true);
sqlCreateFunctionStatement.setName(new SQLIdentifierExpr("func1"));
SQLBlockStatement sqlBlockStatement = new SQLBlockStatement();
sqlCreateFunctionStatement.setBlock(sqlBlockStatement);
// Set sql block statement
SQLExprStatement sqlExprStatement = new SQLExprStatement();
sqlExprStatement.setExpr(new SQLNullExpr());
sqlExprStatement.setAfterSemi(true);
sqlBlockStatement.setHaveBeginEnd(true);
sqlBlockStatement.setIsDollarQuoted(true);
sqlBlockStatement.setLanguage("plpgsql");
List<SQLStatement> sqlExprStatementList = new ArrayList<>();
sqlExprStatementList.add(sqlExprStatement);
sqlBlockStatement.setStatementList(sqlExprStatementList);
String sql = sqlCreateFunctionStatement.toString();
assertEquals("CREATE FUNCTION func1 ()\n" +
"AS $$\n" +
"BEGIN\n" +
"\tNULL;\n" +
"END;\n" +
"$$ LANGUAGE plpgsql;", sql);
}
}