Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.kyle.component.kdb.annotation.compiler.MapperFieldBuilder Maven / Gradle / Ivy
package com.kyle.component.kdb.annotation.compiler;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import java.util.ArrayList;
import java.util.List;
import javax.lang.model.element.Modifier;
/**
* @author Kyle
* @time 2021/5/11
*/
public class MapperFieldBuilder {
public static MethodSpec builderCreateMethod(ClassName clsName) {
return MethodSpec.methodBuilder("create")
.returns(clsName)
.addStatement("return new $T()", clsName)
.addModifiers(Modifier.PUBLIC)
.addModifiers(Modifier.STATIC).build();
}
public static List builderPrimaryKeyMethod(ClassName clsName) {
return builderMethod(clsName, new AnnotationField("primaryKey", "_id", TypeName.INT));
}
public static MethodSpec builderLimitMethod(ClassName clsName) {
return MethodSpec.methodBuilder("limit")
.returns(clsName)
.addParameter(int.class, "n")
.addStatement("$N = $N", "limit", "n")
.addStatement("return this")
.addModifiers(Modifier.PUBLIC).build();
}
public static List builderMethod(ClassName clsName, AnnotationField annotationField) {
List methodSpecs = new ArrayList<>();
String equalMethodName = "equal" + CamelUtils.camelNameUpperCase(annotationField.aliasName);
ClassName conditionCls = ClassName.get("com.kyle.component.kdb.condition", "ColumnCondition");
String parameterName = CamelUtils.camelNameLowerCase(annotationField.aliasName);
MethodSpec equalMethodSpace = MethodSpec.methodBuilder(equalMethodName)
.returns(clsName)
.addParameter(annotationField.type, parameterName)
.addStatement("$T<$T> condition = new $T<>()", conditionCls, annotationField.type.box(), conditionCls)
.addStatement("condition.sqlCondition = $S", "=")
.addStatement("condition.columnName = $S", annotationField.fieldName)
.addStatement("condition.columnType = $S", annotationField.type.toString())
.addStatement("condition.t = $N", parameterName)
.addStatement("addCondition($N)", "condition")
.addStatement("return this")
.addModifiers(Modifier.PUBLIC).build();
methodSpecs.add(equalMethodSpace);
MethodSpec greaterMethodSpace = MethodSpec.methodBuilder("greater" + CamelUtils.camelNameUpperCase(annotationField.aliasName))
.returns(clsName)
.addParameter(annotationField.type, parameterName)
.addStatement("$T<$T> condition = new $T<>()", conditionCls, annotationField.type.box(), conditionCls)
.addStatement("condition.sqlCondition = $S", ">")
.addStatement("condition.columnName = $S", annotationField.fieldName)
.addStatement("condition.columnType = $S", annotationField.type.toString())
.addStatement("condition.t = $N", parameterName)
.addStatement("addCondition($N)", "condition")
.addStatement("return this")
.addModifiers(Modifier.PUBLIC).build();
methodSpecs.add(greaterMethodSpace);
MethodSpec lessMethodSpace = MethodSpec.methodBuilder("less" + CamelUtils.camelNameUpperCase(annotationField.aliasName))
.returns(clsName)
.addParameter(annotationField.type, parameterName)
.addStatement("$T<$T> condition = new $T<>()", conditionCls, annotationField.type.box(), conditionCls)
.addStatement("condition.sqlCondition = $S", "<")
.addStatement("condition.columnName = $S", annotationField.fieldName)
.addStatement("condition.columnType = $S", annotationField.type.toString())
.addStatement("condition.t = $N", parameterName)
.addStatement("addCondition($N)", "condition")
.addStatement("return this")
.addModifiers(Modifier.PUBLIC).build();
methodSpecs.add(lessMethodSpace);
MethodSpec greaterEqualMethodSpace = MethodSpec.methodBuilder("greaterAndEqual" + CamelUtils.camelNameUpperCase(annotationField.aliasName))
.returns(clsName)
.addParameter(annotationField.type, parameterName)
.addStatement("$T<$T> condition = new $T<>()", conditionCls, annotationField.type.box(), conditionCls)
.addStatement("condition.sqlCondition = $S", ">=")
.addStatement("condition.columnName = $S", annotationField.fieldName)
.addStatement("condition.columnType = $S", annotationField.type.toString())
.addStatement("condition.t = $N", parameterName)
.addStatement("addCondition($N)", "condition")
.addStatement("return this")
.addModifiers(Modifier.PUBLIC).build();
methodSpecs.add(greaterEqualMethodSpace);
MethodSpec lessEqualMethodSpace = MethodSpec.methodBuilder("lessAndEqual" + CamelUtils.camelNameUpperCase(annotationField.aliasName))
.returns(clsName)
.addParameter(annotationField.type, parameterName)
.addStatement("$T<$T> condition = new $T<>()", conditionCls, annotationField.type.box(), conditionCls)
.addStatement("condition.sqlCondition = $S", "<=")
.addStatement("condition.columnName = $S", annotationField.fieldName)
.addStatement("condition.columnType = $S", annotationField.type.toString())
.addStatement("condition.t = $N", parameterName)
.addStatement("addCondition($N)", "condition")
.addStatement("return this")
.addModifiers(Modifier.PUBLIC).build();
methodSpecs.add(lessEqualMethodSpace);
MethodSpec betweenMethodSpace = MethodSpec.methodBuilder("between" + CamelUtils.camelNameUpperCase(annotationField.aliasName))
.returns(clsName)
.addParameter(annotationField.type, "less")
.addParameter(annotationField.type, "greater")
.addStatement("$N($N)", "less" + CamelUtils.camelNameUpperCase(annotationField.aliasName), "less")
.addStatement("$N($N)", "greater" + CamelUtils.camelNameUpperCase(annotationField.aliasName), "greater")
.addStatement("return this")
.addModifiers(Modifier.PUBLIC).build();
methodSpecs.add(betweenMethodSpace);
MethodSpec betweenEqualMethodSpace = MethodSpec.methodBuilder("betweenAndEqual" + CamelUtils.camelNameUpperCase(annotationField.aliasName))
.returns(clsName)
.addParameter(annotationField.type, "less")
.addParameter(annotationField.type, "greater")
.addStatement("$N($N)", "lessAndEqual" + CamelUtils.camelNameUpperCase(annotationField.aliasName), "less")
.addStatement("$N($N)", "greaterAndEqual" + CamelUtils.camelNameUpperCase(annotationField.aliasName), "greater")
.addStatement("return this")
.addModifiers(Modifier.PUBLIC).build();
methodSpecs.add(betweenEqualMethodSpace);
MethodSpec inMethodSpace = MethodSpec.methodBuilder("in" + CamelUtils.camelNameUpperCase(annotationField.aliasName))
.returns(clsName)
.addParameter(ParameterizedTypeName.get(ClassName.get(List.class), annotationField.type.box()), parameterName + "List")
.addStatement("$T<$T> condition = new $T<>()", conditionCls, annotationField.type.box(), conditionCls)
.addStatement("condition.sqlCondition = $S", "in")
.addStatement("condition.columnName = $S", annotationField.fieldName)
.addStatement("condition.columnType = $S", annotationField.type.toString())
.addStatement("condition.inRange = $N", parameterName + "List")
.addStatement("addCondition($N)", "condition")
.addStatement("return this")
.addModifiers(Modifier.PUBLIC).build();
methodSpecs.add(inMethodSpace);
MethodSpec orderDescMethodSpace = MethodSpec.methodBuilder("orderBy" + CamelUtils.camelNameUpperCase(annotationField.aliasName) + "Desc")
.returns(clsName)
.addStatement("$N = $S", "orderByName", annotationField.fieldName)
.addStatement("$N = $S", "orderSort", "DESC")
.addStatement("return this")
.addModifiers(Modifier.PUBLIC).build();
methodSpecs.add(orderDescMethodSpace);
MethodSpec orderAscMethodSpace = MethodSpec.methodBuilder("orderBy" + CamelUtils.camelNameUpperCase(annotationField.aliasName) + "Asc")
.returns(clsName)
.addStatement("$N = $S", "orderByName", annotationField.fieldName)
.addStatement("$N = $S", "orderSort", "ASC")
.addStatement("return this")
.addModifiers(Modifier.PUBLIC).build();
methodSpecs.add(orderAscMethodSpace);
return methodSpecs;
}
}