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

com.jladder.db.jdbc.GroupBy Maven / Gradle / Ivy

There is a newer version: 5.0.44
Show newest version
package com.jladder.db.jdbc;

import com.jladder.db.Cnd;
import com.jladder.lang.Collections;
import com.jladder.lang.Strings;

import java.util.ArrayList;
import java.util.List;

public class GroupBy
{
    /// 
    /// 片选字段
    /// 
    private List _cs = new ArrayList();
    /// 
    /// 分组条件
    /// 
    private String _having ="";
    /// 
    /// 初始化
    /// 
    public GroupBy() { }
    /// 
    /// 初始化
    /// 
    /// 分组文本
    public GroupBy(String groupText)
    {
        if (Strings.isBlank(groupText)) return;
        addGroup(groupText);
    }
    /// 
    /// 初始化
    /// 
    /// 条件对象
    public GroupBy(Cnd cnd)
    {
        _having = cnd.getWhere(false,true);
    }
    /// 
    /// 文本化
    /// 
    /// 
    public String toString()
    {
        final String[] groupText = {""};
        if (this._cs.size() == 0) return groupText[0];
        _cs.forEach(x -> groupText[0] += x+",");
        groupText[0] = Strings.rightLess(groupText[0], 1);
        if (!Strings.isBlank(_having)) groupText[0] += " having " + _having;
        return " group by "+ groupText[0];
    }
    /// 
    /// 修改条件文本
    /// 
    /// 条件文本
    public void having(String havingText)
    {
        if (Strings.isBlank(havingText)) this._having = "";
        this._having = havingText;
    }
    /// 
    /// 增加分组条件
    /// 
    /// 条件对象
    /// 
    public GroupBy pushHaving(Cnd cnd)
    {
        this._having += " and " + cnd.getWhere(false,true);
        return this;
    }
    /// 
    /// 增加分组条件
    /// 
    /// 条件文本
    /// 
    public GroupBy pushHaving(String cndStr)
    {
        this._having += " and " + cndStr;
        return this;
    }
    /// 
    /// 增加分组字段
    /// 
    /// 字段文本
    /// 
    public GroupBy pushGroup(String groupText)
    {
        if (Strings.isBlank(groupText)) return this;
        addGroup(groupText);
        return this;
    }
    /// 
    /// 添加一个分组项[内部方法]
    /// 
    /// 分组项文本
    public void addGroup(String text)
    {
        if (Strings.isBlank(text)) return;
        List gs = Strings.splitByComma(text);
        gs.forEach(x -> {
            if (!Strings.isBlank(x) && !Collections.any(_cs, d -> d.equals(x.toLowerCase()))) this._cs.add(x);
        });
    }

    /// 
    /// 清空分组
    /// 
    public void clear()
    {
        this._cs.clear();
        this._having = "";
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy