use of core.framework.db.PrimaryKey in project core-ng-project by neowu.
the class DatabaseClassValidator method visitField.
@Override
public void visitField(Field field, String parentPath) {
Class<?> fieldClass = field.getType();
Column column = field.getDeclaredAnnotation(Column.class);
if (column == null)
throw Exceptions.error("db entity field must have @Column, field={}", Fields.path(field));
if (columns.contains(column.name())) {
throw Exceptions.error("found duplicate column, field={}, column={}", Fields.path(field), column.name());
} else {
columns.add(column.name());
}
if (fieldClass.isEnum()) {
validateEnumClass(fieldClass, field);
}
PrimaryKey primaryKey = field.getDeclaredAnnotation(PrimaryKey.class);
if (primaryKey != null) {
foundPrimaryKey = true;
validatePrimaryKey(primaryKey, fieldClass, field);
}
}
use of core.framework.db.PrimaryKey in project core-ng-project by neowu.
the class EntitySchemaGenerator method schemeStatements.
private List<String> schemeStatements() {
List<String> statements = Lists.newArrayList();
CodeBuilder builder = new CodeBuilder().append("CREATE TABLE ");
Table table = entityClass.getDeclaredAnnotation(Table.class);
builder.append(table.name()).append(" (");
List<String> primaryKeys = Lists.newArrayList();
for (Field field : Classes.instanceFields(entityClass)) {
Column column = field.getDeclaredAnnotation(Column.class);
PrimaryKey primaryKey = field.getDeclaredAnnotation(PrimaryKey.class);
builder.append(column.name()).append(' ');
builder.append(columnType(field.getType(), field.getDeclaredAnnotation(Length.class)));
if (primaryKey != null) {
if (primaryKey.autoIncrement())
builder.append(" AUTO_INCREMENT");
if (!Strings.isEmpty(primaryKey.sequence())) {
statements.add("CREATE SEQUENCE IF NOT EXISTS " + primaryKey.sequence());
}
primaryKeys.add(column.name());
}
if (field.isAnnotationPresent(NotNull.class)) {
builder.append(" NOT NULL");
}
builder.append(", ");
}
builder.append("PRIMARY KEY(").appendCommaSeparatedValues(primaryKeys).append("))");
statements.add(builder.build());
return statements;
}
Aggregations