mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
feature:rename TableInfo methods
This commit is contained in:
parent
8338e14353
commit
ab4bc2d8c5
@ -562,7 +562,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
|
|
||||||
//乐观锁条件
|
//乐观锁条件
|
||||||
if (StringUtil.isNotBlank(versionColumn)) {
|
if (StringUtil.isNotBlank(versionColumn)) {
|
||||||
Object versionValue = tableInfo.getColumnValue(entity, versionColumn);
|
Object versionValue = tableInfo.buildColumnSqlArg(entity, versionColumn);
|
||||||
if (versionValue == null) {
|
if (versionValue == null) {
|
||||||
throw FlexExceptions.wrap("The version value of entity[%s] must not be null.", entity);
|
throw FlexExceptions.wrap("The version value of entity[%s] must not be null.", entity);
|
||||||
}
|
}
|
||||||
@ -605,7 +605,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
|
|
||||||
//乐观锁条件
|
//乐观锁条件
|
||||||
if (StringUtil.isNotBlank(versionColumn)) {
|
if (StringUtil.isNotBlank(versionColumn)) {
|
||||||
Object versionValue = tableInfo.getColumnValue(entity, versionColumn);
|
Object versionValue = tableInfo.buildColumnSqlArg(entity, versionColumn);
|
||||||
if (versionValue == null) {
|
if (versionValue == null) {
|
||||||
throw FlexExceptions.wrap("The version value of entity[%s] must not be null.", entity);
|
throw FlexExceptions.wrap("The version value of entity[%s] must not be null.", entity);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -64,7 +64,7 @@ public class EntitySqlProvider {
|
|||||||
//设置逻辑删除字段的出初始化数据
|
//设置逻辑删除字段的出初始化数据
|
||||||
tableInfo.initLogicDeleteValueIfNecessary(entity);
|
tableInfo.initLogicDeleteValueIfNecessary(entity);
|
||||||
|
|
||||||
Object[] values = tableInfo.obtainInsertValues(entity);
|
Object[] values = tableInfo.buildInsertSqlArgs(entity);
|
||||||
ProviderUtil.setSqlArgs(params, values);
|
ProviderUtil.setSqlArgs(params, values);
|
||||||
|
|
||||||
return DialectFactory.getDialect().forInsertEntity(tableInfo, entity);
|
return DialectFactory.getDialect().forInsertEntity(tableInfo, entity);
|
||||||
@ -94,7 +94,7 @@ public class EntitySqlProvider {
|
|||||||
|
|
||||||
Object[] values = new Object[0];
|
Object[] values = new Object[0];
|
||||||
for (Object entity : entities) {
|
for (Object entity : entities) {
|
||||||
values = ArrayUtil.concat(values, tableInfo.obtainInsertValues(entity));
|
values = ArrayUtil.concat(values, tableInfo.buildInsertSqlArgs(entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
ProviderUtil.setSqlArgs(params, values);
|
ProviderUtil.setSqlArgs(params, values);
|
||||||
@ -185,8 +185,9 @@ public class EntitySqlProvider {
|
|||||||
boolean ignoreNulls = ProviderUtil.isIgnoreNulls(params);
|
boolean ignoreNulls = ProviderUtil.isIgnoreNulls(params);
|
||||||
|
|
||||||
TableInfo tableInfo = ProviderUtil.getTableInfo(context);
|
TableInfo tableInfo = ProviderUtil.getTableInfo(context);
|
||||||
Object[] updateValues = tableInfo.obtainUpdateValues(entity, ignoreNulls, false);
|
|
||||||
Object[] primaryValues = tableInfo.obtainPrimaryValues(entity);
|
Object[] updateValues = tableInfo.buildUpdateSqlArgs(entity, ignoreNulls, false);
|
||||||
|
Object[] primaryValues = tableInfo.buildPkSqlArgs(entity);
|
||||||
|
|
||||||
FlexExceptions.assertAreNotNull(primaryValues, "The value of primary key must not be null, entity[%s]", entity);
|
FlexExceptions.assertAreNotNull(primaryValues, "The value of primary key must not be null, entity[%s]", entity);
|
||||||
|
|
||||||
@ -219,7 +220,7 @@ public class EntitySqlProvider {
|
|||||||
queryWrapper.and(QueryCondition.create(new QueryColumn(tableInfo.getTableName(), logicDeleteColumn), 0));
|
queryWrapper.and(QueryCondition.create(new QueryColumn(tableInfo.getTableName(), logicDeleteColumn), 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
Object[] values = tableInfo.obtainUpdateValues(entity, ignoreNulls, true);
|
Object[] values = tableInfo.buildUpdateSqlArgs(entity, ignoreNulls, true);
|
||||||
|
|
||||||
ProviderUtil.setSqlArgs(params, ArrayUtil.concat(values, CPI.getValueArray(queryWrapper)));
|
ProviderUtil.setSqlArgs(params, ArrayUtil.concat(values, CPI.getValueArray(queryWrapper)));
|
||||||
|
|
||||||
|
|||||||
@ -209,6 +209,7 @@ public class TableInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public List<IdInfo> getPrimaryKeyList() {
|
public List<IdInfo> getPrimaryKeyList() {
|
||||||
return primaryKeyList;
|
return primaryKeyList;
|
||||||
}
|
}
|
||||||
@ -242,20 +243,20 @@ public class TableInfo {
|
|||||||
return ArrayUtil.concat(insertPrimaryKeys, columns);
|
return ArrayUtil.concat(insertPrimaryKeys, columns);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据 插入字段 获取所有插入的值
|
* 构建 insert 的 Sql 参数
|
||||||
*
|
|
||||||
* @param entity 从 entity 中获取
|
* @param entity 从 entity 中获取
|
||||||
* @return 数组
|
* @return 数组
|
||||||
*/
|
*/
|
||||||
public Object[] obtainInsertValues(Object entity) {
|
public Object[] buildInsertSqlArgs(Object entity) {
|
||||||
MetaObject metaObject = EntityMetaObject.forObject(entity, reflectorFactory);
|
MetaObject metaObject = EntityMetaObject.forObject(entity, reflectorFactory);
|
||||||
String[] insertColumns = obtainInsertColumns();
|
String[] insertColumns = obtainInsertColumns();
|
||||||
|
|
||||||
List<Object> values = new ArrayList<>(insertColumns.length);
|
List<Object> values = new ArrayList<>(insertColumns.length);
|
||||||
for (String insertColumn : insertColumns) {
|
for (String insertColumn : insertColumns) {
|
||||||
if (onInsertColumns == null || !onInsertColumns.containsKey(insertColumn)) {
|
if (onInsertColumns == null || !onInsertColumns.containsKey(insertColumn)) {
|
||||||
Object value = getColumnValue(metaObject, insertColumn);
|
Object value = buildColumnSqlArg(metaObject, insertColumn);
|
||||||
values.add(value);
|
values.add(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -279,7 +280,7 @@ public class TableInfo {
|
|||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
}
|
}
|
||||||
for (String property : properties) {
|
for (String property : properties) {
|
||||||
String column = getColumnByProperty(property);
|
String column = propertyColumnMapping.get(property);
|
||||||
if (onUpdateColumns != null && onUpdateColumns.containsKey(column)) {
|
if (onUpdateColumns != null && onUpdateColumns.containsKey(column)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -311,7 +312,7 @@ public class TableInfo {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Object value = getColumnValue(metaObject, column);
|
Object value = buildColumnSqlArg(metaObject, column);
|
||||||
if (ignoreNulls && value == null) {
|
if (ignoreNulls && value == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -338,7 +339,7 @@ public class TableInfo {
|
|||||||
* @param entity 实体对象
|
* @param entity 实体对象
|
||||||
* @return 数组
|
* @return 数组
|
||||||
*/
|
*/
|
||||||
public Object[] obtainUpdateValues(Object entity, boolean ignoreNulls, boolean includePrimary) {
|
public Object[] buildUpdateSqlArgs(Object entity, boolean ignoreNulls, boolean includePrimary) {
|
||||||
MetaObject metaObject = EntityMetaObject.forObject(entity, reflectorFactory);
|
MetaObject metaObject = EntityMetaObject.forObject(entity, reflectorFactory);
|
||||||
List<Object> values = new ArrayList<>();
|
List<Object> values = new ArrayList<>();
|
||||||
if (entity instanceof ModifyAttrsRecord) {
|
if (entity instanceof ModifyAttrsRecord) {
|
||||||
@ -347,7 +348,7 @@ public class TableInfo {
|
|||||||
return values.toArray();
|
return values.toArray();
|
||||||
}
|
}
|
||||||
for (String property : properties) {
|
for (String property : properties) {
|
||||||
String column = getColumnByProperty(property);
|
String column = propertyColumnMapping.get(property);
|
||||||
if (onUpdateColumns != null && onUpdateColumns.containsKey(column)) {
|
if (onUpdateColumns != null && onUpdateColumns.containsKey(column)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -373,12 +374,12 @@ public class TableInfo {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//过滤乐观锁字段
|
//忽略乐观锁字段,乐观锁字段会直接通过 sql 对其操作
|
||||||
if (Objects.equals(column, versionColumn)) {
|
if (Objects.equals(column, versionColumn)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Object value = getColumnValue(metaObject, column);
|
Object value = buildColumnSqlArg(metaObject, column);
|
||||||
if (ignoreNulls && value == null) {
|
if (ignoreNulls && value == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -393,11 +394,15 @@ public class TableInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Object[] obtainPrimaryValues(Object entity) {
|
/**
|
||||||
|
* 构建主键的 sql 参数数据
|
||||||
|
* @param entity
|
||||||
|
*/
|
||||||
|
public Object[] buildPkSqlArgs(Object entity) {
|
||||||
MetaObject metaObject = EntityMetaObject.forObject(entity, reflectorFactory);
|
MetaObject metaObject = EntityMetaObject.forObject(entity, reflectorFactory);
|
||||||
Object[] values = new Object[primaryKeys.length];
|
Object[] values = new Object[primaryKeys.length];
|
||||||
for (int i = 0; i < primaryKeys.length; i++) {
|
for (int i = 0; i < primaryKeys.length; i++) {
|
||||||
values[i] = getColumnValue(metaObject, primaryKeys[i]);
|
values[i] = buildColumnSqlArg(metaObject, primaryKeys[i]);
|
||||||
}
|
}
|
||||||
return values;
|
return values;
|
||||||
}
|
}
|
||||||
@ -445,7 +450,7 @@ public class TableInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private Object getColumnValue(MetaObject metaObject, String column) {
|
private Object buildColumnSqlArg(MetaObject metaObject, String column) {
|
||||||
ColumnInfo columnInfo = columnInfoMapping.get(column);
|
ColumnInfo columnInfo = columnInfoMapping.get(column);
|
||||||
Object value = getPropertyValue(metaObject, columnInfo.property);
|
Object value = getPropertyValue(metaObject, columnInfo.property);
|
||||||
|
|
||||||
@ -458,9 +463,9 @@ public class TableInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Object getColumnValue(Object entityObject, String column) {
|
public Object buildColumnSqlArg(Object entityObject, String column) {
|
||||||
MetaObject metaObject = EntityMetaObject.forObject(entityObject, reflectorFactory);
|
MetaObject metaObject = EntityMetaObject.forObject(entityObject, reflectorFactory);
|
||||||
return getColumnValue(metaObject, column);
|
return buildColumnSqlArg(metaObject, column);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -472,10 +477,6 @@ public class TableInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String getColumnByProperty(String property) {
|
|
||||||
return propertyColumnMapping.get(property);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过 row 实例类转换为一个 entity
|
* 通过 row 实例类转换为一个 entity
|
||||||
@ -494,6 +495,7 @@ public class TableInfo {
|
|||||||
return (T) instance;
|
return (T) instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化乐观锁版本号
|
* 初始化乐观锁版本号
|
||||||
*
|
*
|
||||||
@ -505,7 +507,7 @@ public class TableInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
MetaObject metaObject = EntityMetaObject.forObject(entityObject, reflectorFactory);
|
MetaObject metaObject = EntityMetaObject.forObject(entityObject, reflectorFactory);
|
||||||
Object columnValue = getColumnValue(entityObject, versionColumn);
|
Object columnValue = buildColumnSqlArg(entityObject, versionColumn);
|
||||||
if (columnValue == null) {
|
if (columnValue == null) {
|
||||||
metaObject.setValue(columnInfoMapping.get(versionColumn).property, 0);
|
metaObject.setValue(columnInfoMapping.get(versionColumn).property, 0);
|
||||||
}
|
}
|
||||||
@ -522,7 +524,7 @@ public class TableInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
MetaObject metaObject = EntityMetaObject.forObject(entityObject, reflectorFactory);
|
MetaObject metaObject = EntityMetaObject.forObject(entityObject, reflectorFactory);
|
||||||
Object columnValue = getColumnValue(entityObject, logicDeleteColumn);
|
Object columnValue = buildColumnSqlArg(entityObject, logicDeleteColumn);
|
||||||
if (columnValue == null) {
|
if (columnValue == null) {
|
||||||
metaObject.setValue(columnInfoMapping.get(logicDeleteColumn).property, 0);
|
metaObject.setValue(columnInfoMapping.get(logicDeleteColumn).property, 0);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user