Search in sources :

Example 6 with Query

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);
}
Also used : Query(com.squareup.sqlbrite.SqlBrite.Query) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Func1(rx.functions.Func1) MatrixCursor(android.database.MatrixCursor) Cursor(android.database.Cursor) MatrixCursor(android.database.MatrixCursor) Test(org.junit.Test)

Example 7 with Query

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();
}
Also used : Query(com.squareup.sqlbrite.SqlBrite.Query) MatrixCursor(android.database.MatrixCursor) Test(org.junit.Test)

Example 8 with Query

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"));
}
Also used : Query(com.squareup.sqlbrite.SqlBrite.Query) MatrixCursor(android.database.MatrixCursor) Test(org.junit.Test)

Example 9 with Query

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));
}
Also used : Action1(rx.functions.Action1) Query(com.squareup.sqlbrite.SqlBrite.Query) Cursor(android.database.Cursor) CompositeSubscription(rx.subscriptions.CompositeSubscription) Func2(rx.functions.Func2)

Example 10 with Query

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();
    }
}
Also used : Query(com.squareup.sqlbrite.SqlBrite.Query) Transaction(com.squareup.sqlbrite.BriteDatabase.Transaction) Test(org.junit.Test)

Aggregations

Query (com.squareup.sqlbrite.SqlBrite.Query)12 Test (org.junit.Test)10 Cursor (android.database.Cursor)5 MatrixCursor (android.database.MatrixCursor)5 TestSubscriber (rx.observers.TestSubscriber)5 Func1 (rx.functions.Func1)3 Transaction (com.squareup.sqlbrite.BriteDatabase.Transaction)2 Employee (com.squareup.sqlbrite.TestDb.Employee)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ContentObserver (android.database.ContentObserver)1 CheckResult (android.support.annotation.CheckResult)1 NonNull (android.support.annotation.NonNull)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 OnSubscribe (rx.Observable.OnSubscribe)1 Subscriber (rx.Subscriber)1 Action0 (rx.functions.Action0)1 Action1 (rx.functions.Action1)1 Func2 (rx.functions.Func2)1 CompositeSubscription (rx.subscriptions.CompositeSubscription)1