EntityBuilder优化

This commit is contained in:
redkale
2023-07-25 18:34:38 +08:00
parent 1e87f6497c
commit e86638e490
2 changed files with 21 additions and 24 deletions

View File

@@ -8,6 +8,7 @@ import java.lang.reflect.*;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.redkale.annotation.Nullable; import org.redkale.annotation.Nullable;
import org.redkale.convert.ConvertDisabled;
import org.redkale.persistence.*; import org.redkale.persistence.*;
import org.redkale.util.*; import org.redkale.util.*;
@@ -215,7 +216,7 @@ public class EntityBuilder<T> {
* *
* @return Entity对象 * @return Entity对象
*/ */
public T getEntityValue(final Attribute<T, Serializable>[] constructorAttrs, final Attribute<T, Serializable>[] unconstructorAttrs, final EntityInfo.DataResultSetRow row) { protected T getEntityValue(final Attribute<T, Serializable>[] constructorAttrs, final Attribute<T, Serializable>[] unconstructorAttrs, final EntityInfo.DataResultSetRow row) {
if (row.wasNull()) { if (row.wasNull()) {
return null; return null;
} }
@@ -254,7 +255,7 @@ public class EntityBuilder<T> {
* *
* @return Entity对象 * @return Entity对象
*/ */
public T getEntityValue(final Attribute<T, Serializable>[] constructorAttrs, final Attribute<T, Serializable>[] unconstructorAttrs, final Serializable... values) { protected T getEntityValue(final Attribute<T, Serializable>[] constructorAttrs, final Attribute<T, Serializable>[] unconstructorAttrs, final Serializable... values) {
if (values == null) { if (values == null) {
return null; return null;
} }
@@ -284,7 +285,7 @@ public class EntityBuilder<T> {
return obj; return obj;
} }
public Serializable getFieldValue(Attribute<T, Serializable> attr, final EntityInfo.DataResultSetRow row, int index) { protected Serializable getFieldValue(Attribute<T, Serializable> attr, final EntityInfo.DataResultSetRow row, int index) {
return row.getObject(attr, index, index > 0 ? null : this.getSQLColumn(null, attr.field())); return row.getObject(attr, index, index > 0 ? null : this.getSQLColumn(null, attr.field()));
} }
@@ -305,6 +306,17 @@ public class EntityBuilder<T> {
return constructorAttributes != null; return constructorAttributes != null;
} }
/**
* 判断Entity类的字段名与表字段名s是否存在不一致的值
*
* @return boolean
*/
@ConvertDisabled
public boolean isNoAlias() {
return this.aliasmap == null;
}
@ConvertDisabled
public Creator<T> getCreator() { public Creator<T> getCreator() {
return creator; return creator;
} }

View File

@@ -88,10 +88,6 @@ public final class EntityInfo<T> {
//存放所有与数据库对应的字段, 包括主键 //存放所有与数据库对应的字段, 包括主键
final Attribute<T, Serializable>[] attributes; final Attribute<T, Serializable>[] attributes;
//key是field的name value是Column的别名即数据库表的字段名
//只有field.name 与 Column.name不同才存放在aliasmap里.
private final Map<String, String> aliasmap;
//所有可更新字段,即排除了主键字段和标记为&#064;Column(updatable=false)的字段 //所有可更新字段,即排除了主键字段和标记为&#064;Column(updatable=false)的字段
private final Map<String, Attribute<T, Serializable>> updateAttributeMap = new HashMap<>(); private final Map<String, Attribute<T, Serializable>> updateAttributeMap = new HashMap<>();
@@ -387,7 +383,7 @@ public final class EntityInfo<T> {
} }
String[] constructorParameters = cps; String[] constructorParameters = cps;
Attribute idAttr0 = null; Attribute idAttr0 = null;
Map<String, String> aliasmap0 = null; Map<String, String> aliasmap = null;
Class cltmp = type; Class cltmp = type;
Set<String> fields = new HashSet<>(); Set<String> fields = new HashSet<>();
List<String> queryCols = new ArrayList<>(); List<String> queryCols = new ArrayList<>();
@@ -421,10 +417,10 @@ public final class EntityInfo<T> {
final Column col = field.getAnnotation(Column.class); final Column col = field.getAnnotation(Column.class);
final String sqlField = col == null || col.name().isEmpty() ? fieldName : col.name(); final String sqlField = col == null || col.name().isEmpty() ? fieldName : col.name();
if (!fieldName.equals(sqlField)) { if (!fieldName.equals(sqlField)) {
if (aliasmap0 == null) { if (aliasmap == null) {
aliasmap0 = new HashMap<>(); aliasmap = new HashMap<>();
} }
aliasmap0.put(fieldName, sqlField); aliasmap.put(fieldName, sqlField);
} }
Attribute attr; Attribute attr;
try { try {
@@ -494,7 +490,6 @@ public final class EntityInfo<T> {
this.primary = idAttr0; this.primary = idAttr0;
this.primaryOneArray = new Attribute[]{this.primary}; this.primaryOneArray = new Attribute[]{this.primary};
this.aliasmap = aliasmap0;
List<EntityColumn> ddls = new ArrayList<>(); List<EntityColumn> ddls = new ArrayList<>();
Collections.reverse(ddlList); //父类的字段排在前面 Collections.reverse(ddlList); //父类的字段排在前面
for (List<EntityColumn> ls : ddlList) { for (List<EntityColumn> ls : ddlList) {
@@ -1323,15 +1318,6 @@ public final class EntityInfo<T> {
return this.updateAttributeMap.get(fieldname); return this.updateAttributeMap.get(fieldname);
} }
/**
* 判断Entity类的字段名与表字段名s是否存在不一致的值
*
* @return boolean
*/
public boolean isNoAlias() {
return this.aliasmap == null;
}
/** /**
* 根据Flipper获取ORDER BY的SQL语句不存在Flipper或sort字段返回空字符串 * 根据Flipper获取ORDER BY的SQL语句不存在Flipper或sort字段返回空字符串
* *
@@ -1353,7 +1339,7 @@ public final class EntityInfo<T> {
} }
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
sb.append(" ORDER BY "); sb.append(" ORDER BY ");
if (isNoAlias()) { if (builder.isNoAlias()) {
sb.append(sort); sb.append(sort);
} else { } else {
boolean flag = false; boolean flag = false;
@@ -1387,8 +1373,7 @@ public final class EntityInfo<T> {
* @return String * @return String
*/ */
public String getSQLColumn(String tabalis, String fieldname) { public String getSQLColumn(String tabalis, String fieldname) {
return this.aliasmap == null ? (tabalis == null ? fieldname : (tabalis + '.' + fieldname)) return builder.getSQLColumn(tabalis, fieldname);
: (tabalis == null ? aliasmap.getOrDefault(fieldname, fieldname) : (tabalis + '.' + aliasmap.getOrDefault(fieldname, fieldname)));
} }
/** /**