-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathInsert.java
More file actions
67 lines (47 loc) · 1.64 KB
/
Copy pathInsert.java
File metadata and controls
67 lines (47 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package advancedsql.query;
import advancedsql.table.ITable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Insert extends ExecuteUpdate<Insert> {
private final List<String> fields = new ArrayList<>();
private final List<Object> values = new ArrayList<>();
public Insert(ITable table) {
super(table);
}
public Insert(ITable table, Map<String, Object> fields) {
super(table);
this.fields(fields);
}
/**
* Columns and values that you want to insert.
* @param field Column name
* @param value Row value
* @return Query object.
*/
public Insert field(String field, Object value) {
this.fields.add(field);
this.values.add(value);
this.execute.add(value);
return this;
}
/**
* Columns and values that you want to insert.
* @param values Map
* @return Query object.
*/
public Insert fields(Map<String, Object> values) {
for (Map.Entry<String, Object> entry: values.entrySet()) this.field(entry.getKey(), entry.getValue());
return this;
}
@Override
public String toQuery() {
StringBuilder query = new StringBuilder("INSERT INTO " + this.table + " (");
for (int i = 0; i < this.fields.size(); i++) query.append(i != this.fields.size() - 1 ? this.fields.get(i) + ", " : this.fields.get(i));
query.append(") VALUES (");
for (int i = 0; i < this.values.size(); i++) query.append(i != this.values.size() - 1 ? "?, " : "?");
query.append(")");
return query.toString().trim();
}
}