Search in sources :

Example 6 with InsertQuery

use of com.pushtorefresh.storio3.sqlite.queries.InsertQuery in project storio by pushtorefresh.

the class DefaultPutResolver method performPut.

/**
 * {@inheritDoc}
 */
@NonNull
@Override
public PutResult performPut(@NonNull StorIOContentResolver storIOContentResolver, @NonNull T object) {
    final UpdateQuery updateQuery = mapToUpdateQuery(object);
    final Query query = Query.builder().uri(updateQuery.uri()).where(nullableString(updateQuery.where())).whereArgs((Object[]) nullableArrayOfStringsFromListOfStrings(updateQuery.whereArgs())).build();
    final StorIOContentResolver.LowLevel lowLevel = storIOContentResolver.lowLevel();
    final Cursor cursor = lowLevel.query(query);
    try {
        final ContentValues contentValues = mapToContentValues(object);
        if (cursor.getCount() == 0) {
            final InsertQuery insertQuery = mapToInsertQuery(object);
            final Uri insertedUri = lowLevel.insert(insertQuery, contentValues);
            return PutResult.newInsertResult(insertedUri, insertQuery.uri());
        } else {
            final int numberOfRowsUpdated = lowLevel.update(updateQuery, contentValues);
            return PutResult.newUpdateResult(numberOfRowsUpdated, updateQuery.uri());
        }
    } finally {
        cursor.close();
    }
}
Also used : ContentValues(android.content.ContentValues) InsertQuery(com.pushtorefresh.storio3.contentresolver.queries.InsertQuery) InsertQuery(com.pushtorefresh.storio3.contentresolver.queries.InsertQuery) Query(com.pushtorefresh.storio3.contentresolver.queries.Query) UpdateQuery(com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery) UpdateQuery(com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery) Cursor(android.database.Cursor) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Uri(android.net.Uri) NonNull(android.support.annotation.NonNull)

Example 7 with InsertQuery

use of com.pushtorefresh.storio3.sqlite.queries.InsertQuery in project storio by pushtorefresh.

the class DefaultPutResolverTest method shouldCloseCursorIfInsertThrowsException.

@Test
public void shouldCloseCursorIfInsertThrowsException() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
    when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
    // item without id, should be inserted
    final TestItem testItem = new TestItem(null);
    final Query expectedQuery = Query.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
    final Cursor cursor = mock(Cursor.class);
    when(lowLevel.query(eq(expectedQuery))).thenReturn(cursor);
    when(cursor.getCount()).thenReturn(// No results -> insert should be performed
    0);
    final InsertQuery expectedInsertQuery = InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
    when(lowLevel.insert(eq(expectedInsertQuery), any(ContentValues.class))).thenThrow(new IllegalStateException("Fake exception from ContentResolver"));
    final PutResolver<TestItem> putResolver = new DefaultPutResolver<TestItem>() {

        @NonNull
        @Override
        protected InsertQuery mapToInsertQuery(@NonNull TestItem object) {
            return InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
        }

        @NonNull
        @Override
        protected UpdateQuery mapToUpdateQuery(@NonNull TestItem object) {
            return UpdateQuery.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(object.getId()).build();
        }

        @NonNull
        @Override
        protected ContentValues mapToContentValues(@NonNull TestItem object) {
            try {
                return TestItem.MAP_TO_CONTENT_VALUES.apply(object);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    try {
        putResolver.performPut(storIOContentResolver, testItem);
        failBecauseExceptionWasNotThrown(IllegalStateException.class);
    } catch (IllegalStateException expected) {
        assertThat(expected).hasMessage("Fake exception from ContentResolver");
        verify(storIOContentResolver).lowLevel();
        // Checks that it asks actual ContentResolver for results
        verify(lowLevel).query(eq(expectedQuery));
        verify(cursor).getCount();
        // Cursor must be closed in case of exception!
        verify(cursor).close();
        verify(lowLevel).insert(eq(expectedInsertQuery), any(ContentValues.class));
        verifyNoMoreInteractions(storIOContentResolver, lowLevel, cursor);
    }
}
Also used : ContentValues(android.content.ContentValues) InsertQuery(com.pushtorefresh.storio3.contentresolver.queries.InsertQuery) Query(com.pushtorefresh.storio3.contentresolver.queries.Query) UpdateQuery(com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery) Cursor(android.database.Cursor) InsertQuery(com.pushtorefresh.storio3.contentresolver.queries.InsertQuery) NonNull(android.support.annotation.NonNull) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 8 with InsertQuery

use of com.pushtorefresh.storio3.sqlite.queries.InsertQuery in project storio by pushtorefresh.

the class DefaultPutResolverTest method shouldCloseCursorIfUpdateThrowsException.

@Test
public void shouldCloseCursorIfUpdateThrowsException() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
    when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
    // item with some id, should be updated
    final TestItem testItem = new TestItem(1L);
    final Query expectedQuery = Query.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
    final Cursor cursor = mock(Cursor.class);
    when(lowLevel.query(eq(expectedQuery))).thenReturn(cursor);
    when(cursor.getCount()).thenReturn(// One result -> update should be performed
    1);
    final UpdateQuery expectedUpdateQuery = UpdateQuery.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
    when(lowLevel.update(eq(expectedUpdateQuery), any(ContentValues.class))).thenThrow(new IllegalStateException("Fake exception from ContentResolver"));
    final PutResolver<TestItem> putResolver = new DefaultPutResolver<TestItem>() {

        @NonNull
        @Override
        protected InsertQuery mapToInsertQuery(@NonNull TestItem object) {
            return InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
        }

        @NonNull
        @Override
        protected UpdateQuery mapToUpdateQuery(@NonNull TestItem object) {
            return UpdateQuery.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(object.getId()).build();
        }

        @NonNull
        @Override
        protected ContentValues mapToContentValues(@NonNull TestItem object) {
            try {
                return TestItem.MAP_TO_CONTENT_VALUES.apply(object);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    try {
        putResolver.performPut(storIOContentResolver, testItem);
        failBecauseExceptionWasNotThrown(IllegalStateException.class);
    } catch (IllegalStateException expected) {
        assertThat(expected).hasMessage("Fake exception from ContentResolver");
        verify(storIOContentResolver).lowLevel();
        // Checks that it asks actual ContentResolver for results
        verify(lowLevel).query(eq(expectedQuery));
        verify(cursor).getCount();
        // Cursor must be closed in case of exception!
        verify(cursor).close();
        verify(lowLevel).update(eq(expectedUpdateQuery), any(ContentValues.class));
        verifyNoMoreInteractions(storIOContentResolver, lowLevel, cursor);
    }
}
Also used : ContentValues(android.content.ContentValues) InsertQuery(com.pushtorefresh.storio3.contentresolver.queries.InsertQuery) Query(com.pushtorefresh.storio3.contentresolver.queries.Query) UpdateQuery(com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery) UpdateQuery(com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery) Cursor(android.database.Cursor) NonNull(android.support.annotation.NonNull) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 9 with InsertQuery

use of com.pushtorefresh.storio3.sqlite.queries.InsertQuery in project storio by pushtorefresh.

the class CarPersonRelationPutResolver method performPut.

@Override
@NonNull
public PutResult performPut(@NonNull StorIOSQLite storIOSQLite, @NonNull ContentValues contentValues) {
    final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
    lowLevel.beginTransaction();
    try {
        final InsertQuery insertQuery = InsertQuery.builder().table(TABLE).build();
        final long insertedId = lowLevel.insert(insertQuery, contentValues);
        final PutResult putResult = PutResult.newInsertResult(insertedId, insertQuery.table());
        lowLevel.setTransactionSuccessful();
        return putResult;
    } finally {
        lowLevel.endTransaction();
    }
}
Also used : InsertQuery(com.pushtorefresh.storio3.sqlite.queries.InsertQuery) PutResult(com.pushtorefresh.storio3.sqlite.operations.put.PutResult) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) NonNull(android.support.annotation.NonNull)

Example 10 with InsertQuery

use of com.pushtorefresh.storio3.sqlite.queries.InsertQuery in project storio by pushtorefresh.

the class DefaultPutResolverTest method update.

/**
 * Verifies behavior of {@link DefaultPutResolver} for "update"
 */
@Test
public void update() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.LowLevel lowLevel = mock(StorIOSQLite.LowLevel.class);
    // item with some id, should be updated
    final TestItem testItem = new TestItem(null);
    when(storIOSQLite.lowLevel()).thenReturn(lowLevel);
    final Query expectedQuery = Query.builder().table(TestItem.TABLE).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
    final Cursor cursor = mock(Cursor.class);
    when(lowLevel.query(eq(expectedQuery))).thenReturn(cursor);
    when(cursor.getCount()).thenReturn(// Some rows already in db -> update should be performed
    1);
    final Integer expectedNumberOfRowsUpdated = 1;
    when(lowLevel.update(any(UpdateQuery.class), any(ContentValues.class))).thenReturn(expectedNumberOfRowsUpdated);
    final Set<String> tags = singleton("test_tag");
    final UpdateQuery expectedUpdateQuery = UpdateQuery.builder().table(TestItem.TABLE).affectsTags(tags).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
    final PutResolver<TestItem> putResolver = new DefaultPutResolver<TestItem>() {

        @NonNull
        @Override
        protected InsertQuery mapToInsertQuery(@NonNull TestItem object) {
            fail("Should not be called");
            return null;
        }

        @NonNull
        @Override
        protected UpdateQuery mapToUpdateQuery(@NonNull TestItem object) {
            return expectedUpdateQuery;
        }

        @NonNull
        @Override
        protected ContentValues mapToContentValues(@NonNull TestItem object) {
            return TestItem.MAP_TO_CONTENT_VALUES.apply(object);
        }
    };
    final ContentValues expectedContentValues = TestItem.MAP_TO_CONTENT_VALUES.apply(testItem);
    // Performing Put that should "update"
    final PutResult putResult = putResolver.performPut(storIOSQLite, testItem);
    verify(lowLevel).beginTransaction();
    verify(lowLevel).setTransactionSuccessful();
    verify(lowLevel).endTransaction();
    // checks that it asks db for results
    verify(lowLevel).query(eq(expectedQuery));
    // checks that cursor was closed
    verify(cursor).close();
    // only one query should occur
    verify(lowLevel).query(any(Query.class));
    // checks that required update was performed
    verify(lowLevel).update(eq(expectedUpdateQuery), eq(expectedContentValues));
    // only one update should occur
    verify(lowLevel).update(any(UpdateQuery.class), any(ContentValues.class));
    // no inserts should occur
    verify(lowLevel, never()).insert(any(InsertQuery.class), any(ContentValues.class));
    // put result checks
    assertThat(putResult.wasInserted()).isFalse();
    assertThat(putResult.wasUpdated()).isTrue();
    assertThat(putResult.numberOfRowsUpdated()).isEqualTo(expectedNumberOfRowsUpdated);
    assertThat(putResult.insertedId()).isNull();
    assertThat(putResult.affectedTables()).containsExactly(TestItem.TABLE);
    assertThat(putResult.affectedTags()).isEqualTo(tags);
}
Also used : ContentValues(android.content.ContentValues) Query(com.pushtorefresh.storio3.sqlite.queries.Query) UpdateQuery(com.pushtorefresh.storio3.sqlite.queries.UpdateQuery) InsertQuery(com.pushtorefresh.storio3.sqlite.queries.InsertQuery) UpdateQuery(com.pushtorefresh.storio3.sqlite.queries.UpdateQuery) Cursor(android.database.Cursor) InsertQuery(com.pushtorefresh.storio3.sqlite.queries.InsertQuery) NonNull(android.support.annotation.NonNull) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) Test(org.junit.Test)

Aggregations

ContentValues (android.content.ContentValues)9 NonNull (android.support.annotation.NonNull)9 Cursor (android.database.Cursor)8 Test (org.junit.Test)7 StorIOContentResolver (com.pushtorefresh.storio3.contentresolver.StorIOContentResolver)5 InsertQuery (com.pushtorefresh.storio3.contentresolver.queries.InsertQuery)5 Query (com.pushtorefresh.storio3.contentresolver.queries.Query)5 UpdateQuery (com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery)5 InsertQuery (com.pushtorefresh.storio3.sqlite.queries.InsertQuery)5 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)4 UpdateQuery (com.pushtorefresh.storio3.sqlite.queries.UpdateQuery)3 Uri (android.net.Uri)2 Query (com.pushtorefresh.storio3.sqlite.queries.Query)2 PutResult (com.pushtorefresh.storio3.sqlite.operations.put.PutResult)1