mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor
This commit is contained in:
@@ -4,14 +4,18 @@ import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.mapping.DruidMappingException;
|
||||
import com.alibaba.druid.mapping.Entity;
|
||||
import com.alibaba.druid.mapping.MappingEngine;
|
||||
import com.alibaba.druid.mapping.Property;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
|
||||
public class MappingVisitorUtils {
|
||||
|
||||
@@ -122,4 +126,24 @@ public class MappingVisitorUtils {
|
||||
return entity;
|
||||
}
|
||||
|
||||
public static void setDataSource(MappingEngine engine, SQLDeleteStatement stmt) {
|
||||
if (stmt.getTableSource() == null) {
|
||||
Entity entity = engine.getFirstEntity();
|
||||
stmt.setTableSource(new SQLIdentifierExpr(entity.getName()));
|
||||
}
|
||||
}
|
||||
|
||||
public static void setDataSource(MappingEngine engine, SQLUpdateStatement stmt) {
|
||||
if (stmt.getTableSource() == null) {
|
||||
Entity entity = engine.getFirstEntity();
|
||||
stmt.setTableSource(new SQLIdentifierExpr(entity.getName()));
|
||||
}
|
||||
}
|
||||
|
||||
public static void setDataSource(MappingEngine engine, SQLInsertStatement stmt) {
|
||||
if (stmt.getTableSource() == null) {
|
||||
Entity entity = engine.getFirstEntity();
|
||||
stmt.setTableSource(new SQLIdentifierExpr(entity.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,7 @@ package com.alibaba.druid.mapping.spi;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.mapping.Entity;
|
||||
import com.alibaba.druid.mapping.MappingEngine;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
@@ -57,10 +55,7 @@ public class MySqlMappingProvider implements MappingProvider {
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
|
||||
MySqlDeleteStatement stmt = parser.parseDeleteStatement();
|
||||
if (stmt.getTableSource() == null) {
|
||||
Entity entity = engine.getFirstEntity();
|
||||
stmt.setTableSource(new SQLIdentifierExpr(entity.getName()));
|
||||
}
|
||||
MappingVisitorUtils.setDataSource(engine, stmt);
|
||||
|
||||
return stmt;
|
||||
}
|
||||
@@ -69,10 +64,7 @@ public class MySqlMappingProvider implements MappingProvider {
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLUpdateStatement stmt = parser.parseUpdateStatement();
|
||||
|
||||
if (stmt.getTableSource() == null) {
|
||||
Entity entity = engine.getFirstEntity();
|
||||
stmt.setTableSource(new SQLIdentifierExpr(entity.getName()));
|
||||
}
|
||||
MappingVisitorUtils.setDataSource(engine, stmt);
|
||||
|
||||
return stmt;
|
||||
}
|
||||
@@ -80,11 +72,8 @@ public class MySqlMappingProvider implements MappingProvider {
|
||||
public SQLInsertStatement explainToInsertSQLObject(MappingEngine engine, String sql) {
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLInsertStatement stmt = (SQLInsertStatement) parser.parseInsert();
|
||||
|
||||
if (stmt.getTableSource() == null) {
|
||||
Entity entity = engine.getFirstEntity();
|
||||
stmt.setTableSource(new SQLIdentifierExpr(entity.getName()));
|
||||
}
|
||||
|
||||
MappingVisitorUtils.setDataSource(engine, stmt);
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
@@ -2,9 +2,7 @@ package com.alibaba.druid.mapping.spi;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.mapping.Entity;
|
||||
import com.alibaba.druid.mapping.MappingEngine;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
@@ -37,23 +35,27 @@ public class OracleMappingProvider implements MappingProvider {
|
||||
public OracleDeleteStatement explainToDeleteSQLObject(MappingEngine engine, String sql) {
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
OracleDeleteStatement stmt = parser.parseDeleteStatement();
|
||||
|
||||
if (stmt.getTableSource() == null) {
|
||||
Entity entity = engine.getFirstEntity();
|
||||
stmt.setTableName(new SQLIdentifierExpr(entity.getName()));
|
||||
}
|
||||
|
||||
MappingVisitorUtils.setDataSource(engine, stmt);
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public SQLUpdateStatement explainToUpdateSQLObject(MappingEngine engine, String sql) {
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
return parser.parseUpdateStatement();
|
||||
SQLUpdateStatement stmt = parser.parseUpdateStatement();
|
||||
|
||||
MappingVisitorUtils.setDataSource(engine, stmt);
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public SQLInsertStatement explainToInsertSQLObject(MappingEngine engine, String sql) {
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
return (SQLInsertStatement) parser.parseInsert();
|
||||
SQLInsertStatement stmt = (SQLInsertStatement) parser.parseInsert();
|
||||
MappingVisitorUtils.setDataSource(engine, stmt);
|
||||
return stmt;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user