-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertQueryGenerator.java
More file actions
executable file
·149 lines (127 loc) · 4.22 KB
/
InsertQueryGenerator.java
File metadata and controls
executable file
·149 lines (127 loc) · 4.22 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package devinfalgoust.sqlquerygenerator.insert;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.RandomStringUtils;
import devinfalgoust.sqlquerygenerator.Queries;
import devinfalgoust.sqlquerygenerator.QueryException;
/**
* This is the class that generates the Insert Queries. a list of InsertQueryFields,
* the tableName, and the start and end variables.
*
* The start and end variables are used to calculate 1) the IDs of each
* record and 2) the number of records to generate.
*
* This class includes the org.apache.commons.lang3.RandomStringUtils.randomAlphabetic
* function. You can either go download this and add it to your Build Path, or
* write your own random string generation function.
*
* @author Devin Falgoust
*/
public class InsertQueryGenerator {
private List<InsertQueryField> fields;
private Integer start;
private Integer end;
private String tableName;
/**
* Constructor to create the InsertQueryGenerator
*
* @param tableName
* - table that you wish to insert a record into
* @param start
* - starting index
* @param end
* - ending index
*/
public InsertQueryGenerator(String tableName, int start, int end) {
this.tableName = tableName;
this.start = start;
this.end = end;
fields = new ArrayList<InsertQueryField>();
}
/**
* This function generates a String housing all of the Queries from
* start to end with all of the given fields for the given tableName
*
* @return
*/
public String generate() {
Queries queries = new Queries();
for (Integer i = start; i <= end; i++) {
InsertQuery query = new InsertQuery(tableName);
for (InsertQueryField field : fields) {
String value = "";
if (field.isID()) {
value = i.toString();
} else if (field.isEmail()) {
value = RandomStringUtils.randomAlphabetic(8) + i.toString() + "@test.com";
} else if (field.isName()) {
value = RandomStringUtils.randomAlphabetic(1).toUpperCase() + RandomStringUtils.randomAlphabetic(7).toLowerCase();
} else if (field.isRandomText()) {
value = RandomStringUtils.randomAlphabetic(8);
} else if (field.isTextFromChoices()) {
Double rand = Math.random() * 1000;
Integer index = rand.intValue() % field.getOptions().size();
value = field.getOption(index);
} else {
// Should never reach here
}
query.addField(field.getName(), value);
}
queries.addQuery(query);
}
return queries.generateAll();
}
/**
* The addField function takes these parameters:
* 1) the name of the field
* 2) the type of field
* 3...) any options for the field. Currently these are only used when type = TEXT
*
* The valid types are:
* ID - generates an ID starting at "start" and ending at "end"
* NAME - generates a random string of 8 characters where the first is capitalized
* EMAIL - generates a random email address. This is created using 8 random
* characters, then the ID, then @test.com
* TEXT - adds a random text of the options given
* If there are no options, it generates a random string of 8 characters
* If one option is provided, it adds that text to every record
* If more than one option is provided, it chooses a random option from these
*
* @param name - name of the field
* @param type - type of field
* @param options - options for choosing text
*/
public void addField(String name, InsertQueryFieldType type, String... options) {
InsertQueryField field = null;
if (options != null && options.length > 0) {
field = new InsertQueryField(name, type, options);
} else {
field = new InsertQueryField(name, type, new String[] { "" });
}
fields.add(field);
}
/*
* Getters and Setters for:
* start
* end
* tableName
*/
public Integer getStart() {
return start;
}
public void setStart(Integer start) {
this.start = start;
}
public Integer getEnd() {
return end;
}
public void setEnd(Integer end) {
this.end = end;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
}