Search in sources :

Example 1 with UpdateQuery

use of com.pushtorefresh.storio.sqlite.queries.UpdateQuery in project storio by pushtorefresh.

the class DefaultPutResolver method performPut.

/**
     * {@inheritDoc}
     */
@NonNull
@Override
public PutResult performPut(@NonNull StorIOSQLite storIOSQLite, @NonNull T object) {
    final UpdateQuery updateQuery = mapToUpdateQuery(object);
    final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
    // for data consistency in concurrent environment, encapsulate Put Operation into transaction
    lowLevel.beginTransaction();
    try {
        final Cursor cursor = lowLevel.query(Query.builder().table(updateQuery.table()).where(nullableString(updateQuery.where())).whereArgs((Object[]) nullableArrayOfStringsFromListOfStrings(updateQuery.whereArgs())).build());
        final PutResult putResult;
        try {
            final ContentValues contentValues = mapToContentValues(object);
            if (cursor.getCount() == 0) {
                final InsertQuery insertQuery = mapToInsertQuery(object);
                final long insertedId = lowLevel.insert(insertQuery, contentValues);
                putResult = PutResult.newInsertResult(insertedId, insertQuery.table());
            } else {
                final int numberOfRowsUpdated = lowLevel.update(updateQuery, contentValues);
                putResult = PutResult.newUpdateResult(numberOfRowsUpdated, updateQuery.table());
            }
        } finally {
            cursor.close();
        }
        // everything okay
        lowLevel.setTransactionSuccessful();
        return putResult;
    } finally {
        // in case of bad situations, db won't be affected
        lowLevel.endTransaction();
    }
}
Also used : ContentValues(android.content.ContentValues) InsertQuery(com.pushtorefresh.storio.sqlite.queries.InsertQuery) UpdateQuery(com.pushtorefresh.storio.sqlite.queries.UpdateQuery) Cursor(android.database.Cursor) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) NonNull(android.support.annotation.NonNull)

Example 2 with UpdateQuery

use of com.pushtorefresh.storio.sqlite.queries.UpdateQuery in project storio by pushtorefresh.

the class DefaultPutResolverTest method insert.

/**
     * Verifies behavior of {@link DefaultPutResolver} for "insert"
     */
@Test
public void insert() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.Internal internal = mock(StorIOSQLite.Internal.class);
    // item without id, should be inserted
    final TestItem testItem = new TestItem(null);
    when(storIOSQLite.lowLevel()).thenReturn(internal);
    final Long expectedInsertedId = 24L;
    final Query expectedQuery = Query.builder().table(TestItem.TABLE).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
    final Cursor cursor = mock(Cursor.class);
    when(internal.query(eq(expectedQuery))).thenReturn(cursor);
    when(cursor.getCount()).thenReturn(// No results -> insert should be performed
    0);
    when(internal.insert(any(InsertQuery.class), any(ContentValues.class))).thenReturn(expectedInsertedId);
    final InsertQuery expectedInsertQuery = InsertQuery.builder().table(TestItem.TABLE).nullColumnHack(null).build();
    final PutResolver<TestItem> putResolver = new DefaultPutResolver<TestItem>() {

        @NonNull
        @Override
        protected InsertQuery mapToInsertQuery(@NonNull TestItem object) {
            return expectedInsertQuery;
        }

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

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

Example 3 with UpdateQuery

use of com.pushtorefresh.storio.sqlite.queries.UpdateQuery 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.Internal internal = mock(StorIOSQLite.Internal.class);
    // item with some id, should be updated
    final TestItem testItem = new TestItem(null);
    when(storIOSQLite.lowLevel()).thenReturn(internal);
    final Query expectedQuery = Query.builder().table(TestItem.TABLE).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
    final Cursor cursor = mock(Cursor.class);
    when(internal.query(eq(expectedQuery))).thenReturn(cursor);
    when(cursor.getCount()).thenReturn(// Some rows already in db -> update should be performed
    1);
    final Integer expectedNumberOfRowsUpdated = 1;
    when(internal.update(any(UpdateQuery.class), any(ContentValues.class))).thenReturn(expectedNumberOfRowsUpdated);
    final UpdateQuery expectedUpdateQuery = UpdateQuery.builder().table(TestItem.TABLE).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 UpdateQuery.builder().table(TestItem.TABLE).where(TestItem.COLUMN_ID + " = ?").whereArgs(object.getId()).build();
        }

        @NonNull
        @Override
        protected ContentValues mapToContentValues(@NonNull TestItem object) {
            return TestItem.MAP_TO_CONTENT_VALUES.call(object);
        }
    };
    final ContentValues expectedContentValues = TestItem.MAP_TO_CONTENT_VALUES.call(testItem);
    // Performing Put that should "update"
    final PutResult putResult = putResolver.performPut(storIOSQLite, testItem);
    verify(internal, times(1)).beginTransaction();
    verify(internal, times(1)).setTransactionSuccessful();
    verify(internal, times(1)).endTransaction();
    // checks that it asks db for results
    verify(internal, times(1)).query(eq(expectedQuery));
    // checks that cursor was closed
    verify(cursor, times(1)).close();
    // only one query should occur
    verify(internal, times(1)).query(any(Query.class));
    // checks that required update was performed
    verify(internal, times(1)).update(eq(expectedUpdateQuery), eq(expectedContentValues));
    // only one update should occur
    verify(internal, times(1)).update(any(UpdateQuery.class), any(ContentValues.class));
    // no inserts should occur
    verify(internal, times(0)).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();
}
Also used : ContentValues(android.content.ContentValues) Query(com.pushtorefresh.storio.sqlite.queries.Query) InsertQuery(com.pushtorefresh.storio.sqlite.queries.InsertQuery) UpdateQuery(com.pushtorefresh.storio.sqlite.queries.UpdateQuery) UpdateQuery(com.pushtorefresh.storio.sqlite.queries.UpdateQuery) Cursor(android.database.Cursor) InsertQuery(com.pushtorefresh.storio.sqlite.queries.InsertQuery) NonNull(android.support.annotation.NonNull) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Aggregations

ContentValues (android.content.ContentValues)3 Cursor (android.database.Cursor)3 NonNull (android.support.annotation.NonNull)3 StorIOSQLite (com.pushtorefresh.storio.sqlite.StorIOSQLite)3 InsertQuery (com.pushtorefresh.storio.sqlite.queries.InsertQuery)3 UpdateQuery (com.pushtorefresh.storio.sqlite.queries.UpdateQuery)3 Query (com.pushtorefresh.storio.sqlite.queries.Query)2 Test (org.junit.Test)2