use of org.litepal.exceptions.DataSupportException in project LitePal by LitePalFramework.
the class DataSupport method updateAll.
/**
* Updates all records with details given if they match a set of conditions
* supplied. This method constructs a single SQL UPDATE statement and sends
* it to the database.
*
* <pre>
* Person person = new Person();
* person.setName("Jim");
* person.updateAll("name = ?", "Tom");
* </pre>
*
* This means that all the records which name is Tom will be updated into
* Jim.<br>
*
* <b>Note: </b> 1. If you set a default value to a field, the corresponding
* column won't be updated. Use {@link #setToDefault(String)} to update
* columns into default value. 2. This method couldn't update foreign key in
* database. So do not use setXxx to set associations between models.
*
* @param conditions
* A string array representing the WHERE part of an SQL
* statement. First parameter is the WHERE clause to apply when
* updating. The way of specifying place holders is to insert one
* or more question marks in the SQL. The first question mark is
* replaced by the second element of the array, the next question
* mark by the third, and so on. Passing empty string will update
* all rows.
* @return The number of rows affected.
*/
public synchronized int updateAll(String... conditions) {
try {
UpdateHandler updateHandler = new UpdateHandler(Connector.getDatabase());
int rowsAffected = updateHandler.onUpdateAll(this, conditions);
getFieldsToSetToDefault().clear();
return rowsAffected;
} catch (Exception e) {
throw new DataSupportException(e.getMessage(), e);
}
}
use of org.litepal.exceptions.DataSupportException in project LitePal by LitePalFramework.
the class DeleteHandler method analyzeAssociations.
/**
* Analyze the associations of baseObj and store the result in it. The
* associations will be used when deleting referenced data of baseObj.
*
* @param baseObj
* The record to delete.
*/
private Collection<AssociationsInfo> analyzeAssociations(DataSupport baseObj) {
try {
Collection<AssociationsInfo> associationInfos = getAssociationInfo(baseObj.getClassName());
analyzeAssociatedModels(baseObj, associationInfos);
return associationInfos;
} catch (Exception e) {
throw new DataSupportException(e.getMessage(), e);
}
}
use of org.litepal.exceptions.DataSupportException in project LitePal by LitePalFramework.
the class DynamicExecutor method send.
/**
* This method use java reflect API to execute method dynamically. Most
* importantly, it could access the methods with private modifier to break
* encapsulation.
*
* @param object
* The object to invoke method.
* @param methodName
* The method name to invoke.
* @param parameters
* The parameters.
* @param objectClass
* Use objectClass to find method to invoke.
* @param parameterTypes
* The parameter types.
* @return Returns the result of dynamically invoking method.
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws java.lang.reflect.InvocationTargetException
*/
static Object send(Object object, String methodName, Object[] parameters, Class<?> objectClass, Class<?>[] parameterTypes) throws SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
try {
if (parameters == null) {
parameters = new Object[] {};
}
if (parameterTypes == null) {
parameterTypes = new Class[] {};
}
Method method = objectClass.getDeclaredMethod(methodName, parameterTypes);
method.setAccessible(true);
return method.invoke(object, parameters);
} catch (NoSuchMethodException e) {
throw new DataSupportException(DataSupportException.noSuchMethodException(objectClass.getSimpleName(), methodName), e);
}
}
use of org.litepal.exceptions.DataSupportException in project LitePal by LitePalFramework.
the class SaveHandler method giveModelIdValue.
/**
* After saving a model, the id for this model will be returned. Assign this
* id to the model's id or _id field if it exists.
*
* @param baseObj
* The class of base object.
* @param idName
* The name of id. Only id or _id is valid.
* @param idType
* The type of id. Only int or long is valid.
* @param id
* The value of id.
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
private void giveModelIdValue(DataSupport baseObj, String idName, Class<?> idType, long id) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
if (shouldGiveModelIdValue(idName, idType, id)) {
Object value;
if (idType == int.class || idType == Integer.class) {
value = (int) id;
} else if (idType == long.class || idType == Long.class) {
value = id;
} else {
throw new DataSupportException(DataSupportException.ID_TYPE_INVALID_EXCEPTION);
}
DynamicExecutor.setField(baseObj, idName, value, baseObj.getClass());
}
}
use of org.litepal.exceptions.DataSupportException in project LitePal by LitePalFramework.
the class UpdateHandler method putFieldsToDefaultValue.
/**
* Iterate all the fields that need to set to default value. If the field is
* id, ignore it. Or put the default value of field into ContentValues.
*
* @param baseObj
* Which table to update by model instance.
* @param values
* To store data of current model for persisting or updating.
* @param ids
* The id array of query result.
*/
private void putFieldsToDefaultValue(DataSupport baseObj, ContentValues values, long... ids) {
String fieldName = null;
try {
DataSupport emptyModel = getEmptyModel(baseObj);
Class<?> emptyModelClass = emptyModel.getClass();
for (String name : baseObj.getFieldsToSetToDefault()) {
if (!isIdColumn(name)) {
fieldName = name;
Field field = emptyModelClass.getDeclaredField(fieldName);
if (isCollection(field.getType())) {
if (ids != null && ids.length > 0) {
String genericTypeName = getGenericTypeName(field);
if (BaseUtility.isGenericTypeSupported(genericTypeName)) {
String tableName = DBUtility.getGenericTableName(baseObj.getClassName(), field.getName());
String genericValueIdColumnName = DBUtility.getGenericValueIdColumnName(baseObj.getClassName());
StringBuilder whereClause = new StringBuilder();
boolean needOr = false;
for (long id : ids) {
if (needOr) {
whereClause.append(" or ");
}
whereClause.append(genericValueIdColumnName).append(" = ").append(id);
needOr = true;
}
mDatabase.delete(tableName, whereClause.toString(), null);
}
}
} else {
putContentValuesForUpdate(emptyModel, field, values);
}
}
}
} catch (NoSuchFieldException e) {
throw new DataSupportException(DataSupportException.noSuchFieldExceptioin(baseObj.getClassName(), fieldName), e);
} catch (Exception e) {
throw new DataSupportException(e.getMessage(), e);
}
}
Aggregations