Search in sources :

Example 56 with SQLiteQueryBuilder

use of android.database.sqlite.SQLiteQueryBuilder in project mobile-center-sdk-android by Microsoft.

the class DatabaseManagerTest method switchInMemory.

@Test
public void switchInMemory() throws Exception {
    DatabaseManager databaseManagerMock;
    /* Put. */
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.put(new ContentValues());
    verify(databaseManagerMock).switchToInMemory(eq("put"), any(RuntimeException.class));
    /* Update. */
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.update(0, new ContentValues());
    verify(databaseManagerMock).switchToInMemory(eq("update"), any(RuntimeException.class));
    /* Get. */
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.get(0);
    verify(databaseManagerMock).switchToInMemory(eq("get"), any(RuntimeException.class));
    /* Scanner. */
    {
        databaseManagerMock = getDatabaseManagerMock();
        databaseManagerMock.getScanner(null, null, false).iterator();
        verify(databaseManagerMock).switchToInMemory(eq("scan.iterator"), any(RuntimeException.class));
    }
    {
        databaseManagerMock = getDatabaseManagerMock();
        databaseManagerMock.getScanner(null, null, false).getCount();
        verify(databaseManagerMock).switchToInMemory(eq("scan.count"), any(RuntimeException.class));
    }
    {
        /* Cursor next failing but closing working. */
        databaseManagerMock = spy(new DatabaseManager(null, "database", "table", 1, null, null));
        when(databaseManagerMock.getDatabase()).thenReturn(mock(SQLiteDatabase.class));
        mockStatic(SQLiteUtils.class);
        Cursor cursor = mock(Cursor.class);
        SQLiteQueryBuilder sqLiteQueryBuilder = mock(SQLiteQueryBuilder.class, new Returns(cursor));
        when(SQLiteUtils.newSQLiteQueryBuilder()).thenReturn(sqLiteQueryBuilder);
        when(cursor.moveToNext()).thenThrow(new RuntimeException());
        DatabaseManager.Scanner scanner = databaseManagerMock.getScanner(null, null, false);
        assertFalse(scanner.iterator().hasNext());
        verify(databaseManagerMock).switchToInMemory(eq("scan.hasNext"), any(RuntimeException.class));
        /* We switched over in memory so closing will not switch again. Cursor is closed already. */
        doThrow(new RuntimeException()).when(cursor).close();
        scanner.close();
        verify(databaseManagerMock, never()).switchToInMemory(eq("scan.close"), any(RuntimeException.class));
    }
    {
        /* Cursor next failing and closing failing. */
        databaseManagerMock = spy(new DatabaseManager(null, "database", "table", 1, null, null));
        when(databaseManagerMock.getDatabase()).thenReturn(mock(SQLiteDatabase.class));
        mockStatic(SQLiteUtils.class);
        Cursor cursor = mock(Cursor.class);
        SQLiteQueryBuilder sqLiteQueryBuilder = mock(SQLiteQueryBuilder.class, new Returns(cursor));
        when(SQLiteUtils.newSQLiteQueryBuilder()).thenReturn(sqLiteQueryBuilder);
        when(cursor.moveToNext()).thenThrow(new RuntimeException());
        doThrow(new RuntimeException()).when(cursor).close();
        DatabaseManager.Scanner scanner = databaseManagerMock.getScanner(null, null, false);
        assertFalse(scanner.iterator().hasNext());
        verify(databaseManagerMock).switchToInMemory(eq("scan.hasNext"), any(RuntimeException.class));
        /* We switched over in memory so closing will not switch again. Cursor is closed already in hasNext(). */
        scanner.close();
        verify(databaseManagerMock, never()).switchToInMemory(eq("scan.close"), any(RuntimeException.class));
    }
    {
        /* Cursor closing failing. */
        databaseManagerMock = spy(new DatabaseManager(null, "database", "table", 1, null, null));
        when(databaseManagerMock.getDatabase()).thenReturn(mock(SQLiteDatabase.class));
        mockStatic(SQLiteUtils.class);
        Cursor cursor = mock(Cursor.class);
        SQLiteQueryBuilder sqLiteQueryBuilder = mock(SQLiteQueryBuilder.class, new Returns(cursor));
        when(SQLiteUtils.newSQLiteQueryBuilder()).thenReturn(sqLiteQueryBuilder);
        doThrow(new RuntimeException()).when(cursor).close();
        DatabaseManager.Scanner scanner = databaseManagerMock.getScanner(null, null, false);
        assertFalse(scanner.iterator().hasNext());
        scanner.close();
        verify(databaseManagerMock).switchToInMemory(eq("scan.close"), any(RuntimeException.class));
    }
    /* Delete. */
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.delete(0);
    verify(databaseManagerMock).switchToInMemory(eq("delete"), any(RuntimeException.class));
    /* Delete multiple IDs. */
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.delete(new ArrayList<Long>());
    verify(databaseManagerMock, never()).switchToInMemory(eq("delete"), any(RuntimeException.class));
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.delete(Arrays.asList(new Long[] { 0L, 1L }));
    verify(databaseManagerMock).switchToInMemory(eq("delete"), any(RuntimeException.class));
    /* Clear. */
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.clear();
    verify(databaseManagerMock).switchToInMemory(eq("clear"), any(RuntimeException.class));
    /* Close. */
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.close();
    verify(databaseManagerMock).switchToInMemory(eq("close"), any(RuntimeException.class));
    /* Row count. */
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.getRowCount();
    verify(databaseManagerMock).switchToInMemory(eq("count"), any(RuntimeException.class));
}
Also used : ContentValues(android.content.ContentValues) Returns(org.mockito.internal.stubbing.answers.Returns) Matchers.anyLong(org.mockito.Matchers.anyLong) Cursor(android.database.Cursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 57 with SQLiteQueryBuilder

use of android.database.sqlite.SQLiteQueryBuilder in project android-sqlite-asset-helper by jgilfelt.

the class MyDatabase method getEmployees.

public Cursor getEmployees() {
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    String[] sqlSelect = { "0 _id", "FullName" };
    String sqlTables = "Employees";
    qb.setTables(sqlTables);
    Cursor c = qb.query(db, sqlSelect, null, null, null, null, null);
    c.moveToFirst();
    return c;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder)

Example 58 with SQLiteQueryBuilder

use of android.database.sqlite.SQLiteQueryBuilder in project android-jigsaw-puzzle by julesbond007.

the class ImageProvider method query.

@Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(JIGSAW_TABLE);
    switch(uriMatcher.match(uri)) {
        case IMAGES_ID:
            queryBuilder.appendWhere(ID_SELECTION + uri.getLastPathSegment());
            break;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
    }
    Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}
Also used : Cursor(android.database.Cursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder)

Example 59 with SQLiteQueryBuilder

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

the class LauncherProvider method query.

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    qb.setTables(args.table);
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
    result.setNotificationUri(getContext().getContentResolver(), uri);
    return result;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder)

Example 60 with SQLiteQueryBuilder

use of android.database.sqlite.SQLiteQueryBuilder in project android-sqlite-asset-helper by jgilfelt.

the class MyDatabase method getUpgradeVersion.

public int getUpgradeVersion() {
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    String[] sqlSelect = { "MAX (version)" };
    String sqlTables = "upgrades";
    qb.setTables(sqlTables);
    Cursor c = qb.query(db, sqlSelect, null, null, null, null, null);
    int v = 0;
    c.moveToFirst();
    if (!c.isAfterLast()) {
        v = c.getInt(0);
    }
    c.close();
    return v;
}
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