package com.changhong.autoform.entity.sql;
|
|
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
public class Where {
|
|
private String name;
|
|
private String sql;
|
|
public String getName() {
|
return name;
|
}
|
|
public void setName(String name) {
|
this.name = name;
|
}
|
|
public Where(Conditions... conditions) {
|
super();
|
this.conditions = conditions;
|
}
|
|
public Where(String sql, Conditions... conditions) {
|
super();
|
this.sql = sql;
|
this.conditions = conditions;
|
}
|
|
public Where(String sql){
|
this.sql = sql;
|
}
|
|
private Conditions[] conditions;
|
|
private List<Object> params = new ArrayList<>();
|
|
private String whereSql;
|
|
public String getSql() {
|
if(whereSql != null){
|
return whereSql;
|
}
|
StringBuilder where = new StringBuilder();
|
if(sql != null){
|
where.append(sql);
|
}
|
boolean isOne = true;
|
if(conditions != null) {
|
for (Conditions c : conditions) {
|
if (!isOne) {
|
where.append(c.joinType.sql);
|
} else {
|
where.append(" AND ");
|
}
|
where.append(c.field).append(c.getType().sql);
|
setConditionsSql(where, c.getType(), c.getVal());
|
isOne = false;
|
}
|
}
|
if(where.length() > 0){
|
String whereSql = where.toString().trim().replaceAll("^(?i)(or|and)|(?i)(or|and)$", "");
|
String[] whereArray = whereSql.split("(?i)where");
|
if(whereArray.length == 1) {
|
return " WHERE " + whereSql;
|
}
|
else if(whereArray.length == 2){
|
if(ObjectUtil.empty(whereArray[1])){
|
return " "+whereArray[0];
|
}
|
}
|
}
|
return " "+(whereSql = where.toString());
|
}
|
//public static void main(String... args){
|
// System.out.println(
|
// new Where("left join aa on a = b where 1 = 1", new Conditions("deleteFlg", Type.EQ, JoinType.AND, 0)).getSql()
|
// );
|
//}
|
private void setConditionsSql(StringBuilder where, Type type, Object val){
|
if(type.isIn){
|
where.append('(');
|
}
|
if(val instanceof List){
|
boolean isOne = true;
|
for(Object item : (List)val){
|
if(!isOne){
|
where.append(" , ");
|
}
|
where.append(" ? ");
|
params.add(item);
|
isOne = false;
|
}
|
}else{
|
where.append(" ? ");
|
params.add(val);
|
}
|
if(type.isIn){
|
where.append(')');
|
}
|
}
|
|
public Object[] getParams() {
|
return this.params.toArray();
|
}
|
|
/**
|
* 关联类型
|
* @author WangYX
|
*
|
*/
|
public static enum JoinType{
|
OR (" OR "),
|
AND (" AND ");
|
|
private String sql;
|
|
private JoinType(String sql) {
|
this.sql = sql;
|
}
|
}
|
|
/**
|
* 条件类型
|
* @author WangYX
|
*
|
*/
|
public static enum Type{
|
IN (" IN " , true),
|
NIN (" NOT IN " , true),
|
EQ (" = " ),
|
NEQ (" != " ),
|
AS (" AS " ),
|
LIKE(" LIKE " ),
|
COMMA(" , " );
|
|
private String sql;
|
|
private boolean isIn;
|
|
private Type(String sql,boolean isIn) {
|
this.sql = sql;
|
this.isIn = isIn;
|
}
|
|
private Type(String sql) {
|
this.sql = sql;
|
this.isIn = false;
|
}
|
}
|
|
/**
|
* 条件实体类
|
* @author WangYX
|
*
|
*/
|
public static class Conditions{
|
|
public Conditions(String field, Type type, JoinType joinType, Object val) {
|
super();
|
this.field = field;
|
this.type = type;
|
this.joinType = joinType;
|
this.val = val;
|
}
|
|
public Conditions(String field, Type type, Object val) {
|
super();
|
this.field = field;
|
this.type = type;
|
this.joinType = null;
|
this.val = val;
|
}
|
|
|
public Conditions(String conditionSql){
|
this.conditionSql = conditionSql;
|
}
|
|
private String conditionSql;
|
|
private String field;
|
|
private Type type;
|
|
private JoinType joinType;
|
|
private Object val;
|
|
|
public String getConditionSql() {
|
return conditionSql;
|
}
|
|
public void setConditionSql(String conditionSql) {
|
this.conditionSql = conditionSql;
|
}
|
|
public String getField() {
|
return field;
|
}
|
|
public void setField(String field) {
|
this.field = field;
|
}
|
|
public Type getType() {
|
return type;
|
}
|
|
public void setType(Type type) {
|
this.type = type;
|
}
|
|
public Object getVal() {
|
return val;
|
}
|
|
public void setVal(Object val) {
|
this.val = val;
|
}
|
|
public JoinType getJoinType() {
|
return joinType;
|
}
|
|
public void setJoinType(JoinType joinType) {
|
this.joinType = joinType;
|
}
|
|
}
|
|
}
|