Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SqliteDatabase::executeWithRegulator #2054

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/workerd/util/sqlite.c++
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,17 @@ kj::StringPtr SqliteDatabase::ingestSql(Regulator& regulator, kj::StringPtr sqlC
return sqlCode;
}

void SqliteDatabase::executeWithRegulator(Regulator& regulator, kj::FunctionParam<void()> func) {
// currentRegulator would only be set if we're running this method while running something else
// with a regulator. I'm not sure what the ramifications are, so for now, we'll just assume that
// we can only call executeWithRegulator when no regulator is currently set.
KJ_REQUIRE(currentRegulator == kj::none);

currentRegulator = regulator;
KJ_DEFER(currentRegulator = kj::none);
func();
}

bool SqliteDatabase::isAuthorized(int actionCode,
kj::Maybe<kj::StringPtr> param1, kj::Maybe<kj::StringPtr> param2,
kj::Maybe<kj::StringPtr> dbName, kj::Maybe<kj::StringPtr> triggerName) {
Expand Down
3 changes: 3 additions & 0 deletions src/workerd/util/sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ class SqliteDatabase {
// that was not processed. This is used for streaming SQL ingestion.
kj::StringPtr ingestSql(Regulator& regulator, kj::StringPtr sqlCode);

// Execute a function with the given regulator.
void executeWithRegulator(Regulator& regulator, kj::FunctionParam<void()> func);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, but I've got one brain cell that won't quit screaming about something like

template <typename Ret, typename... Params>
Ret executeWithRegulator(Regulator& regulator, kj::FunctionParam<Ret(Params...)> func, Params... params) {
// ...
return func(kj::fwd<Params>(params)...);

Let's ignore that brain cell, though, since we can go back and generalize this later if we need to. YAGNI, blah blah blah.


private:
sqlite3* db;

Expand Down
Loading