/
DbFill.java
265 lines (215 loc) · 8.93 KB
/
DbFill.java
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package org.jumpmind.symmetric;
import java.math.BigDecimal;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.model.Column;
import org.jumpmind.db.model.Database;
import org.jumpmind.db.model.ForeignKey;
import org.jumpmind.db.model.Reference;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.platform.JdbcDatabasePlatformFactory;
import org.jumpmind.db.sql.DmlStatement;
import org.jumpmind.db.sql.DmlStatement.DmlType;
import org.jumpmind.db.sql.ISqlTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Generate data for populating databases.
*/
class DbFill {
final Logger log = LoggerFactory.getLogger(getClass());
private String catalog;
private String schema;
private IDatabasePlatform platform;
private boolean ignoreMissingTables;
private int inputLength = 10;
public DbFill() {
}
public DbFill(IDatabasePlatform platform) {
this.platform = platform;
}
public DbFill(DataSource dataSource) {
platform = JdbcDatabasePlatformFactory.createNewPlatformInstance(dataSource, null, true);
}
public void setDataSource(DataSource dataSource) {
platform = JdbcDatabasePlatformFactory.createNewPlatformInstance(dataSource, null, true);
}
public void fillTables(String... tableNames) {
ArrayList<Table> tableList = new ArrayList<Table>();
for (String tableName : tableNames) {
Table table = platform.readTableFromDatabase(getCatalogToUse(), getSchemaToUse(),
tableName);
if (table != null) {
tableList.add(table);
} else if (!ignoreMissingTables) {
throw new RuntimeException("Cannot find table " + tableName + " in catalog "
+ getCatalogToUse() + " and schema " + getSchemaToUse());
}
}
fillTables(tableList.toArray(new Table[tableList.size()]));
}
private void fillTables(Table... tables) {
ISqlTemplate sqlTemplate = platform.getSqlTemplate();
tables = Database.sortByForeignKeys(tables);
// Maintain a map of previously inserted values for foreign key
// constraints
Map<String, Object> insertedColumns = new HashMap<String, Object>(tables.length);
// Insert multiple records
for (int i = 0; i < inputLength; i++) {
for (Table table : tables) {
DmlStatement statement = platform.createDmlStatement(DmlType.INSERT, table);
Column[] tableColumns = table.getColumns();
Object[] columnValues = generateRandomValues(insertedColumns, table);
for (int j = 0; j < tableColumns.length; j++) {
insertedColumns.put(table.getQualifiedColumnName(tableColumns[j]),
columnValues[j]);
}
Column[] statementColumns = statement.getMetaData();
Object[] statementValues = new Object[statementColumns.length];
for (int j = 0; j < statementColumns.length; j++) {
statementValues[j] = insertedColumns.get(table
.getQualifiedColumnName(statementColumns[j]));
}
sqlTemplate.update(statement.getSql(), statementValues);
}
insertedColumns.clear();
}
}
private Object[] generateRandomValues(Map<String, Object> insertedColumns, Table table) {
Column[] columns = table.getColumns();
List<Object> list = new ArrayList<Object>(columns.length);
for_column: for (Column column : columns) {
Object objectValue = null;
for (ForeignKey fk : table.getForeignKeys()) {
for (Reference ref : fk.getReferences()) {
objectValue = insertedColumns.get(fk.getForeignTableName() + "."
+ ref.getForeignColumnName());
if (objectValue != null) {
list.add(objectValue);
continue for_column;
} else {
throw new RuntimeException("The foreign key column, " + column.getName()
+ " found in " + table.getName() + " references " + "table "
+ fk.getForeignTableName() + " which was not included.");
}
}
}
int type = column.getMappedTypeCode();
if (type == Types.DATE || type == Types.TIMESTAMP || type == Types.TIME) {
objectValue = randomDate();
} else if (type == Types.CHAR) {
objectValue = randomChar().toString();
} else if (type == Types.INTEGER) {
objectValue = randomInt();
} else if (type == Types.BIT) {
objectValue = randomBit();
} else if (type == Types.SMALLINT) {
objectValue = randomSmallInt();
} else if (type == Types.TINYINT) {
objectValue = randomTinyInt();
} else if (type == Types.NUMERIC || type == Types.DECIMAL || type == Types.FLOAT
|| type == Types.DOUBLE || type == Types.REAL) {
objectValue = randomBigDecimal();
} else if (type == Types.BOOLEAN) {
objectValue = randomBoolean();
} else if (type == Types.BLOB || type == Types.LONGVARBINARY || type == Types.BINARY
|| type == Types.VARBINARY || type == Types.CLOB ||
// SQLServer next type
type == -10) {
objectValue = randomBytes();
} else if (type == Types.ARRAY) {
objectValue = null;
} else if (type == Types.VARCHAR) {
objectValue = randomString(column.getSizeAsInt());
}
if (objectValue == null) {
throw new RuntimeException("No random value generated for the object.");
}
list.add(objectValue);
}
return list.toArray();
}
private Object randomSmallInt() {
// TINYINT (-32768 32767)
return new Integer(new java.util.Random().nextInt(65535) - 32768);
}
private Object randomTinyInt() {
// TINYINT (-128 to 127)
return new Integer(new java.util.Random().nextInt(256) - 128);
}
private String randomString(int maxLength) {
StringBuilder str = new StringBuilder(maxLength);
for (int i = 0; i < maxLength; i++) {
str.append(randomChar());
}
return str.toString();
}
private byte[] randomBytes() {
int length = 10;
java.util.Random rnd = new java.util.Random();
byte array[] = new byte[length];
for (int i = 0; i < length; i++) {
array[i] = (byte) rnd.nextInt(256);
}
return array;
}
private boolean randomBoolean() {
return new java.util.Random().nextBoolean();
}
private BigDecimal randomBigDecimal() {
return new BigDecimal(new java.util.Random().nextDouble());
}
private Character randomChar() {
int rnd = new java.util.Random().nextInt(52);
char base = (rnd < 26) ? 'A' : 'a';
return (char) (base + rnd % 26);
}
private Date randomDate() {
// Random date between 1970 and 2020
long l = Math.abs(new java.util.Random().nextLong());
long ms = (50L * 365 * 24 * 60 * 60 * 1000);
return new Date(l % ms);
}
private Integer randomInt() {
return new Integer(new java.util.Random().nextInt(1000000));
}
private Integer randomBit() {
return new Integer(new java.util.Random().nextInt(1));
}
protected String getSchemaToUse() {
if (StringUtils.isBlank(schema)) {
return platform.getDefaultSchema();
} else {
return schema;
}
}
protected String getCatalogToUse() {
if (StringUtils.isBlank(catalog)) {
return platform.getDefaultCatalog();
} else {
return catalog;
}
}
public void setPlatform(IDatabasePlatform platform) {
this.platform = platform;
}
public int getRecordCount() {
return inputLength;
}
public void setRecordCount(int recordCount) {
this.inputLength = recordCount;
}
public void setCatalog(String catalog) {
this.catalog = catalog;
}
public void setSchema(String schema) {
this.schema = schema;
}
}