use of com.pushtorefresh.storio3.sqlite.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();
}
}
use of com.pushtorefresh.storio3.sqlite.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();
}
}
use of com.pushtorefresh.storio3.sqlite.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));
}
use of com.pushtorefresh.storio3.sqlite.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));
}
use of com.pushtorefresh.storio3.sqlite.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));
}
Aggregations