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.
com.webapp.mybatis.helper.Where Maven / Gradle / Ivy
package com.webapp.mybatis.helper;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.webapp.mybatis.helper.DaoHelper.AndOr;
import com.webapp.mybatis.helper.DaoHelper.Like;
/**
* where
*/
public final class Where {
private StringBuffer where = new StringBuffer();
private StringBuffer orderLimit = new StringBuffer();
private Where() {}
private Where(String where) {
this.where.append(where);
}
public static Where of() {
return new Where();
}
public static Where of(String name, Op op, Object ...vals) {
return new Where(DaoHelper.all(AndOr.AND, name, op, vals));
}
public static Where of(Map param) {
return new Where(DaoHelper.where(param, AndOr.AND.name()));
}
public static Where of(T model) {
return new Where(DaoHelper.where(model, AndOr.AND.name()));
}
public enum Order {
DESC, ASC;
}
public enum Op{
EQ("="), NEQ("!="), GT(">"), GTE(">="), LT("<"), LTE("<="),
LIKE("LIKE"), NLike("NOT LIKE"),
IN("IN"), NIN("NOT IN"), ISNULL("IS NULL"),
NISNULL("NOT IS NULL"), BETWEEN("BETWEEN");
private String op;
Op(String op){
this.op = op;
}
public String toString() {
return this.op.toString();
}
public static Op of(String op){
List ops = Arrays.asList(values());
for(Op ofOp : ops){
if(ofOp.op.equals(op.toUpperCase())) return ofOp;
}
return null;
}
}
private Where wrapAppend(AndOr andOr, String cnd){
if(!where.toString().isEmpty()){
where.append(" " + andOr);
}
where.append(" (");
where.append(cnd);
where.append(")");
return this;
}
private Where append(String cnd){
where.append(" " + cnd);
return this;
}
private Where orderLimitAppend(String cnd){
orderLimit.append(" " + cnd);
return this;
}
public Where and(String name, Op op, Object ...vals){
return append(DaoHelper.all(AndOr.AND, name, op, vals));
}
public Where andBetween(String name, Object min, Object max){
return append(DaoHelper.between(AndOr.AND, name, min, max));
}
public Where andEq(String name, Object val){
return append(DaoHelper.where(AndOr.AND, name, Op.EQ, val));
}
public Where andNotEq(String name, Object val){
return append(DaoHelper.where(AndOr.AND, name, Op.NEQ, val));
}
public Where andGT(String name, Object val){
return append(DaoHelper.where(AndOr.AND, name, Op.GT, val));
}
public Where andGTE(String name, Object val){
return append(DaoHelper.where(AndOr.AND, name, Op.GTE, val));
}
public Where andLT(String name, Object val){
return append(DaoHelper.where(AndOr.AND, name, Op.LT, val));
}
public Where andLTE(String name, Object val){
return append(DaoHelper.where(AndOr.AND, name, Op.LTE, val));
}
public Where andIn(String name, Object ...vals){
return append(DaoHelper.in(AndOr.AND, name, false, vals));
}
public Where andNotIn(String name, Object ...vals){
return append(DaoHelper.in(AndOr.AND, name, true, vals));
}
public Where andIsNull(String name){
return append(DaoHelper.where(AndOr.AND, name, Op.ISNULL));
}
public Where andIsNotNull(String name){
return append(DaoHelper.where(AndOr.AND, name, Op.NISNULL));
}
public Where andLike(String name, String val){
return append(DaoHelper.like(AndOr.AND, name, false, Like.ALL, val));
}
public Where andNotLike(String name, String val){
return append(DaoHelper.like(AndOr.AND, name, true, Like.ALL, val));
}
public Where andLikeL(String name, String val){
return append(DaoHelper.like(AndOr.AND, name, false, Like.LEFT, val));
}
public Where andNotLikeL(String name, String val){
return append(DaoHelper.like(AndOr.AND, name, true, Like.LEFT, val));
}
public Where andLikeR(String name, String val){
return append(DaoHelper.like(AndOr.AND, name, false, Like.RIGHT, val));
}
public Where andNotLikeR(String name, String val){
return append(DaoHelper.like(AndOr.AND, name, true, Like.RIGHT, val));
}
public Where or(String name, Op op, Object ...vals){
return append(DaoHelper.all(AndOr.OR, name, op, vals));
}
public Where orBetween(String name, Object min, Object max){
return append(DaoHelper.between(AndOr.OR, name, min, max));
}
public Where orEq(String name, Object val){
return append(DaoHelper.where(AndOr.OR, name, Op.EQ, val));
}
public Where orNotEq(String name, Object val){
return append(DaoHelper.where(AndOr.OR, name, Op.NEQ, val));
}
public Where orGT(String name, Object val){
return append(DaoHelper.where(AndOr.OR, name, Op.GT, val));
}
public Where orGTE(String name, Object val){
return append(DaoHelper.where(AndOr.OR, name, Op.GTE, val));
}
public Where orLT(String name, Object val){
return append(DaoHelper.where(AndOr.OR, name, Op.LT, val));
}
public Where orLTE(String name, Object val){
return append(DaoHelper.where(AndOr.OR, name, Op.LTE, val));
}
public Where orIn(String name, Object ...vals){
return append(DaoHelper.in(AndOr.OR, name, false, vals));
}
public Where orNotIn(String name, Object ...vals){
return append(DaoHelper.in(AndOr.OR, name, true, vals));
}
public Where orIsNull(String name){
return append(DaoHelper.where(AndOr.OR, name, Op.ISNULL));
}
public Where orIsNotNull(String name){
return append(DaoHelper.where(AndOr.OR, name, Op.NISNULL));
}
public Where orLike(String name, String val){
return append(DaoHelper.like(AndOr.OR, name, false, Like.ALL, val));
}
public Where orNotLike(String name, String val){
return append(DaoHelper.like(AndOr.OR, name, true, Like.ALL, val));
}
public Where orLikeL(String name, String val){
return append(DaoHelper.like(AndOr.OR, name, false, Like.LEFT, val));
}
public Where orNotLikeL(String name, String val){
return append(DaoHelper.like(AndOr.OR, name, true, Like.LEFT, val));
}
public Where orLikeR(String name, String val){
return append(DaoHelper.like(AndOr.OR, name, false, Like.RIGHT, val));
}
public Where orNotLikeR(String name, String val){
return append(DaoHelper.like(AndOr.OR, name, true, Like.RIGHT, val));
}
public Where andWrap(Where wrap){
return wrapAppend(AndOr.AND, wrap.toWrapSql());
}
public Where orWrap(Where wrap){
return wrapAppend(AndOr.OR, wrap.toWrapSql());
}
public Where order(String ...name){
return orderLimitAppend(DaoHelper.orderBy(Order.ASC, name));
}
public Where order(Order order, String ...name){
return orderLimitAppend(DaoHelper.orderBy(order, name));
}
public Where limit(int count){
return orderLimitAppend(DaoHelper.limits(0, count));
}
public Where limit(int index, int count){
return orderLimitAppend(DaoHelper.limits(index, count));
}
private String toWrapSql(){
String sql = toBaseSql();
if(sql.trim().equals("")){
return orderLimit.toString();
}
return String.format(" %1$s %2$s", sql, orderLimit.toString().trim());
}
public String toSql() {
String sql = toBaseSql();
if(sql.trim().equals("")){
return orderLimit.toString();
}
return String.format(" WHERE %1$s %2$s", sql, orderLimit.toString().trim());
}
private String toBaseSql() {
String sql = where.toString().trim();
if(sql.startsWith(AndOr.AND.toString())){
sql = sql.replaceFirst(AndOr.AND.toString(), "").trim();
}else if(sql.startsWith(AndOr.OR.toString())){
sql = sql.replaceFirst(AndOr.OR.toString(), "").trim();
}
return sql;
}
public String toString(){
return toSql();
}
}