mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -218,6 +218,8 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
private static final String DELAYED = "DELAYED";
|
||||
private static final String LOW_PRIORITY = "LOW_PRIORITY";
|
||||
|
||||
private int maxIntoClause = -1;
|
||||
|
||||
public MySqlStatementParser(String sql){
|
||||
super(new MySqlExprParser(sql));
|
||||
}
|
||||
@@ -226,6 +228,14 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
super(new MySqlExprParser(lexer));
|
||||
}
|
||||
|
||||
public int getMaxIntoClause() {
|
||||
return maxIntoClause;
|
||||
}
|
||||
|
||||
public void setMaxIntoClause(int maxIntoClause) {
|
||||
this.maxIntoClause = maxIntoClause;
|
||||
}
|
||||
|
||||
public SQLCreateTableStatement parseCreateTable() {
|
||||
MySqlCreateTableParser parser = new MySqlCreateTableParser(this.exprParser);
|
||||
return parser.parseCrateTable();
|
||||
@@ -2087,7 +2097,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
}
|
||||
|
||||
private void parseValueClause(List<ValuesClause> valueClauseList, int columnSize) {
|
||||
for (;;) {
|
||||
for (int i = 0;;++i) {
|
||||
if (lexer.token() != Token.LPAREN) {
|
||||
throw new ParserException("syntax error, expect ')'");
|
||||
}
|
||||
|
||||
+1
-2
@@ -48,7 +48,7 @@ public class MySqlExportParameterVisitor extends MySqlOutputVisitor implements E
|
||||
}
|
||||
|
||||
public MySqlExportParameterVisitor(List<Object> parameters) {
|
||||
this(parameters, new StringBuilder(), false);
|
||||
this(parameters, null, false);
|
||||
}
|
||||
|
||||
public MySqlExportParameterVisitor(final Appendable appender) {
|
||||
@@ -58,7 +58,6 @@ public class MySqlExportParameterVisitor extends MySqlOutputVisitor implements E
|
||||
public List<Object> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean visit(final SQLSelectItem x) {
|
||||
|
||||
@@ -40,7 +40,7 @@ public class ExportParameterizedOutputVisitor extends SQLASTOutputVisitor implem
|
||||
}
|
||||
|
||||
public ExportParameterizedOutputVisitor(final Appendable appender) {
|
||||
this(new ArrayList<Object>(),appender,true);
|
||||
this(new ArrayList<Object>(), appender, true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -78,6 +78,25 @@ public class ParameterizedOutputVisitorUtils {
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
public static String parameterize(List<SQLStatement> statementList, String dbType) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
ParameterizedVisitor visitor = createParameterizedOutputVisitor(out, dbType);
|
||||
|
||||
for (int i = 0; i < statementList.size(); i++) {
|
||||
if (i > 0) {
|
||||
out.append(";\n");
|
||||
}
|
||||
SQLStatement stmt = statementList.get(i);
|
||||
|
||||
if (stmt.hasBeforeComment()) {
|
||||
stmt.getBeforeCommentsDirect().clear();
|
||||
}
|
||||
stmt.accept(visitor);
|
||||
}
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
public static ParameterizedVisitor createParameterizedOutputVisitor(Appendable out, String dbType) {
|
||||
if (JdbcUtils.ORACLE.equals(dbType) || JdbcUtils.ALI_ORACLE.equals(dbType)) {
|
||||
return new OracleParameterizedOutputVisitor(out);
|
||||
|
||||
@@ -134,6 +134,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
}
|
||||
|
||||
public void print(char value) {
|
||||
if (this.appender == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this.appender.append(value);
|
||||
} catch (IOException e) {
|
||||
@@ -142,10 +146,18 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
}
|
||||
|
||||
public void print(int value) {
|
||||
if (this.appender == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
print0(Integer.toString(value));
|
||||
}
|
||||
|
||||
public void print(Date date) {
|
||||
if (this.appender == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
SimpleDateFormat dateFormat;
|
||||
if (date instanceof java.sql.Timestamp) {
|
||||
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
@@ -156,14 +168,24 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
}
|
||||
|
||||
public void print(long value) {
|
||||
if (this.appender == null) {
|
||||
return;
|
||||
}
|
||||
print0(Long.toString(value));
|
||||
}
|
||||
|
||||
public void print(String text) {
|
||||
if (this.appender == null) {
|
||||
return;
|
||||
}
|
||||
print0(text);
|
||||
}
|
||||
|
||||
protected void print0(String text) {
|
||||
if (appender == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this.appender.append(text);
|
||||
} catch (IOException e) {
|
||||
|
||||
Reference in New Issue
Block a user