Skip to content

Commit

Permalink
【ISSUE #12911】将for循环拼接的SQL语句改为Lambda表达式拼接,使得代码简洁清晰,并添加相关测试用例 (#12914)
Browse files Browse the repository at this point in the history
* feat(优化抽象类AbstractMapper) 将for循环拼接的SQL语句改为Lambda表达式拼接,使得代码简洁清晰,并添加相关测试用例

* [ISSUE #12914] 优化抽象类AbstractMapper,将for循环拼接的SQL语句改为Lambda表达式拼接,使得代码简洁清晰,并添加相关测试用例

* [ISSUE #12911] resolve code conflict
  • Loading branch information
20144101037 authored Dec 6, 2024
1 parent d4f5421 commit cb3685d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -33,17 +34,9 @@ public String select(List<String> columns, List<String> 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();
Expand Down Expand Up @@ -111,7 +104,6 @@ public String update(List<String> columns, List<String> where) {
return sql.toString();
}

sql.append(" ");
appendWhereClause(where, sql);

return sql.toString();
Expand All @@ -121,7 +113,7 @@ public String update(List<String> columns, List<String> where) {
public String delete(List<String> 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();
Expand All @@ -134,9 +126,8 @@ public String count(List<String> 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();
}

Expand All @@ -150,13 +141,8 @@ public String[] getPrimaryKeyGeneratedKeys() {
return new String[]{"id"};
}

protected void appendWhereClause(List<String> 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<String> where, StringBuilder sql) {
sql.append(" WHERE ");
sql.append(where.stream().map(str -> (str + " = ?")).collect(Collectors.joining(" AND ")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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() {
Expand All @@ -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);
}
}

0 comments on commit cb3685d

Please sign in to comment.