Skip to content

Commit

Permalink
修改nop-dyn的reference实现
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jul 25, 2024
1 parent 202aaf2 commit 9e44447
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -60,6 +62,7 @@ public class DynEntityMetaToOrmModel {
private final IEntityModel dynEntityModel;
private final IEntityModel dynRelationModel;
private final boolean forceRealTable;
private Map<String, OrmEntityModel> middleTables = new HashMap<>();

static final List<String> STD_PROPS = Arrays.asList(NopDynEntity.PROP_NAME_version,
NopDynEntity.PROP_NAME_createdBy, NopDynEntity.PROP_NAME_createTime,
Expand Down Expand Up @@ -91,7 +94,7 @@ public OrmModel transformModule(NopDynModule module) {

model.setEntities(toOrmEntityModels(module.getEntityMetas(), basePackageName));

if(!forceRealTable) {
if (!forceRealTable) {
addExternalExtTable(model);
}
model.init();
Expand All @@ -112,7 +115,7 @@ void addExternalExtTable(OrmModel model) {
}

List<OrmEntityModel> toOrmEntityModels(Collection<NopDynEntityMeta> entityMetas, String basePackageName) {
// 如果不是外部表,也没有属性,则不需要生成对应的实体定义
// 如果是外部表,或者没有属性,则不需要生成对应的实体定义
List<OrmEntityModel> ret = entityMetas.stream().filter(entityMeta -> {
return entityMeta.isHasProp() || Boolean.TRUE.equals(entityMeta.getIsExternal());
}).map(this::toOrmEntityModel).collect(Collectors.toList());
Expand Down Expand Up @@ -208,11 +211,11 @@ protected void buildRealEntityModel(OrmEntityModel entityModel, NopDynEntityMeta
});

entityMeta.getRelationMetasForEntity().forEach(rel -> {
handlerRelation(entityModel, rel);
handleRelationMeta(entityModel, rel);
});
}

private void handlerRelation(OrmEntityModel entityModel, NopDynEntityRelationMeta rel) {
private void handleRelationMeta(OrmEntityModel entityModel, NopDynEntityRelationMeta rel) {

OrmRelationType ormRelationType = OrmRelationType.valueOf(rel.getRelationType());
if (ormRelationType == OrmRelationType.o2o
Expand All @@ -223,12 +226,12 @@ private void handlerRelation(OrmEntityModel entityModel, NopDynEntityRelationMet
entityModel.addRelation(oneRelationModel);

} else if (ormRelationType == OrmRelationType.m2m) {
handlerManyToManyRelation(entityModel, rel);
handleManyToManyRelation(entityModel, rel);
}
}

private void handlerManyToManyRelation(OrmEntityModel entityModel, NopDynEntityRelationMeta rel) {

private void handleManyToManyRelation(OrmEntityModel entityModel, NopDynEntityRelationMeta rel) {
// 多对多关联的定义是: 新建一个中间表,分别引用左表和右表的主键。这要求rel的leftProp和rightProp都应该是id
OrmReferenceModel manyRelationModel = this.toRelationModel(rel);
entityModel.addRelation(manyRelationModel);
entityModel.setTagSet(TagsHelper.add(StringHelper.parseCsvSet(rel.getTagsText()), OrmModelConstants.TAG_MANY_TO_MANY));
Expand All @@ -237,10 +240,10 @@ private void handlerManyToManyRelation(OrmEntityModel entityModel, NopDynEntityR

private OrmReferenceModel toRelationModel(NopDynEntityRelationMeta rel) {
OrmReferenceModel ret;
if (rel.getRelationType().equals(OrmRelationType.o2o.name())) {
ret = new OrmToOneReferenceModel();
} else {
if (rel.isToMany()) {
ret = new OrmToManyReferenceModel();
} else {
ret = new OrmToOneReferenceModel();
}

ret.setName(rel.getRelationName());
Expand Down Expand Up @@ -302,40 +305,6 @@ protected void buildVirtualEntityModel(OrmEntityModel entityModel, NopDynEntityM
addExtFields(entityModel);
}

protected void addRelation(OrmEntityModel entityModel, NopDynPropMeta propMeta) {
OrmToOneReferenceModel ref = new OrmToOneReferenceModel();
ref.setName(getRefObjName(propMeta));
ref.setDisplayName(getRefObjDisplayName(propMeta));
ref.setTagSet(propMeta.getTagSet());
// ref.setRefEntityName(propMeta.getRefEntityName());
// ref.setRefPropName(propMeta.getRefPropName());
// ref.setRefDisplayName(propMeta.getRefPropDisplayName());

OrmJoinOnModel join = new OrmJoinOnModel();
join.setLeftProp(propMeta.getPropName());
join.setRightProp(OrmModelConstants.PROP_ID);

ref.setJoin(Arrays.asList(join));
entityModel.addRelation(ref);
}

private String getRefObjName(NopDynPropMeta propMeta) {
if (propMeta.getPropName().endsWith("Id"))
return StringHelper.removeTail(propMeta.getPropName(), "Id");
return propMeta.getPropName() + "Obj";
}

private String getRefObjDisplayName(NopDynPropMeta propMeta) {
String displayName = propMeta.getDisplayName();
if (displayName == null)
return null;
if (displayName.endsWith("Id") || displayName.endsWith("ID")) {
displayName = displayName.substring(0, displayName.length() - 2);
displayName = displayName.trim();
}
return displayName;
}

protected void addExtFields(OrmEntityModel entityModel) {
IEntityRelationModel rel = dynEntityModel.getRelation(NopDynEntity.PROP_NAME_extFields, false);
OrmToManyReferenceModel ref = ((OrmToManyReferenceModel) rel).cloneInstance();
Expand All @@ -347,36 +316,6 @@ protected String buildVirtualPropPath(OrmAliasModel alias) {
return "extFields." + alias.getName() + "." + type;
}

protected OrmReferenceModel toRefModel(NopDynPropMeta propMeta) {
OrmToOneReferenceModel ret = new OrmToOneReferenceModel();
ret.setName(toRelationName(propMeta.getPropName()));
ret.setDisplayName(toDisplayName(propMeta.getDisplayName()));
// ret.setRefEntityName(propMeta.getRefEntityName());
// ret.setRefPropName(propMeta.getRefPropName());
// ret.setRefDisplayName(propMeta.getRefPropDisplayName());
ret.setTagSet(propMeta.getTagSet());

List<OrmJoinOnModel> join = new ArrayList<>(1);
OrmJoinOnModel joinOn = new OrmJoinOnModel();
joinOn.setLeftProp(propMeta.getPropName());
joinOn.setRightProp(OrmModelConstants.PROP_ID);
join.add(joinOn);
ret.setJoin(join);
return ret;
}

protected String toRelationName(String propName) {
if (propName.endsWith("Id")) return propName.substring(0, propName.length() - 2);
return propName + "Obj";
}

protected String toDisplayName(String displayName) {
if (displayName == null) return null;
if (displayName.endsWith("ID") || displayName.endsWith("Id"))
return displayName.substring(0, displayName.length() - 2).trim();
return displayName;
}

protected OrmAliasModel toAliasModel(NopDynPropMeta propMeta) {
OrmAliasModel ret = new OrmAliasModel();
ret.setName(propMeta.getPropName());
Expand Down Expand Up @@ -551,18 +490,4 @@ private void addJoinRelation(OrmEntityModel relTable, NopDynEntityRelationMeta r
// relTable.addRelation(ref1);
// relTable.addRelation(ref2);
}

protected OrmReferenceModel toRelationRefModel(String propName, String refPropName, String refDisplayName) {
OrmToOneReferenceModel ret = new OrmToOneReferenceModel();
ret.setName(refPropName);
ret.setDisplayName(refDisplayName);

List<OrmJoinOnModel> join = new ArrayList<>(1);
OrmJoinOnModel joinOn = new OrmJoinOnModel();
joinOn.setLeftProp(propName);
joinOn.setRightProp(OrmModelConstants.PROP_ID);
join.add(joinOn);
ret.setJoin(join);
return ret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.nop.orm.eql.eval;

import io.nop.orm.eql.ast.SqlExpr;
import io.nop.xlang.ast.Expression;

/**
* 将SQL表达式转换为XLang表达式
*/
public class SQLExprToExpression {
public Expression transform(SqlExpr expr) {
return null;
}
}

0 comments on commit 9e44447

Please sign in to comment.