Search in sources :

Example 1 with PutResult

use of com.pushtorefresh.storio3.contentresolver.operations.put.PutResult in project storio by pushtorefresh.

the class CarRelationsPutResolver method performPut.

@Override
@NonNull
public PutResult performPut(@NonNull StorIOSQLite storIOSQLite, @NonNull Car object) {
    final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
    lowLevel.beginTransaction();
    try {
        final PutResult putResult = super.performPut(storIOSQLite, object);
        // noinspection ConstantConditions
        final long carId = putResult.wasInserted() ? putResult.insertedId() : object.id();
        storIOSQLite.delete().byQuery(DeleteQuery.builder().table(PersonCarRelationTable.TABLE).where(COLUMN_CAR_ID + " = ?").whereArgs(carId).build()).prepare().executeAsBlocking();
        final List<Person> persons = object.persons();
        if (persons != null) {
            storIOSQLite.put().objects(persons).withPutResolver(personStorIOSQLitePutResolver).prepare().executeAsBlocking();
            final List<ContentValues> contentValuesList = new ArrayList<ContentValues>(persons.size());
            for (Person person : persons) {
                final ContentValues cv = new ContentValues(2);
                cv.put(COLUMN_CAR_ID, carId);
                cv.put(COLUMN_PERSON_ID, person.id());
                contentValuesList.add(cv);
            }
            storIOSQLite.put().contentValues(contentValuesList).withPutResolver(carPersonRelationPutResolver).prepare().executeAsBlocking();
        }
        lowLevel.setTransactionSuccessful();
        return putResult;
    } finally {
        lowLevel.endTransaction();
    }
}
Also used : ContentValues(android.content.ContentValues) ArrayList(java.util.ArrayList) PutResult(com.pushtorefresh.storio3.sqlite.operations.put.PutResult) Person(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Person) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) NonNull(android.support.annotation.NonNull)

Example 2 with PutResult

use of com.pushtorefresh.storio3.contentresolver.operations.put.PutResult in project storio by pushtorefresh.

the class PersonRelationsPutResolver method performPut.

@Override
@NonNull
public PutResult performPut(@NonNull StorIOSQLite storIOSQLite, @NonNull Person object) {
    final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
    lowLevel.beginTransaction();
    try {
        final PutResult putResult = super.performPut(storIOSQLite, object);
        // noinspection ConstantConditions
        final long personId = putResult.wasInserted() ? putResult.insertedId() : object.id();
        storIOSQLite.delete().byQuery(DeleteQuery.builder().table(PersonCarRelationTable.TABLE).where(COLUMN_PERSON_ID + " = ?").whereArgs(personId).build()).prepare().executeAsBlocking();
        final List<Car> cars = object.cars();
        if (cars != null) {
            final PutResults<Car> carsPutResults = storIOSQLite.put().objects(cars).withPutResolver(carStorIOSQLitePutResolver).prepare().executeAsBlocking();
            final List<ContentValues> contentValuesList = new ArrayList<ContentValues>(cars.size());
            for (Car car : cars) {
                final PutResult carPutResult = carsPutResults.results().get(car);
                // noinspection ConstantConditions
                final long carId = carPutResult.wasInserted() ? carPutResult.insertedId() : car.id();
                final ContentValues cv = new ContentValues(2);
                cv.put(COLUMN_PERSON_ID, personId);
                cv.put(PersonCarRelationTable.COLUMN_CAR_ID, carId);
                contentValuesList.add(cv);
            }
            storIOSQLite.put().contentValues(contentValuesList).withPutResolver(carPersonRelationPutResolver).prepare().executeAsBlocking();
        }
        lowLevel.setTransactionSuccessful();
        return putResult;
    } finally {
        lowLevel.endTransaction();
    }
}
Also used : ContentValues(android.content.ContentValues) Car(com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Car) ArrayList(java.util.ArrayList) PutResult(com.pushtorefresh.storio3.sqlite.operations.put.PutResult) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) NonNull(android.support.annotation.NonNull)

Example 3 with PutResult

use of com.pushtorefresh.storio3.contentresolver.operations.put.PutResult in project storio by pushtorefresh.

the class PutOperationTest method updateNullFieldToNotNull.

@Test
public void updateNullFieldToNotNull() {
    TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(2).subscribe(changesTestSubscriber);
    Uri insertedUri = // firstly, optional value is null
    contentResolver.insert(TestItem.CONTENT_URI, TestItem.create(null, "value", null).toContentValues());
    // change to not null
    TestItem testItem = TestItem.create(ContentUris.parseId(insertedUri), "value", "optionalValue");
    PutResult updateResult = storIOContentResolver.put().object(testItem).prepare().executeAsBlocking();
    assertThat(updateResult.wasUpdated()).isTrue();
    Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(cursor).hasCount(1);
    cursor.moveToFirst();
    assertThat(testItem).isEqualTo(TestItem.fromCursor(cursor));
    changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
    changesTestSubscriber.assertNoErrors();
    changesTestSubscriber.assertValues(Changes.newInstance(TestItem.CONTENT_URI), Changes.newInstance(TestItem.CONTENT_URI));
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Cursor(android.database.Cursor) Uri(android.net.Uri) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Example 4 with PutResult

use of com.pushtorefresh.storio3.contentresolver.operations.put.PutResult in project storio by pushtorefresh.

the class PutOperationTest method insertContentValuesAsRxFlowable.

@Test
public void insertContentValuesAsRxFlowable() {
    TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(1).subscribe(changesTestSubscriber);
    TestItem testItem = TestItem.create(null, "value");
    ContentValues cv = testItem.toContentValues();
    PutResult insertResult = storIOContentResolver.put().contentValues(cv).withPutResolver(testItemContentValuesPutResolver).prepare().asRxFlowable(BackpressureStrategy.MISSING).blockingFirst();
    assertThat(insertResult.wasInserted()).isTrue();
    Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(cursor).hasCount(1);
    cursor.moveToFirst();
    assertThat(testItem.equalsWithoutId(TestItem.fromCursor(cursor))).isTrue();
    changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
    changesTestSubscriber.assertNoErrors();
    changesTestSubscriber.assertValue(Changes.newInstance(TestItem.CONTENT_URI));
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) ContentValues(android.content.ContentValues) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Cursor(android.database.Cursor) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Example 5 with PutResult

use of com.pushtorefresh.storio3.contentresolver.operations.put.PutResult in project storio by pushtorefresh.

the class PutOperationTest method insertObjectAsRxFlowable.

@Test
public void insertObjectAsRxFlowable() {
    TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(1).subscribe(changesTestSubscriber);
    TestItem testItem = TestItem.create(null, "value");
    PutResult insertResult = storIOContentResolver.put().object(testItem).prepare().asRxFlowable(BackpressureStrategy.MISSING).blockingFirst();
    assertThat(insertResult.wasInserted()).isTrue();
    Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(cursor).hasCount(1);
    cursor.moveToFirst();
    assertThat(testItem.equalsWithoutId(TestItem.fromCursor(cursor))).isTrue();
    cursor.close();
    changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
    changesTestSubscriber.assertNoErrors();
    changesTestSubscriber.assertValue(Changes.newInstance(TestItem.CONTENT_URI));
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Cursor(android.database.Cursor) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)27 Cursor (android.database.Cursor)21 PutResult (com.pushtorefresh.storio3.contentresolver.operations.put.PutResult)19 Changes (com.pushtorefresh.storio3.contentresolver.Changes)15 TestSubscriber (io.reactivex.subscribers.TestSubscriber)15 ContentValues (android.content.ContentValues)14 Uri (android.net.Uri)10 NonNull (android.support.annotation.NonNull)10 PutResult (com.pushtorefresh.storio3.sqlite.operations.put.PutResult)9 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)6 InsertQuery (com.pushtorefresh.storio3.sqlite.queries.InsertQuery)4 StorIOContentResolver (com.pushtorefresh.storio3.contentresolver.StorIOContentResolver)3 InsertQuery (com.pushtorefresh.storio3.contentresolver.queries.InsertQuery)3 Query (com.pushtorefresh.storio3.contentresolver.queries.Query)3 UpdateQuery (com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery)3 UpdateQuery (com.pushtorefresh.storio3.sqlite.queries.UpdateQuery)3 Query (com.pushtorefresh.storio3.sqlite.queries.Query)2 ArrayList (java.util.ArrayList)2 Car (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Car)1 Person (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Person)1