Search in sources :

Example 1 with DataSupportException

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;
}
Also used : DataSupportException(org.litepal.exceptions.DataSupportException) Method(java.lang.reflect.Method) Cursor(android.database.Cursor) DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException) DataSupportException(org.litepal.exceptions.DataSupportException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with DataSupportException

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);
    }
}
Also used : DataSupportException(org.litepal.exceptions.DataSupportException) AssociationsInfo(org.litepal.crud.model.AssociationsInfo) DataSupportException(org.litepal.exceptions.DataSupportException)

Example 3 with DataSupportException

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);
    }
}
Also used : DataSupportException(org.litepal.exceptions.DataSupportException) Method(java.lang.reflect.Method)

Example 4 with DataSupportException

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());
    }
}
Also used : DataSupportException(org.litepal.exceptions.DataSupportException)

Example 5 with DataSupportException

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(&quot;Jim&quot;);
	 * person.updateAll(&quot;name = ?&quot;, &quot;Tom&quot;);
	 * </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);
    }
}
Also used : DataSupportException(org.litepal.exceptions.DataSupportException) DataSupportException(org.litepal.exceptions.DataSupportException)

Aggregations

DataSupportException (org.litepal.exceptions.DataSupportException)20 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 Cursor (android.database.Cursor)4 Field (java.lang.reflect.Field)4 DatabaseGenerateException (org.litepal.exceptions.DatabaseGenerateException)4 AssociationsInfo (org.litepal.crud.model.AssociationsInfo)3 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2 SQLiteException (android.database.sqlite.SQLiteException)2 SparseArray (android.util.SparseArray)2 Teacher (com.litepaltest.model.Teacher)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 GenericModel (org.litepal.tablemanager.model.GenericModel)2 ContentValues (android.content.ContentValues)1 Computer (com.litepaltest.model.Computer)1 Student (com.litepaltest.model.Student)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1