mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
feat: add UpdateWrapper
This commit is contained in:
parent
6d452b0c06
commit
e7fc09922b
@ -24,6 +24,7 @@ import com.mybatisflex.core.query.*;
|
|||||||
import com.mybatisflex.core.row.Row;
|
import com.mybatisflex.core.row.Row;
|
||||||
import com.mybatisflex.core.row.RowCPI;
|
import com.mybatisflex.core.row.RowCPI;
|
||||||
import com.mybatisflex.core.table.TableInfo;
|
import com.mybatisflex.core.table.TableInfo;
|
||||||
|
import com.mybatisflex.core.update.RawValue;
|
||||||
import com.mybatisflex.core.util.ArrayUtil;
|
import com.mybatisflex.core.util.ArrayUtil;
|
||||||
import com.mybatisflex.core.util.CollectionUtil;
|
import com.mybatisflex.core.util.CollectionUtil;
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
@ -71,12 +72,12 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
StringBuilder fields = new StringBuilder();
|
StringBuilder fields = new StringBuilder();
|
||||||
StringBuilder questions = new StringBuilder();
|
StringBuilder questions = new StringBuilder();
|
||||||
|
|
||||||
Set<String> attrs = row.obtainModifyAttrs();
|
Set<String> modifyAttrs = RowCPI.getModifyAttrs(row);
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (String attr : attrs) {
|
for (String attr : modifyAttrs) {
|
||||||
fields.append(wrap(attr));
|
fields.append(wrap(attr));
|
||||||
questions.append(PLACEHOLDER);
|
questions.append(PLACEHOLDER);
|
||||||
if (index != attrs.size() - 1) {
|
if (index != modifyAttrs.size() - 1) {
|
||||||
fields.append(DELIMITER);
|
fields.append(DELIMITER);
|
||||||
questions.append(DELIMITER);
|
questions.append(DELIMITER);
|
||||||
}
|
}
|
||||||
@ -100,7 +101,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
StringBuilder questions = new StringBuilder();
|
StringBuilder questions = new StringBuilder();
|
||||||
|
|
||||||
Row firstRow = rows.get(0);
|
Row firstRow = rows.get(0);
|
||||||
Set<String> attrs = firstRow.obtainModifyAttrs();
|
Set<String> attrs = RowCPI.getModifyAttrs(firstRow);
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (String column : attrs) {
|
for (String column : attrs) {
|
||||||
fields.append(wrap(column));
|
fields.append(wrap(column));
|
||||||
@ -198,7 +199,8 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
public String forUpdateById(String schema, String tableName, Row row) {
|
public String forUpdateById(String schema, String tableName, Row row) {
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
|
|
||||||
Set<String> modifyAttrs = row.obtainModifyAttrs();
|
Set<String> modifyAttrs = RowCPI.getModifyAttrs(row);
|
||||||
|
Map<String, RawValue> rawValueMap = RowCPI.getRawValueMap(row);
|
||||||
String[] primaryKeys = RowCPI.obtainsPrimaryKeyStrings(row);
|
String[] primaryKeys = RowCPI.obtainsPrimaryKeyStrings(row);
|
||||||
|
|
||||||
sql.append(UPDATE);
|
sql.append(UPDATE);
|
||||||
@ -213,7 +215,14 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
if (index > 0) {
|
if (index > 0) {
|
||||||
sql.append(DELIMITER);
|
sql.append(DELIMITER);
|
||||||
}
|
}
|
||||||
sql.append(wrap(colName)).append(EQUALS_PLACEHOLDER);
|
sql.append(wrap(colName));
|
||||||
|
|
||||||
|
if (rawValueMap.containsKey(colName)) {
|
||||||
|
sql.append(EQUALS).append(rawValueMap.get(colName).toSql(this));
|
||||||
|
} else {
|
||||||
|
sql.append(EQUALS_PLACEHOLDER);
|
||||||
|
}
|
||||||
|
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -232,7 +241,8 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
public String forUpdateByQuery(QueryWrapper queryWrapper, Row row) {
|
public String forUpdateByQuery(QueryWrapper queryWrapper, Row row) {
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
|
|
||||||
Set<String> modifyAttrs = row.obtainModifyAttrs();
|
Set<String> modifyAttrs = RowCPI.getModifyAttrs(row);
|
||||||
|
Map<String, RawValue> rawValueMap = RowCPI.getRawValueMap(row);
|
||||||
|
|
||||||
List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper);
|
List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper);
|
||||||
if (queryTables == null || queryTables.size() != 1) {
|
if (queryTables == null || queryTables.size() != 1) {
|
||||||
@ -247,7 +257,15 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
if (index > 0) {
|
if (index > 0) {
|
||||||
sql.append(DELIMITER);
|
sql.append(DELIMITER);
|
||||||
}
|
}
|
||||||
sql.append(wrap(modifyAttr)).append(EQUALS_PLACEHOLDER);
|
|
||||||
|
sql.append(wrap(modifyAttr));
|
||||||
|
|
||||||
|
if (rawValueMap.containsKey(modifyAttr)) {
|
||||||
|
sql.append(EQUALS).append(rawValueMap.get(modifyAttr).toSql(this));
|
||||||
|
} else {
|
||||||
|
sql.append(EQUALS_PLACEHOLDER);
|
||||||
|
}
|
||||||
|
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -690,15 +708,20 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
public String forUpdateEntity(TableInfo tableInfo, Object entity, boolean ignoreNulls) {
|
public String forUpdateEntity(TableInfo tableInfo, Object entity, boolean ignoreNulls) {
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
|
|
||||||
Set<String> modifyAttrs = tableInfo.obtainUpdateColumns(entity, ignoreNulls, false);
|
Set<String> updateColumns = tableInfo.obtainUpdateColumns(entity, ignoreNulls, false);
|
||||||
|
Map<String, RawValue> rawValueMap = tableInfo.obtainUpdateRawValueMap(entity);
|
||||||
String[] primaryKeys = tableInfo.getPrimaryKeys();
|
String[] primaryKeys = tableInfo.getPrimaryKeys();
|
||||||
|
|
||||||
sql.append(UPDATE).append(tableInfo.getWrapSchemaAndTableName(this)).append(SET);
|
sql.append(UPDATE).append(tableInfo.getWrapSchemaAndTableName(this)).append(SET);
|
||||||
|
|
||||||
StringJoiner stringJoiner = new StringJoiner(DELIMITER);
|
StringJoiner stringJoiner = new StringJoiner(DELIMITER);
|
||||||
|
|
||||||
for (String modifyAttr : modifyAttrs) {
|
for (String modifyAttr : updateColumns) {
|
||||||
stringJoiner.add(wrap(modifyAttr) + EQUALS_PLACEHOLDER);
|
if (rawValueMap.containsKey(modifyAttr)) {
|
||||||
|
stringJoiner.add(wrap(modifyAttr)).add(EQUALS).add(rawValueMap.get(modifyAttr).toSql(this));
|
||||||
|
} else {
|
||||||
|
stringJoiner.add(wrap(modifyAttr) + EQUALS_PLACEHOLDER);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, String> onUpdateColumns = tableInfo.getOnUpdateColumns();
|
Map<String, String> onUpdateColumns = tableInfo.getOnUpdateColumns();
|
||||||
@ -756,17 +779,23 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
public String forUpdateEntityByQuery(TableInfo tableInfo, Object entity, boolean ignoreNulls, QueryWrapper queryWrapper) {
|
public String forUpdateEntityByQuery(TableInfo tableInfo, Object entity, boolean ignoreNulls, QueryWrapper queryWrapper) {
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
|
|
||||||
Set<String> modifyAttrs = tableInfo.obtainUpdateColumns(entity, ignoreNulls, true);
|
Set<String> updateColumns = tableInfo.obtainUpdateColumns(entity, ignoreNulls, true);
|
||||||
|
Map<String, RawValue> rawValueMap = tableInfo.obtainUpdateRawValueMap(entity);
|
||||||
|
|
||||||
sql.append(UPDATE).append(forHint(CPI.getHint(queryWrapper)))
|
sql.append(UPDATE).append(forHint(CPI.getHint(queryWrapper)))
|
||||||
.append(tableInfo.getWrapSchemaAndTableName(this)).append(SET);
|
.append(tableInfo.getWrapSchemaAndTableName(this)).append(SET);
|
||||||
|
|
||||||
StringJoiner stringJoiner = new StringJoiner(DELIMITER);
|
StringJoiner stringJoiner = new StringJoiner(DELIMITER);
|
||||||
|
|
||||||
for (String modifyAttr : modifyAttrs) {
|
for (String modifyAttr : updateColumns) {
|
||||||
stringJoiner.add(wrap(modifyAttr) + EQUALS_PLACEHOLDER);
|
if (rawValueMap.containsKey(modifyAttr)) {
|
||||||
|
stringJoiner.add(wrap(modifyAttr)).add(EQUALS).add(rawValueMap.get(modifyAttr).toSql(this));
|
||||||
|
} else {
|
||||||
|
stringJoiner.add(wrap(modifyAttr) + EQUALS_PLACEHOLDER);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Map<String, String> onUpdateColumns = tableInfo.getOnUpdateColumns();
|
Map<String, String> onUpdateColumns = tableInfo.getOnUpdateColumns();
|
||||||
if (onUpdateColumns != null && !onUpdateColumns.isEmpty()) {
|
if (onUpdateColumns != null && !onUpdateColumns.isEmpty()) {
|
||||||
onUpdateColumns.forEach((column, value) -> stringJoiner.add(wrap(column) + EQUALS + value));
|
onUpdateColumns.forEach((column, value) -> stringJoiner.add(wrap(column) + EQUALS + value));
|
||||||
|
|||||||
@ -19,6 +19,7 @@ import com.mybatisflex.core.constant.SqlConsts;
|
|||||||
import com.mybatisflex.core.dialect.KeywordWrap;
|
import com.mybatisflex.core.dialect.KeywordWrap;
|
||||||
import com.mybatisflex.core.dialect.LimitOffsetProcessor;
|
import com.mybatisflex.core.dialect.LimitOffsetProcessor;
|
||||||
import com.mybatisflex.core.row.Row;
|
import com.mybatisflex.core.row.Row;
|
||||||
|
import com.mybatisflex.core.row.RowCPI;
|
||||||
import com.mybatisflex.core.table.TableInfo;
|
import com.mybatisflex.core.table.TableInfo;
|
||||||
import com.mybatisflex.core.util.CollectionUtil;
|
import com.mybatisflex.core.util.CollectionUtil;
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
@ -150,7 +151,7 @@ public class OracleDialect extends CommonsDialectImpl {
|
|||||||
*/
|
*/
|
||||||
StringBuilder fields = new StringBuilder();
|
StringBuilder fields = new StringBuilder();
|
||||||
Row firstRow = rows.get(0);
|
Row firstRow = rows.get(0);
|
||||||
Set<String> attrs = firstRow.obtainModifyAttrs();
|
Set<String> attrs = RowCPI.getModifyAttrs(firstRow);
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (String column : attrs) {
|
for (String column : attrs) {
|
||||||
fields.append(wrap(column));
|
fields.append(wrap(column));
|
||||||
|
|||||||
@ -1,52 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
|
||||||
* <p>
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
* <p>
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* <p>
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package com.mybatisflex.core.javassist;
|
|
||||||
|
|
||||||
import org.apache.ibatis.javassist.util.proxy.ProxyObject;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public interface ModifyAttrsRecord extends Serializable {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 注意:
|
|
||||||
* 对于 entity 来说,这里存放的只是 属性的名称,而非字段
|
|
||||||
* 对于 row 来说,存放的则是 字段 名称
|
|
||||||
*/
|
|
||||||
default Set<String> getModifyAttrs(){
|
|
||||||
ModifyAttrsRecordHandler handler = (ModifyAttrsRecordHandler) ((ProxyObject) this).getHandler();
|
|
||||||
return handler.getModifyAttrs();
|
|
||||||
}
|
|
||||||
|
|
||||||
default void addModifyAttr(String attr) {
|
|
||||||
getModifyAttrs().add(attr);
|
|
||||||
}
|
|
||||||
|
|
||||||
default void removeModifyAttr(String attr) {
|
|
||||||
getModifyAttrs().remove(attr);
|
|
||||||
}
|
|
||||||
|
|
||||||
default Set<String> obtainModifyAttrs() {
|
|
||||||
return getModifyAttrs();
|
|
||||||
}
|
|
||||||
|
|
||||||
default void clearModifyFlag() {
|
|
||||||
getModifyAttrs().clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -87,8 +87,8 @@ public class RowSqlProvider {
|
|||||||
|
|
||||||
// 让所有 row 的列顺序和值的数量与第条数据保持一致
|
// 让所有 row 的列顺序和值的数量与第条数据保持一致
|
||||||
// 这个必须 new 一个 LinkedHashSet,因为 keepModifyAttrs 会清除 row 所有的 modifyAttrs
|
// 这个必须 new 一个 LinkedHashSet,因为 keepModifyAttrs 会清除 row 所有的 modifyAttrs
|
||||||
Set<String> modifyAttrs = new LinkedHashSet<>(rows.get(0).obtainModifyAttrs());
|
Set<String> modifyAttrs = new LinkedHashSet<>(RowCPI.getModifyAttrs(rows.get(0)));
|
||||||
rows.forEach(row -> row.prepareAttrs(modifyAttrs));
|
rows.forEach(row -> RowCPI.resetByAttrs(row, modifyAttrs));
|
||||||
|
|
||||||
|
|
||||||
Object[] values = new Object[]{};
|
Object[] values = new Object[]{};
|
||||||
|
|||||||
@ -16,8 +16,11 @@
|
|||||||
package com.mybatisflex.core.row;
|
package com.mybatisflex.core.row;
|
||||||
|
|
||||||
import com.mybatisflex.core.FlexConsts;
|
import com.mybatisflex.core.FlexConsts;
|
||||||
import com.mybatisflex.core.javassist.ModifyAttrsRecord;
|
|
||||||
import com.mybatisflex.core.query.QueryColumn;
|
import com.mybatisflex.core.query.QueryColumn;
|
||||||
|
import com.mybatisflex.core.query.QueryCondition;
|
||||||
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
|
import com.mybatisflex.core.update.RawValue;
|
||||||
|
import com.mybatisflex.core.update.UpdateWrapper;
|
||||||
import com.mybatisflex.core.util.ArrayUtil;
|
import com.mybatisflex.core.util.ArrayUtil;
|
||||||
import com.mybatisflex.core.util.ConvertUtil;
|
import com.mybatisflex.core.util.ConvertUtil;
|
||||||
import com.mybatisflex.core.util.SqlUtil;
|
import com.mybatisflex.core.util.SqlUtil;
|
||||||
@ -30,7 +33,7 @@ import java.sql.Timestamp;
|
|||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class Row extends LinkedHashMap<String, Object> implements ModifyAttrsRecord {
|
public class Row extends LinkedHashMap<String, Object> implements UpdateWrapper {
|
||||||
|
|
||||||
//主键,多个主键用英文逗号隔开
|
//主键,多个主键用英文逗号隔开
|
||||||
private RowKey[] primaryKeys;
|
private RowKey[] primaryKeys;
|
||||||
@ -40,15 +43,11 @@ public class Row extends LinkedHashMap<String, Object> implements ModifyAttrsRec
|
|||||||
return row.set(key, value);
|
return row.set(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private final Set<String> modifyAttrs = new LinkedHashSet<>();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<String> getModifyAttrs() {
|
public Map<String, Object> getUpdates() {
|
||||||
return modifyAttrs;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static Row ofKey(String primaryKey, Object value) {
|
public static Row ofKey(String primaryKey, Object value) {
|
||||||
Row row = new Row();
|
Row row = new Row();
|
||||||
String[] primaryKeyStrings = primaryKey.split(",");
|
String[] primaryKeyStrings = primaryKey.split(",");
|
||||||
@ -98,6 +97,7 @@ public class Row extends LinkedHashMap<String, Object> implements ModifyAttrsRec
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
public Row set(String column, Object value) {
|
public Row set(String column, Object value) {
|
||||||
if (StringUtil.isBlank(column)) {
|
if (StringUtil.isBlank(column)) {
|
||||||
throw new IllegalArgumentException("key column not be null or empty.");
|
throw new IllegalArgumentException("key column not be null or empty.");
|
||||||
@ -105,28 +105,23 @@ public class Row extends LinkedHashMap<String, Object> implements ModifyAttrsRec
|
|||||||
|
|
||||||
SqlUtil.keepColumnSafely(column);
|
SqlUtil.keepColumnSafely(column);
|
||||||
|
|
||||||
//覆盖 put
|
if (value instanceof QueryWrapper || value instanceof QueryCondition) {
|
||||||
super.put(column, value);
|
setRaw(column, value);
|
||||||
|
} else {
|
||||||
boolean isPrimaryKey = false;
|
super.put(column, value);
|
||||||
if (this.primaryKeys != null) {
|
|
||||||
for (RowKey rowKey : primaryKeys) {
|
|
||||||
if (rowKey.getKeyColumn().equals(column)) {
|
|
||||||
isPrimaryKey = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isPrimaryKey) {
|
|
||||||
addModifyAttr(column);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public Row set(QueryColumn queryColumn, Object value) {
|
public Row set(QueryColumn queryColumn, Object value) {
|
||||||
return set(queryColumn.getName(), value);
|
if (value instanceof QueryWrapper || value instanceof QueryCondition) {
|
||||||
|
seRaw(queryColumn, value);
|
||||||
|
} else {
|
||||||
|
super.put(queryColumn.getName(), value);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -287,7 +282,6 @@ public class Row extends LinkedHashMap<String, Object> implements ModifyAttrsRec
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object remove(Object key) {
|
public Object remove(Object key) {
|
||||||
removeModifyAttr(key.toString());
|
|
||||||
return super.remove(key);
|
return super.remove(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -315,57 +309,54 @@ public class Row extends LinkedHashMap<String, Object> implements ModifyAttrsRec
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void prepareAttrsByKeySet(){
|
|
||||||
this.modifyAttrs.clear();
|
|
||||||
this.modifyAttrs.addAll(keySet());
|
|
||||||
|
|
||||||
if (this.primaryKeys != null){
|
|
||||||
for (RowKey primaryKey : primaryKeys) {
|
|
||||||
this.modifyAttrs.removeIf(s -> s.equalsIgnoreCase(primaryKey.getKeyColumn()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void prepareAttrsByKeySet(RowKey ... primaryKeys){
|
|
||||||
this.modifyAttrs.clear();
|
|
||||||
this.modifyAttrs.addAll(keySet());
|
|
||||||
this.primaryKeys = primaryKeys;
|
|
||||||
|
|
||||||
for (RowKey primaryKey : primaryKeys) {
|
|
||||||
this.modifyAttrs.removeIf(s -> s.equalsIgnoreCase(primaryKey.getKeyColumn()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void prepareAttrs(Collection<String> attrs) {
|
|
||||||
if (attrs == null) {
|
|
||||||
throw new NullPointerException("attrs is null.");
|
|
||||||
}
|
|
||||||
clearModifyFlag();
|
|
||||||
modifyAttrs.addAll(attrs);
|
|
||||||
}
|
|
||||||
|
|
||||||
public RowKey[] getPrimaryKeys() {
|
public RowKey[] getPrimaryKeys() {
|
||||||
return primaryKeys;
|
return primaryKeys;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPrimaryKeys(RowKey... primaryKeys) {
|
public void setPrimaryKeys(RowKey... primaryKeys) {
|
||||||
this.primaryKeys = primaryKeys;
|
this.primaryKeys = primaryKeys;
|
||||||
for (RowKey primaryKey : primaryKeys) {
|
}
|
||||||
this.modifyAttrs.removeIf(s -> s.equalsIgnoreCase(primaryKey.getKeyColumn()));
|
|
||||||
|
Set<String> getModifyAttrs() {
|
||||||
|
int pkCount = primaryKeys != null ? primaryKeys.length : 0;
|
||||||
|
if (pkCount == 0) {
|
||||||
|
return keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Set<String> attrs = new LinkedHashSet<>(keySet());
|
||||||
|
attrs.removeIf(this::isPk);
|
||||||
|
return attrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String,RawValue> getRawValueMap(){
|
||||||
|
Map<String,RawValue> map = new HashMap<>();
|
||||||
|
forEach((s, o) -> {
|
||||||
|
if (o instanceof RawValue){
|
||||||
|
map.put(s, (RawValue) o);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void resetByAttrs(Set<String> resetAttrs) {
|
||||||
|
keySet().removeIf(s -> !resetAttrs.contains(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取修改的值,值需要保持顺序,返回的内容不包含主键的值
|
* 获取修改的值,值需要保持顺序,返回的内容不包含主键的值
|
||||||
*/
|
*/
|
||||||
Object[] obtainModifyValues() {
|
Object[] obtainModifyValuesWithoutPk() {
|
||||||
Object[] values = new Object[modifyAttrs.size()];
|
List<Object> values = new ArrayList<>();
|
||||||
int index = 0;
|
for (String key : keySet()) {
|
||||||
for (String modifyAttr : modifyAttrs) {
|
Object value = get(key);
|
||||||
values[index++] = get(modifyAttr);
|
if (!isPk(key) && !(value instanceof RawValue)) {
|
||||||
|
values.add(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return values;
|
return values.toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -396,9 +387,20 @@ public class Row extends LinkedHashMap<String, Object> implements ModifyAttrsRec
|
|||||||
|
|
||||||
|
|
||||||
Object[] obtainAllModifyValues() {
|
Object[] obtainAllModifyValues() {
|
||||||
return ArrayUtil.concat(obtainModifyValues(), obtainsPrimaryValues());
|
return ArrayUtil.concat(obtainModifyValuesWithoutPk(), obtainsPrimaryValues());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private boolean isPk(String attr) {
|
||||||
|
if (primaryKeys != null && primaryKeys.length > 0) {
|
||||||
|
for (RowKey primaryKey : primaryKeys) {
|
||||||
|
if (primaryKey.keyColumn.equalsIgnoreCase(attr)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,15 +15,21 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.core.row;
|
package com.mybatisflex.core.row;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.update.RawValue;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* cross package invoker
|
* cross package invoker
|
||||||
*/
|
*/
|
||||||
public class RowCPI {
|
public class RowCPI {
|
||||||
|
|
||||||
private RowCPI() {}
|
private RowCPI() {
|
||||||
|
}
|
||||||
|
|
||||||
public static Object[] obtainModifyValues(Row row) {
|
public static Object[] obtainModifyValues(Row row) {
|
||||||
return row.obtainModifyValues();
|
return row.obtainModifyValuesWithoutPk();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String[] obtainsPrimaryKeyStrings(Row row) {
|
public static String[] obtainsPrimaryKeyStrings(Row row) {
|
||||||
@ -42,4 +48,15 @@ public class RowCPI {
|
|||||||
return row.obtainAllModifyValues();
|
return row.obtainAllModifyValues();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Set<String> getModifyAttrs(Row row) {
|
||||||
|
return row.getModifyAttrs();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, RawValue> getRawValueMap(Row row){
|
||||||
|
return row.getRawValueMap();
|
||||||
|
}
|
||||||
|
public static void resetByAttrs(Row row, Set<String> resetAttrs) {
|
||||||
|
row.resetByAttrs(resetAttrs);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,12 +21,13 @@ import com.mybatisflex.core.FlexGlobalConfig;
|
|||||||
import com.mybatisflex.core.constant.SqlConsts;
|
import com.mybatisflex.core.constant.SqlConsts;
|
||||||
import com.mybatisflex.core.dialect.IDialect;
|
import com.mybatisflex.core.dialect.IDialect;
|
||||||
import com.mybatisflex.core.exception.FlexExceptions;
|
import com.mybatisflex.core.exception.FlexExceptions;
|
||||||
import com.mybatisflex.core.javassist.ModifyAttrsRecord;
|
|
||||||
import com.mybatisflex.core.logicdelete.LogicDeleteManager;
|
import com.mybatisflex.core.logicdelete.LogicDeleteManager;
|
||||||
import com.mybatisflex.core.mybatis.TypeHandlerObject;
|
import com.mybatisflex.core.mybatis.TypeHandlerObject;
|
||||||
import com.mybatisflex.core.query.*;
|
import com.mybatisflex.core.query.*;
|
||||||
import com.mybatisflex.core.row.Row;
|
import com.mybatisflex.core.row.Row;
|
||||||
import com.mybatisflex.core.tenant.TenantManager;
|
import com.mybatisflex.core.tenant.TenantManager;
|
||||||
|
import com.mybatisflex.core.update.RawValue;
|
||||||
|
import com.mybatisflex.core.update.UpdateWrapper;
|
||||||
import com.mybatisflex.core.util.*;
|
import com.mybatisflex.core.util.*;
|
||||||
import org.apache.ibatis.mapping.ResultFlag;
|
import org.apache.ibatis.mapping.ResultFlag;
|
||||||
import org.apache.ibatis.mapping.ResultMap;
|
import org.apache.ibatis.mapping.ResultMap;
|
||||||
@ -298,7 +299,8 @@ public class TableInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getColumnByProperty(String property) {
|
public String getColumnByProperty(String property) {
|
||||||
return propertyColumnMapping.get(property);
|
String column = propertyColumnMapping.get(property);
|
||||||
|
return StringUtil.isNotBlank(column) ? column : property;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Class<?>> getAssociationType() {
|
public Map<String, Class<?>> getAssociationType() {
|
||||||
@ -479,6 +481,28 @@ public class TableInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Map<String, RawValue> obtainUpdateRawValueMap(Object entity) {
|
||||||
|
if (!(entity instanceof UpdateWrapper)) {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, Object> updates = ((UpdateWrapper) entity).getUpdates();
|
||||||
|
if (updates.isEmpty()) {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, RawValue> map = new HashMap<>();
|
||||||
|
updates.forEach((key, value) -> {
|
||||||
|
if (value instanceof RawValue) {
|
||||||
|
String column = getColumnByProperty(key);
|
||||||
|
map.put(column, (RawValue) value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取要修改的值
|
* 获取要修改的值
|
||||||
*
|
*
|
||||||
@ -488,13 +512,14 @@ public class TableInfo {
|
|||||||
public Set<String> obtainUpdateColumns(Object entity, boolean ignoreNulls, boolean includePrimary) {
|
public Set<String> obtainUpdateColumns(Object entity, boolean ignoreNulls, boolean includePrimary) {
|
||||||
MetaObject metaObject = EntityMetaObject.forObject(entity, reflectorFactory);
|
MetaObject metaObject = EntityMetaObject.forObject(entity, reflectorFactory);
|
||||||
Set<String> columns = new LinkedHashSet<>(); //需使用 LinkedHashSet 保证 columns 的顺序
|
Set<String> columns = new LinkedHashSet<>(); //需使用 LinkedHashSet 保证 columns 的顺序
|
||||||
if (entity instanceof ModifyAttrsRecord) {
|
if (entity instanceof UpdateWrapper) {
|
||||||
Set<String> properties = ((ModifyAttrsRecord) entity).obtainModifyAttrs();
|
// Set<String> properties = ((UpdateWrapper) entity).getModifyAttrs();
|
||||||
if (properties.isEmpty()) {
|
Map<String, Object> updates = ((UpdateWrapper) entity).getUpdates();
|
||||||
|
if (updates.isEmpty()) {
|
||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
}
|
}
|
||||||
for (String property : properties) {
|
for (String property : updates.keySet()) {
|
||||||
String column = propertyColumnMapping.get(property);
|
String column = getColumnByProperty(property);
|
||||||
if (onUpdateColumns != null && onUpdateColumns.containsKey(column)) {
|
if (onUpdateColumns != null && onUpdateColumns.containsKey(column)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -508,6 +533,7 @@ public class TableInfo {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Object value = updates.get(property);
|
||||||
// ModifyAttrsRecord 忽略 ignoreNulls 的设置
|
// ModifyAttrsRecord 忽略 ignoreNulls 的设置
|
||||||
// Object value = getPropertyValue(metaObject, property);
|
// Object value = getPropertyValue(metaObject, property);
|
||||||
// if (ignoreNulls && value == null) {
|
// if (ignoreNulls && value == null) {
|
||||||
@ -557,15 +583,19 @@ public class TableInfo {
|
|||||||
* @return 数组
|
* @return 数组
|
||||||
*/
|
*/
|
||||||
public Object[] buildUpdateSqlArgs(Object entity, boolean ignoreNulls, boolean includePrimary) {
|
public Object[] buildUpdateSqlArgs(Object entity, boolean ignoreNulls, boolean includePrimary) {
|
||||||
MetaObject metaObject = EntityMetaObject.forObject(entity, reflectorFactory);
|
|
||||||
List<Object> values = new ArrayList<>();
|
List<Object> values = new ArrayList<>();
|
||||||
if (entity instanceof ModifyAttrsRecord) {
|
if (entity instanceof UpdateWrapper) {
|
||||||
Set<String> properties = ((ModifyAttrsRecord) entity).obtainModifyAttrs();
|
Map<String, Object> updates = ((UpdateWrapper) entity).getUpdates();
|
||||||
if (properties.isEmpty()) {
|
if (updates.isEmpty()) {
|
||||||
return values.toArray();
|
return FlexConsts.EMPTY_ARRAY;
|
||||||
}
|
}
|
||||||
for (String property : properties) {
|
// Set<String> properties = (Set<String>) updates;
|
||||||
String column = propertyColumnMapping.get(property);
|
// if (properties.isEmpty()) {
|
||||||
|
// return values.toArray();
|
||||||
|
// }
|
||||||
|
for (String property : updates.keySet()) {
|
||||||
|
String column = getColumnByProperty(property);
|
||||||
if (onUpdateColumns != null && onUpdateColumns.containsKey(column)) {
|
if (onUpdateColumns != null && onUpdateColumns.containsKey(column)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -578,7 +608,19 @@ public class TableInfo {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Object value = buildColumnSqlArg(metaObject, column);
|
Object value = updates.get(property);
|
||||||
|
if (value instanceof RawValue) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value != null) {
|
||||||
|
ColumnInfo columnInfo = columnInfoMapping.get(column);
|
||||||
|
TypeHandler typeHandler = columnInfo.buildTypeHandler();
|
||||||
|
if (typeHandler != null) {
|
||||||
|
value = new TypeHandlerObject(typeHandler, value, columnInfo.getJdbcType());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ModifyAttrsRecord 忽略 ignoreNulls 的设置,
|
// ModifyAttrsRecord 忽略 ignoreNulls 的设置,
|
||||||
// 当使用 ModifyAttrsRecord 时,可以理解为要对字段进行 null 值进行更新,否则没必要使用 ModifyAttrsRecord
|
// 当使用 ModifyAttrsRecord 时,可以理解为要对字段进行 null 值进行更新,否则没必要使用 ModifyAttrsRecord
|
||||||
// if (ignoreNulls && value == null) {
|
// if (ignoreNulls && value == null) {
|
||||||
@ -589,6 +631,8 @@ public class TableInfo {
|
|||||||
}
|
}
|
||||||
// normal entity. not ModifyAttrsRecord
|
// normal entity. not ModifyAttrsRecord
|
||||||
else {
|
else {
|
||||||
|
MetaObject metaObject = EntityMetaObject.forObject(entity, reflectorFactory);
|
||||||
|
|
||||||
for (String column : this.columns) {
|
for (String column : this.columns) {
|
||||||
if (onUpdateColumns != null && onUpdateColumns.containsKey(column)) {
|
if (onUpdateColumns != null && onUpdateColumns.containsKey(column)) {
|
||||||
continue;
|
continue;
|
||||||
@ -915,6 +959,20 @@ public class TableInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// private Object buildColumnSqlArg(Object value, String column) {
|
||||||
|
// ColumnInfo columnInfo = columnInfoMapping.get(column);
|
||||||
|
//// Object value = getPropertyValue(metaObject, columnInfo.property);
|
||||||
|
//
|
||||||
|
// if (value != null) {
|
||||||
|
// TypeHandler typeHandler = columnInfo.buildTypeHandler();
|
||||||
|
// if (typeHandler != null) {
|
||||||
|
// return new TypeHandlerObject(typeHandler, value, columnInfo.getJdbcType());
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// return value;
|
||||||
|
// }
|
||||||
|
|
||||||
private Object buildColumnSqlArg(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);
|
||||||
|
|||||||
@ -13,39 +13,42 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package com.mybatisflex.core.javassist;
|
package com.mybatisflex.core.update;
|
||||||
|
|
||||||
|
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
import org.apache.ibatis.javassist.util.proxy.MethodHandler;
|
import org.apache.ibatis.javassist.util.proxy.MethodHandler;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Set;
|
import java.util.Map;
|
||||||
|
|
||||||
|
class ModifyAttrsRecordHandler implements MethodHandler {
|
||||||
|
|
||||||
public class ModifyAttrsRecordHandler implements MethodHandler {
|
//更新内容
|
||||||
|
private final Map<String, Object> updates = new LinkedHashMap<>();
|
||||||
|
|
||||||
private final Set<String> modifyAttrs = new LinkedHashSet<>();
|
public Map<String, Object> getUpdates() {
|
||||||
|
return updates;
|
||||||
public Set<String> getModifyAttrs() {
|
|
||||||
return modifyAttrs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object invoke(Object self, Method originalMethod, Method proxyMethod, Object[] args) throws Throwable {
|
public Object invoke(Object self, Method originalMethod, Method proxyMethod, Object[] args) throws Throwable {
|
||||||
|
|
||||||
if (originalMethod.getName().startsWith("set")){
|
String methodName = originalMethod.getName();
|
||||||
|
if (methodName.startsWith("set")
|
||||||
|
&& methodName.length() > 3
|
||||||
|
&& Character.isUpperCase(methodName.charAt(3))
|
||||||
|
&& originalMethod.getParameterCount() == 1) {
|
||||||
String property = StringUtil.firstCharToLowerCase(originalMethod.getName().substring(3));
|
String property = StringUtil.firstCharToLowerCase(originalMethod.getName().substring(3));
|
||||||
modifyAttrs.add(property);
|
updates.put(property, args[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return proxyMethod.invoke(self, args);
|
return proxyMethod.invoke(self, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -13,7 +13,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package com.mybatisflex.core.javassist;
|
package com.mybatisflex.core.update;
|
||||||
|
|
||||||
import org.apache.ibatis.javassist.util.proxy.ProxyFactory;
|
import org.apache.ibatis.javassist.util.proxy.ProxyFactory;
|
||||||
import org.apache.ibatis.javassist.util.proxy.ProxyObject;
|
import org.apache.ibatis.javassist.util.proxy.ProxyObject;
|
||||||
@ -37,7 +37,7 @@ public class ModifyAttrsRecordProxyFactory {
|
|||||||
factory.setSuperclass(target);
|
factory.setSuperclass(target);
|
||||||
|
|
||||||
Class<?>[] interfaces = Arrays.copyOf(target.getInterfaces(), target.getInterfaces().length + 1);
|
Class<?>[] interfaces = Arrays.copyOf(target.getInterfaces(), target.getInterfaces().length + 1);
|
||||||
interfaces[interfaces.length - 1] = ModifyAttrsRecord.class;
|
interfaces[interfaces.length - 1] = UpdateWrapper.class;
|
||||||
factory.setInterfaces(interfaces);
|
factory.setInterfaces(interfaces);
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
|
* <p>
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* <p>
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* <p>
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.mybatisflex.core.update;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.dialect.IDialect;
|
||||||
|
import com.mybatisflex.core.query.QueryCondition;
|
||||||
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class RawValue implements Serializable {
|
||||||
|
|
||||||
|
private Object object;
|
||||||
|
|
||||||
|
public RawValue(Object object) {
|
||||||
|
this.object = object;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String toSql(IDialect dialect) {
|
||||||
|
if (object instanceof String) {
|
||||||
|
return (String) object;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (object instanceof QueryWrapper) {
|
||||||
|
return dialect.buildSelectSql((QueryWrapper) object);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (object instanceof QueryCondition) {
|
||||||
|
return ((QueryCondition) object).toSql(null, dialect);
|
||||||
|
}
|
||||||
|
|
||||||
|
return object.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,82 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
|
* <p>
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* <p>
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* <p>
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.mybatisflex.core.update;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.query.QueryColumn;
|
||||||
|
import com.mybatisflex.core.query.QueryCondition;
|
||||||
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
|
import com.mybatisflex.core.util.LambdaGetter;
|
||||||
|
import com.mybatisflex.core.util.LambdaUtil;
|
||||||
|
import org.apache.ibatis.javassist.util.proxy.ProxyObject;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public interface UpdateWrapper extends Serializable {
|
||||||
|
|
||||||
|
default Map<String, Object> getUpdates() {
|
||||||
|
ModifyAttrsRecordHandler handler = (ModifyAttrsRecordHandler) ((ProxyObject) this).getHandler();
|
||||||
|
return handler.getUpdates();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
default UpdateWrapper set(String property, Object value) {
|
||||||
|
if (value instanceof QueryWrapper || value instanceof QueryCondition) {
|
||||||
|
setRaw(property, value);
|
||||||
|
} else {
|
||||||
|
getUpdates().put(property, value);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
default <T> UpdateWrapper set(LambdaGetter<T> getter, Object value) {
|
||||||
|
if (value instanceof QueryWrapper || value instanceof QueryCondition) {
|
||||||
|
seRaw(getter, value);
|
||||||
|
} else {
|
||||||
|
getUpdates().put(LambdaUtil.getFieldName(getter), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
default <T> UpdateWrapper set(QueryColumn queryColumn, Object value) {
|
||||||
|
if (value instanceof QueryWrapper || value instanceof QueryCondition) {
|
||||||
|
seRaw(queryColumn, value);
|
||||||
|
} else {
|
||||||
|
getUpdates().put(queryColumn.getName(), value);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
default UpdateWrapper setRaw(String property, Object value) {
|
||||||
|
getUpdates().put(property, new RawValue(value));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
default <T> UpdateWrapper seRaw(LambdaGetter<T> getter, Object value) {
|
||||||
|
getUpdates().put(LambdaUtil.getFieldName(getter), new RawValue(value));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
default <T> UpdateWrapper seRaw(QueryColumn queryColumn, Object value) {
|
||||||
|
getUpdates().put(queryColumn.getName(), new RawValue(value));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -17,4 +17,4 @@
|
|||||||
/**
|
/**
|
||||||
* 处理部分字段更新。
|
* 处理部分字段更新。
|
||||||
*/
|
*/
|
||||||
package com.mybatisflex.core.javassist;
|
package com.mybatisflex.core.update;
|
||||||
@ -15,7 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.core.util;
|
package com.mybatisflex.core.util;
|
||||||
|
|
||||||
import com.mybatisflex.core.javassist.ModifyAttrsRecordProxyFactory;
|
import com.mybatisflex.core.update.ModifyAttrsRecordProxyFactory;
|
||||||
import com.mybatisflex.core.table.IdInfo;
|
import com.mybatisflex.core.table.IdInfo;
|
||||||
import com.mybatisflex.core.table.TableInfo;
|
import com.mybatisflex.core.table.TableInfo;
|
||||||
import com.mybatisflex.core.table.TableInfoFactory;
|
import com.mybatisflex.core.table.TableInfoFactory;
|
||||||
|
|||||||
@ -92,11 +92,11 @@ public class DbTestStarter {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
// rows.forEach(row2 -> row2.setPrimaryKeys(RowKey.AUTO));
|
// rows.forEach(row2 -> row2.setPrimaryKeys(RowKey.AUTO));
|
||||||
rows.forEach(r -> {
|
// rows.forEach(r -> {
|
||||||
r.prepareAttrsByKeySet();
|
// r.prepareAttrsByKeySet();
|
||||||
r.setPrimaryKeys(RowKey.AUTO);
|
// r.setPrimaryKeys(RowKey.AUTO);
|
||||||
});
|
// });
|
||||||
Db.insertBatch(null,"tb_account", rows, 100);
|
// Db.insertBatch(null,"tb_account", rows, 100);
|
||||||
|
|
||||||
//再次查询全部数据
|
//再次查询全部数据
|
||||||
rows = Db.selectAll(null,"tb_account");
|
rows = Db.selectAll(null,"tb_account");
|
||||||
|
|||||||
@ -44,6 +44,7 @@ public class MapperProxyCacheTestStarter {
|
|||||||
.addMapper(AccountMapper.class)
|
.addMapper(AccountMapper.class)
|
||||||
.start();
|
.start();
|
||||||
|
|
||||||
|
|
||||||
//开启审计功能
|
//开启审计功能
|
||||||
AuditManager.setAuditEnable(true);
|
AuditManager.setAuditEnable(true);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user