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

Make SqliteDatabase write mode a kj::Maybe #1809

Merged
merged 1 commit into from
Mar 12, 2024

Conversation

justin-mp
Copy link
Contributor

I have a use case for SqliteDatabase where I need to parameterize whether the database is read-only or read/write. Having two constructors for these cases and no default constructor means that I cannot parameterize the read/write-mode of the SqliteDatabase in the constructor of another class.

It’s much easier to use this if we can indicate we want read-only mode using a parameter. I considered using a WriteMode of 0 to mean “read-only”, but the kj::WriteMode documentation says:

(To open a file or directory read-only, do not specify a mode.)

The best way to “not specify a mode” as a parameter is to make it a kj::Maybe.

I made the write mode an optional parameter so that to make the new interface compatible the previous one.

@justin-mp justin-mp requested review from a team as code owners March 12, 2024 15:12
Copy link
Member

@kentonv kentonv left a comment

Choose a reason for hiding this comment

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

I guess that makes sense. FWIW, I had done it this way for consistency with the KJ filesystem APIs, which similarly have methods like openFile() which have read-only and writable overloads, based on whether you provide a WriteMode. But in the case of openFile, the two overloads actually return different types (ReadableFile vs File), so it really does have to be an overload in that case. Here, using Maybe is fine.

Whitespace nitpick: It looks like you have several instances of }\n else { that should be } else { instead.

Comment on lines 338 to 339
}
else {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
}
else {
} else {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in d415067.

Comment on lines 349 to 350
}
else {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
}
else {
} else {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in d415067.

} else {
SQLITE_CALL_NODB(sqlite3_open_v2(path.toString().cStr(), &db,
flags, vfs.getName().cStr()));
else {
Copy link
Member

Choose a reason for hiding this comment

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

Need to fix this too. (Github won't let me propose a fix here since the } on the previous line is now considered new.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in d415067.

@justin-mp
Copy link
Contributor Author

Whitespace nitpick: It looks like you have several instances of }\n else { that should be } else { instead.

That'll teach me to run clang-format on my changes in workerd.

I have a use case for SqliteDatabase where I need to parameterize
whether the database is read-only or read/write.  Having two
constructors for these cases and no default constructor means that I
cannot parameterize the read/write-mode of the SqliteDatabase in the
constructor of another class.

It’s much easier to use this if we can indicate we want read-only mode
using a parameter.  I considered using a WriteMode of 0 to mean
“read-only”, but the kj::WriteMode documentation says:

> (To open a file or directory read-only, do not specify a mode.)

The best way to “not specify a mode” as a parameter is to make it a
kj::Maybe.

I made the write mode an optional parameter so that to make the new
interface compatible the previous one.
@justin-mp justin-mp force-pushed the jmp/sqlite-optional-writemode branch from d415067 to f3f1ff5 Compare March 12, 2024 16:10
@justin-mp justin-mp merged commit ba8291e into main Mar 12, 2024
10 checks passed
@justin-mp justin-mp deleted the jmp/sqlite-optional-writemode branch March 12, 2024 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants