Example usage for android.database.sqlite SQLiteDatabase rawQueryWithFactory

List of usage examples for android.database.sqlite SQLiteDatabase rawQueryWithFactory

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase rawQueryWithFactory.

Prototype

public Cursor rawQueryWithFactory(CursorFactory cursorFactory, String sql, String[] selectionArgs,
        String editTable, CancellationSignal cancellationSignal) 

Source Link

Document

Runs the provided SQL and returns a cursor over the result set.

Usage

From source file:io.requery.android.database.sqlite.SQLiteQueryBuilder.java

/**
 * Perform a query by combining all current settings and the
 * information passed into this method.//from w  ww .  ja  va  2s . c o m
 *
 * @param db the database to query on
 * @param projectionIn 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 selection A filter declaring which rows to return,
 *   formatted as an SQL WHERE clause (excluding the WHERE
 *   itself). Passing null will return all rows for the given URL.
 * @param selectionArgs You may include ?s in selection, which
 *   will be replaced by the values from selectionArgs, in order
 *   that they appear in the selection. The values will be bound
 *   as Strings.
 * @param groupBy A filter declaring how to group rows, formatted
 *   as an SQL GROUP BY clause (excluding the GROUP BY
 *   itself). Passing null will cause the rows to not be grouped.
 * @param having A filter declare which row groups to include in
 *   the cursor, if row grouping is being used, formatted as an
 *   SQL HAVING clause (excluding the HAVING itself).  Passing
 *   null will cause all row groups to be included, and is
 *   required when row grouping is not being used.
 * @param sortOrder How to order the rows, formatted as an SQL
 *   ORDER BY clause (excluding the ORDER BY itself). Passing null
 *   will use the default sort order, which may be unordered.
 * @param limit Limits the number of rows returned by the query,
 *   formatted as LIMIT clause. Passing null denotes no LIMIT clause.
 * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
 * If the operation is canceled, then {@link OperationCanceledException} will be thrown
 * when the query is executed.
 * @return a cursor over the result set
 * @see android.content.ContentResolver#query(android.net.Uri, String[],
 *      String, String[], String)
 */
public Cursor query(SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs,
        String groupBy, String having, String sortOrder, String limit, CancellationSignal cancellationSignal) {
    if (mTables == null) {
        return null;
    }

    if (mStrict && selection != null && selection.length() > 0) {
        // Validate the user-supplied selection to detect syntactic anomalies
        // in the selection string that could indicate a SQL injection attempt.
        // The idea is to ensure that the selection clause is a valid SQL expression
        // by compiling it twice: once wrapped in parentheses and once as
        // originally specified. An attacker cannot create an expression that
        // would escape the SQL expression while maintaining balanced parentheses
        // in both the wrapped and original forms.
        String sqlForValidation = buildQuery(projectionIn, "(" + selection + ")", groupBy, having, sortOrder,
                limit);
        validateQuerySql(db, sqlForValidation, cancellationSignal); // will throw if query is invalid
    }

    String sql = buildQuery(projectionIn, selection, groupBy, having, sortOrder, limit);

    if (Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG, "Performing query: " + sql);
    }
    return db.rawQueryWithFactory(mFactory, sql, selectionArgs, SQLiteDatabase.findEditTable(mTables),
            cancellationSignal); // will throw if query is invalid
}