Skip to content

Commit

Permalink
Enhance the construction of the delete statement in AbstractMapper by…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
gongycn committed Aug 1, 2024
1 parent 1d0c97c commit 21f0bab
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,8 @@ 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(" ").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();
}
Expand Down Expand Up @@ -155,7 +150,7 @@ public String[] getPrimaryKeyGeneratedKeys() {
return new String[]{"id"};
}

private void appendWhereClause(List<String> where, StringBuilder sql) {
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("?");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 21f0bab

Please sign in to comment.