Search in sources :

Example 1 with Column

use of core.framework.db.Column 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 Column

use of core.framework.db.Column 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)

Example 3 with Column

use of core.framework.db.Column in project core-ng-project by neowu.

the class DeleteQueryBuilder method build.

static String build(Class<?> entityClass) {
    Table table = entityClass.getDeclaredAnnotation(Table.class);
    StringBuilder builder = new StringBuilder("DELETE FROM ").append(table.name()).append(" WHERE ");
    int index = 0;
    for (Field field : Classes.instanceFields(entityClass)) {
        if (field.isAnnotationPresent(PrimaryKey.class)) {
            Column column = field.getDeclaredAnnotation(Column.class);
            if (index > 0)
                builder.append(" AND ");
            builder.append(column.name()).append(" = ?");
            index++;
        }
    }
    return builder.toString();
}
Also used : Field(java.lang.reflect.Field) Table(core.framework.db.Table) Column(core.framework.db.Column)

Example 4 with Column

use of core.framework.db.Column in project core-ng-project by neowu.

the class RowMapperBuilder method mapMethod.

private String mapMethod() {
    CodeBuilder builder = new CodeBuilder().append("public Object map({} resultSet) {\n", type(ResultSetWrapper.class));
    String entityClassLiteral = type(entityClass);
    builder.indent(1).append("{} entity = new {}();\n", entityClassLiteral, entityClassLiteral);
    for (Field field : Classes.instanceFields(entityClass)) {
        String fieldName = field.getName();
        Class<?> fieldClass = field.getType();
        String column = field.getDeclaredAnnotation(Column.class).name();
        if (Integer.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getInt(\"{}\");\n", fieldName, column);
        } else if (String.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getString(\"{}\");\n", fieldName, column);
        } else if (Boolean.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getBoolean(\"{}\");\n", fieldName, column);
        } else if (Long.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getLong(\"{}\");\n", fieldName, column);
        } else if (LocalDateTime.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getLocalDateTime(\"{}\");\n", fieldName, column);
        } else if (LocalDate.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getLocalDate(\"{}\");\n", fieldName, column);
        } else if (ZonedDateTime.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getZonedDateTime(\"{}\");\n", fieldName, column);
        } else if (fieldClass.isEnum()) {
            registerEnumClass(fieldClass);
            this.builder.addField("private final {} {}Mappings = new {}({});", type(DBEnumMapper.class), fieldName, type(DBEnumMapper.class), variable(fieldClass));
            builder.indent(1).append("entity.{} = ({}){}Mappings.getEnum(resultSet.getString(\"{}\"));\n", fieldName, type(fieldClass), fieldName, column);
        } else if (Double.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getDouble(\"{}\");\n", fieldName, column);
        } else if (BigDecimal.class.equals(fieldClass)) {
            builder.indent(1).append("entity.{} = resultSet.getBigDecimal(\"{}\");\n", fieldName, column);
        }
    }
    builder.indent(1).append("return entity;\n");
    builder.append("}");
    return builder.build();
}
Also used : Field(java.lang.reflect.Field) Column(core.framework.db.Column) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Aggregations

Column (core.framework.db.Column)4 Field (java.lang.reflect.Field)3 PrimaryKey (core.framework.db.PrimaryKey)2 Table (core.framework.db.Table)2 CodeBuilder (core.framework.impl.asm.CodeBuilder)2 BigDecimal (java.math.BigDecimal)1 LocalDate (java.time.LocalDate)1