From 21f0bab2afedb36867988e17efa4788cd697f62e Mon Sep 17 00:00:00 2001 From: Ferdinand Date: Thu, 1 Aug 2024 16:21:24 +0800 Subject: [PATCH] Enhance the construction of the delete statement in AbstractMapper by using a unified appendWhereClause method to construct the WHERE clause. Modify appendWhereClause to be protected, allowing for customization based on different database types, such as adjustments according to column names. --- .../plugin/datasource/mapper/AbstractMapper.java | 11 +++-------- .../plugin/datasource/mapper/AbstractMapperTest.java | 2 +- 2 files changed, 4 insertions(+), 9 deletions(-) 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 ba85b7ffaaa..b86e481efc9 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 @@ -121,13 +121,8 @@ 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(" ").append("WHERE "); - for (int i = 0; i < params.size(); i++) { - sql.append(params.get(i)).append(" ").append("=").append(" ? "); - if (i != params.size() - 1) { - sql.append("AND "); - } - } + sql.append(method).append("FROM ").append(getTableName()).append(" "); + appendWhereClause(params, sql); return sql.toString(); } @@ -155,7 +150,7 @@ public String[] getPrimaryKeyGeneratedKeys() { return new String[]{"id"}; } - private void appendWhereClause(List where, StringBuilder sql) { + 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("?"); 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 899cb01adf1..6531881e782 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 @@ -71,7 +71,7 @@ void testUpdate() { @Test void testDelete() { String sql = abstractMapper.delete(Arrays.asList("id")); - assertEquals("DELETE FROM tenant_info WHERE id = ? ", sql); + assertEquals("DELETE FROM tenant_info WHERE id = ?", sql); } @Test