org.rx.io.EntityQueryLambda Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxlib Show documentation
Show all versions of rxlib Show documentation
A set of utilities for Java
package org.rx.io;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import org.rx.bean.BiTuple;
import org.rx.bean.Tuple;
import org.rx.core.Extends;
import org.rx.core.Linq;
import org.rx.core.Reflects;
import org.rx.core.StringBuilder;
import org.rx.third.guava.CaseFormat;
import org.rx.util.function.BiFunc;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@RequiredArgsConstructor
public class EntityQueryLambda implements Extends {
private static final long serialVersionUID = 6543834322640433503L;
@RequiredArgsConstructor
enum Operator {
AND("(%s)AND(%s)"), OR("(%s)OR(%s)"),
EQ("%s=%s"), NE("%s!=%s"),
GT("%s>%s"), LT("%s<%s"),
GE("%s>=%s"), LE("%s<=%s"),
IN("%s IN(%s)"), NOT_IN("%s NOT IN(%s)"),
BETWEEN("%s BETWEEN %s AND %s"), NOT_BETWEEN("%s NOT BETWEEN %s AND %s"),
LIKE("%s LIKE %s"), NOT_LIKE("%s NOT LIKE %s");
final String format;
}
enum Order {
ASC,
DESC
}
static final String WHERE = " WHERE ", ORDER_BY = " ORDER BY ", GROUP_BY = " GROUP BY ", LIMIT = " LIMIT ",
OP_AND = " AND ", DB_NULL = "NULL", PARAM_HOLD = "?";
static final String FUNC_RAND = "RAND()";
static void pkClaus(StringBuilder sql, String pk) {
sql.append(WHERE).appendFormat(Operator.EQ.format, pk, PARAM_HOLD);
}
final Class entityType;
@Setter
boolean autoUnderscoreColumnName;
final ArrayList> conditions = new ArrayList<>();
final List, Order>> orders = new ArrayList<>();
boolean orderByRand;
Integer limit, offset;
public EntityQueryLambda limit(int limit) {
this.limit = limit;
return this;
}
public EntityQueryLambda limit(int offset, int limit) {
this.offset = offset;
this.limit = limit;
return this;
}
public EntityQueryLambda newClause() {
return new EntityQueryLambda<>(entityType);
}
public EntityQueryLambda orderBy(BiFunc fn) {
orders.add(Tuple.of(fn, Order.ASC));
return this;
}
public EntityQueryLambda orderByDescending(BiFunc fn) {
orders.add(Tuple.of(fn, Order.DESC));
return this;
}
public EntityQueryLambda orderByRand() {
orderByRand = true;
return this;
}
public EntityQueryLambda and(EntityQueryLambda lambda) {
ArrayList> copy = new ArrayList<>(conditions);
conditions.clear();
conditions.add(BiTuple.of(copy, Operator.AND, lambda));
orders.addAll(lambda.orders);
lambda.orders.clear();
return this;
}
public EntityQueryLambda or(EntityQueryLambda lambda) {
ArrayList> copy = new ArrayList<>(conditions);
conditions.clear();
conditions.add(BiTuple.of(copy, Operator.OR, lambda));
orders.addAll(lambda.orders);
lambda.orders.clear();
return this;
}
public EntityQueryLambda eq(BiFunc fn, R val) {
conditions.add(BiTuple.of(fn, Operator.EQ, val));
return this;
}
public EntityQueryLambda ne(BiFunc fn, R val) {
conditions.add(BiTuple.of(fn, Operator.NE, val));
return this;
}
public EntityQueryLambda gt(BiFunc fn, R val) {
conditions.add(BiTuple.of(fn, Operator.GT, val));
return this;
}
public EntityQueryLambda lt(BiFunc fn, R val) {
conditions.add(BiTuple.of(fn, Operator.LT, val));
return this;
}
public EntityQueryLambda ge(BiFunc fn, R val) {
conditions.add(BiTuple.of(fn, Operator.GE, val));
return this;
}
public EntityQueryLambda le(BiFunc fn, R val) {
conditions.add(BiTuple.of(fn, Operator.LE, val));
return this;
}
@SafeVarargs
public final EntityQueryLambda in(BiFunc fn, R... vals) {
conditions.add(BiTuple.of(fn, Operator.IN, vals));
return this;
}
@SafeVarargs
public final EntityQueryLambda notIn(BiFunc fn, R... vals) {
conditions.add(BiTuple.of(fn, Operator.NOT_IN, vals));
return this;
}
public EntityQueryLambda between(BiFunc fn, R start, R end) {
conditions.add(BiTuple.of(fn, Operator.BETWEEN, new Object[]{start, end}));
return this;
}
public EntityQueryLambda notBetween(BiFunc fn, R start, R end) {
conditions.add(BiTuple.of(fn, Operator.NOT_BETWEEN, new Object[]{start, end}));
return this;
}
public EntityQueryLambda like(BiFunc fn, String expr) {
conditions.add(BiTuple.of(fn, Operator.LIKE, expr));
return this;
}
public EntityQueryLambda notLike(BiFunc fn, String expr) {
conditions.add(BiTuple.of(fn, Operator.NOT_LIKE, expr));
return this;
}
@Override
public String toString() {
return toString(null);
}
public String toString(List