use of com.pushtorefresh.storio3.sqlite.queries.Query in project storio by pushtorefresh.
the class CarRelationsGetResolver method mapFromCursor.
@Override
@NonNull
public Car mapFromCursor(@NonNull StorIOSQLite storIOSQLite, @NonNull Cursor cursor) {
final Car car = super.mapFromCursor(storIOSQLite, cursor);
final List<Person> persons = storIOSQLite.get().listOfObjects(Person.class).withQuery(RawQuery.builder().query("SELECT " + PersonTable.NAME + ".*" + " FROM " + PersonTable.NAME + " JOIN " + PersonCarRelationTable.TABLE + " ON " + PersonTable.ID_COLUMN + " = " + PersonCarRelationTable.COLUMN_PERSON_ID + " AND " + PersonCarRelationTable.COLUMN_CAR_ID + " = ?").args(car.id()).build()).withGetResolver(// without relations to prevent cycling
personStorIOSQLiteGetResolver).prepare().executeAsBlocking();
return new Car(car.id(), car.mark(), persons);
}
use of com.pushtorefresh.storio3.sqlite.queries.Query in project storio by pushtorefresh.
the class PersonRelationsGetResolver method mapFromCursor.
@Override
@NonNull
public Person mapFromCursor(@NonNull StorIOSQLite storIOSQLite, @NonNull Cursor cursor) {
final Person person = super.mapFromCursor(storIOSQLite, cursor);
final List<Car> cars = storIOSQLite.get().listOfObjects(Car.class).withQuery(RawQuery.builder().query("SELECT " + CarTable.NAME + ".*" + " FROM " + CarTable.NAME + " JOIN " + PersonCarRelationTable.TABLE + " ON " + CarTable.ID_COLUMN + " = " + PersonCarRelationTable.COLUMN_CAR_ID + " AND " + PersonCarRelationTable.COLUMN_PERSON_ID + " = ?").args(person.id()).build()).withGetResolver(carStorIOSQLiteGetResolver).prepare().executeAsBlocking();
return new Person(person.id(), person.name(), cars);
}
use of com.pushtorefresh.storio3.sqlite.queries.Query in project storio by pushtorefresh.
the class TweetsSQLiteFragment method reloadData.
void reloadData() {
uiStateController.setUiStateLoading();
final Disposable disposable = storIOSQLite.get().listOfObjects(Tweet.class).withQuery(QUERY_ALL).prepare().asRxFlowable(// it will be subscribed to changes in tweets table!
LATEST).delay(1, // for better User Experience :) Actually, StorIO is so fast that we need to delay emissions (it's a joke, or not)
SECONDS).observeOn(mainThread()).subscribe(new Consumer<List<Tweet>>() {
@Override
public void accept(List<Tweet> tweets) {
// So you just need to check if it's empty or not
if (tweets.isEmpty()) {
uiStateController.setUiStateEmpty();
tweetsAdapter.setTweets(Collections.<Tweet>emptyList());
} else {
uiStateController.setUiStateContent();
tweetsAdapter.setTweets(tweets);
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) {
// In cases when you are not sure that query will be successful
// You can prevent crash of the application via error handler
Timber.e(throwable, "reloadData()");
uiStateController.setUiStateError();
tweetsAdapter.setTweets(Collections.<Tweet>emptyList());
}
});
// Preventing memory leak (other rx operations: Put, Delete emit result once so memory leak won't live long)
// Because io.reactivex.Flowable from Get Operation is endless (it watches for changes of tables from query)
// You can easily create memory leak (in this case you'll leak the Fragment and all it's fields)
// So please, PLEASE manage your subscriptions
// We suggest same mechanism via storing all disposables that you want to dispose
// In something like CompositeSubscription and dispose them in appropriate moment of component lifecycle
disposeOnStop(disposable);
}
use of com.pushtorefresh.storio3.sqlite.queries.Query in project storio by pushtorefresh.
the class TweetsContentResolverFragment method reloadData.
void reloadData() {
uiStateController.setUiStateLoading();
final Disposable disposable = storIOContentResolver.get().listOfObjects(Tweet.class).withQuery(QUERY_ALL).prepare().asRxFlowable(// it will be subscribed to changes in tweets table!
LATEST).observeOn(mainThread()).subscribe(new Consumer<List<Tweet>>() {
@Override
public void accept(List<Tweet> tweets) {
// So you just need to check if it's empty or not
if (tweets.isEmpty()) {
uiStateController.setUiStateEmpty();
tweetsAdapter.setTweets(Collections.<Tweet>emptyList());
} else {
uiStateController.setUiStateContent();
tweetsAdapter.setTweets(tweets);
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) {
// In cases when you are not sure that query will be successful
// You can prevent crash of the application via error handler
Timber.e(throwable, "reloadData()");
uiStateController.setUiStateError();
tweetsAdapter.setTweets(Collections.<Tweet>emptyList());
}
});
// Preventing memory leak (other Flowables: Put, Delete emit result once so memory leak won't live long)
// Because io.reactivex.Flowable from Get Operation is endless (it watches for changes of tables from query)
// You can easily create memory leak (in this case you'll leak the Fragment and all it's fields)
// So please, PLEASE manage your subscriptions
// We suggest same mechanism via storing all disposables that you want to dispose
// In something like CompositeSubscription and dispose them in appropriate moment of component lifecycle
disposeOnStop(disposable);
}
use of com.pushtorefresh.storio3.sqlite.queries.Query in project storio by pushtorefresh.
the class DefaultPutResolverTest method insert.
/**
* Verifies behavior of {@link DefaultPutResolver} for "insert"
*/
@Test
public void insert() throws Exception {
final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
// item without id, should be inserted
final TestItem testItem = new TestItem(null);
when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
final Uri expectedInsertedUri = mock(Uri.class);
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);
when(lowLevel.insert(any(InsertQuery.class), any(ContentValues.class))).thenReturn(expectedInsertedUri);
final InsertQuery expectedInsertQuery = InsertQuery.builder().uri(TestItem.CONTENT_URI).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().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);
}
}
};
final ContentValues expectedContentValues = TestItem.MAP_TO_CONTENT_VALUES.apply(testItem);
// Performing Put that should "insert"
final PutResult putResult = putResolver.performPut(storIOContentResolver, testItem);
// 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 insert was performed
verify(lowLevel).insert(eq(expectedInsertQuery), eq(expectedContentValues));
// only one insert should occur
verify(lowLevel).insert(any(InsertQuery.class), any(ContentValues.class));
// no updates should occur
verify(lowLevel, never()).update(any(UpdateQuery.class), any(ContentValues.class));
// put result checks
assertThat(putResult.wasInserted()).isTrue();
assertThat(putResult.wasUpdated()).isFalse();
assertThat(putResult.insertedUri()).isEqualTo(expectedInsertedUri);
assertThat(putResult.numberOfRowsUpdated()).isNull();
}
Aggregations