(String[] args)
| 12 | |
| 13 | public class TableCreator { |
| 14 | public static void |
| 15 | main(String[] args) throws Exception { |
| 16 | if(args.length < 1) { |
| 17 | System.out.println( |
| 18 | "arguments: annotated classes"); |
| 19 | System.exit(0); |
| 20 | } |
| 21 | for(String className : args) { |
| 22 | Class<?> cl = Class.forName(className); |
| 23 | DBTable dbTable = cl.getAnnotation(DBTable.class); |
| 24 | if(dbTable == null) { |
| 25 | System.out.println( |
| 26 | "No DBTable annotations in class " + |
| 27 | className); |
| 28 | continue; |
| 29 | } |
| 30 | String tableName = dbTable.name(); |
| 31 | // If the name is empty, use the Class name: |
| 32 | if(tableName.length() < 1) |
| 33 | tableName = cl.getName().toUpperCase(); |
| 34 | List<String> columnDefs = new ArrayList<>(); |
| 35 | for(Field field : cl.getDeclaredFields()) { |
| 36 | String columnName = null; |
| 37 | Annotation[] anns = |
| 38 | field.getDeclaredAnnotations(); |
| 39 | if(anns.length < 1) |
| 40 | continue; // Not a db table column |
| 41 | if(anns[0] instanceof SQLInteger) { |
| 42 | SQLInteger sInt = (SQLInteger) anns[0]; |
| 43 | // Use field name if name not specified |
| 44 | if(sInt.name().length() < 1) |
| 45 | columnName = field.getName().toUpperCase(); |
| 46 | else |
| 47 | columnName = sInt.name(); |
| 48 | columnDefs.add(columnName + " INT" + |
| 49 | getConstraints(sInt.constraints())); |
| 50 | } |
| 51 | if(anns[0] instanceof SQLString) { |
| 52 | SQLString sString = (SQLString) anns[0]; |
| 53 | // Use field name if name not specified. |
| 54 | if(sString.name().length() < 1) |
| 55 | columnName = field.getName().toUpperCase(); |
| 56 | else |
| 57 | columnName = sString.name(); |
| 58 | columnDefs.add(columnName + " VARCHAR(" + |
| 59 | sString.value() + ")" + |
| 60 | getConstraints(sString.constraints())); |
| 61 | } |
| 62 | StringBuilder createCommand = new StringBuilder( |
| 63 | "CREATE TABLE " + tableName + "("); |
| 64 | for(String columnDef : columnDefs) |
| 65 | createCommand.append( |
| 66 | "\n " + columnDef + ","); |
| 67 | // Remove trailing comma |
| 68 | String tableCreate = createCommand.substring( |
| 69 | 0, createCommand.length() - 1) + ");"; |
| 70 | System.out.println("Table Creation SQL for " + |
| 71 | className + " is:\n" + tableCreate); |
nothing calls this directly
no test coverage detected