mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
enhance mapping for ORM
This commit is contained in:
@@ -71,4 +71,17 @@ public class Entity {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("{name=");
|
||||
buf.append(name);
|
||||
|
||||
buf.append(", tableName=");
|
||||
buf.append(tableName);
|
||||
|
||||
buf.append("}");
|
||||
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.alibaba.druid.mapping;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -75,6 +76,10 @@ public class MappingEngine {
|
||||
public MappingVisitor createMappingVisitor() {
|
||||
return provider.createMappingVisitor(this);
|
||||
}
|
||||
|
||||
public MappingVisitor createMappingVisitor(List<Object> parameters) {
|
||||
return provider.createMappingVisitor(this, parameters);
|
||||
}
|
||||
|
||||
public SQLASTOutputVisitor createOutputVisitor(Appendable out) {
|
||||
return provider.createOutputVisitor(this, out);
|
||||
@@ -83,11 +88,16 @@ public class MappingEngine {
|
||||
public SQLSelectQueryBlock explainToSelectSQLObject(String sql) {
|
||||
return provider.explainToSelectSQLObject(this, sql);
|
||||
}
|
||||
|
||||
|
||||
public String explainToSelectSQL(String sql) {
|
||||
return explainToSelectSQL(sql, Collections.emptyList());
|
||||
}
|
||||
|
||||
public String explainToSelectSQL(String sql, List<Object> parameters) {
|
||||
SQLSelectQueryBlock query = explainToSelectSQLObject(sql);
|
||||
|
||||
query.accept(this.createMappingVisitor());
|
||||
MappingVisitor visitor = this.createMappingVisitor(parameters);
|
||||
query.accept(visitor);
|
||||
|
||||
return toSQL(query);
|
||||
}
|
||||
@@ -97,11 +107,20 @@ public class MappingEngine {
|
||||
}
|
||||
|
||||
public String explainToDeleteSQL(String sql) {
|
||||
SQLDeleteStatement query = explainToDeleteSQLObject(sql);
|
||||
SQLDeleteStatement stmt = explainToDeleteSQLObject(sql);
|
||||
|
||||
query.accept(this.createMappingVisitor());
|
||||
MappingVisitor visitor = this.createMappingVisitor(Collections.emptyList());
|
||||
stmt.accept(visitor);
|
||||
|
||||
return toSQL(query);
|
||||
return toSQL(stmt);
|
||||
}
|
||||
|
||||
public String resolveTableName(Entity entity, List<Object> parameters) {
|
||||
return entity.getTableName();
|
||||
}
|
||||
|
||||
public String resovleColumnName(Entity entity, Property property, List<Object> parameters) {
|
||||
return property.getDbColumnName();
|
||||
}
|
||||
|
||||
public SQLUpdateStatement explainToUpdateSQLObject(String sql) {
|
||||
@@ -109,11 +128,12 @@ public class MappingEngine {
|
||||
}
|
||||
|
||||
public String explainToUpdateSQL(String sql) {
|
||||
SQLUpdateStatement query = explainToUpdateSQLObject(sql);
|
||||
SQLUpdateStatement stmt = explainToUpdateSQLObject(sql);
|
||||
|
||||
query.accept(this.createMappingVisitor());
|
||||
MappingVisitor visitor = this.createMappingVisitor(Collections.emptyList());
|
||||
stmt.accept(visitor);
|
||||
|
||||
return toSQL(query);
|
||||
return toSQL(stmt);
|
||||
}
|
||||
|
||||
public SQLInsertStatement explainToInsertSQLObject(String sql) {
|
||||
@@ -121,11 +141,12 @@ public class MappingEngine {
|
||||
}
|
||||
|
||||
public String explainToInsertSQL(String sql) {
|
||||
SQLInsertStatement query = explainToInsertSQLObject(sql);
|
||||
SQLInsertStatement stmt = explainToInsertSQLObject(sql);
|
||||
|
||||
query.accept(this.createMappingVisitor());
|
||||
MappingVisitor visitor = this.createMappingVisitor(Collections.emptyList());
|
||||
stmt.accept(visitor);
|
||||
|
||||
return toSQL(query);
|
||||
return toSQL(stmt);
|
||||
}
|
||||
|
||||
public List<Object> exportParameters(SQLObject sqlObject) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.alibaba.druid.mapping;
|
||||
|
||||
public class Property {
|
||||
|
||||
private String name;
|
||||
private String desciption;
|
||||
private String dbColumnName;
|
||||
@@ -39,4 +38,16 @@ public class Property {
|
||||
this.desciption = desciption;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("{name=");
|
||||
buf.append(name);
|
||||
|
||||
buf.append(", dbColumnName=");
|
||||
buf.append(dbColumnName);
|
||||
|
||||
buf.append("}");
|
||||
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,12 @@ public class MSSQLServerMappingProvider implements MappingProvider {
|
||||
|
||||
@Override
|
||||
public MappingVisitor createMappingVisitor(MappingEngine engine) {
|
||||
return new MSSQLServerMappingVisitor(engine.getEntities());
|
||||
return new MSSQLServerMappingVisitor(engine);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MappingVisitor createMappingVisitor(MappingEngine engine, List<Object> parameters) {
|
||||
return new MSSQLServerMappingVisitor(engine, parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
package com.alibaba.druid.mapping.spi;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.mapping.Entity;
|
||||
import com.alibaba.druid.mapping.MappingEngine;
|
||||
import com.alibaba.druid.mapping.Property;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
@@ -16,16 +24,36 @@ import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitorAdapte
|
||||
|
||||
public class MSSQLServerMappingVisitor extends SQLServerASTVisitorAdapter implements MappingVisitor {
|
||||
|
||||
private final LinkedHashMap<String, Entity> entities;
|
||||
private final Map<String, SQLTableSource> tableSources = new LinkedHashMap<String, SQLTableSource>();
|
||||
private final MappingEngine engine;
|
||||
private final Map<String, SQLTableSource> tableSources = new LinkedHashMap<String, SQLTableSource>();
|
||||
|
||||
public MSSQLServerMappingVisitor(LinkedHashMap<String, Entity> entities){
|
||||
super();
|
||||
this.entities = entities;
|
||||
private final List<Object> parameters;
|
||||
private final List<PropertyValue> propertyValues = new ArrayList<PropertyValue>();
|
||||
private int variantIndex = 0;
|
||||
|
||||
public MSSQLServerMappingVisitor(MappingEngine engine){
|
||||
this(engine, Collections.emptyList());
|
||||
}
|
||||
|
||||
public MSSQLServerMappingVisitor(MappingEngine engine, List<Object> parameters){
|
||||
this.engine = engine;
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public MappingEngine getEngine() {
|
||||
return engine;
|
||||
}
|
||||
|
||||
public List<Object> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public List<PropertyValue> getPropertyValues() {
|
||||
return propertyValues;
|
||||
}
|
||||
|
||||
public LinkedHashMap<String, Entity> getEntities() {
|
||||
return entities;
|
||||
return engine.getEntities();
|
||||
}
|
||||
|
||||
public Map<String, SQLTableSource> getTableSources() {
|
||||
@@ -33,12 +61,18 @@ public class MSSQLServerMappingVisitor extends SQLServerASTVisitorAdapter implem
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entity getFirstEntity() {
|
||||
for (Map.Entry<String, Entity> entry : entities.entrySet()) {
|
||||
return entry.getValue();
|
||||
}
|
||||
public String resolveTableName(Entity entity) {
|
||||
return engine.resolveTableName(entity, parameters);
|
||||
}
|
||||
|
||||
return null;
|
||||
@Override
|
||||
public String resovleColumnName(Entity entity, Property property) {
|
||||
return engine.resovleColumnName(entity, property, parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entity getFirstEntity() {
|
||||
return engine.getFirstEntity();
|
||||
}
|
||||
|
||||
public Entity getEntity(String name) {
|
||||
@@ -66,6 +100,21 @@ public class MSSQLServerMappingVisitor extends SQLServerASTVisitorAdapter implem
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLPropertyExpr x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLBinaryOpExpr x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLVariantRefExpr x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLSubqueryTableSource x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
@@ -80,5 +129,13 @@ public class MSSQLServerMappingVisitor extends SQLServerASTVisitorAdapter implem
|
||||
public boolean visit(SQLExprTableSource x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int getAndIncrementVariantIndex() {
|
||||
return variantIndex++;
|
||||
}
|
||||
|
||||
public int getVariantIndex() {
|
||||
return variantIndex;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ public interface MappingProvider {
|
||||
|
||||
MappingVisitor createMappingVisitor(MappingEngine engine);
|
||||
|
||||
MappingVisitor createMappingVisitor(MappingEngine engine, List<Object> parameters);
|
||||
|
||||
ExportParameterVisitor createExportParameterVisitor(List<Object> parameters);
|
||||
|
||||
SQLASTOutputVisitor createOutputVisitor(MappingEngine engine, Appendable out);
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
package com.alibaba.druid.mapping.spi;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.mapping.Entity;
|
||||
import com.alibaba.druid.mapping.MappingEngine;
|
||||
import com.alibaba.druid.mapping.Property;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public interface MappingVisitor extends SQLASTVisitor {
|
||||
MappingEngine getEngine();
|
||||
|
||||
List<Object> getParameters();
|
||||
|
||||
Entity getFirstEntity();
|
||||
|
||||
@@ -15,4 +21,12 @@ public interface MappingVisitor extends SQLASTVisitor {
|
||||
Map<String, Entity> getEntities();
|
||||
|
||||
Map<String, SQLTableSource> getTableSources();
|
||||
|
||||
String resolveTableName(Entity entity);
|
||||
|
||||
String resovleColumnName(Entity entity, Property property);
|
||||
|
||||
List<PropertyValue> getPropertyValues();
|
||||
|
||||
int getAndIncrementVariantIndex();
|
||||
}
|
||||
|
||||
@@ -8,7 +8,13 @@ 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.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
@@ -32,7 +38,9 @@ public class MappingVisitorUtils {
|
||||
throw new DruidMappingException("entity not foudn : " + entityName);
|
||||
}
|
||||
|
||||
tableExpr.setName(entity.getTableName());
|
||||
x.putAttribute("mapping.entity", entity);
|
||||
String tableName = visitor.resolveTableName(entity);
|
||||
tableExpr.setName(tableName);
|
||||
}
|
||||
|
||||
if (x.getAlias() != null) {
|
||||
@@ -58,23 +66,24 @@ public class MappingVisitorUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean visit(MappingVisitor visitor, SQLIdentifierExpr x) {
|
||||
public static boolean visit(MappingVisitor visitor, SQLPropertyExpr x) {
|
||||
SQLIdentifierExpr ownerExpr = (SQLIdentifierExpr) x.getOwner();
|
||||
String ownerName = ownerExpr.getName();
|
||||
|
||||
String propertyName = x.getName();
|
||||
|
||||
Property property = null;
|
||||
for (Entity entity : visitor.getEntities().values()) {
|
||||
property = entity.getProperty(propertyName);
|
||||
if (property != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Entity entity = visitor.getEntity(ownerName);
|
||||
property = entity.getProperty(propertyName);
|
||||
|
||||
if (property == null) {
|
||||
throw new DruidMappingException("property not found : " + propertyName);
|
||||
}
|
||||
|
||||
String dbColumName = property.getDbColumnName();
|
||||
String dbColumName = visitor.resovleColumnName(entity, property);
|
||||
x.setName(dbColumName);
|
||||
x.putAttribute("mapping.property", property);
|
||||
x.putAttribute("mapping.entity", entity);
|
||||
|
||||
if (x.getParent() instanceof SQLSelectItem) {
|
||||
SQLSelectItem selectItem = (SQLSelectItem) x.getParent();
|
||||
@@ -85,6 +94,109 @@ public class MappingVisitorUtils {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean visit(MappingVisitor visitor, SQLIdentifierExpr x) {
|
||||
String propertyName = x.getName();
|
||||
|
||||
Property property = null;
|
||||
Entity propertyEntity = null;
|
||||
for (Entity entity : visitor.getEntities().values()) {
|
||||
property = entity.getProperty(propertyName);
|
||||
if (property != null) {
|
||||
propertyEntity = entity;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (property == null) {
|
||||
throw new DruidMappingException("property not found : " + propertyName);
|
||||
}
|
||||
|
||||
String dbColumName = visitor.resovleColumnName(propertyEntity, property);
|
||||
x.setName(dbColumName);
|
||||
|
||||
x.putAttribute("mapping.property", property);
|
||||
x.putAttribute("mapping.entity", propertyEntity);
|
||||
|
||||
if (x.getParent() instanceof SQLSelectItem) {
|
||||
SQLSelectItem selectItem = (SQLSelectItem) x.getParent();
|
||||
if (selectItem.getAlias() == null) {
|
||||
selectItem.setAlias('"' + property.getName() + '"');
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean visit(MappingVisitor visitor, SQLBinaryOpExpr x) {
|
||||
if (x.getOperator() == SQLBinaryOperator.Equality) {
|
||||
if (x.getLeft() instanceof SQLIdentifierExpr && isSimpleValue(visitor, x.getRight())) {
|
||||
visit(visitor, (SQLIdentifierExpr) x.getLeft());
|
||||
x.getRight().accept(visitor);
|
||||
|
||||
Entity entity = (Entity) x.getLeft().getAttribute("mapping.entity");
|
||||
Property property = (Property) x.getLeft().getAttribute("mapping.property");
|
||||
Object value = x.getRight().getAttribute("mapping.value");
|
||||
|
||||
visitor.getPropertyValues().add(new PropertyValue(entity, property, value));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (x.getLeft() instanceof SQLPropertyExpr && isSimpleValue(visitor, x.getRight())) {
|
||||
visit(visitor, (SQLPropertyExpr) x.getLeft());
|
||||
x.getRight().accept(visitor);
|
||||
|
||||
Entity entity = (Entity) x.getLeft().getAttribute("mapping.entity");
|
||||
Property property = (Property) x.getLeft().getAttribute("mapping.property");
|
||||
Object value = x.getRight().getAttribute("mapping.value");
|
||||
|
||||
visitor.getPropertyValues().add(new PropertyValue(entity, property, value));
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean isSimpleValue(MappingVisitor visitor, SQLExpr expr) {
|
||||
if (expr instanceof SQLNumericLiteralExpr) {
|
||||
expr.putAttribute("mapping.value", ((SQLNumericLiteralExpr) expr).getNumber());
|
||||
return true;
|
||||
}
|
||||
|
||||
if (expr instanceof SQLCharExpr) {
|
||||
expr.putAttribute("mapping.value", ((SQLCharExpr) expr).getText());
|
||||
return true;
|
||||
}
|
||||
|
||||
if (expr instanceof SQLVariantRefExpr) {
|
||||
Map<String, Object> attributes = expr.getAttributes();
|
||||
Integer varIndex = (Integer) attributes.get("mapping.varIndex");
|
||||
if (varIndex == null) {
|
||||
varIndex = visitor.getAndIncrementVariantIndex();
|
||||
expr.putAttribute("mapping.varIndex", varIndex);
|
||||
}
|
||||
|
||||
Object parameter = visitor.getParameters().get(varIndex);
|
||||
|
||||
expr.putAttribute("mapping.value", parameter);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean visit(MappingVisitor visitor, SQLVariantRefExpr x) {
|
||||
Map<String, Object> attributes = x.getAttributes();
|
||||
Integer varIndex = (Integer) attributes.get("mapping.varIndex");
|
||||
if (varIndex == null) {
|
||||
varIndex = visitor.getAndIncrementVariantIndex();
|
||||
x.putAttribute("mapping.varIndex", varIndex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean visit(MappingVisitor visitor, SQLSelectQueryBlock x) {
|
||||
if (x.getSelectList().size() == 0) {
|
||||
@@ -101,6 +213,7 @@ public class MappingVisitorUtils {
|
||||
if (x.getFrom() == null) {
|
||||
Entity firstEntity = visitor.getFirstEntity();
|
||||
SQLExprTableSource from = new SQLExprTableSource(new SQLIdentifierExpr(firstEntity.getName()));
|
||||
from.putAttribute("mapping.entity", firstEntity);
|
||||
x.setFrom(from);
|
||||
}
|
||||
|
||||
@@ -112,6 +225,27 @@ public class MappingVisitorUtils {
|
||||
}
|
||||
|
||||
public static Entity getEntity(MappingVisitor visitor, String name) {
|
||||
SQLTableSource tableSource = visitor.getTableSources().get(name);
|
||||
|
||||
if (tableSource != null) {
|
||||
Entity entity = (Entity) tableSource.getAttribute("mapping.entity");
|
||||
if (entity != null) {
|
||||
return entity;
|
||||
}
|
||||
|
||||
if (tableSource instanceof SQLExprTableSource) {
|
||||
SQLExpr expr = ((SQLExprTableSource) tableSource).getExpr();
|
||||
|
||||
if (expr instanceof SQLIdentifierExpr) {
|
||||
name = ((SQLIdentifierExpr) expr).getName();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Entity entity = visitor.getEntities().get(name);
|
||||
|
||||
if (entity == null) {
|
||||
@@ -132,14 +266,14 @@ public class MappingVisitorUtils {
|
||||
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();
|
||||
|
||||
@@ -22,7 +22,12 @@ public class MySqlMappingProvider implements MappingProvider {
|
||||
|
||||
@Override
|
||||
public MappingVisitor createMappingVisitor(MappingEngine engine) {
|
||||
return new MySqlMappingVisitor(engine.getEntities());
|
||||
return new MySqlMappingVisitor(engine);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MappingVisitor createMappingVisitor(MappingEngine engine, List<Object> parameters) {
|
||||
return new MySqlMappingVisitor(engine, parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
package com.alibaba.druid.mapping.spi;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.mapping.Entity;
|
||||
import com.alibaba.druid.mapping.MappingEngine;
|
||||
import com.alibaba.druid.mapping.Property;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
@@ -16,11 +24,32 @@ import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
|
||||
|
||||
public class MySqlMappingVisitor extends MySqlASTVisitorAdapter implements MappingVisitor {
|
||||
|
||||
private final LinkedHashMap<String, Entity> entities;
|
||||
private final Map<String, SQLTableSource> tableSources = new LinkedHashMap<String, SQLTableSource>();
|
||||
private final MappingEngine engine;
|
||||
private final Map<String, SQLTableSource> tableSources = new LinkedHashMap<String, SQLTableSource>();
|
||||
|
||||
public MySqlMappingVisitor(LinkedHashMap<String, Entity> entities){
|
||||
this.entities = entities;
|
||||
private final List<Object> parameters;
|
||||
private final List<PropertyValue> propertyValues = new ArrayList<PropertyValue>();
|
||||
private int variantIndex = 0;
|
||||
|
||||
public MySqlMappingVisitor(MappingEngine engine){
|
||||
this(engine, Collections.emptyList());
|
||||
}
|
||||
|
||||
public MySqlMappingVisitor(MappingEngine engine, List<Object> parameters){
|
||||
this.engine = engine;
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public MappingEngine getEngine() {
|
||||
return engine;
|
||||
}
|
||||
|
||||
public List<Object> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public List<PropertyValue> getPropertyValues() {
|
||||
return propertyValues;
|
||||
}
|
||||
|
||||
public Map<String, SQLTableSource> getTableSources() {
|
||||
@@ -28,15 +57,21 @@ public class MySqlMappingVisitor extends MySqlASTVisitorAdapter implements Mappi
|
||||
}
|
||||
|
||||
public Map<String, Entity> getEntities() {
|
||||
return entities;
|
||||
return engine.getEntities();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String resolveTableName(Entity entity) {
|
||||
return engine.resolveTableName(entity, parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String resovleColumnName(Entity entity, Property property) {
|
||||
return engine.resovleColumnName(entity, property, parameters);
|
||||
}
|
||||
|
||||
public Entity getFirstEntity() {
|
||||
for (Map.Entry<String, Entity> entry : entities.entrySet()) {
|
||||
return entry.getValue();
|
||||
}
|
||||
|
||||
return null;
|
||||
return engine.getFirstEntity();
|
||||
}
|
||||
|
||||
public Entity getEntity(String name) {
|
||||
@@ -64,6 +99,21 @@ public class MySqlMappingVisitor extends MySqlASTVisitorAdapter implements Mappi
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLPropertyExpr x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLBinaryOpExpr x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLVariantRefExpr x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLSubqueryTableSource x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
@@ -78,4 +128,13 @@ public class MySqlMappingVisitor extends MySqlASTVisitorAdapter implements Mappi
|
||||
public boolean visit(SQLExprTableSource x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAndIncrementVariantIndex() {
|
||||
return variantIndex++;
|
||||
}
|
||||
|
||||
public int getVariantIndex() {
|
||||
return variantIndex;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,12 @@ public class OracleMappingProvider implements MappingProvider {
|
||||
|
||||
@Override
|
||||
public MappingVisitor createMappingVisitor(MappingEngine engine) {
|
||||
return new OracleMappingVisitor(engine.getEntities());
|
||||
return new OracleMappingVisitor(engine);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MappingVisitor createMappingVisitor(MappingEngine engine, List<Object> parameters) {
|
||||
return new OracleMappingVisitor(engine, parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
package com.alibaba.druid.mapping.spi;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.mapping.Entity;
|
||||
import com.alibaba.druid.mapping.MappingEngine;
|
||||
import com.alibaba.druid.mapping.Property;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
@@ -18,28 +26,54 @@ import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitorAdapter;
|
||||
|
||||
public class OracleMappingVisitor extends OracleASTVisitorAdapter implements MappingVisitor {
|
||||
|
||||
private final LinkedHashMap<String, Entity> entities;
|
||||
private final Map<String, SQLTableSource> tableSources = new LinkedHashMap<String, SQLTableSource>();
|
||||
private final MappingEngine engine;
|
||||
private final Map<String, SQLTableSource> tableSources = new LinkedHashMap<String, SQLTableSource>();
|
||||
|
||||
public OracleMappingVisitor(LinkedHashMap<String, Entity> entities){
|
||||
super();
|
||||
this.entities = entities;
|
||||
private final List<Object> parameters;
|
||||
private final List<PropertyValue> propertyValues = new ArrayList<PropertyValue>();
|
||||
private int variantIndex = 0;
|
||||
|
||||
public OracleMappingVisitor(MappingEngine engine){
|
||||
this(engine, Collections.emptyList());
|
||||
}
|
||||
|
||||
public OracleMappingVisitor(MappingEngine engine, List<Object> parameters){
|
||||
this.engine = engine;
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public MappingEngine getEngine() {
|
||||
return engine;
|
||||
}
|
||||
|
||||
public List<Object> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public List<PropertyValue> getPropertyValues() {
|
||||
return propertyValues;
|
||||
}
|
||||
|
||||
public LinkedHashMap<String, Entity> getEntities() {
|
||||
return entities;
|
||||
return engine.getEntities();
|
||||
}
|
||||
|
||||
public Map<String, SQLTableSource> getTableSources() {
|
||||
return tableSources;
|
||||
}
|
||||
|
||||
public Entity getFirstEntity() {
|
||||
for (Map.Entry<String, Entity> entry : entities.entrySet()) {
|
||||
return entry.getValue();
|
||||
}
|
||||
@Override
|
||||
public String resolveTableName(Entity entity) {
|
||||
return engine.resolveTableName(entity, parameters);
|
||||
}
|
||||
|
||||
return null;
|
||||
@Override
|
||||
public String resovleColumnName(Entity entity, Property property) {
|
||||
return engine.resovleColumnName(entity, property, parameters);
|
||||
}
|
||||
|
||||
public Entity getFirstEntity() {
|
||||
return engine.getFirstEntity();
|
||||
}
|
||||
|
||||
public Entity getEntity(String name) {
|
||||
@@ -67,6 +101,21 @@ public class OracleMappingVisitor extends OracleASTVisitorAdapter implements Map
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLPropertyExpr x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLBinaryOpExpr x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLVariantRefExpr x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLSubqueryTableSource x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
@@ -86,10 +135,19 @@ public class OracleMappingVisitor extends OracleASTVisitorAdapter implements Map
|
||||
public boolean visit(OracleSelectTableReference x) {
|
||||
return MappingVisitorUtils.visit(this, x);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean visit(OracleDeleteStatement x) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAndIncrementVariantIndex() {
|
||||
return variantIndex++;
|
||||
}
|
||||
|
||||
public int getVariantIndex() {
|
||||
return variantIndex;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.alibaba.druid.mapping.spi;
|
||||
|
||||
import com.alibaba.druid.mapping.Entity;
|
||||
import com.alibaba.druid.mapping.Property;
|
||||
|
||||
public class PropertyValue {
|
||||
|
||||
private Property property;
|
||||
private Entity entity;
|
||||
private Object value;
|
||||
|
||||
public PropertyValue(Entity entity, Property property, Object value){
|
||||
this.property = property;
|
||||
this.entity = entity;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public PropertyValue(){
|
||||
}
|
||||
|
||||
public Property getProperty() {
|
||||
return property;
|
||||
}
|
||||
|
||||
public void setProperty(Property property) {
|
||||
this.property = property;
|
||||
}
|
||||
|
||||
public Entity getEntity() {
|
||||
return entity;
|
||||
}
|
||||
|
||||
public void setEntity(Entity entity) {
|
||||
this.entity = entity;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.alibaba.druid.bvt.mapping;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.mapping.Entity;
|
||||
import com.alibaba.druid.mapping.MappingEngine;
|
||||
import com.alibaba.druid.mapping.Property;
|
||||
|
||||
|
||||
public class MySqlMappingTest extends TestCase {
|
||||
MappingEngine engine = new MappingEngine();
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
Entity entity = new Entity();
|
||||
entity.setName("用户");
|
||||
entity.setTableName("user");
|
||||
|
||||
entity.addProperty(new Property("名称", "", "uid"));
|
||||
entity.addProperty(new Property("昵称", "", "name"));
|
||||
|
||||
engine.addEntity(entity);
|
||||
}
|
||||
|
||||
public void test_0 () throws Exception {
|
||||
String oql = "select * from 用户 u where u.名称 = 'a'";
|
||||
|
||||
String sql = engine.explainToSelectSQL(oql);
|
||||
|
||||
System.out.println(sql);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user