Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
db.sql.api.impl.cmd.executor.AbstractQuery Maven / Gradle / Ivy
package db.sql.api.impl.cmd.executor;
import db.sql.api.Cmd;
import db.sql.api.Getter;
import db.sql.api.cmd.ColumnField;
import db.sql.api.cmd.GetterField;
import db.sql.api.cmd.IColumnField;
import db.sql.api.cmd.JoinMode;
import db.sql.api.cmd.basic.*;
import db.sql.api.cmd.executor.IQuery;
import db.sql.api.cmd.executor.IWithQuery;
import db.sql.api.cmd.struct.Joins;
import db.sql.api.cmd.struct.query.Unions;
import db.sql.api.cmd.struct.query.Withs;
import db.sql.api.impl.cmd.CmdFactory;
import db.sql.api.impl.cmd.ConditionFactory;
import db.sql.api.impl.cmd.Methods;
import db.sql.api.impl.cmd.basic.*;
import db.sql.api.impl.cmd.struct.*;
import db.sql.api.impl.cmd.struct.query.*;
import db.sql.api.impl.tookit.LambdaUtil;
import db.sql.api.impl.tookit.SqlConst;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
public abstract class AbstractQuery,
CMD_FACTORY extends CmdFactory>
extends BaseExecutor
implements IQuery,
Where,
GroupBy,
Having,
OrderBy,
Limit,
ForUpdate,
Union
>, Cmd {
protected final ConditionFactory conditionFactory;
protected final CMD_FACTORY $;
protected Select select;
protected Withs withs;
protected From from;
protected Where where;
protected Joins joins;
protected GroupBy groupBy;
protected Having having;
protected OrderBy orderBy;
protected Limit limit;
protected ForUpdate forUpdate;
protected Unions unions;
protected Map> fetchFilters;
public AbstractQuery(CMD_FACTORY $) {
this.$ = $;
this.conditionFactory = new ConditionFactory($);
}
public AbstractQuery(Where where) {
this.$ = (CMD_FACTORY) where.getConditionFactory().getCmdFactory();
this.conditionFactory = where.getConditionFactory();
this.where = where;
this.append(where);
}
@Override
public CMD_FACTORY $() {
return $;
}
public TableField $(Getter getter) {
return this.$(getter, 1);
}
public TableField $(Getter getter, int storey) {
return $().field(getter, storey);
}
public Table $(Class entityType) {
return this.$(entityType, 1);
}
public Table $(Class entityType, int storey) {
return $().table(entityType, storey);
}
public TableField $(Class entityType, String fieldName) {
return this.$(entityType, fieldName, 1);
}
public TableField $(Class entityType, String fieldName, int storey) {
return $().field(entityType, fieldName, storey);
}
public , DATASET_FIELD extends IDatasetField> DATASET_FIELD $(IDataset dataset, String columnName) {
return this.$().field(dataset, columnName);
}
public , DATASET_FIELD extends IDatasetField> DATASET_FIELD $(IDataset dataset, Getter getter) {
return this.$().field(dataset, getter);
}
@Override
public SELF fetchFilter(Getter getter, Consumer where) {
LambdaUtil.LambdaFieldInfo lambdaFieldInfo = LambdaUtil.getFieldInfo(getter);
String key = lambdaFieldInfo.getType().getName() + "." + lambdaFieldInfo.getName();
if (Objects.isNull(fetchFilters)) {
this.fetchFilters = new HashMap<>();
}
fetchFilters.put(key, where);
return (SELF) this;
}
@Override
public Map> getFetchFilters() {
return fetchFilters;
}
public void setFetchFilters(Map> fetchFilters) {
if (Objects.nonNull(this.fetchFilters)) {
throw new RuntimeException("Can't call setFetchFilters when the this.fetchFilters has value");
}
this.fetchFilters = fetchFilters;
}
@Override
protected void initCmdSorts(Map, Integer> cmdSorts) {
int i = 0;
cmdSorts.put(Withs.class, i += 10);
cmdSorts.put(Select.class, i += 10);
cmdSorts.put(From.class, i += 10);
cmdSorts.put(Joins.class, i += 10);
cmdSorts.put(Where.class, i += 10);
cmdSorts.put(GroupBy.class, i += 10);
cmdSorts.put(Having.class, i += 10);
cmdSorts.put(OrderBy.class, i += 10);
cmdSorts.put(Limit.class, i += 10);
cmdSorts.put(ForUpdate.class, i += 10);
cmdSorts.put(Unions.class, i += 10);
cmdSorts.put(UnionsCmdLists.class, i += 10);
}
@Override
public With $with(IWithQuery withQuery) {
if (Objects.isNull(this.withs)) {
this.withs = new Withs();
this.append(this.withs);
}
With with = new With(withQuery);
this.withs.add(with);
return with;
}
@Override
public Select $select() {
if (select == null) {
select = new Select();
this.append(select);
}
return select;
}
@Override
public SELF selectCount1() {
this.select(Count1.INSTANCE);
return (SELF) this;
}
@Override
public SELF selectCountAll() {
this.select(CountAll.INSTANCE);
return (SELF) this;
}
/**
* select 子查询 列
*
* @param column 列
* @param storey 列存储层级
* @param 列的实体类
* @param f 转换函数
* @return 自己
*/
@Override
public SELF select(Getter column, int storey, Function f) {
if (Objects.isNull(f)) {
return this.select($.field(column, storey));
}
return this.select(f.apply($.field(column, storey)));
}
@Override
public SELF select(GetterField[] getterFields, Function f) {
if (Objects.isNull(f)) {
return this.select($.fields(getterFields));
}
return this.select(f.apply($.fields(getterFields)));
}
@Override
public SELF select(int storey, Getter... columns) {
return this.select($.fields(storey, columns));
}
@Override
public SELF select(String columnName) {
return this.select(Methods.column(columnName));
}
@Override
public SELF select(String columnName, Function f) {
return this.select(f.apply(Methods.column(columnName)));
}
/**
* select 子查询 列
*
* @param dataset 子查询
* @param columnName 列
* @param f 转换函数
* @return
*/
@Override
public , DATASET_FIELD extends IDatasetField> SELF select(IDataset dataset, String columnName, Function f) {
return this.select(f.apply(this.$(dataset, columnName)));
}
@Override
public , DATASET_FIELD extends IDatasetField> SELF select(IDataset dataset, GetterField[] getterFields, Function f) {
if (Objects.isNull(f)) {
return this.select(this.getDatasetFields(dataset, getterFields));
}
return this.select(this.apply(dataset, f, getterFields));
}
@Override
public , DATASET_FIELD extends IDatasetField> SELF select(IDataset dataset, String columnName) {
return this.select(this.$(dataset, columnName));
}
public , DATASET_FIELD extends IDatasetField> SELF select(IDataset dataset, Getter column, Function f) {
return this.select(f.apply(this.$(dataset, column)));
}
@Override
public From $from(IDataset table) {
if (this.from == null) {
from = new From();
this.append(from);
}
this.from.append(table);
return from;
}
@Override
public SELF from(Class entity, int storey, Consumer consumer) {
this.fromEntityIntercept(entity, storey);
Table table = $.table(entity, storey);
this.from(table);
if (Objects.nonNull(consumer)) {
consumer.accept(table);
}
return (SELF) this;
}
@Override
public Join $join(JoinMode mode, IDataset mainTable, IDataset secondTable) {
Join join = new Join(mode, mainTable, secondTable, (joinDataset -> new On(this.conditionFactory, joinDataset)));
if (Objects.isNull(joins)) {
joins = new Joins();
this.append(joins);
}
joins.add(join);
return join;
}
@Override
public SELF join(JoinMode mode, Class mainTable, int mainTableStorey, Class secondTable, int secondTableStorey, Consumer consumer) {
consumer = this.joinEntityIntercept(mainTable, mainTableStorey, secondTable, secondTableStorey, consumer);
return this.join(mode, $.table(mainTable, mainTableStorey), $.table(secondTable, secondTableStorey), consumer);
}
@Override
public SELF join(JoinMode mode, Class mainTable, int mainTableStorey, Class secondTable, int secondTableStorey, BiConsumer consumer) {
return this.join(mode, mainTable, mainTableStorey, secondTable, secondTableStorey, (on) -> {
consumer.accept((Table) on.getJoin().getSecondTable(), on);
});
}
@Override
public , DATASET_FIELD extends IDatasetField> SELF join(JoinMode mode, Class mainTable, int mainTableStorey, DATASET secondTable, Consumer consumer) {
return this.join(mode, $.table(mainTable, mainTableStorey), secondTable, consumer);
}
@Override
public Where $where() {
if (where == null) {
where = new Where(this.conditionFactory);
this.append(where);
}
return where;
}
@Override
public SELF and(Getter column, int storey, Function f) {
$where().and(column, storey, f);
return (SELF) this;
}
@Override
public SELF or(Getter column, int storey, Function f) {
$where().or(column, storey, f);
return (SELF) this;
}
@Override
public , DATASET_FIELD extends IDatasetField, DATASET2 extends IDataset, DATASET_FIELD2 extends IDatasetField> SELF join(JoinMode mode, DATASET mainTable, DATASET2 secondTable, Consumer consumer) {
Join join = $join(mode, mainTable, secondTable);
if (consumer != null) {
consumer.accept(join.getOn());
}
return (SELF) this;
}
@Override
public GroupBy $groupBy() {
if (groupBy == null) {
groupBy = new GroupBy();
this.append(groupBy);
}
return groupBy;
}
/**
* groupBy 子查询 列
*
* @param column 列
* @param storey 列存储层级
* @param 列的实体类
* @param f 转换函数
* @return 自己
*/
@Override
public SELF groupBy(Getter column, int storey, Function f) {
if (Objects.isNull(f)) {
return this.groupBy($.field(column, storey));
}
return this.groupBy(f.apply($.field(column, storey)));
}
@Override
public SELF groupBy(GetterField[] getterFields, Function f) {
if (Objects.isNull(f)) {
return this.groupBy($.fields(getterFields));
}
return this.groupBy(f.apply($.fields(getterFields)));
}
@Override
public SELF groupBy(int storey, Getter... columns) {
return this.groupBy($.fields(storey, columns));
}
@Override
public SELF groupBy(String columnName) {
return this.groupBy(Methods.column(columnName));
}
@Override
public SELF groupBy(String columnName, Function f) {
return this.groupBy(f.apply(Methods.column(columnName)));
}
/**
* groupBy 子查询 列
*
* @param dataset 子查询
* @param column 列
* @param 列的实体类
* @return
*/
@Override
public , DATASET_FIELD extends IDatasetField> SELF groupBy(IDataset dataset, Getter column) {
return this.groupBy(this.$(dataset, column));
}
/**
* groupBy 子查询 列
*
* @param dataset 子查询
* @param columnName 列
* @param f 转换函数
* @return
*/
@Override
public , DATASET_FIELD extends IDatasetField> SELF groupBy(IDataset dataset, String columnName, Function f) {
return this.groupBy(f.apply(this.$(dataset, columnName)));
}
@Override
public , DATASET_FIELD extends IDatasetField> SELF groupBy(IDataset dataset, GetterField[] getterFields, Function f) {
if (Objects.isNull(f)) {
return this.groupBy(this.getDatasetFields(dataset, getterFields));
}
return this.groupBy(this.apply(dataset, f, getterFields));
}
@Override
public , DATASET_FIELD extends IDatasetField> SELF groupBy(IDataset dataset, Getter column, Function f) {
return this.groupBy(f.apply(this.$(dataset, column)));
}
@Override
public Having $having() {
if (having == null) {
having = new Having(this.$);
this.append(having);
}
return having;
}
@Override
public SELF havingAnd(boolean when, Getter column, int storey, Function f) {
if (!when) {
return (SELF) this;
}
return this.havingAnd(f.apply($.field(column, storey)));
}
@Override
public SELF havingOr(boolean when, Getter column, int storey, Function f) {
if (!when) {
return (SELF) this;
}
return this.havingOr(f.apply($.field(column, storey)));
}
@Override
public , DATASET_FIELD extends IDatasetField> SELF havingAnd(boolean when, IDataset dataset, Getter column, Function f) {
if (!when) {
return (SELF) this;
}
return this.havingAnd(f.apply(this.$(dataset, column)));
}
@Override
public , DATASET_FIELD extends IDatasetField> SELF havingOr(boolean when, IDataset dataset, Getter column, Function f) {
if (!when) {
return (SELF) this;
}
return this.havingOr(f.apply(this.$(dataset, column)));
}
@Override
public , DATASET_FIELD extends IDatasetField> SELF havingAnd(IDataset dataset, String columnName, Function f) {
return this.havingAnd(f.apply(this.$(dataset, columnName)));
}
@Override
public , DATASET_FIELD extends IDatasetField> SELF havingOr(IDataset dataset, String columnName, Function f) {
return this.havingOr(f.apply(this.$(dataset, columnName)));
}
@Override
public SELF havingAnd(boolean when, GetterField[] getterFields, Function f) {
if (!when) {
return (SELF) this;
}
return this.havingAnd(f.apply($.fields(getterFields)));
}
@Override
public SELF havingOr(boolean when, GetterField[] getterFields, Function f) {
if (!when) {
return (SELF) this;
}
return this.havingOr(f.apply($.fields(getterFields)));
}
private , DATASET_FIELD extends IDatasetField> IDatasetField[] getDatasetFields(IDataset dataset, IColumnField... columnFields) {
IDatasetField[] datasetFields = new IDatasetField[columnFields.length];
for (int i = 0; i < columnFields.length; i++) {
IColumnField columnField = columnFields[i];
if (columnField instanceof ColumnField) {
datasetFields[i] = this.$(dataset, ((ColumnField) columnField).getColumnName());
} else if (columnField instanceof GetterField) {
datasetFields[i] = this.$(dataset, ((GetterField>) columnField).getGetter());
} else {
throw new RuntimeException("Not Supported");
}
}
return datasetFields;
}
private , DATASET_FIELD extends IDatasetField> R apply(IDataset dataset, Function f, IColumnField... columnFields) {
return f.apply(getDatasetFields(dataset, columnFields));
}
@Override
public , DATASET_FIELD extends IDatasetField> SELF havingAnd(boolean when, IDataset dataset, String columnName, Function f) {
if (!when) {
return (SELF) this;
}
return this.havingAnd(f.apply(this.$(dataset, columnName)));
}
@Override
public , DATASET_FIELD extends IDatasetField> SELF havingOr(boolean when, IDataset dataset, String columnName, Function f) {
if (!when) {
return (SELF) this;
}
return this.havingOr(f.apply(this.$(dataset, columnName)));
}
@Override
public , DATASET_FIELD extends IDatasetField> SELF havingAnd(boolean when, IDataset dataset, GetterField[] getterFields, Function f) {
if (!when) {
return (SELF) this;
}
return this.havingAnd(this.apply(dataset, f, getterFields));
}
@Override
public , DATASET_FIELD extends IDatasetField> SELF havingOr(boolean when, IDataset dataset, GetterField[] getterFields, Function f) {
if (!when) {
return (SELF) this;
}
return this.havingOr(this.apply(dataset, f, getterFields));
}
@Override
public OrderBy $orderBy() {
if (orderBy == null) {
orderBy = new OrderBy();
this.append(orderBy);
}
return orderBy;
}
@Override
public IOrderByDirection ascOrderByDirection() {
return OrderByDirection.ASC;
}
@Override
public IOrderByDirection descOrderByDirection() {
return OrderByDirection.DESC;
}
@Override
public ForUpdate $forUpdate() {
if (forUpdate == null) {
forUpdate = new ForUpdate();
this.append(forUpdate);
}
return forUpdate;
}
@Override
public Limit $limit() {
if (this.limit == null) {
this.limit = new Limit(0, 0);
this.append(this.limit);
}
return this.limit;
}
/**
* orderBy 列
*
* @param column 列
* @param storey 列存储层级
* @param 列的实体类
* @param f 转换函数
* @return 自己
*/
@Override
public SELF orderBy(IOrderByDirection orderByDirection, Getter column, int storey, Function f) {
if (Objects.isNull(f)) {
return this.orderBy(orderByDirection, $.field(column, storey));
}
return this.orderBy(orderByDirection, f.apply($.field(column, storey)));
}
@Override
public SELF orderBy(IOrderByDirection orderByDirection, GetterField[] getterFields, Function f) {
if (Objects.isNull(f)) {
return this.orderBy(orderByDirection, $.fields(getterFields));
}
return this.orderBy(orderByDirection, f.apply($.fields(getterFields)));
}
@Override
public SELF orderBy(IOrderByDirection orderByDirection, int storey, Getter... columns) {
return this.orderBy(orderByDirection, $.fields(storey, columns));
}
@Override
public SELF orderBy(IOrderByDirection orderByDirection, String columnName) {
return this.orderBy(orderByDirection, Methods.column(columnName));
}
@Override
public SELF orderBy(IOrderByDirection orderByDirection, String columnName, Function f) {
if (Objects.isNull(f)) {
return this.orderBy(orderByDirection, Methods.column(columnName));
}
return this.orderBy(orderByDirection, f.apply(Methods.column(columnName)));
}
/**
* orderBy 子查询 列
*
* @param dataset 子查询
* @param column 列
* @param f 转换函数
* @param 列的实体类
* @return
*/
@Override
public , DATASET_FIELD extends IDatasetField> SELF orderBy(IDataset dataset, IOrderByDirection orderByDirection, Getter column, Function f) {
if (Objects.isNull(f)) {
return this.orderBy(orderByDirection, this.$(dataset, column));
}
return this.orderBy(orderByDirection, f.apply(this.$(dataset, column)));
}
/**
* orderBy 子查询 列
*
* @param dataset 子查询
* @param columnName 列
* @param f 转换函数
* @return
*/
@Override
public , DATASET_FIELD extends IDatasetField> SELF orderBy(IDataset dataset, IOrderByDirection orderByDirection, String columnName, Function f) {
if (Objects.isNull(f)) {
return this.orderBy(orderByDirection, this.$(dataset, columnName));
}
return this.orderBy(orderByDirection, f.apply(this.$(dataset, columnName)));
}
@Override
public , DATASET_FIELD extends IDatasetField> SELF orderBy(IDataset dataset, IOrderByDirection orderByDirection, GetterField[] getterFields, Function f) {
if (Objects.isNull(f)) {
return this.orderBy(orderByDirection, this.getDatasetFields(dataset, getterFields));
}
return this.orderBy(orderByDirection, this.apply(dataset, f, getterFields));
}
@Override
public , DATASET_FIELD extends IDatasetField> SELF orderBy(IDataset dataset, IOrderByDirection orderByDirection, String columnName) {
return this.orderBy(orderByDirection, this.$(dataset, columnName));
}
public Unions $unions() {
if (this.unions == null) {
this.unions = new Unions();
this.cmds.add(unions);
}
return this.unions;
}
@Override
public SELF union(IQuery unionQuery) {
$unions().add(new Union(unionQuery));
return (SELF) this;
}
@Override
public SELF unionAll(IQuery unionQuery) {
$unions().add(new Union(SqlConst.UNION_ALL, unionQuery));
return (SELF) this;
}
@Override
public Select getSelect() {
return this.select;
}
@Override
public From getFrom() {
return this.from;
}
@Override
public Joins getJoins() {
return this.joins;
}
@Override
public Where getWhere() {
return this.where;
}
@Override
public GroupBy getGroupBy() {
return this.groupBy;
}
@Override
public OrderBy getOrderBy() {
return this.orderBy;
}
@Override
public Limit getLimit() {
return this.limit;
}
@Override
public Unions getUnions() {
return this.unions;
}
@Override
public ForUpdate getForUpdate() {
return forUpdate;
}
}