Search in sources :

Example 71 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 72 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 73 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)

Aggregations

SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)73 Cursor (android.database.Cursor)61 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)44 Account (android.accounts.Account)7 ContentValues (android.content.ContentValues)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 AbstractCursor (android.database.AbstractCursor)1 MergeCursor (android.database.MergeCursor)1 AtomicFile (android.util.AtomicFile)1 AtomicFile (com.android.internal.os.AtomicFile)1 HashSet (java.util.HashSet)1 Database (org.greenrobot.greendao.database.Database)1 StandardDatabase (org.greenrobot.greendao.database.StandardDatabase)1 Before (org.junit.Before)1