package util;
import java.sql.Date;
public class Expression {
StringBuffer exp = new StringBuffer();
int count = 0;
boolean leftbra = false;
boolean orderflag = false;
boolean between = false;
public void appendValue(Object value, Class valueClz) {
Object paraValue = "";
paraValue = value;
if (valueClz == Integer.class) {
exp.append(paraValue + " ");
} else if (valueClz == Long.class) {
exp.append(paraValue + " ");
} else if (valueClz == String.class) {
exp.append("'" + paraValue + "' ");
} else if (valueClz == Date.class) {
exp.append("'" + paraValue + "' ");
} else if (valueClz == Double.class) {
exp.append(paraValue + " ");
} else if (valueClz == Float.class) {
exp.append(paraValue + " ");
} else if (valueClz == Boolean.class) {
exp.append(paraValue + " ");
}
}
public void andLike(String paraName, String paraValue, Class valueClz) {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
} else {
exp.append(" and ");
exp.append(paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
}
count++;
}
public void andBetween(String paraName, String paraValue, Class valueClz) {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " between ");
appendValue(paraValue, valueClz);
} else {
exp.append(" and ");
exp.append(paraName + " between ");
appendValue(paraValue, valueClz);
}
count++;
between = true;
}
public void andAnd(String paraValue, Class valueClz) {
if (between) {
exp.append(" and ");
appendValue(paraValue, valueClz);
count++;
}
}
public void andLeftBraLike(String paraName, String paraValue, Class valueClz) {
if (leftbra == false) {
if (count <= 0) {
exp.append(" where ");
exp.append("( " + paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
} else {
exp.append(" and ");
exp.append("( " + paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
}
count++;
leftbra = true;
} else {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
} else {
exp.append(" and ");
exp.append(paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
}
count++;
}
}
public void andRightBraLike(String paraName, String paraValue,
Class valueClz) {
if (leftbra == true) {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
} else {
exp.append(" and ");
exp.append(paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
exp.append(" )");
}
count++;
leftbra = false;
} else {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
} else {
exp.append(" and ");
exp.append(paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
}
count++;
}
}
public void andEqu(String paraName, Object paraValue, Class valueClz) {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " = ");
appendValue(paraValue, valueClz);
} else {
exp.append(" and ");
exp.append(paraName + " = ");
appendValue(paraValue, valueClz);
}
count++;
}
public void andNotEqu(String paraName, Object paraValue, Class valueClz) {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " <> ");
appendValue(paraValue, valueClz);
} else {
exp.append(" and ");
exp.append(paraName + " <> ");
appendValue(paraValue, valueClz);
}
count++;
}
public void orLike(String paraName, String paraValue, Class valueClz) {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
} else {
exp.append(" or ");
exp.append(paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
}
count++;
}
public void orLeftBraLike(String paraName, String paraValue, Class valueClz) {
if (leftbra == false) {
if (count <= 0) {
exp.append(" where ");
exp.append("( " + paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
} else {
exp.append(" or ");
exp.append("( " + paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
}
count++;
leftbra = true;
} else {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
} else {
exp.append(" or ");
exp.append(paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
}
count++;
}
}
public void orRightBraLike(String paraName, String paraValue, Class valueClz) {
if (leftbra == true) {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
} else {
exp.append(" or ");
exp.append(paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
exp.append(" )");
}
count++;
leftbra = false;
} else {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
} else {
exp.append(" or ");
exp.append(paraName + " like ");
appendValue("%" + paraValue + "%", valueClz);
}
count++;
}
}
public void orEqu(String paraName, Object paraValue, Class valueClz) {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " = ");
appendValue(paraValue, valueClz);
} else {
exp.append(" or ");
exp.append(paraName + " = ");
appendValue(paraValue, valueClz);
}
count++;
}
public void orNotEqu(String paraName, Object paraValue, Class valueClz) {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " <> ");
appendValue(paraValue, valueClz);
} else {
exp.append(" or ");
exp.append(paraName + " <> ");
appendValue(paraValue, valueClz);
}
count++;
}
public void orderByAsc(String paraName) {
if (orderflag == false) {
exp.append(" order by ");
exp.append(paraName + " asc ");
orderflag = true;
} else {
exp.append(", ");
exp.append(paraName + " asc ");
}
}
public void orderByDesc(String paraName) {
if (orderflag == false) {
exp.append(" order by ");
exp.append(paraName + " desc ");
orderflag = true;
} else {
exp.append(", ");
exp.append(paraName + " desc ");
}
}
public void orLeftBraAnd(String paraName, Object paraValue, Class valueClz) {
if (leftbra == false) {
if (count <= 0) {
exp.append(" where ");
exp.append("( " + paraName + " = ");
appendValue(paraValue, valueClz);
} else {
exp.append(" or ");
exp.append("( " + paraName + " = ");
appendValue(paraValue, valueClz);
}
count++;
leftbra = true;
} else {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " = ");
appendValue(paraValue, valueClz);
} else {
exp.append(" or ");
exp.append(paraName + " = ");
appendValue(paraValue, valueClz);
}
count++;
}
}
public void orRightBraAnd(String paraName, Object paraValue, Class valueClz) {
if (leftbra == true) {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " = ");
appendValue(paraValue, valueClz);
} else {
exp.append(" or ");
exp.append(paraName + " = ");
appendValue(paraValue, valueClz);
exp.append(" )");
}
count++;
leftbra = false;
} else {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " = ");
appendValue(paraValue, valueClz);
} else {
exp.append(" or ");
exp.append(paraName + " = ");
appendValue(paraValue, valueClz);
}
count++;
}
}
public void andLeftBraAnd(String paraName, Object paraValue, Class valueClz) {
if (leftbra == false) {
if (count <= 0) {
exp.append(" where ");
exp.append("( " + paraName + " = ");
appendValue(paraValue, valueClz);
} else {
exp.append(" and ");
exp.append("( " + paraName + " = ");
appendValue(paraValue, valueClz);
}
count++;
leftbra = true;
} else {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " = ");
appendValue(paraValue, valueClz);
} else {
exp.append(" and ");
exp.append(paraName + " = ");
appendValue(paraValue, valueClz);
}
count++;
}
}
public void andRightBraAnd(String paraName, Object paraValue, Class valueClz) {
if (leftbra == true) {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " = ");
appendValue(paraValue, valueClz);
} else {
exp.append(" and ");
exp.append(paraName + " = ");
appendValue(paraValue, valueClz);
exp.append(" )");
}
count++;
leftbra = false;
} else {
if (count <= 0) {
exp.append(" where ");
exp.append(paraName + " = ");
appendValue(paraValue, valueClz);
} else {
exp.append(" and ");
exp.append(paraName + " = ");
appendValue(paraValue, valueClz);
}
count++;
}
}
public String toString() {
return exp.toString();
}
public void clear() {
count = 0;
exp.delete(0, exp.length());
}
public static void main(String[] args) {
Expression exp = new Expression();
exp.andLeftBraAnd("userid", "zhangjs", String.class);
exp.orRightBraAnd("pwd", "123456", String.class);
exp.andLeftBraAnd("userid", "zhangjs", String.class);
exp.orRightBraAnd("pwd", "123456", String.class);
exp.orEqu("isdelete", "true", Boolean.class);
exp.orderByAsc("createtime");
System.out.println(exp.toString());
}
}