use of com.squareup.sqlbrite.SqlBrite.Query in project sqlbrite by square.
the class SqlBriteTest method asRowsStopsWhenUnsubscribed.
@Test
public void asRowsStopsWhenUnsubscribed() {
MatrixCursor cursor = new MatrixCursor(COLUMN_NAMES);
cursor.addRow(new Object[] { "Alice", "Allison" });
cursor.addRow(new Object[] { "Bob", "Bobberson" });
Query query = new CursorQuery(cursor);
final AtomicInteger count = new AtomicInteger();
query.asRows(new Func1<Cursor, Name>() {
@Override
public Name call(Cursor cursor) {
count.incrementAndGet();
return Name.MAP.call(cursor);
}
}).take(1).toBlocking().first();
assertThat(count.get()).isEqualTo(1);
}
use of com.squareup.sqlbrite.SqlBrite.Query in project sqlbrite by square.
the class SqlBriteTest method asRowsEmpty.
@Test
public void asRowsEmpty() {
MatrixCursor cursor = new MatrixCursor(COLUMN_NAMES);
Query query = new CursorQuery(cursor);
List<Name> names = query.asRows(Name.MAP).toList().toBlocking().first();
assertThat(names).isEmpty();
}
use of com.squareup.sqlbrite.SqlBrite.Query in project sqlbrite by square.
the class SqlBriteTest method asRows.
@Test
public void asRows() {
MatrixCursor cursor = new MatrixCursor(COLUMN_NAMES);
cursor.addRow(new Object[] { "Alice", "Allison" });
cursor.addRow(new Object[] { "Bob", "Bobberson" });
Query query = new CursorQuery(cursor);
List<Name> names = query.asRows(Name.MAP).toList().toBlocking().first();
assertThat(names).containsExactly(new Name("Alice", "Allison"), new Name("Bob", "Bobberson"));
}
use of com.squareup.sqlbrite.SqlBrite.Query in project sqlbrite by square.
the class ItemsFragment method onResume.
@Override
public void onResume() {
super.onResume();
String listId = String.valueOf(getListId());
subscriptions = new CompositeSubscription();
Observable<Integer> itemCount = //
db.createQuery(TodoItem.TABLE, COUNT_QUERY, listId).map(new Func1<Query, Integer>() {
@Override
public Integer call(Query query) {
Cursor cursor = query.run();
try {
if (!cursor.moveToNext()) {
throw new AssertionError("No rows");
}
return cursor.getInt(0);
} finally {
cursor.close();
}
}
});
Observable<String> listName = db.createQuery(TodoList.TABLE, TITLE_QUERY, listId).map(new Func1<Query, String>() {
@Override
public String call(Query query) {
Cursor cursor = query.run();
try {
if (!cursor.moveToNext()) {
throw new AssertionError("No rows");
}
return cursor.getString(0);
} finally {
cursor.close();
}
}
});
subscriptions.add(Observable.combineLatest(listName, itemCount, new Func2<String, Integer, String>() {
@Override
public String call(String listName, Integer itemCount) {
return listName + " (" + itemCount + ")";
}
}).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {
@Override
public void call(String title) {
getActivity().setTitle(title);
}
}));
subscriptions.add(db.createQuery(TodoItem.TABLE, LIST_QUERY, listId).mapToList(TodoItem.MAPPER).observeOn(AndroidSchedulers.mainThread()).subscribe(adapter));
}
use of com.squareup.sqlbrite.SqlBrite.Query in project sqlbrite by square.
the class BriteDatabaseTest method transactionCreatedFromTransactionNotificationWorks.
@Test
public void transactionCreatedFromTransactionNotificationWorks() {
// Tests the case where a transaction is created in the subscriber to a query which gets
// notified as the result of another transaction being committed. With improper ordering, this
// can result in creating a new transaction before the old is committed on the underlying DB.
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(new Action1<Query>() {
@Override
public void call(Query query) {
db.newTransaction().end();
}
});
Transaction transaction = db.newTransaction();
try {
db.insert(TABLE_EMPLOYEE, employee("john", "John Johnson"));
transaction.markSuccessful();
} finally {
transaction.end();
}
}
Aggregations