com.jn.sqlhelper.dialect.scriptfile.DB2SqlStatementBuilder Maven / Gradle / Ivy
package com.jn.sqlhelper.dialect.scriptfile;
import com.jn.langx.util.Strings;
import com.jn.sqlhelper.common.sql.sqlscript.PlainSqlDelimiter;
import com.jn.sqlhelper.common.sql.sqlscript.PlainSqlStatementBuilder;
/**
* supporting DB2-specific delimiter changes.
*/
public class DB2SqlStatementBuilder extends PlainSqlStatementBuilder {
/**
* Are we currently inside a BEGIN END; block?
*/
private boolean insideBeginEndBlock;
/**
* Holds the beginning of the statement.
*/
private String statementStart = "";
@Override
protected PlainSqlDelimiter changeDelimiterIfNecessary(String line, PlainSqlDelimiter delimiter) {
if (Strings.countOccurrencesOf(statementStart, " ") < 4) {
statementStart += line;
statementStart += " ";
}
if (statementStart.startsWith("CREATE FUNCTION")
|| statementStart.startsWith("CREATE PROCEDURE")
|| statementStart.startsWith("CREATE TRIGGER")
|| statementStart.startsWith("CREATE OR REPLACE FUNCTION")
|| statementStart.startsWith("CREATE OR REPLACE PROCEDURE")
|| statementStart.startsWith("CREATE OR REPLACE TRIGGER")) {
if (line.startsWith("BEGIN")) {
insideBeginEndBlock = true;
}
if (line.endsWith("END;")) {
insideBeginEndBlock = false;
}
}
if (insideBeginEndBlock) {
return null;
}
return getDefaultDelimiter();
}
}