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.ConditionFactory Maven / Gradle / Ivy
package db.sql.api.impl.cmd;
import db.sql.api.Cmd;
import db.sql.api.Getter;
import db.sql.api.cmd.LikeMode;
import db.sql.api.cmd.basic.ICondition;
import db.sql.api.cmd.executor.IQuery;
import db.sql.api.cmd.executor.method.condition.IConditionMethods;
import db.sql.api.impl.exception.ConditionArrayValueEmptyException;
import db.sql.api.impl.exception.ConditionValueNullException;
import db.sql.api.impl.tookit.SqlConst;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.*;
public class ConditionFactory implements IConditionMethods {
protected final CmdFactory cmdFactory;
private boolean isIgnoreEmpty = false;
private boolean isIgnoreNull = false;
private boolean isStringTrim = false;
public ConditionFactory(CmdFactory cmdFactory) {
this.cmdFactory = cmdFactory;
}
public CmdFactory getCmdFactory() {
return cmdFactory;
}
public boolean isIgnoreEmpty() {
return isIgnoreEmpty;
}
public void setIgnoreEmpty(boolean isIgnoreEmpty) {
this.isIgnoreEmpty = isIgnoreEmpty;
}
public boolean isIgnoreNull() {
return isIgnoreNull;
}
public void setIgnoreNull(boolean isIgnoreNull) {
this.isIgnoreNull = isIgnoreNull;
}
public boolean isStringTrim() {
return isStringTrim;
}
public void setStringTrim(boolean isStringTrim) {
this.isStringTrim = isStringTrim;
}
protected boolean isKeyValid(Cmd filed) {
return filed != null;
}
private Object getSingleValue(Object value) {
if (Objects.isNull(value)) {
if (!isIgnoreNull()) {
throw new ConditionValueNullException("条件参数里包含null值");
}
return null;
}
if (value instanceof String) {
String str = (String) value;
str = isStringTrim() ? str.trim() : str;
if (isIgnoreEmpty() && SqlConst.S_EMPTY.equals(str)) {
return null;
}
return str;
}
return value;
}
protected Object checkAndGetValidValue(Object value) {
if (Objects.isNull(value)) {
if (!isIgnoreNull()) {
throw new ConditionValueNullException("条件参数里包含null值");
}
return null;
}
if (value instanceof Object[]) {
Object[] values = (Object[]) value;
List objectList = new ArrayList<>();
for (Object v : values) {
Object nv = getSingleValue(v);
if (Objects.isNull(nv)) {
continue;
}
objectList.add(nv);
}
if (objectList.isEmpty()) {
throw new ConditionArrayValueEmptyException("array can't be empty");
}
int length = objectList.size();
if (length == values.length) {
for (int i = 0; i < length; i++) {
values[i] = objectList.get(i);
}
objectList.clear();
return values;
}
Object[] newObject = (Object[]) Array.newInstance(value.getClass().getComponentType(), length);
for (int i = 0; i < length; i++) {
newObject[i] = objectList.get(i);
}
return newObject;
} else if (value instanceof Collection) {
Collection collection = (Collection) value;
Collection objectList;
if (value instanceof List) {
objectList = new ArrayList<>();
} else if (value instanceof Set) {
objectList = new HashSet<>(collection.size());
} else if (value instanceof Queue) {
objectList = new ArrayDeque<>(collection.size());
} else {
//throw new RuntimeException("Not supported");
objectList = new ArrayList<>();
}
for (Object v : collection) {
Object nv = getSingleValue(v);
if (Objects.isNull(nv)) {
continue;
}
objectList.add(nv);
}
if (objectList.isEmpty()) {
throw new ConditionArrayValueEmptyException("collection can't be empty");
}
return collection;
}
return getSingleValue(value);
}
private Object paramWrap(Object value) {
if (isStringTrim() && value instanceof String) {
String str = (String) value;
value = str.trim();
}
return value;
}
private Cmd createTableField(Getter column, int storey) {
return cmdFactory.field(column, storey);
}
@Override
public ICondition empty(Cmd column) {
if (!isKeyValid(column)) {
return null;
}
return Methods.eq(column, Methods.value(""));
}
@Override
public ICondition empty(boolean when, Getter column, int storey) {
if (!when) {
return null;
}
return Methods.empty(createTableField(column, storey));
}
@Override
public ICondition notEmpty(Cmd column) {
if (!isKeyValid(column)) {
return null;
}
return Methods.ne(column, Methods.value(""));
}
@Override
public ICondition notEmpty(boolean when, Getter column, int storey) {
if (!when) {
return null;
}
return Methods.notEmpty(createTableField(column, storey));
}
@Override
public ICondition eq(Cmd column, Object value) {
if (!isKeyValid(column)) {
return null;
}
value = checkAndGetValidValue(value);
if (Objects.isNull(value)) {
return null;
}
return Methods.eq(column, paramWrap(value));
}
@Override
public ICondition ne(Cmd column, Object value) {
if (!isKeyValid(column)) {
return null;
}
value = checkAndGetValidValue(value);
if (Objects.isNull(value)) {
return null;
}
return Methods.ne(column, paramWrap(value));
}
@Override
public ICondition gt(Cmd column, Object value) {
if (!isKeyValid(column)) {
return null;
}
value = checkAndGetValidValue(value);
if (Objects.isNull(value)) {
return null;
}
return Methods.gt(column, paramWrap(value));
}
@Override
public ICondition gte(Cmd column, Object value) {
if (!isKeyValid(column)) {
return null;
}
value = checkAndGetValidValue(value);
if (Objects.isNull(value)) {
return null;
}
return Methods.gte(column, paramWrap(value));
}
@Override
public ICondition lt(Cmd column, Object value) {
if (!isKeyValid(column)) {
return null;
}
value = checkAndGetValidValue(value);
if (Objects.isNull(value)) {
return null;
}
return Methods.lt(column, paramWrap(value));
}
@Override
public ICondition lte(Cmd column, Object value) {
if (!isKeyValid(column)) {
return null;
}
value = checkAndGetValidValue(value);
if (Objects.isNull(value)) {
return null;
}
return Methods.lte(column, paramWrap(value));
}
@Override
public ICondition like(LikeMode mode, Cmd column, String value) {
if (!isKeyValid(column)) {
return null;
}
value = (String) checkAndGetValidValue(value);
if (Objects.isNull(value)) {
return null;
}
return Methods.like(mode, column, value);
}
@Override
public ICondition like(boolean when, LikeMode mode, Getter column, int storey, String value) {
if (!when) {
return null;
}
value = (String) checkAndGetValidValue(value);
if (Objects.isNull(value)) {
return null;
}
return Methods.like(mode, createTableField(column, storey), value);
}
@Override
public ICondition notLike(LikeMode mode, Cmd column, String value) {
if (!isKeyValid(column)) {
return null;
}
value = (String) checkAndGetValidValue(value);
if (Objects.isNull(value)) {
return null;
}
return Methods.notLike(mode, column, value);
}
@Override
public ICondition notLike(boolean when, LikeMode mode, Getter column, int storey, String value) {
if (!when) {
return null;
}
value = (String) checkAndGetValidValue(value);
if (Objects.isNull(value)) {
return null;
}
return Methods.notLike(mode, createTableField(column, storey), value);
}
@Override
public ICondition between(Cmd column, Serializable value, Serializable value2) {
if (!isKeyValid(column)) {
return null;
}
value = (Serializable) checkAndGetValidValue(value);
value2 = (Serializable) checkAndGetValidValue(value2);
if (Objects.isNull(value) || Objects.isNull(value2)) {
if (Objects.isNull(value) && Objects.isNull(value2)) {
return null;
}
throw new ConditionValueNullException("条件参数里包含null值");
}
return Methods.between(column, value, value2);
}
@Override
public ICondition between(boolean when, Getter column, int storey, Serializable value, Serializable value2) {
value = (Serializable) checkAndGetValidValue(value);
value2 = (Serializable) checkAndGetValidValue(value2);
if (Objects.isNull(value) || Objects.isNull(value2)) {
if (Objects.isNull(value) && Objects.isNull(value2)) {
return null;
}
throw new ConditionValueNullException("条件参数里包含null值");
}
return Methods.between(createTableField(column, storey), value, value2);
}
@Override
public ICondition notBetween(Cmd column, Serializable value, Serializable value2) {
if (!isKeyValid(column)) {
return null;
}
value = (Serializable) checkAndGetValidValue(value);
value2 = (Serializable) checkAndGetValidValue(value2);
if (Objects.isNull(value) || Objects.isNull(value2)) {
if (Objects.isNull(value) && Objects.isNull(value2)) {
return null;
}
throw new ConditionValueNullException("条件参数里包含null值");
}
return Methods.notBetween(column, value, value2);
}
@Override
public ICondition notBetween(boolean when, Getter column, int storey, Serializable value, Serializable value2) {
if (!when) {
return null;
}
value = (Serializable) checkAndGetValidValue(value);
value2 = (Serializable) checkAndGetValidValue(value2);
if (Objects.isNull(value) || Objects.isNull(value2)) {
if (Objects.isNull(value) && Objects.isNull(value2)) {
return null;
}
throw new ConditionValueNullException("条件参数里包含null值");
}
return Methods.notBetween(createTableField(column, storey), value, value2);
}
@Override
public ICondition isNull(Cmd column) {
if (!isKeyValid(column)) {
return null;
}
return Methods.isNull(column);
}
@Override
public ICondition isNotNull(Cmd column) {
if (!isKeyValid(column)) {
return null;
}
return Methods.isNotNull(column);
}
@Override
public ICondition eq(boolean when, Getter column, int storey, Object value) {
if (!when) {
return null;
}
value = checkAndGetValidValue(value);
if (Objects.isNull(value)) {
return null;
}
return Methods.eq(createTableField(column, storey), paramWrap(value));
}
@Override
public ICondition eq(boolean when, Getter column, int columnStorey, Getter value, int valueStorey) {
if (!when) {
return null;
}
return Methods.eq(createTableField(column, columnStorey), createTableField(value, valueStorey));
}
@Override
public ICondition gt(boolean when, Getter column, int storey, Object value) {
if (!when) {
return null;
}
value = checkAndGetValidValue(value);
if (Objects.isNull(value)) {
return null;
}
return Methods.gt(createTableField(column, storey), paramWrap(value));
}
@Override
public ICondition gt(boolean when, Getter column, int columnStorey, Getter value, int valueStorey) {
if (!when) {
return null;
}
return Methods.gt(createTableField(column, columnStorey), createTableField(value, valueStorey));
}
@Override
public ICondition gte(boolean when, Getter column, int storey, Object value) {
if (!when) {
return null;
}
value = checkAndGetValidValue(value);
if (Objects.isNull(value)) {
return null;
}
return Methods.gte(createTableField(column, storey), paramWrap(value));
}
@Override
public ICondition gte(boolean when, Getter column, int columnStorey, Getter value, int valueStorey) {
if (!when) {
return null;
}
return Methods.gte(createTableField(column, columnStorey), createTableField(value, valueStorey));
}
@Override
public ICondition lt(boolean when, Getter column, int storey, Object value) {
if (!when) {
return null;
}
value = checkAndGetValidValue(value);
if (Objects.isNull(value)) {
return null;
}
return Methods.lt(createTableField(column, storey), paramWrap(value));
}
@Override
public ICondition lt(boolean when, Getter column, int columnStorey, Getter value, int valueStorey) {
if (!when) {
return null;
}
return Methods.lt(createTableField(column, columnStorey), createTableField(value, valueStorey));
}
@Override
public ICondition lte(boolean when, Getter column, int storey, Object value) {
if (!when) {
return null;
}
value = checkAndGetValidValue(value);
if (Objects.isNull(value)) {
return null;
}
return Methods.lte(createTableField(column, storey), paramWrap(value));
}
@Override
public ICondition lte(boolean when, Getter column, int columnStorey, Getter value, int valueStorey) {
if (!when) {
return null;
}
return Methods.lte(createTableField(column, columnStorey), createTableField(value, valueStorey));
}
@Override
public ICondition ne(boolean when, Getter column, int storey, Object value) {
if (!when) {
return null;
}
value = checkAndGetValidValue(value);
if (Objects.isNull(value)) {
return null;
}
return Methods.ne(createTableField(column, storey), paramWrap(value));
}
@Override
public ICondition ne(boolean when, Getter column, int columnStorey, Getter value, int valueStorey) {
if (!when) {
return null;
}
return Methods.ne(createTableField(column, columnStorey), createTableField(value, valueStorey));
}
@Override
public ICondition isNull(boolean when, Getter column, int storey) {
if (!when) {
return null;
}
return Methods.isNull(createTableField(column, storey));
}
@Override
public ICondition isNotNull(boolean when, Getter column, int storey) {
if (!when) {
return null;
}
return Methods.isNotNull(createTableField(column, storey));
}
@Override
public ICondition in(Cmd column, IQuery query) {
Objects.requireNonNull(query);
return Methods.in(column, query);
}
@Override
@SafeVarargs
public final ICondition in(Cmd column, Serializable... values) {
values = (Serializable[]) checkAndGetValidValue(values);
if (Objects.isNull(values)) {
return null;
}
return Methods.in(column, (Object[]) values);
}
@Override
public ICondition in(Cmd column, Collection extends Serializable> values) {
values = (List) checkAndGetValidValue(values);
if (Objects.isNull(values)) {
return null;
}
return Methods.in(column, values);
}
@Override
public ICondition in(boolean when, Getter column, int storey, IQuery query) {
if (!when) {
return null;
}
Objects.requireNonNull(query);
return Methods.in(createTableField(column, storey), query);
}
@Override
@SafeVarargs
public final ICondition in(boolean when, Getter column, int storey, Serializable... values) {
if (!when) {
return null;
}
values = (Serializable[]) checkAndGetValidValue(values);
if (Objects.isNull(values)) {
return null;
}
return Methods.in(createTableField(column, storey), (Object[]) values);
}
@Override
public ICondition in(boolean when, Getter column, int storey, Collection extends Serializable> values) {
if (!when) {
return null;
}
values = (Collection) checkAndGetValidValue(values);
if (Objects.isNull(values)) {
return null;
}
return Methods.in(createTableField(column, storey), values);
}
@Override
public ICondition exists(boolean when, IQuery query) {
if (!when) {
return null;
}
Objects.requireNonNull(query);
return Methods.exists(query);
}
@Override
public ICondition notExists(boolean when, IQuery query) {
if (!when) {
return null;
}
Objects.requireNonNull(query);
return Methods.notExists(query);
}
@Override
public ICondition notIn(Cmd column, IQuery query) {
Objects.requireNonNull(query);
return Methods.notIn(column, query);
}
@Override
@SafeVarargs
public final ICondition notIn(Cmd column, Serializable... values) {
values = (Serializable[]) checkAndGetValidValue(values);
if (Objects.isNull(values)) {
return null;
}
return Methods.notIn(column, (Object[]) values);
}
@Override
public ICondition notIn(Cmd column, Collection extends Serializable> values) {
values = (List) checkAndGetValidValue(values);
if (Objects.isNull(values)) {
return null;
}
return Methods.notIn(column, values);
}
@Override
public ICondition notIn(boolean when, Getter column, int storey, IQuery query) {
if (!when) {
return null;
}
Objects.requireNonNull(query);
return Methods.notIn(createTableField(column, storey), query);
}
@Override
@SafeVarargs
public final ICondition notIn(boolean when, Getter column, int storey, Serializable... values) {
if (!when) {
return null;
}
Object[] newValues = (Serializable[]) checkAndGetValidValue(values);
if (Objects.isNull(newValues)) {
return null;
}
return Methods.notIn(createTableField(column, storey), newValues);
}
@Override
public ICondition notIn(boolean when, Getter column, int storey, Collection extends Serializable> values) {
if (!when) {
return null;
}
values = (Collection extends Serializable>) checkAndGetValidValue(values);
if (Objects.isNull(values)) {
return null;
}
return Methods.notIn(createTableField(column, storey), values);
}
}