mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add feature support for issue #1815
This commit is contained in:
@@ -42,7 +42,14 @@ import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleForStatement;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
import static com.alibaba.druid.util.Utils.getBoolean;
|
||||
|
||||
public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements ParameterizedVisitor, PrintableVisitor {
|
||||
public static Boolean defaultPrintStatementAfterSemi;
|
||||
|
||||
static {
|
||||
defaultPrintStatementAfterSemi = getBoolean(System.getProperties(), "druid.sql.output.printStatementAfterSemi"); // compatible for early versions
|
||||
}
|
||||
|
||||
protected final Appendable appender;
|
||||
private String indent = "\t";
|
||||
@@ -74,6 +81,8 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
|
||||
protected boolean desensitize = false;
|
||||
|
||||
protected Boolean printStatementAfterSemi = defaultPrintStatementAfterSemi;
|
||||
|
||||
public SQLASTOutputVisitor(Appendable appender){
|
||||
this.appender = appender;
|
||||
}
|
||||
@@ -4543,7 +4552,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
public void postVisit(SQLObject x) {
|
||||
if (x instanceof SQLStatement) {
|
||||
SQLStatement stmt = (SQLStatement) x;
|
||||
if (stmt.isAfterSemi()) {
|
||||
boolean printSemi = printStatementAfterSemi == null
|
||||
? stmt.isAfterSemi()
|
||||
: printStatementAfterSemi.booleanValue();
|
||||
if (printSemi) {
|
||||
print(';');
|
||||
}
|
||||
}
|
||||
@@ -4680,4 +4692,12 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
x.getQuery().accept(this);
|
||||
return false;
|
||||
}
|
||||
|
||||
public Boolean getPrintStatementAfterSemi() {
|
||||
return printStatementAfterSemi;
|
||||
}
|
||||
|
||||
public void setPrintStatementAfterSemi(Boolean printStatementAfterSemi) {
|
||||
this.printStatementAfterSemi = printStatementAfterSemi;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.alibaba.druid.demo.sql;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 11/07/2017.
|
||||
*/
|
||||
public class Demo_for_issue_1815 extends TestCase {
|
||||
public void test_0() throws Exception {
|
||||
String sql = "select * from t1;select * from t2;";
|
||||
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
|
||||
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM t1;", stmtList.get(0).toString());
|
||||
|
||||
SQLASTOutputVisitor.defaultPrintStatementAfterSemi = false;
|
||||
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM t1", stmtList.get(0).toString());
|
||||
|
||||
SQLASTOutputVisitor.defaultPrintStatementAfterSemi = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user