Search in sources :

Example 66 with SQLiteQueryBuilder

use of android.database.sqlite.SQLiteQueryBuilder in project GridListViewAdapters by birajpatel.

the class TestContentProvider method query.

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    if (null == uri || null == sUriMatcher) {
        return null;
    }
    SQLiteQueryBuilder queryBuilder = getQueryBuilderForUri(uri);
    SQLiteDatabase sqliteDataBase = dbHelper.getReadableDatabase();
    Cursor cursor = null;
    if (null != sqliteDataBase && null != queryBuilder && sqliteDataBase.isOpen()) {
        try {
            sqliteDataBase.beginTransaction();
            cursor = queryBuilder.query(sqliteDataBase, projection, selection, selectionArgs, null, null, sortOrder);
            sqliteDataBase.setTransactionSuccessful();
            sqliteDataBase.endTransaction();
            if (null != cursor) {
                cursor.setNotificationUri(getContext().getContentResolver(), uri);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        // here we can not close the cursor because it will be used by
        // client application to retrieve data from .
        }
    }
    return cursor;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder)

Example 67 with SQLiteQueryBuilder

use of android.database.sqlite.SQLiteQueryBuilder in project GridListViewAdapters by birajpatel.

the class TestContentProvider method getQueryBuilderForUri.

private SQLiteQueryBuilder getQueryBuilderForUri(Uri uri) {
    SQLiteQueryBuilder sQLiteQueryBuilderObj = null;
    switch(sUriMatcher.match(uri)) {
        case EMPLOYEE_TABLE_IDENTIFIER:
            sQLiteQueryBuilderObj = new SQLiteQueryBuilder();
            sQLiteQueryBuilderObj.setTables(EMPLOYEE_TABLE_NAME);
            sQLiteQueryBuilderObj.setProjectionMap(sEmployeeProjectionMap);
            break;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
    }
    return sQLiteQueryBuilderObj;
}
Also used : SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder)

Example 68 with SQLiteQueryBuilder

use of android.database.sqlite.SQLiteQueryBuilder in project muzei by romannurik.

the class MuzeiProvider method queryArtwork.

private Cursor queryArtwork(@NonNull final Uri uri, final String[] projection, final String selection, final String[] selectionArgs, final String sortOrder) {
    ContentResolver contentResolver = getContext() != null ? getContext().getContentResolver() : null;
    if (contentResolver == null) {
        return null;
    }
    final SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    qb.setTables(MuzeiContract.Artwork.TABLE_NAME + " INNER JOIN " + MuzeiContract.Sources.TABLE_NAME + " ON " + MuzeiContract.Artwork.TABLE_NAME + "." + MuzeiContract.Artwork.COLUMN_NAME_SOURCE_COMPONENT_NAME + "=" + MuzeiContract.Sources.TABLE_NAME + "." + MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME);
    qb.setProjectionMap(allArtworkColumnProjectionMap);
    final SQLiteDatabase db = databaseHelper.getReadableDatabase();
    if (MuzeiProvider.uriMatcher.match(uri) == ARTWORK_ID) {
        // If the incoming URI is for a single source identified by its ID, appends "_ID = <artworkId>"
        // to the where clause, so that it selects that single piece of artwork
        qb.appendWhere(MuzeiContract.Artwork.TABLE_NAME + "." + BaseColumns._ID + "=" + uri.getLastPathSegment());
    }
    String orderBy;
    if (TextUtils.isEmpty(sortOrder))
        orderBy = MuzeiContract.Sources.COLUMN_NAME_IS_SELECTED + " DESC, " + MuzeiContract.Artwork.DEFAULT_SORT_ORDER;
    else
        orderBy = sortOrder;
    final Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy, null);
    c.setNotificationUri(contentResolver, uri);
    return c;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder) ContentResolver(android.content.ContentResolver)

Example 69 with SQLiteQueryBuilder

use of android.database.sqlite.SQLiteQueryBuilder in project android by owncloud.

the class FileContentProvider method query.

private Cursor query(SQLiteDatabase db, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder sqlQuery = new SQLiteQueryBuilder();
    sqlQuery.setTables(ProviderTableMeta.FILE_TABLE_NAME);
    switch(mUriMatcher.match(uri)) {
        case ROOT_DIRECTORY:
            break;
        case DIRECTORY:
            String folderId = uri.getPathSegments().get(1);
            sqlQuery.appendWhere(ProviderTableMeta.FILE_PARENT + "=" + folderId);
            break;
        case SINGLE_FILE:
            if (uri.getPathSegments().size() > 1) {
                sqlQuery.appendWhere(ProviderTableMeta._ID + "=" + uri.getPathSegments().get(1));
            }
            break;
        case SHARES:
            sqlQuery.setTables(ProviderTableMeta.OCSHARES_TABLE_NAME);
            if (uri.getPathSegments().size() > 1) {
                sqlQuery.appendWhere(ProviderTableMeta._ID + "=" + uri.getPathSegments().get(1));
            }
            break;
        case CAPABILITIES:
            sqlQuery.setTables(ProviderTableMeta.CAPABILITIES_TABLE_NAME);
            if (uri.getPathSegments().size() > 1) {
                sqlQuery.appendWhere(ProviderTableMeta._ID + "=" + uri.getPathSegments().get(1));
            }
            break;
        case UPLOADS:
            sqlQuery.setTables(ProviderTableMeta.UPLOADS_TABLE_NAME);
            if (uri.getPathSegments().size() > 1) {
                sqlQuery.appendWhere(ProviderTableMeta._ID + "=" + uri.getPathSegments().get(1));
            }
            break;
        default:
            throw new IllegalArgumentException("Unknown uri id: " + uri);
    }
    String order;
    if (TextUtils.isEmpty(sortOrder)) {
        switch(mUriMatcher.match(uri)) {
            case SHARES:
                order = ProviderTableMeta.OCSHARES_DEFAULT_SORT_ORDER;
                break;
            case CAPABILITIES:
                order = ProviderTableMeta.CAPABILITIES_DEFAULT_SORT_ORDER;
                break;
            case UPLOADS:
                order = ProviderTableMeta.UPLOADS_DEFAULT_SORT_ORDER;
                break;
            default:
                // Files
                order = ProviderTableMeta.FILE_DEFAULT_SORT_ORDER;
                break;
        }
    } else {
        order = sortOrder;
    }
    // DB case_sensitive
    db.execSQL("PRAGMA case_sensitive_like = true");
    Cursor c = sqlQuery.query(db, projection, selection, selectionArgs, null, null, order);
    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
}
Also used : Cursor(android.database.Cursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder)

Example 70 with SQLiteQueryBuilder

use of android.database.sqlite.SQLiteQueryBuilder in project Shuttle by timusus.

the class PlayCountContentProvider method query.

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    // Using SQLiteQueryBuilder instead of query() method
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    // Check if the caller has requested a column which does not exists
    checkColumns(projection);
    // Set the table
    queryBuilder.setTables(PlayCountTable.TABLE_PLAY_COUNT);
    int uriType = sURIMatcher.match(uri);
    switch(uriType) {
        case PLAY_COUNT:
            break;
        case PLAY_COUNT_ID:
            // Adding the ID to the original query
            queryBuilder.appendWhere(PlayCountTable.COLUMN_ID + "=" + uri.getLastPathSegment());
            break;
        default:
            throw new IllegalArgumentException("Unknown URI: " + uri);
    }
    SQLiteDatabase db = database.getWritableDatabase();
    Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
    // Make sure that potential listeners are getting notified
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) 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