/**
* Copyright (c) 2013 Cangol
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package mobi.cangol.mobile.db;
import android.content.ContentValues;
import java.util.ArrayList;
import java.util.List;
public class UpdateBuilder {
private String table;
private List paraKey;
private List paraValue;
private List condList;
private ContentValues contentValues;
private DatabaseTable mDbtable;
public UpdateBuilder(Class> clazz) {
mDbtable = clazz.getAnnotation(DatabaseTable.class);
table = mDbtable.value();
paraKey = new ArrayList<>();
paraValue = new ArrayList<>();
condList = new ArrayList<>();
contentValues = new ContentValues();
}
/**
* @param pName 字段名称
* @param pValue 字段值
* @param pType 查询类型{}
*/
public void addQuery(String pName, Object pValue, String pType) {
addQuery(pName, pValue, pType, false);
}
/**
* 添加查询条件
*
* @param pName 字段名称
* @param pValue 字段值
* @param pType 查询类型{}
* @param isOr
*/
public void addQuery(String pName, Object pValue, String pType, boolean isOr) {
if (pName != null && !"".equals(pName) && pValue != null
//&& !"".equals(String.valueOf(pValue))
) {
if (pType.equals("is")) {
paraKey.add(pName + " is ?");
paraValue.add(pValue);
} else if (pType.equals("isnot")) {
paraKey.add(pName + " is not ?");
paraValue.add(pValue);
} else if (pType.equals("like")) {
paraKey.add(pName + " like ?");
paraValue.add("%" + pValue + "%");
} else if (pType.equals("blike")) {
paraKey.add(pName + " like ?");
paraValue.add("%" + pValue);
} else if (pType.equals("elike")) {
paraKey.add(pName + " like ?");
paraValue.add(pValue + "%");
} else if (pType.equals("in")) {
//in查询无法用占位符,只能直接拼成sql
paraKey.add(pName + " in(" + pValue + ")");
} else if (pType.equals("=")) {
paraKey.add(pName + "=?");
paraValue.add(pValue);
} else if (pType.equals(">")) {
paraKey.add(pName + ">?");
paraValue.add(pValue);
} else if (pType.equals("<")) {
paraKey.add(pName + "");
paraValue.add(pValue);
} else if (pType.equals("<>")) {
paraKey.add(pName + "<>?");
paraValue.add(pValue);
} else if (pType.equals("!=")) {
paraKey.add(pName + "!=?");
paraValue.add(pValue);
} else if (pType.equals(">=")) {
paraKey.add(pName + ">=?");
paraValue.add(pValue);
} else if (pType.equals("<=")) {
paraKey.add(pName + "<=?");
paraValue.add(pValue);
} else {
if (pType.indexOf('?') == -1) {
pType += "?";
}
paraKey.add(pName + pType);
paraValue.add(pValue);
}
condList.add(isOr ? " or " : " and ");
}
}
/**
* 添加查询条件
*
* @param pName 字段名称
* @param pValue1 字段值
* @param pValue2 字段值
* @param pType 查询类型{}
* @param isOr
*/
public void addQuery(String pName, Object pValue1, Object pValue2, String pType, boolean isOr) {
if (pName != null && !"".equals(pName) && pValue1 != null && pValue2 != null && !"".equals(String.valueOf(pValue1)) && !"".equals(String.valueOf(pValue2))) {
if (pType.equals("between")) {
paraKey.add(pName + " between " + pValue1 + " and " + pValue2);
}
condList.add(isOr ? " or " : " and ");
}
}
protected String getWhere() {
final StringBuilder sql = new StringBuilder();
if (paraKey != null) {
for (int i = 0; i < paraKey.size(); i++) {
if (i > 0) {
sql.append(condList.get(i));
}
sql.append(paraKey.get(i));
}
return sql.toString();
} else {
return null;
}
}
protected String[] getWhereArgs() {
if (paraValue != null) {
final String[] args = new String[paraValue.size()];
for (int i = 0; i < paraValue.size(); i++) {
paraKey.get(i);
args[i] = String.valueOf(paraValue.get(i));
}
return args;
} else {
return new String[0];
}
}
protected String getTable() {
return table;
}
public void setValue(String pName, Byte pValue) {
contentValues.put(pName, pValue);
}
public void setValue(String pName, String pValue) {
contentValues.put(pName, pValue);
}
public void setValue(String pName, byte[] pValue) {
contentValues.put(pName, pValue);
}
public void setValue(String pName, Boolean pValue) {
contentValues.put(pName, pValue);
}
public void setValue(String pName, Integer pValue) {
contentValues.put(pName, pValue);
}
public void setValue(String pName, Float pValue) {
contentValues.put(pName, pValue);
}
public void setValue(String pName, Double pValue) {
contentValues.put(pName, pValue);
}
public void setValue(String pName, Long pValue) {
contentValues.put(pName, pValue);
}
public void setValue(String pName, Short pValue) {
contentValues.put(pName, pValue);
}
public void setValue(String pName, Object pValue) {
contentValues.put(pName, String.valueOf(pValue));
}
public ContentValues getContentValues() {
return contentValues;
}
}