Skip to content
This repository has been archived by the owner on Aug 17, 2020. It is now read-only.

Commit

Permalink
Add query(SupportSQLiteQuery) method
Browse files Browse the repository at this point in the history
  • Loading branch information
kukuhyoniatmoko committed Feb 20, 2018
1 parent 7a45c85 commit 1e2f1c0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,46 @@ public final class BriteDatabaseTest {
}
}

@Test public void synchronousQueryWithSupportSQLiteQueryDuringTransaction() {
Transaction transaction = db.newTransaction();
try {
transaction.markSuccessful();
assertCursor(db.query(new SimpleSQLiteQuery(SELECT_EMPLOYEES)))
.hasRow("alice", "Alice Allison")
.hasRow("bob", "Bob Bobberson")
.hasRow("eve", "Eve Evenson")
.isExhausted();
} finally {
transaction.end();
}
}

@Test public void synchronousQueryWithSupportSQLiteQueryDuringTransactionSeesChanges() {
Transaction transaction = db.newTransaction();
try {
assertCursor(db.query(new SimpleSQLiteQuery(SELECT_EMPLOYEES)))
.hasRow("alice", "Alice Allison")
.hasRow("bob", "Bob Bobberson")
.hasRow("eve", "Eve Evenson")
.isExhausted();

db.insert(TABLE_EMPLOYEE, CONFLICT_NONE, employee("john", "John Johnson"));
db.insert(TABLE_EMPLOYEE, CONFLICT_NONE, employee("nick", "Nick Nickers"));

assertCursor(db.query(new SimpleSQLiteQuery(SELECT_EMPLOYEES)))
.hasRow("alice", "Alice Allison")
.hasRow("bob", "Bob Bobberson")
.hasRow("eve", "Eve Evenson")
.hasRow("john", "John Johnson")
.hasRow("nick", "Nick Nickers")
.isExhausted();

transaction.markSuccessful();
} finally {
transaction.end();
}
}

@Test public void nestedTransactionsOnlyNotifyOnce() {
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(o);
o.assertCursor()
Expand Down
15 changes: 15 additions & 0 deletions sqlbrite/src/main/java/com/squareup/sqlbrite3/BriteDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,21 @@ public Cursor query(@NonNull String sql, @NonNull Object... args) {
return cursor;
}

/**
* Runs the provided {@link SupportSQLiteQuery} and returns a {@link Cursor} over the result set.
*
* @see SupportSQLiteDatabase#query(SupportSQLiteQuery)
*/
@CheckResult @WorkerThread
public Cursor query(@NonNull SupportSQLiteQuery query) {
Cursor cursor = getReadableDatabase().query(query);
if (logging) {
log("QUERY\n sql: %s", indentSql(query.getSql()));
}

return cursor;
}

/**
* Insert a row into the specified {@code table} and notify any subscribed queries.
*
Expand Down

0 comments on commit 1e2f1c0

Please sign in to comment.