1 | |
package org.apache.torque.engine.database.transform; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
import java.io.BufferedReader; |
17 | |
import java.io.FileReader; |
18 | |
import java.io.IOException; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.List; |
22 | |
|
23 | |
import org.apache.torque.engine.database.model.Column; |
24 | |
import org.apache.torque.engine.database.model.Database; |
25 | |
import org.apache.torque.engine.database.model.ForeignKey; |
26 | |
import org.apache.torque.engine.database.model.IDMethod; |
27 | |
import org.apache.torque.engine.database.model.Table; |
28 | |
import org.apache.torque.engine.sql.ParseException; |
29 | |
import org.apache.torque.engine.sql.SQLScanner; |
30 | |
import org.apache.torque.engine.sql.Token; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class SQLToAppData { |
41 | |
private String sqlFile; |
42 | |
private List tokens; |
43 | |
private Token token; |
44 | |
private Database appDataDB; |
45 | |
private int count; |
46 | |
private String databaseType; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 0 | public SQLToAppData(String sqlFile) { |
55 | 0 | this.sqlFile = sqlFile; |
56 | 0 | } |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 0 | public SQLToAppData(String sqlFile, String databaseType) { |
67 | 0 | this.sqlFile = sqlFile; |
68 | 0 | this.databaseType = databaseType; |
69 | 0 | } |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public String getSqlFile() { |
77 | 0 | return sqlFile; |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public void setSqlFile(String sqlFile) { |
87 | 0 | this.sqlFile = sqlFile; |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
private void next() throws ParseException { |
97 | 0 | if (count < tokens.size()) { |
98 | 0 | token = (Token) tokens.get(count++); |
99 | |
} else { |
100 | 0 | throw new ParseException("No More Tokens"); |
101 | |
} |
102 | 0 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
private void err(String name) throws ParseException { |
112 | 0 | throw new ParseException(name + " at [ line: " + token.getLine() + " col: " + token.getCol() + " ]"); |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
private boolean hasTokens() { |
121 | 0 | return count < tokens.size(); |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
private void create() throws ParseException { |
130 | 0 | next(); |
131 | 0 | if (token.getStr().toUpperCase().equals("TABLE")) { |
132 | 0 | createTable(); |
133 | |
} |
134 | 0 | } |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
private void createTable() throws ParseException { |
143 | 0 | next(); |
144 | 0 | String tableName = token.getStr(); |
145 | 0 | next(); |
146 | 0 | if (!token.getStr().equals("(")) { |
147 | 0 | err("( expected"); |
148 | |
} |
149 | 0 | next(); |
150 | |
|
151 | 0 | Table tbl = new Table(tableName); |
152 | |
|
153 | 0 | while (!token.getStr().equals(";")) { |
154 | 0 | createTableColumn(tbl); |
155 | |
} |
156 | |
|
157 | 0 | if (tbl.getPrimaryKey().size() == 1) { |
158 | 0 | tbl.setIdMethod(IDMethod.ID_BROKER); |
159 | |
} else { |
160 | 0 | tbl.setIdMethod(IDMethod.NO_ID_METHOD); |
161 | |
} |
162 | 0 | appDataDB.addTable(tbl); |
163 | 0 | } |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
private void createTableColumn(Table tbl) throws ParseException { |
172 | |
|
173 | |
|
174 | |
|
175 | 0 | if (token.getStr().equals(",")) { |
176 | 0 | next(); |
177 | |
} |
178 | |
|
179 | 0 | if (token.getStr().toUpperCase().equals("PRIMARY")) { |
180 | 0 | createTableColumnPrimary(tbl); |
181 | 0 | } else if (token.getStr().toUpperCase().equals("FOREIGN")) { |
182 | 0 | createTableColumnForeign(tbl); |
183 | 0 | } else if (token.getStr().toUpperCase().equals("UNIQUE")) { |
184 | 0 | createTableColumnUnique(tbl); |
185 | |
} else { |
186 | 0 | createTableColumnData(tbl); |
187 | |
} |
188 | 0 | } |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
private void createTableColumnPrimary(Table tbl) throws ParseException { |
197 | 0 | next(); |
198 | 0 | if (!token.getStr().toUpperCase().equals("KEY")) { |
199 | 0 | err("KEY expected"); |
200 | |
} |
201 | 0 | next(); |
202 | 0 | if (!token.getStr().toUpperCase().equals("(")) { |
203 | 0 | err("( expected"); |
204 | |
} |
205 | 0 | next(); |
206 | |
|
207 | 0 | String colName = token.getStr(); |
208 | 0 | Column c = tbl.getColumn(colName); |
209 | 0 | if (c == null) { |
210 | 0 | err("Invalid column name: " + colName); |
211 | |
} |
212 | 0 | c.setPrimaryKey(true); |
213 | 0 | next(); |
214 | 0 | while (token.getStr().equals(",")) { |
215 | 0 | next(); |
216 | 0 | colName = token.getStr(); |
217 | 0 | c = tbl.getColumn(colName); |
218 | 0 | if (c == null) { |
219 | 0 | err("Invalid column name: " + colName); |
220 | |
} |
221 | 0 | c.setPrimaryKey(true); |
222 | 0 | next(); |
223 | |
} |
224 | |
|
225 | 0 | if (!token.getStr().toUpperCase().equals(")")) { |
226 | 0 | err(") expected"); |
227 | |
} |
228 | 0 | next(); |
229 | 0 | } |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
private void createTableColumnUnique(Table tbl) throws ParseException { |
238 | 0 | next(); |
239 | 0 | if (!token.getStr().toUpperCase().equals("(")) { |
240 | 0 | err("( expected"); |
241 | |
} |
242 | 0 | next(); |
243 | 0 | while (!token.getStr().equals(")")) { |
244 | 0 | if (!token.getStr().equals(",")) { |
245 | 0 | String colName = token.getStr(); |
246 | 0 | Column c = tbl.getColumn(colName); |
247 | 0 | if (c == null) { |
248 | 0 | err("Invalid column name: " + colName); |
249 | |
} |
250 | 0 | c.setUnique(true); |
251 | |
} |
252 | 0 | next(); |
253 | |
} |
254 | 0 | if (!token.getStr().toUpperCase().equals(")")) { |
255 | 0 | err(") expected got: " + token.getStr()); |
256 | |
} |
257 | |
|
258 | 0 | next(); |
259 | 0 | } |
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
private void createTableColumnForeign(Table tbl) throws ParseException { |
268 | 0 | next(); |
269 | 0 | if (!token.getStr().toUpperCase().equals("KEY")) { |
270 | 0 | err("KEY expected"); |
271 | |
} |
272 | 0 | next(); |
273 | 0 | if (!token.getStr().toUpperCase().equals("(")) { |
274 | 0 | err("( expected"); |
275 | |
} |
276 | 0 | next(); |
277 | |
|
278 | 0 | ForeignKey fk = new ForeignKey(); |
279 | 0 | List localColumns = new ArrayList(); |
280 | 0 | tbl.addForeignKey(fk); |
281 | |
|
282 | 0 | String colName = token.getStr(); |
283 | 0 | localColumns.add(colName); |
284 | 0 | next(); |
285 | 0 | while (token.getStr().equals(",")) { |
286 | 0 | next(); |
287 | 0 | colName = token.getStr(); |
288 | 0 | localColumns.add(colName); |
289 | 0 | next(); |
290 | |
} |
291 | 0 | if (!token.getStr().toUpperCase().equals(")")) { |
292 | 0 | err(") expected"); |
293 | |
} |
294 | |
|
295 | 0 | next(); |
296 | |
|
297 | 0 | if (!token.getStr().toUpperCase().equals("REFERENCES")) { |
298 | 0 | err("REFERENCES expected"); |
299 | |
} |
300 | |
|
301 | 0 | next(); |
302 | |
|
303 | 0 | fk.setForeignTableName(token.getStr()); |
304 | |
|
305 | 0 | next(); |
306 | |
|
307 | 0 | if (token.getStr().toUpperCase().equals("(")) { |
308 | 0 | next(); |
309 | 0 | int i = 0; |
310 | 0 | fk.addReference((String) localColumns.get(i++), token.getStr()); |
311 | 0 | next(); |
312 | 0 | while (token.getStr().equals(",")) { |
313 | 0 | next(); |
314 | 0 | fk.addReference((String) localColumns.get(i++), token.getStr()); |
315 | 0 | next(); |
316 | |
} |
317 | 0 | if (!token.getStr().toUpperCase().equals(")")) { |
318 | 0 | err(") expected"); |
319 | |
} |
320 | 0 | next(); |
321 | |
} |
322 | 0 | } |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
private void createTableColumnData(Table tbl) throws ParseException { |
331 | 0 | String columnSize = null; |
332 | 0 | String columnPrecision = null; |
333 | 0 | String columnDefault = null; |
334 | 0 | boolean inEnum = false; |
335 | |
|
336 | 0 | String columnName = token.getStr(); |
337 | 0 | next(); |
338 | 0 | String columnType = token.getStr(); |
339 | |
|
340 | 0 | if (columnName.equals(")") && columnType.equals(";")) { |
341 | 0 | return; |
342 | |
} |
343 | |
|
344 | 0 | next(); |
345 | |
|
346 | |
|
347 | |
|
348 | 0 | if (columnType.toUpperCase().equals("ENUM")) { |
349 | 0 | inEnum = true; |
350 | 0 | next(); |
351 | 0 | while (!token.getStr().equals(")")) { |
352 | |
|
353 | 0 | next(); |
354 | |
} |
355 | 0 | while (!token.getStr().equals(",")) { |
356 | 0 | if (token.getStr().toUpperCase().equals("DEFAULT")) { |
357 | 0 | next(); |
358 | 0 | if (token.getStr().equals("'")) { |
359 | 0 | next(); |
360 | |
} |
361 | 0 | columnDefault = token.getStr(); |
362 | 0 | next(); |
363 | 0 | if (token.getStr().equals("'")) { |
364 | 0 | next(); |
365 | |
} |
366 | |
} |
367 | |
|
368 | 0 | next(); |
369 | |
} |
370 | 0 | next(); |
371 | 0 | columnType = "VARCHAR"; |
372 | 0 | } else if (token.getStr().toUpperCase().equals("(")) { |
373 | 0 | next(); |
374 | 0 | columnSize = token.getStr(); |
375 | 0 | next(); |
376 | 0 | if (token.getStr().equals(",")) { |
377 | 0 | next(); |
378 | 0 | columnPrecision = token.getStr(); |
379 | 0 | next(); |
380 | |
} |
381 | |
|
382 | 0 | if (!token.getStr().equals(")")) { |
383 | 0 | err(") expected"); |
384 | |
} |
385 | 0 | next(); |
386 | |
} |
387 | |
|
388 | 0 | Column col = new Column(columnName); |
389 | 0 | if (columnPrecision != null) { |
390 | 0 | columnSize = columnSize + columnPrecision; |
391 | |
} |
392 | 0 | col.setTypeFromString(columnType, columnSize); |
393 | 0 | tbl.addColumn(col); |
394 | |
|
395 | 0 | if (inEnum) { |
396 | 0 | col.setNotNull(true); |
397 | 0 | if (columnDefault != null) { |
398 | 0 | col.setDefaultValue(columnDefault); |
399 | |
} |
400 | |
} else { |
401 | 0 | while (!token.getStr().equals(",") && !token.getStr().equals(")")) { |
402 | 0 | if (token.getStr().toUpperCase().equals("NOT")) { |
403 | 0 | next(); |
404 | 0 | if (!token.getStr().toUpperCase().equals("NULL")) { |
405 | 0 | err("NULL expected after NOT"); |
406 | |
} |
407 | 0 | col.setNotNull(true); |
408 | 0 | next(); |
409 | 0 | } else if (token.getStr().toUpperCase().equals("PRIMARY")) { |
410 | 0 | next(); |
411 | 0 | if (!token.getStr().toUpperCase().equals("KEY")) { |
412 | 0 | err("KEY expected after PRIMARY"); |
413 | |
} |
414 | 0 | col.setPrimaryKey(true); |
415 | 0 | next(); |
416 | 0 | } else if (token.getStr().toUpperCase().equals("UNIQUE")) { |
417 | 0 | col.setUnique(true); |
418 | 0 | next(); |
419 | 0 | } else if (token.getStr().toUpperCase().equals("NULL")) { |
420 | 0 | col.setNotNull(false); |
421 | 0 | next(); |
422 | 0 | } else if (token.getStr().toUpperCase().equals("AUTO_INCREMENT")) { |
423 | 0 | col.setAutoIncrement(true); |
424 | 0 | next(); |
425 | 0 | } else if (token.getStr().toUpperCase().equals("DEFAULT")) { |
426 | 0 | next(); |
427 | 0 | if (token.getStr().equals("'")) { |
428 | 0 | next(); |
429 | |
} |
430 | 0 | col.setDefaultValue(token.getStr()); |
431 | 0 | next(); |
432 | 0 | if (token.getStr().equals("'")) { |
433 | 0 | next(); |
434 | |
} |
435 | |
} else { |
436 | 0 | StringBuffer line = new StringBuffer(); |
437 | 0 | for (Iterator tokenIt = tokens.iterator(); tokenIt.hasNext();) { |
438 | 0 | line.append(tokenIt.next()); |
439 | 0 | if (tokenIt.hasNext()) { |
440 | 0 | line.append(" "); |
441 | |
} |
442 | |
} |
443 | 0 | throw new ParseException("Error parsing line " + line + " : Unknown token Nr. " + count + " : " |
444 | |
+ token); |
445 | |
} |
446 | |
} |
447 | 0 | next(); |
448 | |
} |
449 | 0 | } |
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
public Database execute() throws IOException, ParseException { |
460 | 0 | count = 0; |
461 | 0 | appDataDB = new Database(databaseType); |
462 | |
|
463 | 0 | FileReader fr = new FileReader(sqlFile); |
464 | 0 | BufferedReader br = new BufferedReader(fr); |
465 | 0 | SQLScanner scanner = new SQLScanner(br); |
466 | |
|
467 | 0 | tokens = scanner.scan(); |
468 | |
|
469 | 0 | br.close(); |
470 | |
|
471 | 0 | while (hasTokens()) { |
472 | 0 | if (token == null) { |
473 | 0 | next(); |
474 | |
} |
475 | |
|
476 | 0 | if (token.getStr().toUpperCase().equals("CREATE")) { |
477 | 0 | create(); |
478 | |
} |
479 | 0 | if (hasTokens()) { |
480 | 0 | next(); |
481 | |
} |
482 | |
} |
483 | 0 | return appDataDB; |
484 | |
} |
485 | |
} |