2015-09-07 11:44:36 -06:00
|
|
|
// annotations/database/TableCreator.java
|
2020-10-07 13:35:40 -06:00
|
|
|
// (c)2020 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-01-25 18:05:55 -08:00
|
|
|
// Reflection-based annotation processor
|
2016-07-28 12:48:23 -06:00
|
|
|
// {java annotations.database.TableCreator
|
|
|
|
// annotations.database.Member}
|
2015-06-15 17:47:35 -07:00
|
|
|
package annotations.database;
|
|
|
|
import java.lang.annotation.*;
|
|
|
|
import java.lang.reflect.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class TableCreator {
|
2016-01-25 18:05:55 -08:00
|
|
|
public static void
|
|
|
|
main(String[] args) throws Exception {
|
2015-06-15 17:47:35 -07:00
|
|
|
if(args.length < 1) {
|
2017-05-10 11:45:39 -06:00
|
|
|
System.out.println(
|
|
|
|
"arguments: annotated classes");
|
2015-06-15 17:47:35 -07:00
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
for(String className : args) {
|
|
|
|
Class<?> cl = Class.forName(className);
|
|
|
|
DBTable dbTable = cl.getAnnotation(DBTable.class);
|
|
|
|
if(dbTable == null) {
|
|
|
|
System.out.println(
|
2017-05-10 11:45:39 -06:00
|
|
|
"No DBTable annotations in class " +
|
|
|
|
className);
|
2015-06-15 17:47:35 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
String tableName = dbTable.name();
|
|
|
|
// If the name is empty, use the Class name:
|
|
|
|
if(tableName.length() < 1)
|
|
|
|
tableName = cl.getName().toUpperCase();
|
|
|
|
List<String> columnDefs = new ArrayList<>();
|
|
|
|
for(Field field : cl.getDeclaredFields()) {
|
|
|
|
String columnName = null;
|
2016-01-25 18:05:55 -08:00
|
|
|
Annotation[] anns =
|
|
|
|
field.getDeclaredAnnotations();
|
2015-06-15 17:47:35 -07:00
|
|
|
if(anns.length < 1)
|
|
|
|
continue; // Not a db table column
|
|
|
|
if(anns[0] instanceof SQLInteger) {
|
|
|
|
SQLInteger sInt = (SQLInteger) anns[0];
|
|
|
|
// Use field name if name not specified
|
|
|
|
if(sInt.name().length() < 1)
|
|
|
|
columnName = field.getName().toUpperCase();
|
|
|
|
else
|
|
|
|
columnName = sInt.name();
|
|
|
|
columnDefs.add(columnName + " INT" +
|
|
|
|
getConstraints(sInt.constraints()));
|
|
|
|
}
|
|
|
|
if(anns[0] instanceof SQLString) {
|
|
|
|
SQLString sString = (SQLString) anns[0];
|
|
|
|
// Use field name if name not specified.
|
|
|
|
if(sString.name().length() < 1)
|
|
|
|
columnName = field.getName().toUpperCase();
|
|
|
|
else
|
|
|
|
columnName = sString.name();
|
|
|
|
columnDefs.add(columnName + " VARCHAR(" +
|
|
|
|
sString.value() + ")" +
|
|
|
|
getConstraints(sString.constraints()));
|
|
|
|
}
|
|
|
|
StringBuilder createCommand = new StringBuilder(
|
|
|
|
"CREATE TABLE " + tableName + "(");
|
|
|
|
for(String columnDef : columnDefs)
|
2016-01-25 18:05:55 -08:00
|
|
|
createCommand.append(
|
|
|
|
"\n " + columnDef + ",");
|
2015-06-15 17:47:35 -07:00
|
|
|
// Remove trailing comma
|
|
|
|
String tableCreate = createCommand.substring(
|
|
|
|
0, createCommand.length() - 1) + ");";
|
|
|
|
System.out.println("Table Creation SQL for " +
|
2017-05-10 11:45:39 -06:00
|
|
|
className + " is:\n" + tableCreate);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 11:45:39 -06:00
|
|
|
private static
|
|
|
|
String getConstraints(Constraints con) {
|
2015-06-15 17:47:35 -07:00
|
|
|
String constraints = "";
|
|
|
|
if(!con.allowNull())
|
|
|
|
constraints += " NOT NULL";
|
|
|
|
if(con.primaryKey())
|
|
|
|
constraints += " PRIMARY KEY";
|
|
|
|
if(con.unique())
|
|
|
|
constraints += " UNIQUE";
|
|
|
|
return constraints;
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2017-05-10 11:45:39 -06:00
|
|
|
Table Creation SQL for annotations.database.Member is:
|
2015-06-15 17:47:35 -07:00
|
|
|
CREATE TABLE MEMBER(
|
|
|
|
FIRSTNAME VARCHAR(30));
|
2017-05-10 11:45:39 -06:00
|
|
|
Table Creation SQL for annotations.database.Member is:
|
2015-06-15 17:47:35 -07:00
|
|
|
CREATE TABLE MEMBER(
|
|
|
|
FIRSTNAME VARCHAR(30),
|
|
|
|
LASTNAME VARCHAR(50));
|
2017-05-10 11:45:39 -06:00
|
|
|
Table Creation SQL for annotations.database.Member is:
|
2015-06-15 17:47:35 -07:00
|
|
|
CREATE TABLE MEMBER(
|
|
|
|
FIRSTNAME VARCHAR(30),
|
|
|
|
LASTNAME VARCHAR(50),
|
|
|
|
AGE INT);
|
2017-05-10 11:45:39 -06:00
|
|
|
Table Creation SQL for annotations.database.Member is:
|
2015-06-15 17:47:35 -07:00
|
|
|
CREATE TABLE MEMBER(
|
|
|
|
FIRSTNAME VARCHAR(30),
|
|
|
|
LASTNAME VARCHAR(50),
|
|
|
|
AGE INT,
|
2016-07-22 14:45:35 -06:00
|
|
|
REFERENCE VARCHAR(30) PRIMARY KEY);
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|