Search in sources :

Example 86 with SQLiteQueryBuilder

use of android.database.sqlite.SQLiteQueryBuilder in project android_frameworks_base by DirtyUnicorns.

the class MtpDatabase method queryRoots.

/**
     * Queries roots information.
     * @param columnNames Column names defined in {@link android.provider.DocumentsContract.Root}.
     * @return Database cursor.
     */
Cursor queryRoots(Resources resources, String[] columnNames) {
    final String selection = COLUMN_ROW_STATE + " IN (?, ?) AND " + COLUMN_DOCUMENT_TYPE + " = ?";
    final Cursor deviceCursor = mDatabase.query(TABLE_DOCUMENTS, strings(COLUMN_DEVICE_ID), selection, strings(ROW_STATE_VALID, ROW_STATE_INVALIDATED, DOCUMENT_TYPE_DEVICE), COLUMN_DEVICE_ID, null, null, null);
    try {
        final SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        builder.setTables(JOIN_ROOTS);
        builder.setProjectionMap(COLUMN_MAP_ROOTS);
        final MatrixCursor result = new MatrixCursor(columnNames);
        final ContentValues values = new ContentValues();
        while (deviceCursor.moveToNext()) {
            final int deviceId = deviceCursor.getInt(0);
            final Cursor storageCursor = builder.query(mDatabase, columnNames, selection + " AND " + COLUMN_DEVICE_ID + " = ?", strings(ROW_STATE_VALID, ROW_STATE_INVALIDATED, DOCUMENT_TYPE_STORAGE, deviceId), null, null, null);
            try {
                values.clear();
                try (final Cursor deviceRoot = builder.query(mDatabase, columnNames, selection + " AND " + COLUMN_DEVICE_ID + " = ?", strings(ROW_STATE_VALID, ROW_STATE_INVALIDATED, DOCUMENT_TYPE_DEVICE, deviceId), null, null, null)) {
                    deviceRoot.moveToNext();
                    DatabaseUtils.cursorRowToContentValues(deviceRoot, values);
                }
                if (storageCursor.getCount() != 0) {
                    long capacityBytes = 0;
                    long availableBytes = 0;
                    final int capacityIndex = storageCursor.getColumnIndex(Root.COLUMN_CAPACITY_BYTES);
                    final int availableIndex = storageCursor.getColumnIndex(Root.COLUMN_AVAILABLE_BYTES);
                    while (storageCursor.moveToNext()) {
                        // don't calculate corresponding values.
                        if (capacityIndex != -1) {
                            capacityBytes += storageCursor.getLong(capacityIndex);
                        }
                        if (availableIndex != -1) {
                            availableBytes += storageCursor.getLong(availableIndex);
                        }
                    }
                    values.put(Root.COLUMN_CAPACITY_BYTES, capacityBytes);
                    values.put(Root.COLUMN_AVAILABLE_BYTES, availableBytes);
                } else {
                    values.putNull(Root.COLUMN_CAPACITY_BYTES);
                    values.putNull(Root.COLUMN_AVAILABLE_BYTES);
                }
                if (storageCursor.getCount() == 1 && values.containsKey(Root.COLUMN_TITLE)) {
                    storageCursor.moveToFirst();
                    // Add storage name to device name if we have only 1 storage.
                    values.put(Root.COLUMN_TITLE, resources.getString(R.string.root_name, values.getAsString(Root.COLUMN_TITLE), storageCursor.getString(storageCursor.getColumnIndex(Root.COLUMN_TITLE))));
                }
            } finally {
                storageCursor.close();
            }
            final RowBuilder row = result.newRow();
            for (final String key : values.keySet()) {
                row.add(key, values.get(key));
            }
        }
        return result;
    } finally {
        deviceCursor.close();
    }
}
Also used : ContentValues(android.content.ContentValues) RowBuilder(android.database.MatrixCursor.RowBuilder) Cursor(android.database.Cursor) MatrixCursor(android.database.MatrixCursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder) MatrixCursor(android.database.MatrixCursor)

Example 87 with SQLiteQueryBuilder

use of android.database.sqlite.SQLiteQueryBuilder in project android_frameworks_base by crdroidandroid.

the class MtpDatabase method queryRoots.

/**
     * Queries roots information.
     * @param columnNames Column names defined in {@link android.provider.DocumentsContract.Root}.
     * @return Database cursor.
     */
Cursor queryRoots(Resources resources, String[] columnNames) {
    final String selection = COLUMN_ROW_STATE + " IN (?, ?) AND " + COLUMN_DOCUMENT_TYPE + " = ?";
    final Cursor deviceCursor = mDatabase.query(TABLE_DOCUMENTS, strings(COLUMN_DEVICE_ID), selection, strings(ROW_STATE_VALID, ROW_STATE_INVALIDATED, DOCUMENT_TYPE_DEVICE), COLUMN_DEVICE_ID, null, null, null);
    try {
        final SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        builder.setTables(JOIN_ROOTS);
        builder.setProjectionMap(COLUMN_MAP_ROOTS);
        final MatrixCursor result = new MatrixCursor(columnNames);
        final ContentValues values = new ContentValues();
        while (deviceCursor.moveToNext()) {
            final int deviceId = deviceCursor.getInt(0);
            final Cursor storageCursor = builder.query(mDatabase, columnNames, selection + " AND " + COLUMN_DEVICE_ID + " = ?", strings(ROW_STATE_VALID, ROW_STATE_INVALIDATED, DOCUMENT_TYPE_STORAGE, deviceId), null, null, null);
            try {
                values.clear();
                try (final Cursor deviceRoot = builder.query(mDatabase, columnNames, selection + " AND " + COLUMN_DEVICE_ID + " = ?", strings(ROW_STATE_VALID, ROW_STATE_INVALIDATED, DOCUMENT_TYPE_DEVICE, deviceId), null, null, null)) {
                    deviceRoot.moveToNext();
                    DatabaseUtils.cursorRowToContentValues(deviceRoot, values);
                }
                if (storageCursor.getCount() != 0) {
                    long capacityBytes = 0;
                    long availableBytes = 0;
                    final int capacityIndex = storageCursor.getColumnIndex(Root.COLUMN_CAPACITY_BYTES);
                    final int availableIndex = storageCursor.getColumnIndex(Root.COLUMN_AVAILABLE_BYTES);
                    while (storageCursor.moveToNext()) {
                        // don't calculate corresponding values.
                        if (capacityIndex != -1) {
                            capacityBytes += storageCursor.getLong(capacityIndex);
                        }
                        if (availableIndex != -1) {
                            availableBytes += storageCursor.getLong(availableIndex);
                        }
                    }
                    values.put(Root.COLUMN_CAPACITY_BYTES, capacityBytes);
                    values.put(Root.COLUMN_AVAILABLE_BYTES, availableBytes);
                } else {
                    values.putNull(Root.COLUMN_CAPACITY_BYTES);
                    values.putNull(Root.COLUMN_AVAILABLE_BYTES);
                }
                if (storageCursor.getCount() == 1 && values.containsKey(Root.COLUMN_TITLE)) {
                    storageCursor.moveToFirst();
                    // Add storage name to device name if we have only 1 storage.
                    values.put(Root.COLUMN_TITLE, resources.getString(R.string.root_name, values.getAsString(Root.COLUMN_TITLE), storageCursor.getString(storageCursor.getColumnIndex(Root.COLUMN_TITLE))));
                }
            } finally {
                storageCursor.close();
            }
            final RowBuilder row = result.newRow();
            for (final String key : values.keySet()) {
                row.add(key, values.get(key));
            }
        }
        return result;
    } finally {
        deviceCursor.close();
    }
}
Also used : ContentValues(android.content.ContentValues) RowBuilder(android.database.MatrixCursor.RowBuilder) Cursor(android.database.Cursor) MatrixCursor(android.database.MatrixCursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder) MatrixCursor(android.database.MatrixCursor)

Example 88 with SQLiteQueryBuilder

use of android.database.sqlite.SQLiteQueryBuilder in project apps-android-commons by commons-app.

the class ContributionsContentProvider method query.

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(Contribution.Table.TABLE_NAME);
    int uriType = uriMatcher.match(uri);
    SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
    Cursor cursor;
    switch(uriType) {
        case CONTRIBUTIONS:
            cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
            break;
        case CONTRIBUTIONS_ID:
            cursor = queryBuilder.query(db, Contribution.Table.ALL_FIELDS, "_id = ?", new String[] { uri.getLastPathSegment() }, null, null, sortOrder);
            break;
        default:
            throw new IllegalArgumentException("Unknown URI" + uri);
    }
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder)

Example 89 with SQLiteQueryBuilder

use of android.database.sqlite.SQLiteQueryBuilder in project android_frameworks_base by crdroidandroid.

the class LocalProvider method query.

@Override
public Cursor query(Uri url, String[] projectionIn, String selection, String[] selectionArgs, String sort) {
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    // Generate the body of the query
    int match = sURLMatcher.match(url);
    switch(match) {
        case DATA:
            qb.setTables("data");
            break;
        case DATA_ID:
            qb.setTables("data");
            qb.appendWhere("_id=");
            qb.appendWhere(url.getPathSegments().get(1));
            break;
        default:
            throw new IllegalArgumentException("Unknown URL " + url);
    }
    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    Cursor ret = qb.query(db, projectionIn, selection, selectionArgs, null, null, sort);
    if (ret == null) {
        if (false)
            Log.d(TAG, "Alarms.query: failed");
    } else {
        ret.setNotificationUri(getContext().getContentResolver(), url);
    }
    return ret;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder)

Example 90 with SQLiteQueryBuilder

use of android.database.sqlite.SQLiteQueryBuilder in project 360-Engine-for-Android by 360.

the class DatabaseProvider method query.

/**
 * Allows direct access to the 360 Service database. A properly formulated
 * query will allow you to get any data you want from the service. Simply
 * use the table name (available from the documentation, or checking the
 * array at the top of this class).
 *
 * Example query:
 * "content://com.vodafone360.people.service.aidl.databaseaccess/Activities"
 *
 * This method is synchronised, as we need it to be thread safe.  I.e.
 * several concurrent queries could be made from multiple processes.
 *
 * @param uri Database URI.
 * @param projection Database query projection.
 * @param selection Database query selection.
 * @param selectionArgs Database query selectionArgs.
 * @param sortOrder Database query sortOrder.
 * @return Cursor containing database query result.
 */
@Override
public final Cursor query(final Uri uri, final String[] projection, final String selection, final String[] selectionArgs, final String sortOrder) {
    LogUtils.logI("DatabaseProvider.query() Received incoming query uri[" + uri + "]");
    if (!SettingsManager.getBooleanProperty(Settings.ENABLE_AIDL_KEY)) {
        LogUtils.logI("DatabaseProvider.query() 360 Engine database not " + "available to third parties (set at build time)");
        throw new RuntimeException("Looks like you tried to query the " + "com.vodafone360.people database, but access was " + "disabled at build time.");
    }
    /**
     * Return the result of a direct query to the database on the
     * appropriate table (set by URI).
     */
    String queriedTable = uri.getPath();
    if (queriedTable.startsWith("/")) {
        queriedTable = queriedTable.replaceFirst("/", "");
    }
    final SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
    boolean matchedATable = false;
    for (String table : TABLE_NAMES) {
        if (table.equals(queriedTable)) {
            LogUtils.logI("DatabaseProvider.query() Matched incoming URI " + "query to the [" + table + "] table.");
            qBuilder.setTables(queriedTable);
            matchedATable = true;
            break;
        }
    }
    /*
         * If we found a table, perform the query as expected; otherwise throw
         * an exception. This might crash the client process, but
         * com.vodafone360.people is safe, i.e. the Exception gets parceled up
         * and passed out.
         */
    if (matchedATable) {
        return qBuilder.query(mDatabaseHelper.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder);
    } else {
        LogUtils.logE("DatabaseProvider.query() Oops! Someone tried to " + "query a database table that wasn't there; passing out " + "an exception");
        throw new IllegalArgumentException("DatabaseProvider.query() " + "Couldn't find table [" + queriedTable + "], did you " + "properly form your query?");
    }
}
Also used : SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder)

Aggregations

SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)106 Cursor (android.database.Cursor)93 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)44 ContentValues (android.content.ContentValues)8 Account (android.accounts.Account)7 SQLiteException (android.database.sqlite.SQLiteException)7 File (java.io.File)7 HashMap (java.util.HashMap)7 SyncStatusInfo (android.content.SyncStatusInfo)6 MatrixCursor (android.database.MatrixCursor)5 RowBuilder (android.database.MatrixCursor.RowBuilder)5 ContentResolver (android.content.ContentResolver)4 Test (org.junit.Test)2 Matchers.anyLong (org.mockito.Matchers.anyLong)2 Returns (org.mockito.internal.stubbing.answers.Returns)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 AbstractCursor (android.database.AbstractCursor)1 MergeCursor (android.database.MergeCursor)1 AtomicFile (android.util.AtomicFile)1 AtomicFile (com.android.internal.os.AtomicFile)1