-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertQuery.java
More file actions
executable file
·85 lines (75 loc) · 2.07 KB
/
InsertQuery.java
File metadata and controls
executable file
·85 lines (75 loc) · 2.07 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package devinfalgoust.sqlquerygenerator.insert;
import java.util.ArrayList;
import java.util.List;
import devinfalgoust.sqlquerygenerator.Query;
/**
* The InsertQuery class is used by the InsertQueryGenerator.
* It implements the Query interface so that it can be stored
* in a Queries object. It signifies a single Query in that list.
* -
* It has a tableName, and a list of field names and values
*
* @author Devin Falgoust
*/
public class InsertQuery implements Query {
private String tableName;
private List<String> fieldNames;
private List<String> fieldValues;
/**
* Constructor setting the tableName and initializing the
* name and value lists
*
* @param tableName
*/
public InsertQuery(String tableName) {
this.tableName = tableName;
fieldNames = new ArrayList<String>();
fieldValues = new ArrayList<String>();
}
/**
* This function implements the generate function of the Query
* interface. It prints out an insert statement with the given
* table name and fields
*
* @return
*/
@Override
public String generate() {
StringBuilder query = new StringBuilder();
query.append("INSERT INTO ").append(tableName).append(" (");
for (int i = 0; i < fieldNames.size(); i++) {
query.append(fieldNames.get(i));
if (i < fieldNames.size() - 1) {
query.append(", ");
}
}
query.append(") VALUES (");
for (int i = 0; i < fieldValues.size(); i++) {
query.append("'").append(fieldValues.get(i)).append("'");
if (i < fieldValues.size() - 1) {
query.append(", ");
}
}
query.append(");");
return query.toString();
}
/**
* This function allows you to add a field via name-value pair
*
* @param fieldName
* @param fieldValue
*/
public void addField(String fieldName, String fieldValue) {
fieldNames.add(fieldName);
fieldValues.add(fieldValue);
}
/*
* Getter and setter for tableName
*/
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getTableName() {
return tableName;
}
}