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

xin.xihc.jba.sql.Update Maven / Gradle / Ivy

package xin.xihc.jba.sql;

import xin.xihc.jba.scan.TableManager;
import xin.xihc.jba.sql.clause.Eq;
import xin.xihc.jba.sql.clause.NativeExp;

import java.util.LinkedList;
import java.util.List;

/**
 * Update语句生成器
 *
 * @author Leo.Xi
 * @date 2020/3/5
 * @since 1.0
 **/
public class Update implements SqlType {

    /**
     * TABLE 表
     */
    private String tableName;
    /**
     * 排序字段列表
     */
    private final List sets = new LinkedList<>();
    private KV kv = new KV("t");
    private Where where;

    private Update() {
    }

    public static Update from(Class clazz) {
        Update update = new Update();
        update.tableName = TableManager.getTable(clazz).getTableName();
        return update;
    }

    public static Update from(String tableName) {
        Update update = new Update();
        update.tableName = tableName;
        return update;
    }

    public  Update set(T eq) {
        this.sets.add(eq.toSql(this.kv));
        return this;
    }

    public Update set(NativeExp eq) {
        this.sets.add(eq.toSql(this.kv));
        return this;
    }

    public Update where(Where where) {
        this.where = where;
        if (null != where) {
            this.kv.merge(where.getKv());
        }
        return this;
    }

    public KV getKv() {
        return this.kv;
    }

    @Override
    public String toSql() {
        String sql = action() + this.tableName + SqlConstants.SET + String.join(",", this.sets);
        if (this.where != null) {
            sql += this.where.toSql();
        }
        return sql;
    }

    @Override
    public String action() {
        return SqlConstants.UPDATE;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy