All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy