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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations