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

tech.guyi.ipojo.module.h2.where.condition.builder.WhereConditionBuilder Maven / Gradle / Ivy

The newest version!
package tech.guyi.ipojo.module.h2.where.condition.builder;

import tech.guyi.ipojo.application.utils.StringUtils;
import tech.guyi.ipojo.module.h2.entity.Entity;
import tech.guyi.ipojo.module.h2.where.condition.converter.WhereConditionTypeConverter;
import tech.guyi.ipojo.module.h2.where.SqlRuntimeException;
import tech.guyi.ipojo.module.h2.where.WhereBuilder;
import tech.guyi.ipojo.module.h2.where.condition.WhereConditionItem;
import tech.guyi.ipojo.module.h2.where.condition.type.WhereConditionType;

import java.util.List;
import java.util.Map;

public abstract class WhereConditionBuilder {

    private WhereConditionItem item;
    private WhereBuilder where;
    private Map types;

    public WhereConditionBuilder(WhereBuilder where,Map types) {
        this.types = types;
        this.where = where;
    }
    public WhereConditionBuilder(WhereBuilder where,Map types,String field,Object value) {
        this(where,types);
        this.name(field);
        this.value(value);
    }

    public int orderNum(){
        return 0;
    }

    public WhereConditionItem getItem(){
        if (this.item == null){
            this.item = new WhereConditionItem(this.where.getEntity());
        }
        return this.item;
    }

    protected abstract String getConnectSql();

    private void check(){
        if (StringUtils.isEmpty(this.getItem().getFieldName())){
            throw new SqlRuntimeException("条件字段名不能为空");
        }
    }

    public WhereConditionBuilder name(String fieldName){
        this.getItem().setFieldName(fieldName);
        return this;
    }

    public WhereConditionBuilder value(Object value){
        this.getItem().setValue(value);
        return this;
    }

    public WhereBuilder custom(String type){
        this.check();
        this.getItem().setType(type);
        return this.where;
    }

    public WhereBuilder eq(){
        return this.custom("eq");
    }

    public WhereBuilder lt(){
        this.check();
        return this.custom("lt");
    }

    public WhereBuilder gt(){
        this.check();
        return this.custom("gt");
    }

    public WhereBuilder not(){
        this.check();
        return this.custom("not");
    }

    public WhereBuilder gtAndEq(){
        this.check();
        return this.custom("gt_eq");
    }

    public WhereBuilder ltAndEq(){
        this.check();
        return this.custom("lt_eq");
    }

    public WhereBuilder order(){
        this.check();
        return this.custom("order");
    }

    public String getSql(List converters){
        String sql = "";
        WhereConditionType type = this.types.get(this.getItem().getType());
        if (type == null){
            throw new SqlRuntimeException(String.format("不存在的条件类型[%s]",this.getItem().getType()));
        }
        for (WhereConditionTypeConverter converter : converters) {
            if (converter.check(this.getItem())){
                sql = converter.convert(type,this.getItem());
            }
        }
        return String.format("%s %s",this.getConnectSql(),sql);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy