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

Commit 02b66a1

Browse files
committed
Merge pull request #68 from square/jw/raw
Add APIs for executing raw SQL in both trigger and no-trigger forms.
2 parents fed28af + 791b8d1 commit 02b66a1

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

sqlbrite/src/androidTest/java/com/squareup/sqlbrite/BriteDatabaseTest.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import android.content.ContentValues;
1919
import android.database.Cursor;
20+
import android.database.SQLException;
2021
import android.database.sqlite.SQLiteDatabase;
2122
import android.database.sqlite.SQLiteException;
2223
import android.support.test.InstrumentationRegistry;
@@ -476,6 +477,86 @@ public final class BriteDatabaseTest {
476477
.isExhausted();
477478
}
478479

480+
@Test public void executeSqlNoTrigger() {
481+
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES)
482+
.skip(1) // Skip initial
483+
.subscribe(o);
484+
485+
db.execute("UPDATE " + TABLE_EMPLOYEE + " SET " + TestDb.EmployeeTable.NAME + " = 'Zach'");
486+
o.assertNoMoreEvents();
487+
}
488+
489+
@Test public void executeSqlWithArgsNoTrigger() {
490+
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES)
491+
.skip(1) // Skip initial
492+
.subscribe(o);
493+
494+
db.execute("UPDATE " + TABLE_EMPLOYEE + " SET " + TestDb.EmployeeTable.NAME + " = ?", "Zach");
495+
o.assertNoMoreEvents();
496+
}
497+
498+
@Test public void executeSqlAndTrigger() {
499+
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(o);
500+
o.assertCursor()
501+
.hasRow("alice", "Alice Allison")
502+
.hasRow("bob", "Bob Bobberson")
503+
.hasRow("eve", "Eve Evenson")
504+
.isExhausted();
505+
506+
db.executeAndTrigger(TABLE_EMPLOYEE,
507+
"UPDATE " + TABLE_EMPLOYEE + " SET " + TestDb.EmployeeTable.NAME + " = 'Zach'");
508+
o.assertCursor()
509+
.hasRow("alice", "Zach")
510+
.hasRow("bob", "Zach")
511+
.hasRow("eve", "Zach")
512+
.isExhausted();
513+
}
514+
515+
@Test public void executeSqlThrowsAndDoesNotTrigger() {
516+
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES)
517+
.skip(1) // Skip initial
518+
.subscribe(o);
519+
520+
try {
521+
db.executeAndTrigger(TABLE_EMPLOYEE,
522+
"UPDATE not_a_table SET " + TestDb.EmployeeTable.NAME + " = 'Zach'");
523+
fail();
524+
} catch (SQLException ignored) {
525+
}
526+
o.assertNoMoreEvents();
527+
}
528+
529+
@Test public void executeSqlWithArgsAndTrigger() {
530+
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(o);
531+
o.assertCursor()
532+
.hasRow("alice", "Alice Allison")
533+
.hasRow("bob", "Bob Bobberson")
534+
.hasRow("eve", "Eve Evenson")
535+
.isExhausted();
536+
537+
db.executeAndTrigger(TABLE_EMPLOYEE,
538+
"UPDATE " + TABLE_EMPLOYEE + " SET " + TestDb.EmployeeTable.NAME + " = ?", "Zach");
539+
o.assertCursor()
540+
.hasRow("alice", "Zach")
541+
.hasRow("bob", "Zach")
542+
.hasRow("eve", "Zach")
543+
.isExhausted();
544+
}
545+
546+
@Test public void executeSqlWithArgsThrowsAndDoesNotTrigger() {
547+
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES)
548+
.skip(1) // Skip initial
549+
.subscribe(o);
550+
551+
try {
552+
db.executeAndTrigger(TABLE_EMPLOYEE,
553+
"UPDATE not_a_table SET " + TestDb.EmployeeTable.NAME + " = ?", "Zach");
554+
fail();
555+
} catch (SQLException ignored) {
556+
}
557+
o.assertNoMoreEvents();
558+
}
559+
479560
@Test public void transactionOnlyNotifiesOnce() {
480561
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(o);
481562
o.assertCursor()

sqlbrite/src/main/java/com/squareup/sqlbrite/BriteDatabase.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,66 @@ public int update(@NonNull String table, @NonNull ContentValues values,
425425
return rows;
426426
}
427427

428+
/**
429+
* Execute {@code sql} provided it is NOT a {@code SELECT} or any other SQL statement that
430+
* returns data. No data can be returned (such as the number of affected rows). Instead, use
431+
* {@link #insert}, {@link #update}, et al, when possible.
432+
* <p>
433+
* No notifications will be sent to queries if {@code sql} affects the data of a table.
434+
*
435+
* @see SQLiteDatabase#execSQL(String)
436+
*/
437+
public void execute(String sql) {
438+
SQLiteDatabase db = getWriteableDatabase();
439+
db.execSQL(sql);
440+
}
441+
442+
/**
443+
* Execute {@code sql} provided it is NOT a {@code SELECT} or any other SQL statement that
444+
* returns data. No data can be returned (such as the number of affected rows). Instead, use
445+
* {@link #insert}, {@link #update}, et al, when possible.
446+
* <p>
447+
* No notifications will be sent to queries if {@code sql} affects the data of a table.
448+
*
449+
* @see SQLiteDatabase#execSQL(String, Object[])
450+
*/
451+
public void execute(String sql, Object... args) {
452+
SQLiteDatabase db = getWriteableDatabase();
453+
db.execSQL(sql, args);
454+
}
455+
456+
/**
457+
* Execute {@code sql} provided it is NOT a {@code SELECT} or any other SQL statement that
458+
* returns data. No data can be returned (such as the number of affected rows). Instead, use
459+
* {@link #insert}, {@link #update}, et al, when possible.
460+
* <p>
461+
* A notification to queries for {@code table} will be sent after the statement is executed.
462+
*
463+
* @see SQLiteDatabase#execSQL(String)
464+
*/
465+
public void executeAndTrigger(String table, String sql) {
466+
SQLiteDatabase db = getWriteableDatabase();
467+
db.execSQL(sql);
468+
469+
sendTableTrigger(Collections.singleton(table));
470+
}
471+
472+
/**
473+
* Execute {@code sql} provided it is NOT a {@code SELECT} or any other SQL statement that
474+
* returns data. No data can be returned (such as the number of affected rows). Instead, use
475+
* {@link #insert}, {@link #update}, et al, when possible.
476+
* <p>
477+
* A notification to queries for {@code table} will be sent after the statement is executed.
478+
*
479+
* @see SQLiteDatabase#execSQL(String, Object[])
480+
*/
481+
public void executeAndTrigger(String table, String sql, Object... args) {
482+
SQLiteDatabase db = getWriteableDatabase();
483+
db.execSQL(sql, args);
484+
485+
sendTableTrigger(Collections.singleton(table));
486+
}
487+
428488
/** An in-progress database transaction. */
429489
public interface Transaction extends Closeable {
430490
/**

0 commit comments

Comments
 (0)