Skip to content

Commit 521a991

Browse files
committed
feat: improved QueryBuilder with deleteFrom and update function
1 parent a8e7b42 commit 521a991

3 files changed

Lines changed: 293 additions & 32 deletions

File tree

‎src/main/java/com/github/ezframework/javaquerybuilder/query/builder/CreateBuilder.java‎

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@
1010

1111
/**
1212
* Builder for SQL CREATE TABLE statements.
13-
/**
14-
* Builds the SQL CREATE TABLE statement with a dialect.
15-
* @param dialect the SQL dialect (optional)
16-
* @return the SQL result
17-
* @throws IllegalStateException if table or columns are missing
18-
*/
19-
public SqlResult build(SqlDialect dialect) {
13+
*
14+
* @author EzFramework
15+
* @version 1.0.0
16+
*/
17+
public class CreateBuilder {
18+
19+
/** The table to create. */
2020
private String table;
21+
2122
/** The columns and their SQL types. */
2223
private final Map<String, String> columns = new LinkedHashMap<>();
24+
2325
/** The primary key columns. */
2426
private final List<String> primaryKeys = new ArrayList<>();
25-
/** If true, adds IF NOT EXISTS. */
27+
28+
/** Whether to add IF NOT EXISTS. */
2629
private boolean ifNotExists = false;
2730

2831
/**
@@ -67,22 +70,22 @@ public CreateBuilder ifNotExists() {
6770

6871
/**
6972
* Builds the SQL CREATE TABLE statement.
73+
*
7074
* @return the SQL result
71-
* @throws IllegalStateException if table or columns are missing
75+
* @throws IllegalStateException if table name or columns are missing
7276
*/
73-
7477
public SqlResult build() {
7578
return build(null);
7679
}
7780

7881
/**
79-
* Builds the SQL CREATE TABLE statement with a dialect.
80-
* @param dialect the SQL dialect (optional)
82+
* Builds the SQL CREATE TABLE statement using the given dialect.
83+
*
84+
* @param dialect the SQL dialect to use; if {@code null}, the standard dialect is used
8185
* @return the SQL result
82-
* @throws IllegalStateException if table or columns are missing
83-
86+
* @throws IllegalStateException if table name or columns are missing
87+
*/
8488
public SqlResult build(SqlDialect dialect) {
85-
8689
if (table == null || columns.isEmpty()) {
8790
throw new IllegalStateException("Table name and at least one column are required");
8891
}
@@ -112,6 +115,7 @@ public SqlResult build(SqlDialect dialect) {
112115
public String getSql() {
113116
return sql.toString();
114117
}
118+
115119
@Override
116120
public List<Object> getParameters() {
117121
return List.of();

‎src/main/java/com/github/ezframework/javaquerybuilder/query/builder/DeleteBuilder.java‎

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
import java.util.Map;
56

67
import com.github.ezframework.javaquerybuilder.query.condition.Condition;
78
import com.github.ezframework.javaquerybuilder.query.condition.ConditionEntry;
9+
import com.github.ezframework.javaquerybuilder.query.condition.Connector;
10+
import com.github.ezframework.javaquerybuilder.query.condition.Operator;
11+
import com.github.ezframework.javaquerybuilder.query.sql.SqlDialect;
12+
import com.github.ezframework.javaquerybuilder.query.sql.SqlResult;
813

914
/**
1015
* Builder for SQL DELETE statements.
@@ -14,6 +19,16 @@
1419
*/
1520
public class DeleteBuilder {
1621

22+
/** Simple comparison operators mapped to their SQL fragments. */
23+
private static final Map<Operator, String> SIMPLE_OPS = Map.of(
24+
Operator.EQ, " = ?",
25+
Operator.NEQ, " != ?",
26+
Operator.GT, " > ?",
27+
Operator.GTE, " >= ?",
28+
Operator.LT, " < ?",
29+
Operator.LTE, " <= ?"
30+
);
31+
1732
/** The table to delete from. */
1833
private String table;
1934

@@ -74,6 +89,32 @@ public DeleteBuilder whereIn(String column, List<?> values) {
7489
/**
7590
* Builds the SQL DELETE statement.
7691
*
92+
* @return the SQL result
93+
*/
94+
public SqlResult build() {
95+
return build(null);
96+
}
97+
98+
/**
99+
* Builds the SQL DELETE statement using the given dialect.
100+
*
101+
* @param dialect the SQL dialect (may be null for standard SQL)
102+
* @return the SQL result
103+
*/
104+
public SqlResult build(final SqlDialect dialect) {
105+
final StringBuilder sql = new StringBuilder();
106+
final List<Object> params = new ArrayList<>();
107+
sql.append("DELETE FROM ").append(table);
108+
if (!conditions.isEmpty()) {
109+
sql.append(" WHERE ");
110+
for (int j = 0; j < conditions.size(); j++) {
111+
final ConditionEntry cond = conditions.get(j);
112+
if (j > 0) {
113+
sql.append(" ").append(cond.getConnector().name()).append(" ");
114+
}
115+
appendDmlCondition(sql, params, cond);
116+
}
117+
}
77118
return new SqlResult() {
78119
@Override
79120
public String getSql() {
@@ -87,6 +128,24 @@ public List<Object> getParameters() {
87128
};
88129
}
89130

131+
private void appendDmlCondition(StringBuilder sql, List<Object> params, ConditionEntry cond) {
132+
final Operator op = cond.getCondition().getOperator();
133+
sql.append(cond.getColumn());
134+
if (SIMPLE_OPS.containsKey(op)) {
135+
sql.append(SIMPLE_OPS.get(op));
136+
params.add(cond.getCondition().getValue());
137+
} else if (op == Operator.IN) {
138+
handleInOperator(sql, params, cond);
139+
} else if (op == Operator.NOT_IN) {
140+
handleNotInOperator(sql, params, cond);
141+
} else if (op == Operator.BETWEEN) {
142+
handleBetweenOperator(sql, params, cond);
143+
} else {
144+
sql.append(" = ?");
145+
params.add(cond.getCondition().getValue());
146+
}
147+
}
148+
90149
/**
91150
* Handles the NOT IN operator for SQL generation.
92151
*
@@ -113,14 +172,13 @@ private void handleNotInOperator(StringBuilder sql, List<Object> params, Conditi
113172
}
114173

115174
/**
116-
/**
117-
* Handles the BETWEEN operator for SQL generation.
118-
*
119-
* @param sql the SQL string builder
120-
* @param params the parameter list
121-
* @param cond the condition entry
122-
* @throws UnsupportedOperationException if the value list is null or not exactly two values
123-
*/
175+
* Handles the BETWEEN operator for SQL generation.
176+
*
177+
* @param sql the SQL string builder
178+
* @param params the parameter list
179+
* @param cond the condition entry
180+
* @throws UnsupportedOperationException if the value list is null or not exactly two values
181+
*/
124182
private void handleBetweenOperator(StringBuilder sql, List<Object> params, ConditionEntry cond) {
125183
@SuppressWarnings("unchecked")
126184
final List<?> betweenValues = (List<?>) cond.getCondition().getValue();

0 commit comments

Comments
 (0)