
com.invms.x.builders.GroupBuilder Maven / Gradle / Ivy
package com.invms.x.builders;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import com.invms.x.utils.ComUtils;
/**
* 分组构造器
*/
public class GroupBuilder {
/**
* 分组对象
*/
private List groupBys;
/**
* 获取分组语句
*
* @return 返回分组语句
*/
public String getGroupBy() {
return getGroupBy("t");
}
/**
* 获取分组语句
*
* @param prefix
* 字段前缀
* @return 返回分组语句
*/
public String getGroupBy(String prefix) {
if (groupBys != null && !groupBys.isEmpty()) {
Pattern pattern = Pattern.compile("\\.|^\\s*\\(");
List list = new ArrayList();
for (String item : groupBys) {
if (!ComUtils.empty(item)) {
String column = item;
if (!ComUtils.empty(prefix) && !pattern.matcher(column).find()) {
column = prefix + "." + column;
}
list.add(column);
}
}
return ComUtils.join(", ", list);
}
return "";
}
/**
* 设置分组语句
*
* @param groupBys
* 分组语句
*/
public void setGroupBy(String... groupBys) {
this.groupBys = null;
addGroupBy(groupBys);
}
/**
* 添加分组语句
*
* @param groupBys
* 分组语句
*/
public void addGroupBy(String... groupBys) {
if (groupBys != null && groupBys.length > 0) {
for (String groupBy : groupBys) {
String[] columns = groupBy.split(",|\\s+");
addGroupBuilder(columns);
}
}
}
/**
* 添加分组构造器
*
* @param columns
* 列名
*/
public void addGroupBuilder(String... columns) {
if (columns != null && columns.length > 0) {
if (groupBys == null) {
groupBys = new ArrayList();
}
groupBys.addAll(Arrays.asList(columns));
}
}
/**
* 添加分组构造器
*
* @param groupBuilders
* 分组构造器
*/
public void addGroupBuilder(GroupBuilder... groupBuilders) {
if (groupBuilders != null && groupBuilders.length > 0) {
for (GroupBuilder groupBuilder : groupBuilders) {
addGroupBy(groupBuilder.getGroupBy());
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy