From 7ff9d06b35534c9e52c58ccde9224a6120c652a4 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sun, 23 Jul 2023 08:57:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=AD=A3=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=20primaryKeys=20=E5=BA=94=E8=AF=A5=E4=B8=BA=20Set=20?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=EF=BC=8C=E4=B8=BB=E9=94=AE=E4=B9=9F=E4=B8=8D?= =?UTF-8?q?=E5=8F=AF=E9=87=8D=E5=A4=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/core/row/Row.java | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Row.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Row.java index d23301ae..09840852 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Row.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Row.java @@ -21,10 +21,7 @@ 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.ConvertUtil; -import com.mybatisflex.core.util.SqlUtil; -import com.mybatisflex.core.util.StringUtil; +import com.mybatisflex.core.util.*; import java.math.BigDecimal; import java.math.BigInteger; @@ -36,7 +33,7 @@ import java.util.*; public class Row extends LinkedHashMap implements UpdateWrapper { //主键,多个主键用英文逗号隔开 - private RowKey[] primaryKeys; + private Set primaryKeys; public static Row of(String key, Object value) { Row row = new Row(); @@ -51,10 +48,10 @@ public class Row extends LinkedHashMap implements UpdateWrapper public static Row ofKey(String primaryKey, Object value) { Row row = new Row(); String[] primaryKeyStrings = primaryKey.split(","); - row.primaryKeys = new RowKey[primaryKeyStrings.length]; + row.primaryKeys = new HashSet<>(primaryKeyStrings.length); - for (int i = 0; i < primaryKeyStrings.length; i++) { - row.primaryKeys[i] = RowKey.of(primaryKeyStrings[i].trim()); + for (String primaryKeyString : primaryKeyStrings) { + row.primaryKeys.add(RowKey.of(primaryKeyString.trim())); } if (primaryKeyStrings.length > 1 && !value.getClass().isArray()) { @@ -74,14 +71,14 @@ public class Row extends LinkedHashMap implements UpdateWrapper public static Row ofKey(RowKey... rowKeys) { Row row = new Row(); - row.primaryKeys = rowKeys; + row.getPrimaryKeys().addAll(Arrays.asList(rowKeys)); return row; } public static Row ofKey(RowKey rowKey, Object value) { Row row = new Row(); - row.primaryKeys = new RowKey[]{rowKey}; + row.getPrimaryKeys().add(rowKey); row.put(rowKey.keyColumn, value); return row; } @@ -89,7 +86,7 @@ public class Row extends LinkedHashMap implements UpdateWrapper public static Row ofKey(RowKey[] rowKeys, Object[] value) { Row row = new Row(); - row.primaryKeys = rowKeys; + row.getPrimaryKeys().addAll(Arrays.asList(rowKeys)); for (int i = 0; i < rowKeys.length; i++) { row.put(rowKeys[i].keyColumn, value[i]); } @@ -328,11 +325,14 @@ public class Row extends LinkedHashMap implements UpdateWrapper return ret; } - public RowKey[] getPrimaryKeys() { + public Set getPrimaryKeys() { + if (primaryKeys == null) { + primaryKeys = new HashSet<>(); + } return primaryKeys; } - public void setPrimaryKeys(RowKey... primaryKeys) { + public void setPrimaryKeys(Set primaryKeys) { this.primaryKeys = primaryKeys; } @@ -347,7 +347,7 @@ public class Row extends LinkedHashMap implements UpdateWrapper Set getModifyAttrs() { - int pkCount = primaryKeys != null ? primaryKeys.length : 0; + int pkCount = primaryKeys != null ? primaryKeys.size() : 0; if (pkCount == 0) { return keySet(); } @@ -384,26 +384,29 @@ public class Row extends LinkedHashMap implements UpdateWrapper String[] obtainsPrimaryKeyStrings() { - String[] returnKeys = new String[primaryKeys.length]; - for (int i = 0; i < primaryKeys.length; i++) { - returnKeys[i] = primaryKeys[i].keyColumn; + String[] returnKeys = new String[primaryKeys.size()]; + int index = 0; + for (RowKey primaryKey : primaryKeys) { + returnKeys[index++] = primaryKey.keyColumn; } return returnKeys; } RowKey[] obtainsPrimaryKeys() { - return this.primaryKeys; + return getPrimaryKeys().toArray(new RowKey[0]); } Object[] obtainsPrimaryValues() { - if (ArrayUtil.isEmpty(primaryKeys)) { + if (CollectionUtil.isEmpty(primaryKeys)) { return FlexConsts.EMPTY_ARRAY; } - Object[] values = new Object[primaryKeys.length]; - for (int i = 0; i < primaryKeys.length; i++) { - values[i] = get(primaryKeys[i].keyColumn); + Object[] values = new Object[primaryKeys.size()]; + + int index = 0; + for (RowKey primaryKey : primaryKeys) { + values[index++] = get(primaryKey.keyColumn); } return values; } @@ -415,7 +418,7 @@ public class Row extends LinkedHashMap implements UpdateWrapper private boolean isPk(String attr) { - if (primaryKeys != null && primaryKeys.length > 0) { + if (primaryKeys != null && !primaryKeys.isEmpty()) { for (RowKey primaryKey : primaryKeys) { if (primaryKey.keyColumn.equalsIgnoreCase(attr)) { return true;