Search in sources :

Example 16 with CodeBuilder

use of core.framework.impl.asm.CodeBuilder in project core-ng-project by neowu.

the class EntityIdHandlerBuilder method generateIdIfAbsentMethod.

private String generateIdIfAbsentMethod() {
    CodeBuilder builder = new CodeBuilder();
    builder.append("public boolean generateIdIfAbsent() {\n").indent(1).append("return {};\n", ObjectId.class.equals(idField.getType()) ? "true" : "false").append("}");
    return builder.build();
}
Also used : ObjectId(org.bson.types.ObjectId) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 17 with CodeBuilder

use of core.framework.impl.asm.CodeBuilder in project core-ng-project by neowu.

the class EntityIdHandlerBuilder method getMethod.

private String getMethod() {
    CodeBuilder builder = new CodeBuilder();
    builder.append("public Object get(Object value) {\n").indent(1).append("{} entity = ({}) value;\n", type(entityClass), type(entityClass)).indent(1).append("return entity.{};\n", idField.getName()).append("}");
    return builder.build();
}
Also used : CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 18 with CodeBuilder

use of core.framework.impl.asm.CodeBuilder in project core-ng-project by neowu.

the class InsertQuery method paramBuilder.

private Function<T, Object[]> paramBuilder(Class<T> entityClass, List<Field> paramFields) {
    CodeBuilder builder = new CodeBuilder();
    String entityClassLiteral = type(entityClass);
    builder.append("public Object apply(Object value) {\n").indent(1).append("{} entity = ({}) value;", entityClassLiteral, entityClassLiteral).indent(1).append("Object[] params = new Object[{}];\n", paramFields.size());
    int index = 0;
    for (Field paramField : paramFields) {
        builder.indent(1).append("params[{}] = entity.{};\n", index, paramField.getName());
        index++;
    }
    builder.append("return params;\n").append("}");
    DynamicInstanceBuilder<Function<T, Object[]>> dynamicInstanceBuilder = new DynamicInstanceBuilder<>(Function.class, InsertQuery.class.getCanonicalName() + "$" + entityClass.getSimpleName() + "$InsertQueryParamBuilder");
    dynamicInstanceBuilder.addMethod(builder.build());
    return dynamicInstanceBuilder.build();
}
Also used : Field(java.lang.reflect.Field) Function(java.util.function.Function) DynamicInstanceBuilder(core.framework.impl.asm.DynamicInstanceBuilder) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 19 with CodeBuilder

use of core.framework.impl.asm.CodeBuilder 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)

Example 20 with CodeBuilder

use of core.framework.impl.asm.CodeBuilder in project core-ng-project by neowu.

the class UpdateQueryBuilder method updateMethod.

private String updateMethod(Class<T> entityClass, List<Field> primaryKeyFields, List<Field> columnFields) {
    CodeBuilder builder = new CodeBuilder();
    builder.append("public {} update(Object value) {\n", type(UpdateQuery.Statement.class)).indent(1).append("{} entity = ({}) value;\n", type(entityClass), type(entityClass));
    for (Field primaryKeyField : primaryKeyFields) {
        builder.indent(1).append("if (entity.{} == null) throw new Error(\"primary key must not be null, field={}\");\n", primaryKeyField.getName(), primaryKeyField.getName());
    }
    builder.indent(1).append("StringBuilder sql = new StringBuilder(\"UPDATE {} SET \");\n", entityClass.getDeclaredAnnotation(Table.class).name());
    builder.indent(1).append("java.util.List params = new java.util.ArrayList();\n");
    builder.indent(1).append("int index = 0;\n");
    for (Field field : columnFields) {
        builder.indent(1).append("if (entity.{} != null) {\n", field.getName()).indent(2).append("if (index > 0) sql.append(\", \");\n").indent(2).append("sql.append(\"{} = ?\");\n", field.getDeclaredAnnotation(Column.class).name()).indent(2).append("params.add(entity.{});\n", field.getName()).indent(2).append("index++;\n").indent(1).append("}\n");
    }
    builder.indent(1).append("sql.append(\"");
    int index = 0;
    for (Field primaryKeyField : primaryKeyFields) {
        if (index == 0) {
            builder.append(" WHERE ");
        } else {
            builder.append(" AND ");
        }
        builder.append("{} = ?", primaryKeyField.getDeclaredAnnotation(Column.class).name());
        index++;
    }
    builder.append("\");\n");
    for (Field primaryKeyField : primaryKeyFields) {
        builder.indent(1).append("params.add(entity.{});\n", primaryKeyField.getName());
    }
    builder.indent(1).append("return new {}(sql.toString(), params.toArray());\n", type(UpdateQuery.Statement.class)).append("}");
    return builder.build();
}
Also used : Field(java.lang.reflect.Field) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Aggregations

CodeBuilder (core.framework.impl.asm.CodeBuilder)23 Field (java.lang.reflect.Field)9 Type (java.lang.reflect.Type)3 PathParam (core.framework.api.web.service.PathParam)2 QueryParam (core.framework.api.web.service.QueryParam)2 Column (core.framework.db.Column)2 LocalDate (java.time.LocalDate)2 NotNull (core.framework.api.validate.NotNull)1 Path (core.framework.api.web.service.Path)1 PrimaryKey (core.framework.db.PrimaryKey)1 Table (core.framework.db.Table)1 HTTPMethod (core.framework.http.HTTPMethod)1 DynamicInstanceBuilder (core.framework.impl.asm.DynamicInstanceBuilder)1 TemplateContext (core.framework.impl.template.TemplateContext)1 Request (core.framework.web.Request)1 Response (core.framework.web.Response)1 BigDecimal (java.math.BigDecimal)1 Function (java.util.function.Function)1 ObjectId (org.bson.types.ObjectId)1 Logger (org.slf4j.Logger)1