From c5f3c7faf2b7a0a40f3bd89c686f2169bf75bd09 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 22 Sep 2023 15:12:39 -0700 Subject: [PATCH] Update api/sql.{h|c++} to use KJ_IF_SOME --- src/workerd/api/sql.c++ | 50 ++++++++++++++++++++--------------------- src/workerd/api/sql.h | 10 ++++----- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/workerd/api/sql.c++ b/src/workerd/api/sql.c++ index 70d4677f633..e72aac1e292 100644 --- a/src/workerd/api/sql.c++ +++ b/src/workerd/api/sql.c++ @@ -69,10 +69,10 @@ SqlStorage::Cursor::State::State( SqlStorage::Cursor::~Cursor() noexcept(false) { // If this Cursor was created from a Statement, clear the Statement's currentCursor weak ref. - KJ_IF_MAYBE(s, selfRef) { - KJ_IF_MAYBE(p, *s) { - if (p == this) { - *s = nullptr; + KJ_IF_SOME(s, selfRef) { + KJ_IF_SOME(p, s) { + if (&p == this) { + s = nullptr; } } } @@ -80,7 +80,7 @@ SqlStorage::Cursor::~Cursor() noexcept(false) { void SqlStorage::Cursor::CachedColumnNames::ensureInitialized( jsg::Lock& js, SqliteDatabase::Query& source) { - if (names == nullptr) { + if (names == kj::none) { js.withinHandleScope([&] { auto builder = kj::heapArrayBuilder>(source.columnCount()); for (auto i: kj::zeroTo(builder.capacity())) { @@ -92,24 +92,24 @@ void SqlStorage::Cursor::CachedColumnNames::ensureInitialized( } double SqlStorage::Cursor::getRowsRead() { - KJ_IF_MAYBE(st, state) { - return static_cast((**st).query.getRowsRead()); + KJ_IF_SOME(st, state) { + return static_cast(st->query.getRowsRead()); } else { return static_cast(rowsRead); } } double SqlStorage::Cursor::getRowsWritten() { - KJ_IF_MAYBE(st, state) { - return static_cast((**st).query.getRowsWritten()); + KJ_IF_SOME(st, state) { + return static_cast(st->query.getRowsWritten()); } else { return static_cast(rowsWritten); } } jsg::Ref SqlStorage::Cursor::rows(jsg::Lock& js) { - KJ_IF_MAYBE(s, state) { - cachedColumnNames.ensureInitialized(js, (*s)->query); + KJ_IF_SOME(s, state) { + cachedColumnNames.ensureInitialized(js, s->query); } return jsg::alloc(JSG_THIS); } @@ -139,8 +139,8 @@ jsg::Ref SqlStorage::Cursor::raw(jsg::Lock&) { // iterator has already been fully consumed. The resulting columns may contain duplicate entries, // for instance a `SELECT *` across a join of two tables that share a column name. kj::Array> SqlStorage::Cursor::getColumnNames(jsg::Lock& js) { - KJ_IF_MAYBE(s, state) { - cachedColumnNames.ensureInitialized(js, (*s)->query); + KJ_IF_SOME(s, state) { + cachedColumnNames.ensureInitialized(js, s->query); return KJ_MAP(name, this->cachedColumnNames.get()) { return name.addRef(js); }; @@ -171,7 +171,7 @@ auto SqlStorage::Cursor::iteratorImpl(jsg::Lock& js, jsg::Ref& obj, Func "prepared statement objects."); } else { // Query already done. - return nullptr; + return kj::none; } }); @@ -190,8 +190,8 @@ auto SqlStorage::Cursor::iteratorImpl(jsg::Lock& js, jsg::Ref& obj, Func obj->rowsRead = query.getRowsRead(); obj->rowsWritten = query.getRowsWritten(); // Clean up the query proactively. - obj->state = nullptr; - return nullptr; + obj->state = kj::none; + return kj::none; } auto results = kj::heapArrayBuilder(query.columnCount()); @@ -229,8 +229,8 @@ SqlStorage::Statement::Statement(SqliteDatabase::Statement&& statement) kj::Array SqlStorage::Cursor::mapBindings( kj::ArrayPtr values) { return KJ_MAP(value, values) -> SqliteDatabase::Query::ValuePtr { - KJ_IF_MAYBE(v, value) { - KJ_SWITCH_ONEOF(*v) { + KJ_IF_SOME(v, value) { + KJ_SWITCH_ONEOF(v) { KJ_CASE_ONEOF(data, kj::Array) { return data.asPtr(); } @@ -251,20 +251,20 @@ kj::Array SqlStorage::Cursor::mapBindings jsg::Ref SqlStorage::Statement::run(jsg::Arguments bindings) { auto& statementRef = *statement; // validate we're in the right IoContext - KJ_IF_MAYBE(c, currentCursor) { + KJ_IF_SOME(c, currentCursor) { // Invalidate previous cursor if it's still running. We have to do this because SQLite only // allows one execution of a statement at a time. // // If this is a problem, we could consider a scheme where we dynamically instantiate copies of // the statement as needed. However, that risks wasting memory if the app commonly leaves // cursors open and the GC doesn't run proactively enough. - KJ_IF_MAYBE(s, c->state) { - c->canceled = !(*s)->query.isDone(); - c->state = nullptr; + KJ_IF_SOME(s, c.state) { + c.canceled = !s->query.isDone(); + c.state = kj::none; } - c->selfRef = nullptr; - c->statement = nullptr; - currentCursor = nullptr; + c.selfRef = kj::none; + c.statement = kj::none; + currentCursor = kj::none; } auto result = jsg::alloc(cachedColumnNames, statementRef, kj::mv(bindings)); diff --git a/src/workerd/api/sql.h b/src/workerd/api/sql.h index ee9b444b907..da1bf38900a 100644 --- a/src/workerd/api/sql.h +++ b/src/workerd/api/sql.h @@ -64,8 +64,8 @@ class SqlStorage final: public jsg::Object, private SqliteDatabase::Regulator { // for future calls. SqliteDatabase::Statement* stmt; - KJ_IF_MAYBE(s, slot) { - stmt = &**s; + KJ_IF_SOME(s, slot) { + stmt = &*s; } else { stmt = &*slot.emplace(IoContext::current().addObject( kj::heap(sqlite->prepare(sqlCode)))); @@ -74,8 +74,8 @@ class SqlStorage final: public jsg::Object, private SqliteDatabase::Regulator { } uint64_t getPageSize() { - KJ_IF_MAYBE(p, pageSize) { - return *p; + KJ_IF_SOME(p, pageSize) { + return p; } else { return pageSize.emplace(sqlite->run("PRAGMA page_size;").getInt64(0)); } @@ -88,7 +88,7 @@ class SqlStorage::Cursor final: public jsg::Object { template Cursor(Params&&... params) : state(IoContext::current().addObject(kj::heap(kj::fwd(params)...))), - ownCachedColumnNames(nullptr), // silence bogus Clang warning on next line + ownCachedColumnNames(kj::none), // silence bogus Clang warning on next line cachedColumnNames(ownCachedColumnNames.emplace()) {} template