Java: SQL- |
public static class Person {
public String firstName;
public String lastName;
public int age;
}
System.out.println(MySQLQueryGenerator.generateCreateTableQuery(Person.class));
CREATE TABLE `Person_table` (
`firstName` VARCHAR(256),
`lastName` VARCHAR(256),
`age` INT);
@IfNotExists // CREATE- IF NOT EXISTS
@TableName("persons") //
public static class Person {
@AutoIncrement // AUTO_INCREMENT
@PrimaryKey // PRIMARY KEY
public int id;
@NotNull // NOT NULL
public long createTime;
@NotNull
public String firstName;
@NotNull
public String lastName;
@Default("21") //
public Integer age;
@Default("")
@MaxLength(1024) // VARCHAR
public String address;
@ColumnName("letter") //
public Character someLetter;
}
CREATE TABLE IF NOT EXISTS `persons` (
`id` INT AUTO_INCREMENT,
`createTime` BIGINT NOT NULL,
`firstName` VARCHAR(256) NOT NULL,
`lastName` VARCHAR(256) NOT NULL,
`age` INT DEFAULT '21',
`address` VARCHAR(1024) DEFAULT '',
`letter` VARCHAR(1),
PRIMARY KEY (`id`));
MySQLClient client = new MySQLClient("login", "password", "dbName");
client.connect(); //
client.createTable(PersonV1.class); //
client.alterTable(PersonV1.class, PersonV2.class); //
PersonV2 person = new PersonV2();
person.createTime = new Date().getTime();
person.firstName = "Ivan";
person.lastName = "Ivanov";
client.insert(person); //
person.age = 28;
person.createTime = new Date().getTime();
person.address = "Zimbabve";
client.insert(person);
person.createTime = new Date().getTime();
person.firstName = "John";
person.lastName = "Johnson";
person.someLetter = 'i';
client.insert(person);
List selected = client.select(PersonV2.class); //
System.out.println("Rows: " + selected.size());
for (Object obj: selected) {
System.out.println(obj);
}
client.disconnect(); //
public static String generateCreateTableQuery(Class clazz) throws MoreThanOnePrimaryKeyException {
List columnList = new ArrayList<>();
Field[] fields = clazz.getFields(); //
for (Field field: fields) {
int modifiers = field.getModifiers();
if (Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers)) { // public static
Column column = Column.fromField(field); // Field Column
if (column!=null) columnList.add(column);
}
}
/* Column */
}
/***************************/
public static Column fromField(Field field) {
Class fieldType = field.getType(); //
ColumnType columnType;
if (fieldType == boolean.class || fieldType == Boolean.class) {
columnType = ColumnType.BOOL;
} /* */ {
} else if (fieldType==String.class) {
columnType = ColumnType.VARCHAR;
} else { //
return null;
}
Column column = new Column();
column.columnType = columnType;
column.name = field.getName();
column.isAutoIncrement = field.isAnnotationPresent(AutoIncrement.class);
/* */
if (field.isAnnotationPresent(ColumnName.class)) { //
ColumnName columnName = (ColumnName)field.getAnnotation(ColumnName.class);
String name = columnName.value();
if (!name.trim().isEmpty()) column.name = name;
}
return column;
}
Column column = Column.fromField(field);
if (column!=null) {
if (column.isAutoIncrement) continue;
Object value = field.get(obj);
if (value==null && column.hasDefaultValue) continue; // :
if (column.isNotNull && value==null) {
throw new NotNullColumnHasNullValueException();
}
String valueString = value!=null ? "'" + value.toString().replace("'","\'") + "'" : "NULL";
String setValueString = "`"+column.name+"`="+valueString;
valueStringList.add(setValueString);
}