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.
*
* @author xingwx
* @version $Id: $Id
*/
public class SqlHelper {
public static final String WHERE = " where ";
public static final String HAVING = " having ";
List filters;
List orCond = null;
List notCond = null;
private int databaseType = MYSQL;
private boolean useNullAsCondition = false;
private boolean acceptEmptyStringAsNullObjectInCondition = false;
private boolean conditionTypeIsWhere = true;
/**
*
Constructor for SqlHelper.
*
* @param src a {@link SqlHelper} object.
*/
public SqlHelper(SqlHelper src) {
this.filters = new ArrayList<>();
this.filters.addAll(src.filters);
if(src.orCond!=null) {
this.orCond = new ArrayList<>();
this.orCond.addAll(src.orCond);
}
if(src.notCond!=null) {
this.notCond = new ArrayList<>();
this.notCond.addAll(src.notCond);
}
this.databaseType = src.databaseType;
this.useNullAsCondition = src.useNullAsCondition;
this.acceptEmptyStringAsNullObjectInCondition = src.acceptEmptyStringAsNullObjectInCondition;
}
/**
*
Constructor for SqlHelper.
*/
public SqlHelper() {
filters = new ArrayList<>();
}
public SqlHelper(boolean isHaving) {
filters = new ArrayList<>();
this.conditionTypeIsWhere = !isHaving;
}
public boolean isAcceptEmptyStringAsNullObjectInCondition() {
return acceptEmptyStringAsNullObjectInCondition;
}
public void setAcceptEmptyStringAsNullObjectInCondition(boolean acceptEmptyStringAsNullObjectInCondition) {
this.acceptEmptyStringAsNullObjectInCondition = acceptEmptyStringAsNullObjectInCondition;
}
/**
*
toCondition.
*
* @return a {@link SqlPocket} object.
*/
public SqlPocket toCondition() {
SqlPocket criteria = new SqlPocket();
if (filters == null || filters.size() <= 0) {
return criteria;
}
for (BaseFilter filter : filters) {
buildCriteria(criteria, filter);
}
StringBuffer condition = criteria.getCondition();
removeLeadAnd(condition);
if (orCond != null) {
condition.insert(0, "(").append(")");
for (SqlHelper it : orCond) {
SqlPocket cond = it.toCondition();
condition.append(" or (")
.append(trimWhere(cond.getCondition())).append(")");
criteria.getParams().addAll(cond.getParams());
}
}
if (notCond != null) {
condition.insert(0, "(").append(")");
for (SqlHelper it : notCond) {
SqlPocket cond = it.toCondition();
condition.append(" and not (")
.append(trimWhere(cond.getCondition())).append(")");
criteria.getParams().addAll(cond.getParams());
}
}
if (condition.length() > 0) {
removeLeadAnd(condition);
condition.insert(0, conditionTypeIsWhere? WHERE: HAVING);
}
return criteria;
}
private void removeLeadAnd(StringBuffer condition) {
if (condition.length() > 0 && condition.indexOf(" and ") == 0) {
condition.delete(0, 5);
}
}
/**
*
*
* @return a {@link SqlPocket} object.
*/
public SqlPocket build() {
return toCondition();
}
private boolean valueIsNotNull(Object v){
if(v==null){
return false;
}
if(!acceptEmptyStringAsNullObjectInCondition){
return true;
}
return !"".equals(v);
}
private void buildCriteria(SqlPocket criteria, BaseFilter f)
{
if (valueIsNotNull(f.value) || useNullAsCondition || f instanceof NullFilter || f instanceof NotNullFilter ) {
f.build(criteria);
}
}
/**
* 增加
*
* @param condition 条件
* @param ignoreNulls 忽略null参数
* @return 当前实例
*/
public SqlHelper eq(Map condition, boolean ignoreNulls) {
Set> e = condition.entrySet();
for (Entry entry:e) {
String key = entry.getKey();
Object value = entry.getValue();
if (ignoreNulls && value == null) {
continue;
}
eq(key, value);
}
return this;
}
/**
*
eq.
*
* @param condition a {@link java.util.Map} object.
* @return a {@link SqlHelper} object.
*/
public SqlHelper eq(Map condition) {
return eq(condition, true);
}
/**
*
Getter for the field databaseType.
*
* @return a int.
*/
public int getDatabaseType() {
return databaseType;
}
/**
* 设置数据库类型
*
* @param databaseType
* 采用BaseDao里的数据类型定义
*/
public void setDatabaseType(int databaseType) {
this.databaseType = databaseType;
}
/**
*
or.
*
* @param cond a {@link SqlHelper} object.
* @return a {@link SqlHelper} object.
*/
public SqlHelper or(SqlHelper cond) {
if (orCond == null) {
orCond = new ArrayList<>();
}
this.orCond.add(cond);
return this;
}
/**
*
not.
*
* @param cond a {@link SqlHelper} object.
* @return a {@link SqlHelper} object.
*/
public SqlHelper not(SqlHelper cond) {
if (notCond == null) {
notCond = new ArrayList<>();
}
this.notCond.add(cond);
return this;
}
/**
*
in.
*
* @param field a {@link java.lang.String} object.
* @param value a {@link java.util.Collection} object.
* @return a {@link SqlHelper} object.
*/
public SqlHelper in(String field, Collection