use of org.litepal.exceptions.DataSupportException in project LitePal by LitePalFramework.
the class DataHandler method mathQuery.
/**
* Handles the math query of the given table.
*
* @param tableName
* Which table to query from.
* @param columns
* A list of which columns to return. Passing null will return
* all columns, which is discouraged to prevent reading data from
* storage that isn't going to be used.
* @param conditions
* A filter declaring which rows to return, formatted as an SQL
* WHERE clause. Passing null will return all rows.
* @param type
* The type of the based on column.
* @return The result calculating by SQL.
*/
@SuppressWarnings("unchecked")
protected <T> T mathQuery(String tableName, String[] columns, String[] conditions, Class<T> type) {
BaseUtility.checkConditionsCorrect(conditions);
Cursor cursor = null;
T result = null;
try {
cursor = mDatabase.query(tableName, columns, getWhereClause(conditions), getWhereArgs(conditions), null, null, null);
if (cursor.moveToFirst()) {
Class<?> cursorClass = cursor.getClass();
Method method = cursorClass.getMethod(genGetColumnMethod(type), int.class);
result = (T) method.invoke(cursor, 0);
}
} catch (Exception e) {
throw new DataSupportException(e.getMessage(), e);
} finally {
if (cursor != null) {
cursor.close();
}
}
return result;
}
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 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);
}
}
Aggregations