-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBeanTranslator.java
More file actions
348 lines (300 loc) · 10.2 KB
/
BeanTranslator.java
File metadata and controls
348 lines (300 loc) · 10.2 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package chkrr00k.sql;
import java.lang.reflect.Field;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
*
* A simple way to translate a java bean object into its own SQL query.<br/><br/>
* <b>Example</b><br/>
* {@code Object o = new Persona();} Given the bean object.<br/>
* {@code BeanTranslator bt = new BeanTranslator();}<br/>
* {@code bt.createTable(o);}<br/>
* {@code bt.insertTable(o);}<br/>
* {@code bt.deleteTable(o);}<br/>
* {@code bt.selectTable(o);}<br/>
* {@code bt.updateTable(o);}<br/>
* {@code bt.deleteByIdTable(o);}<br/>
* {@code bt.selectByIdTable(o);}<br/>
* {@code bt.updateByIdTable(o);}<br/>
* {@code bt.selectAllTable(o);}<br/>
* {@code bt.dropTable(o);}<br/>
*
* @author chkrr00k
*
*/
public class BeanTranslator {
private static final Map<String, String> TYPETRANSLATION = new HashMap<String, String>();
static {
TYPETRANSLATION.put("java.lang.String", "VARCHAR(100)");
TYPETRANSLATION.put("int", "INT");
TYPETRANSLATION.put("double", "DOUBLE");
TYPETRANSLATION.put("float", "FLOAT");
TYPETRANSLATION.put("char", "CHAR");
TYPETRANSLATION.put("java.sql.Date", "DATE");
}
private static <T> Iterable<T> iteratorToIterable(Iterator<T> it) {
return () -> it;
}
private List<Entry<String, String>> getFields(Object o) {
List<Entry<String, String>> fields = new LinkedList<Entry<String, String>>();
for (Field f : o.getClass().getDeclaredFields()) {
/*
* System.out.println( f.getName() + " = " + o.getClass()
* .getMethod( "get" + ("" + f.getName().charAt(0)).toUpperCase() +
* f.getName().substring(1)) .invoke(o) );
*/
fields.add(new AbstractMap.SimpleEntry<String, String>(f.getName(), f.getType().getName()));
}
return fields;
}
private List<String> getPrimaryKeys(Object o) {
List<String> pk = new LinkedList<String>();
for (Field f : o.getClass().getDeclaredFields()) {
if (f.getAnnotation(DatabaseKey.class) != null) {
if (f.getAnnotation(DatabaseKey.class).Type().equals(DatabaseKey.Type.PRIMARY)) {
pk.add(f.getName());
}
}
}
return pk;
}
private Map<String, String> getForeignKeys(Object o) {
Map<String, String> fk = new HashMap<String, String>();
for (Field f : o.getClass().getDeclaredFields()) {
if (f.getAnnotation(DatabaseKey.class) != null) {
if (f.getAnnotation(DatabaseKey.class).Type().equals(DatabaseKey.Type.FOREIGN)) {
fk.put(f.getName(), f.getAnnotation(DatabaseKey.class).Table());
}
}
}
return fk;
}
/**
* Generates the create table query for the given java bean object.
*
* @param o
* The bean to create the query from.
* @return The create table query for the object given.
*/
public String createTable(Object o) {
List<Entry<String, String>> fields = this.getFields(o);
final StringBuilder strBld = new StringBuilder();
strBld.append("CREATE TABLE ");
strBld.append(o.getClass().getSimpleName());
strBld.append(" (\n");
strBld.append(String.join(",\n", iteratorToIterable(fields.stream().map((e) -> {
try {
return "\t" + e.getKey() + " " + BeanTranslator.TYPETRANSLATION.get(e.getValue()) + " NOT NULL";
} catch (Exception ex) {
throw new IllegalArgumentException("Invalid type");
}
}).iterator())));
List<String> pk = this.getPrimaryKeys(o);
Map<String, String> fk = this.getForeignKeys(o);
if (pk.size() > 0) {
strBld.append(",\n\tPRIMARY KEY ( ");
strBld.append(String.join(", ", pk));
strBld.append(" )");
}
if (fk.size() > 0) {
fk.keySet().stream().forEach((e) -> {
strBld.append(",\n\tFOREIGN KEY ( ");
strBld.append(e);
strBld.append(" ) REFERENCES ");
strBld.append(fk.get(e));
});
}
strBld.append("\n)");
return strBld.toString();
}
/**
* Generates the insert table query for the given java bean object.
*
* @param o
* The bean to create the query from.
* @return The insert table query for the object given.
*/
public String insertTable(Object o) {
List<Entry<String, String>> fields = this.getFields(o);
final StringBuilder strBld = new StringBuilder();
strBld.append("INSERT INTO ");
strBld.append(o.getClass().getSimpleName());
strBld.append(" ( ");
strBld.append(String.join(", ", iteratorToIterable(fields.stream().map((e) -> {
return e.getKey();
}).iterator())));
strBld.append(" ) VALUES ( ");
String[] questions = new String[fields.size()];
Arrays.fill(questions, "?");
strBld.append(String.join(", ", questions));
strBld.append(" ) ");
return strBld.toString();
}
/**
* Generates the delete table query for the given java bean object.
*
* @param o
* The bean to create the query from.
* @return The delete table query for the object given.
*/
public String deleteTable(Object o) {
List<Entry<String, String>> fields = this.getFields(o);
final StringBuilder strBld = new StringBuilder();
strBld.append("DELETE FROM ");
strBld.append(o.getClass().getSimpleName());
strBld.append(" WHERE ");
strBld.append(String.join(" AND ", iteratorToIterable(fields.stream().map((e) -> {
return e.getKey() + " = ?";
}).iterator())));
return strBld.toString();
}
/**
* Generates the select from table query for the given java bean object.
*
* @param o
* The bean to create the query from.
* @return The select table query for the object given.
*/
public String selectTable(Object o) {
List<Entry<String, String>> fields = this.getFields(o);
final StringBuilder strBld = new StringBuilder();
strBld.append("SELECT * FROM ");
strBld.append(o.getClass().getSimpleName());
strBld.append(" WHERE ");
strBld.append(String.join(" AND ", iteratorToIterable(fields.stream().map((e) -> {
return e.getKey() + " = ?";
}).iterator())));
return strBld.toString();
}
/**
* Generates the update table query for the given java bean object.
*
* @param o
* The bean to create the query from.
* @return The update table query for the object given.
*/
public String updateTable(Object o) {
List<Entry<String, String>> fields = this.getFields(o);
final StringBuilder strBld = new StringBuilder();
strBld.append("UPDATE ");
strBld.append(o.getClass().getSimpleName());
strBld.append(" SET ");
strBld.append(String.join(", ", iteratorToIterable(fields.stream().map((e) -> {
return e.getKey() + " = ?";
}).iterator())));
strBld.append(" WHERE ");
strBld.append(String.join(" AND ", iteratorToIterable(fields.stream().map((e) -> {
return e.getKey() + " = ?";
}).iterator())));
return strBld.toString();
}
/**
* Generates the update table query for the given java bean object.<br/>
* Uses {@code DatabaseKey} to identify the primary keys.
*
* @see DatabaseKey
* @param o
* The bean to create the query from.
* @return The update table query for the object given.
*/
public String updateByIdTable(Object o) {
List<String> pk = this.getPrimaryKeys(o);
if (pk.size() > 0) {
List<Entry<String, String>> fields = this.getFields(o);
final StringBuilder strBld = new StringBuilder();
strBld.append("UPDATE ");
strBld.append(o.getClass().getSimpleName());
strBld.append(" SET ");
strBld.append(String.join(", ", iteratorToIterable(fields.stream().map((e) -> {
return e.getKey() + " = ?";
}).iterator())));
strBld.append(" WHERE ");
strBld.append(String.join(" AND ", iteratorToIterable(pk.stream().map((e) -> {
return e + " = ?";
}).iterator())));
return strBld.toString();
} else {
return this.updateTable(o);
}
}
/**
* Generates the delete table query for the given java bean object.<br/>
* Uses {@code DatabaseKey} to identify the primary keys.
*
* @see DatabaseKey
* @param o
* The bean to create the query from.
* @return The delete table query for the object given.
*/
public String deleteByIdTable(Object o) {
List<String> pk = this.getPrimaryKeys(o);
if (pk.size() > 0) {
final StringBuilder strBld = new StringBuilder();
strBld.append("DELETE FROM ");
strBld.append(o.getClass().getSimpleName());
strBld.append(" WHERE ");
strBld.append(String.join(" AND ", iteratorToIterable(pk.stream().map((e) -> {
return e + " = ?";
}).iterator())));
return strBld.toString();
} else {
return this.deleteTable(o);
}
}
/**
* Generates the select table query for the given java bean object.<br/>
* Uses {@code DatabaseKey} to identify the primary keys.
*
* @see DatabaseKey
* @param o
* The bean to create the query from.
* @return The select table query for the object given.
*/
public String selectByIdTable(Object o) {
List<String> pk = this.getPrimaryKeys(o);
if (pk.size() > 0) {
final StringBuilder strBld = new StringBuilder();
strBld.append("SELECT * FROM ");
strBld.append(o.getClass().getSimpleName());
strBld.append(" WHERE ");
strBld.append(String.join(" AND ", iteratorToIterable(pk.stream().map((e) -> {
return e + " = ?";
}).iterator())));
return strBld.toString();
} else {
return this.selectTable(o);
}
}
/**
* Generates the select all table query for the given java bean object.<br/>
*
* @param o
* The bean to create the query from.
* @return The select table query for the object given.
*/
public String selectAllTable(Object o) {
final StringBuilder strBld = new StringBuilder();
strBld.append("SELECT * FROM ");
strBld.append(o.getClass().getSimpleName());
return strBld.toString();
}
/**
* Generates the drop table query for the given java bean object.<br/>
*
* @param o
* The bean to create the query from.
* @return The drop table query for the object given.
*/
public String dropTable(Object o) {
final StringBuilder strBld = new StringBuilder();
strBld.append("DROP TABLE ");
strBld.append(o.getClass().getSimpleName());
return strBld.toString();
}
}