From 1e2f1c0e6a00772390afae9abcaba44b9e91abd3 Mon Sep 17 00:00:00 2001 From: Kukuh Yoniatmoko Date: Tue, 20 Feb 2018 16:45:51 +0700 Subject: [PATCH] Add query(SupportSQLiteQuery) method --- .../squareup/sqlbrite3/BriteDatabaseTest.java | 40 +++++++++++++++++++ .../com/squareup/sqlbrite3/BriteDatabase.java | 15 +++++++ 2 files changed, 55 insertions(+) diff --git a/sqlbrite/src/androidTest/java/com/squareup/sqlbrite3/BriteDatabaseTest.java b/sqlbrite/src/androidTest/java/com/squareup/sqlbrite3/BriteDatabaseTest.java index 07d528da..8353c045 100644 --- a/sqlbrite/src/androidTest/java/com/squareup/sqlbrite3/BriteDatabaseTest.java +++ b/sqlbrite/src/androidTest/java/com/squareup/sqlbrite3/BriteDatabaseTest.java @@ -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() diff --git a/sqlbrite/src/main/java/com/squareup/sqlbrite3/BriteDatabase.java b/sqlbrite/src/main/java/com/squareup/sqlbrite3/BriteDatabase.java index 6c5432b6..21af6d1f 100644 --- a/sqlbrite/src/main/java/com/squareup/sqlbrite3/BriteDatabase.java +++ b/sqlbrite/src/main/java/com/squareup/sqlbrite3/BriteDatabase.java @@ -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. *