refactor: optimize

This commit is contained in:
开源海哥 2023-07-19 17:34:49 +08:00
parent 8c39797eac
commit 198569e743
5 changed files with 22 additions and 14 deletions

View File

@ -36,12 +36,14 @@ public class QueryTable implements CloneSupport<QueryTable> {
}
public QueryTable(TableDef tableDef) {
this.name = tableDef.getTableName();
this.schema = tableDef.getSchema();
this.name = tableDef.getTableName();
}
public QueryTable(String name) {
this.name = StringUtil.tryTrim(name);
String[] schemaAndTableName = StringUtil.getSchemaAndTableName(name);
this.schema = schemaAndTableName[0];
this.name = schemaAndTableName[1];
}
public QueryTable(String schema, String name) {

View File

@ -35,6 +35,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
/**
* 根据实体类对象构建查询条件
*
* @param entity 实体类对象
* @return 查询对象
*/
@ -133,14 +134,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
if (StringUtil.isBlank(table)) {
throw new IllegalArgumentException("table must not be null or blank.");
}
int indexOf = table.indexOf(".");
if (indexOf > 0) {
String schema = table.substring(0, indexOf);
table = table.substring(indexOf + 1);
from(new QueryTable(schema, table));
} else {
from(new QueryTable(table));
}
from(new QueryTable(table));
}
return this;
}
@ -694,7 +688,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
}
List<QueryWrapper> childQueryWrappers = tableChildQuery == null ? new ArrayList<>()
: new ArrayList<>(tableChildQuery);
: new ArrayList<>(tableChildQuery);
childQueryWrappers.addAll(whereChildQuery);
childQueryWrappers.addAll(havingChildQuery);

View File

@ -66,7 +66,7 @@ public class TableDefs implements Serializable {
return Modifier.isPublic(mod) && Modifier.isStatic(mod);
}).get(null);
String key = StringUtil.isNotBlank(tableDef.getSchema()) ? tableDef.getSchema() + "." + tableDef.getTableName() : tableDef.getTableName();
String key = StringUtil.buildSchemaWithTable(tableDef.getSchema(), tableDef.getTableName());
TABLE_DEF_MAP.put(key, tableDef);

View File

@ -137,7 +137,7 @@ public class TableInfo {
}
public String getTableNameWithSchema() {
return StringUtil.isNotBlank(schema) ? schema + "." + tableName : tableName;
return StringUtil.buildSchemaWithTable(schema, tableName);
}
public String getWrapSchemaAndTableName(IDialect dialect) {
@ -863,7 +863,7 @@ public class TableInfo {
MetaObject metaObject = EntityMetaObject.forObject(entity, reflectorFactory);
propertyColumnMapping.forEach((property, column) -> {
if (column.equals(logicDeleteColumn)){
if (column.equals(logicDeleteColumn)) {
return;
}
Object value = metaObject.getValue(property);
@ -871,6 +871,8 @@ public class TableInfo {
QueryColumn queryColumn = TableDefs.getQueryColumn(entityClass, tableNameWithSchema, column);
if (queryColumn != null) {
queryWrapper.and(queryColumn.eq(value));
} else {
queryWrapper.and(QueryMethods.column(tableNameWithSchema, column).eq(value));
}
}
});

View File

@ -273,6 +273,16 @@ public class StringUtil {
}
}
public static String buildSchemaWithTable(String schema, String tableName) {
return isNotBlank(schema) ? schema + "." + tableName : tableName;
}
public static String[] getSchemaAndTableName(String tableNameWithSchema) {
int index = tableNameWithSchema.indexOf(".");
return index <= 0 ? new String[]{null, tableNameWithSchema.trim()}
: new String[]{tableNameWithSchema.substring(0, index).trim(), tableNameWithSchema.substring(index + 1).trim()};
}
public static String tryTrim(String string) {
return string != null ? string.trim() : null;
}