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.AbstractUpdate Maven / Gradle / Ivy
package db.sql.api.impl.cmd.executor;
import db.sql.api.Cmd;
import db.sql.api.DbType;
import db.sql.api.Getter;
import db.sql.api.SqlBuilderContext;
import db.sql.api.cmd.JoinMode;
import db.sql.api.cmd.basic.ICondition;
import db.sql.api.cmd.basic.IDataset;
import db.sql.api.cmd.basic.IDatasetField;
import db.sql.api.cmd.executor.IUpdate;
import db.sql.api.cmd.struct.Joins;
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.Table;
import db.sql.api.impl.cmd.basic.TableField;
import db.sql.api.impl.cmd.struct.*;
import db.sql.api.impl.cmd.struct.update.UpdateSets;
import db.sql.api.impl.cmd.struct.update.UpdateTable;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
public abstract class AbstractUpdate,
CMD_FACTORY extends CmdFactory
>
extends BaseExecutor
implements IUpdate {
protected final ConditionFactory conditionFactory;
protected final CMD_FACTORY $;
protected UpdateTable updateTable;
protected From from;
protected UpdateSets updateSets;
protected Where where;
protected Joins joins;
public AbstractUpdate(CMD_FACTORY $) {
this.$ = $;
this.conditionFactory = new ConditionFactory($);
}
public AbstractUpdate(Where where) {
this.$ = (CMD_FACTORY) where.getConditionFactory().getCmdFactory();
this.conditionFactory = where.getConditionFactory();
this.where = where;
this.append(where);
}
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);
}
@Override
public CMD_FACTORY $() {
return $;
}
@Override
protected void initCmdSorts(Map, Integer> cmdSorts) {
int i = 0;
cmdSorts.put(UpdateTable.class, i += 10);
cmdSorts.put(UpdateSets.class, i += 10);
cmdSorts.put(From.class, i += 10);
cmdSorts.put(Joins.class, i += 10);
cmdSorts.put(Where.class, i += 10);
}
@Override
public UpdateTable $update(Table... tables) {
if (this.updateTable == null) {
this.updateTable = new UpdateTable(tables);
this.append(this.updateTable);
}
return this.updateTable;
}
@Override
public SELF update(Class entity, Consumer consumer) {
this.updateEntityIntercept(entity);
Table table = this.$.table(entity);
this.update(table);
return (SELF) this;
}
@Override
public SELF update(Class... entities) {
Table[] tables = new Table[entities.length];
for (int i = 0; i < entities.length; i++) {
Class entity = entities[i];
this.updateEntityIntercept(entity);
tables[i] = $.table(entity);
}
return this.update(tables);
}
@Override
public SELF set(TableField field, Object value) {
value = field.paramWrap(value);
Cmd v = Methods.cmd(value);
if (this.updateSets == null) {
this.updateSets = new UpdateSets();
this.append(this.updateSets);
}
this.updateSets.set(field, v);
return (SELF) this;
}
public SELF set(boolean when, Getter field, Object value) {
if (!when) {
return (SELF) this;
}
return this.set(field, value);
}
public SELF set(Getter field, V value, Predicate predicate) {
return this.set(predicate.test(value), field, value);
}
@Override
public SELF set(Getter field, Object value) {
return this.set($.field(field), value);
}
@Override
public SELF set(Getter field, Function f) {
TableField tableField = $.field(field);
return this.set(tableField, f.apply(tableField));
}
@Override
public Join $join(JoinMode mode, IDataset mainTable, IDataset secondTable) {
Join join = new Join(mode, mainTable, secondTable, this::apply);
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, this.$.table(mainTable, mainTableStorey), this.$.table(secondTable, secondTableStorey), consumer);
}
@Override
public , DATASET_FIELD extends IDatasetField> SELF join(JoinMode mode, Class mainTable, int mainTableStorey, DATASET secondTable, Consumer consumer) {
return this.join(mode, this.$.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 From $from(IDataset... tables) {
if (this.from == null) {
from = new From();
this.append(from);
}
this.from.append(tables);
return from;
}
@Override
public SELF from(Class entity, int storey, Consumer consumer) {
this.fromEntityIntercept(entity, storey);
Table table = this.$.table(entity, storey);
this.from(table);
if (Objects.nonNull(consumer)) {
consumer.accept(table);
}
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;
}
public UpdateTable getUpdateTable() {
return this.updateTable;
}
public UpdateSets getUpdateSets() {
return this.updateSets;
}
public Joins getJoins() {
return this.joins;
}
public Where getWhere() {
return this.where;
}
public From getFrom() {
return from;
}
private On apply(Join joinTable) {
return new On(this.conditionFactory, joinTable);
}
@Override
public StringBuilder sql(SqlBuilderContext context, StringBuilder sqlBuilder) {
if (Objects.nonNull(this.getJoins())) {
if (context.getDbType() == DbType.MYSQL || context.getDbType() == DbType.MARIA_DB || context.getDbType() == DbType.DM) {
// mysql dm 类数据库 update join 是在 update table 之后的
this.cmdSorts().remove(Joins.class);
this.cmdSorts().put(Joins.class, this.cmdSorts().get(UpdateTable.class) + 1);
}
}
return super.sql(context, sqlBuilder);
}
}