diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapper.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapper.java index b86e481efc..81c9adc8a8 100644 --- a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapper.java +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapper.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.common.utils.CollectionUtils; import java.util.List; +import java.util.stream.Collectors; /** * The abstract mapper contains CRUD methods. @@ -33,17 +34,9 @@ public String select(List columns, List where) { StringBuilder sql = new StringBuilder(); String method = "SELECT "; sql.append(method); - for (int i = 0; i < columns.size(); i++) { - sql.append(columns.get(i)); - if (i == columns.size() - 1) { - sql.append(" "); - } else { - sql.append(","); - } - } - sql.append("FROM "); + sql.append(columns.stream().collect(Collectors.joining(","))); + sql.append(" FROM "); sql.append(getTableName()); - sql.append(" "); if (CollectionUtils.isEmpty(where)) { return sql.toString(); @@ -111,7 +104,6 @@ public String update(List columns, List where) { return sql.toString(); } - sql.append(" "); appendWhereClause(where, sql); return sql.toString(); @@ -121,7 +113,7 @@ public String update(List columns, List where) { public String delete(List params) { StringBuilder sql = new StringBuilder(); String method = "DELETE "; - sql.append(method).append("FROM ").append(getTableName()).append(" "); + sql.append(method).append("FROM ").append(getTableName()); appendWhereClause(params, sql); return sql.toString(); @@ -134,9 +126,8 @@ public String count(List where) { sql.append(method); sql.append("COUNT(*) FROM "); sql.append(getTableName()); - sql.append(" "); - if (null == where || where.size() == 0) { + if (CollectionUtils.isEmpty(where)) { return sql.toString(); } @@ -150,13 +141,8 @@ public String[] getPrimaryKeyGeneratedKeys() { return new String[]{"id"}; } - protected void appendWhereClause(List where, StringBuilder sql) { - sql.append("WHERE "); - for (int i = 0; i < where.size(); i++) { - sql.append(where.get(i)).append(" = ").append("?"); - if (i != where.size() - 1) { - sql.append(" AND "); - } - } + private void appendWhereClause(List where, StringBuilder sql) { + sql.append(" WHERE "); + sql.append(where.stream().map(str -> (str + " = ?")).collect(Collectors.joining(" AND "))); } } diff --git a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapperTest.java b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapperTest.java index 6531881e78..d2531e40a3 100644 --- a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapperTest.java +++ b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapperTest.java @@ -49,9 +49,15 @@ public String getFunction(String functionName) { } }; } - + + @Test + void testSelectSingleField() { + String sql = abstractMapper.select(Arrays.asList("id"), Arrays.asList("id")); + assertEquals("SELECT id FROM tenant_info WHERE id = ?", sql); + } + @Test - void testSelect() { + public void testSelectMultiField() { String sql = abstractMapper.select(Arrays.asList("id", "name"), Arrays.asList("id")); assertEquals("SELECT id,name FROM tenant_info WHERE id = ?", sql); } @@ -67,12 +73,18 @@ void testUpdate() { String sql = abstractMapper.update(Arrays.asList("id", "name"), Arrays.asList("id")); assertEquals("UPDATE tenant_info SET id = ?,name = ? WHERE id = ?", sql); } - + @Test - void testDelete() { + public void testDeleteSingleField() { String sql = abstractMapper.delete(Arrays.asList("id")); assertEquals("DELETE FROM tenant_info WHERE id = ?", sql); } + + @Test + public void testDeleteMultiField() { + String sql = abstractMapper.delete(Arrays.asList("id", "name")); + assertEquals("DELETE FROM tenant_info WHERE id = ? AND name = ?", sql); + } @Test void testCount() { @@ -89,12 +101,12 @@ void testGetPrimaryKeyGeneratedKeys() { @Test void testSelectAll() { String sql = abstractMapper.select(Arrays.asList("id", "name"), null); - assertEquals("SELECT id,name FROM tenant_info ", sql); + assertEquals("SELECT id,name FROM tenant_info", sql); } @Test void testCountAll() { String sql = abstractMapper.count(null); - assertEquals("SELECT COUNT(*) FROM tenant_info ", sql); + assertEquals("SELECT COUNT(*) FROM tenant_info", sql); } }