Search in sources :

Example 1 with PrimaryKey

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);
    }
}
Also used : Column(core.framework.db.Column) PrimaryKey(core.framework.db.PrimaryKey)

Example 2 with PrimaryKey

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;
}
Also used : Field(java.lang.reflect.Field) Table(core.framework.db.Table) Column(core.framework.db.Column) PrimaryKey(core.framework.db.PrimaryKey) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Aggregations

Column (core.framework.db.Column)2 PrimaryKey (core.framework.db.PrimaryKey)2 Table (core.framework.db.Table)1 CodeBuilder (core.framework.impl.asm.CodeBuilder)1 Field (java.lang.reflect.Field)1