Skip to content

Commit

Permalink
针对create trigger sql加上postgresql的execute语法支持 #5474
Browse files Browse the repository at this point in the history
针对create trigger sql加上postgresql的execute语法支持 #5474
  • Loading branch information
lizongbo authored and wenshao committed Nov 14, 2023
1 parent b5b2454 commit ef9d14d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public class SQLCreateTriggerStatement extends SQLStatementImpl implements SQLCr
private SQLExpr when;
private SQLStatement body;

private String executeType;

private SQLExpr executeFunc;

public SQLCreateTriggerStatement() {
}

Expand Down Expand Up @@ -144,6 +148,22 @@ public void setForEachRow(boolean forEachRow) {
this.forEachRow = forEachRow;
}

public String getExecuteType() {
return executeType;
}

public void setExecuteType(String executeType) {
this.executeType = executeType;
}

public SQLExpr getExecuteFunc() {
return executeFunc;
}

public void setExecuteFunc(SQLExpr executeFunc) {
this.executeFunc = executeFunc;
}

public List<SQLName> getUpdateOfColumns() {
return updateOfColumns;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4166,6 +4166,16 @@ public SQLStatement parseCreateTrigger() {
stmt.setWhen(condition);
}

//for postgresql https://www.postgresql.org/docs/current/sql-createtrigger.html
if (lexer.identifierEquals("EXECUTE")) {
lexer.nextToken();
String executeType = lexer.stringVal();
stmt.setExecuteType(executeType);
lexer.nextToken();
SQLExpr executeFunc = this.exprParser.expr();
stmt.setExecuteFunc(executeFunc);
return stmt;
}
List<SQLStatement> body = this.parseStatementList();
if (body == null || body.isEmpty()) {
throw new ParserException("syntax error");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6533,7 +6533,14 @@ public boolean visit(SQLCreateTriggerStatement x) {
}
this.indentCount--;
println();
x.getBody().accept(this);
if (x.getExecuteType() != null && x.getExecuteFunc() != null) {
print0(ucase ? x.getExecuteType().toUpperCase() : x.getExecuteType().toLowerCase());
print0(" ");
x.getExecuteFunc().accept(this);
}
if (x.getBody() != null) {
x.getBody().accept(this);
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.alibaba.druid.bvt.sql.postgresql.issues;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.parser.SQLParserUtils;
import com.alibaba.druid.sql.parser.SQLStatementParser;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

/**
* @author lizongbo
* @see <a href="https://github.com/alibaba/druid/issues/5474">Issue来源</a>
* @see <a href="https://www.postgresql.org/docs/current/sql-createtrigger.html">PostgreSQL CREATE TRIGGER — define a new trigger</a>
*/
public class Issue5474 {

@Test
public void test_create_triger_execute() throws Exception {
for (DbType dbType : new DbType[]{DbType.postgresql}) {
String sql = "CREATE TRIGGER \"update_time\" BEFORE UPDATE ON \"poit_cloud\".\"ent_i_checking_analyze\" FOR EACH ROW EXECUTE PROCEDURE poit_cloud.modify_timestamp();";

SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
SQLStatement statement = parser.parseStatement();
System.out.println("原始的sql===" + sql);
String newSql = statement.toString();
System.out.println("生成的sql===" + newSql);

}
}
}

0 comments on commit ef9d14d

Please sign in to comment.