@@ -76,6 +76,7 @@ class SqliteDatabase {
7676
7777 struct QueryOptions {
7878 const Regulator& regulator;
79+ bool allowUnconfirmed = false ;
7980 };
8081
8182 struct IngestResult {
@@ -204,7 +205,7 @@ class SqliteDatabase {
204205 //
205206 // Note that the write callback is NOT called before (or at any point during) a reset(). Use the
206207 // `ResetListener` mechanism or `afterReset()` instead for that case.
207- void onWrite (kj::Function<void ()> callback) {
208+ void onWrite (kj::Function<void (bool allowUnconfirmed )> callback) {
208209 onWriteCallback = kj::mv (callback);
209210 }
210211
@@ -229,7 +230,7 @@ class SqliteDatabase {
229230 // implement explicit transactions. For synchronous transactions, the explicit transaction needs
230231 // to be nested inside the automatic transaction, so we need to force an auto-transaction to
231232 // start before the SAVEPOINT.
232- void notifyWrite ();
233+ void notifyWrite (bool allowUnconfirmed = false );
233234
234235 // Get the currently-executing SQL query for debug purposes. The query is normalized to hide
235236 // any literal values that might contain sensitive information. This is intended to be safe for
@@ -332,7 +333,7 @@ class SqliteDatabase {
332333 kj::Maybe<sqlite3_stmt&> currentStatement;
333334
334335 bool criticalErrorOccurred = false ;
335- kj::Maybe<kj::Function<void ()>> onWriteCallback;
336+ kj::Maybe<kj::Function<void (bool allowUnconfirmed )>> onWriteCallback;
336337 kj::Maybe<kj::Function<void (kj::StringPtr errorMessage, kj::Maybe<kj::Exception> maybeException)>>
337338 onCriticalErrorCallback;
338339 kj::Maybe<kj::Function<void (SqliteDatabase&)>> afterResetCallback;
@@ -446,6 +447,13 @@ class SqliteDatabase::Statement final: private ResetListener {
446447 template <typename ... Params>
447448 Query run (Params&&... bindings);
448449
450+ struct StatementOptions {
451+ bool allowUnconfirmed = false ;
452+ };
453+
454+ template <typename ... Params>
455+ Query run (StatementOptions options, Params&&... bindings);
456+
449457 private:
450458 const Regulator& regulator;
451459 kj::OneOf<kj::String, StatementAndEffect> stmt;
@@ -632,6 +640,9 @@ class SqliteDatabase::Query final: private ResetListener {
632640 uint64_t rowsRead = 0 ;
633641 uint64_t rowsWritten = 0 ;
634642
643+ // Whether this query allows unconfirmed writes.
644+ bool allowUnconfirmed = false ;
645+
635646 friend class SqliteDatabase ;
636647
637648 Query (SqliteDatabase& db,
@@ -647,7 +658,8 @@ class SqliteDatabase::Query final: private ResetListener {
647658 : ResetListener(db),
648659 regulator (options.regulator),
649660 maybeStatement(statement.prepareForExecution()),
650- queryEvent(this ->db.sqliteObserver) {
661+ queryEvent(this ->db.sqliteObserver),
662+ allowUnconfirmed(options.allowUnconfirmed) {
651663 // If we throw from the constructor, the destructor won't run. Need to call destroy()
652664 // explicitly.
653665 KJ_ON_SCOPE_FAILURE (destroy ());
@@ -659,7 +671,8 @@ class SqliteDatabase::Query final: private ResetListener {
659671 regulator(options.regulator),
660672 ownStatement(db.prepareSql(regulator, sqlCode, 0 , MULTI)),
661673 maybeStatement(ownStatement),
662- queryEvent(this ->db.sqliteObserver) {
674+ queryEvent(this ->db.sqliteObserver),
675+ allowUnconfirmed(options.allowUnconfirmed) {
663676 // If we throw from the constructor, the destructor won't run. Need to call destroy()
664677 // explicitly.
665678 KJ_ON_SCOPE_FAILURE (destroy ());
@@ -966,6 +979,12 @@ SqliteDatabase::Query SqliteDatabase::Statement::run(Params&&... params) {
966979 return Query (db, QueryOptions{.regulator = regulator}, *this , kj::fwd<Params>(params)...);
967980}
968981
982+ template <typename ... Params>
983+ SqliteDatabase::Query SqliteDatabase::Statement::run (StatementOptions options, Params&&... params) {
984+ return Query (db, {.regulator = regulator, .allowUnconfirmed = options.allowUnconfirmed }, *this ,
985+ kj::fwd<Params>(params)...);
986+ }
987+
969988template <size_t size, typename ... Params>
970989SqliteDatabase::Query SqliteDatabase::run (const char (&sqlCode)[size], Params&&... params) {
971990 return Query (*this , QueryOptions{.regulator = TRUSTED}, sqlCode, kj::fwd<Params>(params)...);
0 commit comments