22
33import java .util .ArrayList ;
44import java .util .List ;
5+ import java .util .Map ;
56
67import com .github .ezframework .javaquerybuilder .query .condition .Condition ;
78import 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.
1419 */
1520public 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