mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add mapping support
This commit is contained in:
@@ -5,7 +5,11 @@ import java.util.LinkedHashMap;
|
||||
import com.alibaba.druid.mapping.spi.MappingProvider;
|
||||
import com.alibaba.druid.mapping.spi.MappingVisitor;
|
||||
import com.alibaba.druid.mapping.spi.MySqlMappingProvider;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
|
||||
public class MappingEngine {
|
||||
@@ -55,10 +59,50 @@ public class MappingEngine {
|
||||
|
||||
query.accept(this.createMappingVisitor());
|
||||
|
||||
return toSQL(query);
|
||||
}
|
||||
|
||||
public SQLDeleteStatement explainToDeleteSQLObject(String sql) {
|
||||
return provider.explainToDeleteSQLObject(this, sql);
|
||||
}
|
||||
|
||||
public String explainToDeleteSQLObjectSQL(String sql) {
|
||||
SQLDeleteStatement query = explainToDeleteSQLObject(sql);
|
||||
|
||||
query.accept(this.createMappingVisitor());
|
||||
|
||||
return toSQL(query);
|
||||
}
|
||||
|
||||
public String toSQL(SQLObject sqlObject) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
SQLASTOutputVisitor outputVisitor = createOutputVisitor(out);
|
||||
query.accept(outputVisitor);
|
||||
sqlObject.accept(outputVisitor);
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
public SQLUpdateStatement explainToUpdateSQLObject(String sql) {
|
||||
return provider.explainToUpdateSQLObject(this, sql);
|
||||
}
|
||||
|
||||
public String explainToUpdateSQL(String sql) {
|
||||
SQLUpdateStatement query = explainToUpdateSQLObject(sql);
|
||||
|
||||
query.accept(this.createMappingVisitor());
|
||||
|
||||
return toSQL(query);
|
||||
}
|
||||
|
||||
public SQLInsertStatement explainToInsertSQLObject(String sql) {
|
||||
return provider.explainToInsertSQLObject(this, sql);
|
||||
}
|
||||
|
||||
public String explainToInsertSQL(String sql) {
|
||||
SQLInsertStatement query = explainToInsertSQLObject(sql);
|
||||
|
||||
query.accept(this.createMappingVisitor());
|
||||
|
||||
return toSQL(query);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,15 @@ package com.alibaba.druid.mapping.spi;
|
||||
import com.alibaba.druid.mapping.MappingEngine;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNumberExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.Top;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerSelectParser;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerOutputVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
|
||||
@@ -44,4 +49,20 @@ public class MSSQLServerMappingProvider implements MappingProvider {
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
public SQLDeleteStatement explainToDeleteSQLObject(MappingEngine engine, String sql) {
|
||||
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
|
||||
return parser.parseDeleteStatement();
|
||||
}
|
||||
|
||||
public SQLUpdateStatement explainToUpdateSQLObject(MappingEngine engine, String sql) {
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
return parser.parseUpdateStatement();
|
||||
}
|
||||
|
||||
|
||||
public SQLInsertStatement explainToInsertSQLObject(MappingEngine engine, String sql) {
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
return (SQLInsertStatement) parser.parseInsert();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package com.alibaba.druid.mapping.spi;
|
||||
|
||||
import com.alibaba.druid.mapping.MappingEngine;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
|
||||
public interface MappingProvider {
|
||||
@@ -11,4 +14,10 @@ public interface MappingProvider {
|
||||
SQLASTOutputVisitor createOutputVisitor(MappingEngine engine, Appendable out);
|
||||
|
||||
SQLSelectQueryBlock explainToSelectSQLObject(MappingEngine engine, String sql);
|
||||
|
||||
SQLDeleteStatement explainToDeleteSQLObject(MappingEngine engine, String sql);
|
||||
|
||||
SQLUpdateStatement explainToUpdateSQLObject(MappingEngine engine, String sql);
|
||||
|
||||
SQLInsertStatement explainToInsertSQLObject(MappingEngine engine, String sql);
|
||||
}
|
||||
|
||||
@@ -3,10 +3,14 @@ package com.alibaba.druid.mapping.spi;
|
||||
import com.alibaba.druid.mapping.MappingEngine;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlSelectParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
|
||||
@@ -21,7 +25,7 @@ public class MySqlMappingProvider implements MappingProvider {
|
||||
public SQLASTOutputVisitor createOutputVisitor(MappingEngine engine, Appendable out) {
|
||||
return new MySqlOutputVisitor(out);
|
||||
}
|
||||
|
||||
|
||||
public SQLSelectQueryBlock explainToSelectSQLObject(MappingEngine engine, String sql) {
|
||||
MySqlSelectParser selectParser = new MySqlSelectParser(sql);
|
||||
MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) selectParser.query();
|
||||
@@ -44,4 +48,19 @@ public class MySqlMappingProvider implements MappingProvider {
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
public MySqlDeleteStatement explainToDeleteSQLObject(MappingEngine engine, String sql) {
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
return parser.parseDeleteStatement();
|
||||
}
|
||||
|
||||
public SQLUpdateStatement explainToUpdateSQLObject(MappingEngine engine, String sql) {
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
return parser.parseUpdateStatement();
|
||||
}
|
||||
|
||||
public SQLInsertStatement explainToInsertSQLObject(MappingEngine engine, String sql) {
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
return (SQLInsertStatement) parser.parseInsert();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package com.alibaba.druid.mapping.spi;
|
||||
|
||||
import com.alibaba.druid.mapping.MappingEngine;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleDeleteStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleSelectParser;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
|
||||
@@ -26,4 +30,19 @@ public class OracleMappingProvider implements MappingProvider {
|
||||
return query;
|
||||
}
|
||||
|
||||
public OracleDeleteStatement explainToDeleteSQLObject(MappingEngine engine, String sql) {
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
return parser.parseDeleteStatement();
|
||||
}
|
||||
|
||||
public SQLUpdateStatement explainToUpdateSQLObject(MappingEngine engine, String sql) {
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
return parser.parseUpdateStatement();
|
||||
}
|
||||
|
||||
|
||||
public SQLInsertStatement explainToInsertSQLObject(MappingEngine engine, String sql) {
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
return (SQLInsertStatement) parser.parseInsert();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLLiteralExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
@@ -87,7 +86,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
return new SQLSelectStatement(new MySqlSelectParser(lexer).select());
|
||||
}
|
||||
|
||||
public SQLDeleteStatement parseDeleteStatement() throws ParserException {
|
||||
public MySqlDeleteStatement parseDeleteStatement() throws ParserException {
|
||||
accept(Token.DELETE);
|
||||
|
||||
MySqlDeleteStatement deleteStatement = new MySqlDeleteStatement();
|
||||
|
||||
+9
-4
@@ -37,6 +37,7 @@ import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSetStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.OracleHint;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleErrorLoggingClause;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleParameter;
|
||||
@@ -161,7 +162,7 @@ public class OracleStatementParser extends SQLStatementParser {
|
||||
}
|
||||
|
||||
if (lexer.token() == (Token.UPDATE)) {
|
||||
statementList.add(new OracleUpdateParser(this.lexer).parseUpdate());
|
||||
statementList.add(parseUpdateStatement());
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -176,7 +177,7 @@ public class OracleStatementParser extends SQLStatementParser {
|
||||
}
|
||||
|
||||
if (lexer.token() == (Token.DELETE)) {
|
||||
statementList.add(parseDelete());
|
||||
statementList.add(parseDeleteStatement());
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -368,7 +369,7 @@ public class OracleStatementParser extends SQLStatementParser {
|
||||
stmt.setImmediate(Boolean.FALSE);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1413,7 +1414,7 @@ public class OracleStatementParser extends SQLStatementParser {
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public OracleDeleteStatement parseDelete() throws ParserException {
|
||||
public OracleDeleteStatement parseDeleteStatement() throws ParserException {
|
||||
accept(Token.DELETE);
|
||||
|
||||
OracleDeleteStatement deleteStatement = new OracleDeleteStatement();
|
||||
@@ -1595,4 +1596,8 @@ public class OracleStatementParser extends SQLStatementParser {
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public SQLUpdateStatement parseUpdateStatement() throws ParserException {
|
||||
return new OracleUpdateParser(this.lexer).parseUpdateStatement();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ public class OracleUpdateParser extends SQLStatementParser {
|
||||
super(lexer);
|
||||
}
|
||||
|
||||
public OracleUpdateStatement parseUpdate() throws ParserException {
|
||||
public OracleUpdateStatement parseUpdateStatement() throws ParserException {
|
||||
accept(Token.UPDATE);
|
||||
|
||||
OracleUpdateStatement update = new OracleUpdateStatement();
|
||||
|
||||
Reference in New Issue
Block a user