Skip to content

Commit 68c648f

Browse files
committed
fix: fields methods added to execute update
1 parent 4b808a4 commit 68c648f

File tree

3 files changed

+43
-68
lines changed

3 files changed

+43
-68
lines changed

src/main/java/com/code/advancedsql/query/ExecuteUpdate.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,59 @@
44

55
import java.sql.PreparedStatement;
66
import java.sql.SQLException;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Map;
710

811
public abstract class ExecuteUpdate<T extends IQuery> extends Specifiable<T> {
912

10-
public ExecuteUpdate(ITable table) {
13+
protected final List<String> fields = new ArrayList<>();
14+
15+
public ExecuteUpdate(ITable table, Map<String, Object> fields) {
1116
super(table);
17+
18+
if (fields != null) this.fields(fields);
19+
}
20+
21+
public ExecuteUpdate(ITable table) {
22+
this(table, null);
1223
}
1324

1425
@Override
1526
public PreparedStatement executePrepare() throws SQLException {
16-
System.out.println(this.execute);
17-
1827
this.execute();
1928

2029
return this.prepare;
2130
}
2231

32+
/**
33+
* Columns and values that you want to update/insert.
34+
* @param field Column name
35+
* @param value Row value
36+
* @return Query object.
37+
*/
38+
@SuppressWarnings("unchecked")
39+
public T field(String field, Object value) {
40+
this.fields.add(field);
41+
42+
this.execute.add(value);
43+
44+
return (T) this;
45+
}
46+
47+
/**
48+
* Columns and values that you want to update/insert.
49+
* @param values Map
50+
* @return Query object.
51+
*/
52+
@SuppressWarnings("unchecked")
53+
public T fields(Map<String, Object> values) {
54+
for (Map.Entry<String, Object> entry: values.entrySet())
55+
this.field(entry.getKey(), entry.getValue());
56+
57+
return (T) this;
58+
}
59+
2360
/**
2461
* Execute the query.
2562
* @return An integer.

src/main/java/com/code/advancedsql/query/Insert.java

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,12 @@
88

99
public class Insert extends ExecuteUpdate<Insert> {
1010

11-
private final List<String> fields = new ArrayList<>();
12-
13-
private final List<Object> values = new ArrayList<>();
14-
1511
public Insert(ITable table) {
1612
super(table);
1713
}
1814

1915
public Insert(ITable table, Map<String, Object> fields) {
20-
super(table);
21-
22-
this.fields(fields);
23-
}
24-
25-
/**
26-
* Columns and values that you want to insert.
27-
* @param field Column name
28-
* @param value Row value
29-
* @return Query object.
30-
*/
31-
public Insert field(String field, Object value) {
32-
this.fields.add(field);
33-
34-
this.values.add(value);
35-
36-
this.execute.add(value);
37-
38-
return this;
39-
}
40-
41-
/**
42-
* Columns and values that you want to insert.
43-
* @param values Map
44-
* @return Query object.
45-
*/
46-
public Insert fields(Map<String, Object> values) {
47-
for (Map.Entry<String, Object> entry: values.entrySet()) this.field(entry.getKey(), entry.getValue());
48-
49-
return this;
16+
super(table, fields);
5017
}
5118

5219
@Override
@@ -57,7 +24,7 @@ public String toQuery() {
5724

5825
query.append(") VALUES (");
5926

60-
for (int i = 0; i < this.values.size(); i++) query.append(i != this.values.size() - 1 ? "?, " : "?");
27+
for (int i = 0; i < this.fields.size(); i++) query.append(i != this.fields.size() - 1 ? "?, " : "?");
6128

6229
query.append(")");
6330

src/main/java/com/code/advancedsql/query/Update.java

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,12 @@
88

99
public class Update extends ExecuteUpdate<Update> {
1010

11-
private final List<String> fields = new ArrayList<>();
12-
1311
public Update(ITable table) {
1412
super(table);
1513
}
1614

1715
public Update(ITable table, Map<String, Object> fields) {
18-
super(table);
19-
20-
this.fields(fields);
21-
}
22-
23-
/**
24-
* Columns and values that you want to update.
25-
* @param field Column name
26-
* @param value Row value
27-
* @return Query object.
28-
*/
29-
public Update field(String field, Object value) {
30-
this.fields.add(field);
31-
32-
this.execute.add(value);
33-
34-
return this;
35-
}
36-
37-
/**
38-
* Columns and values that you want to update.
39-
* @param values Map
40-
* @return Query object.
41-
*/
42-
public Update fields(Map<String, Object> values) {
43-
for (Map.Entry<String, Object> entry: values.entrySet()) this.field(entry.getKey(), entry.getValue());
44-
45-
return this;
16+
super(table, fields);
4617
}
4718

4819
@Override

0 commit comments

Comments
 (0)