sqlite: open read-only when appropriate

This commit is contained in:
Alessandro Di Federico
2026-06-16 10:25:47 +02:00
parent b5e3ccba9e
commit ffaf735219
3 changed files with 18 additions and 5 deletions
+15 -3
View File
@@ -151,20 +151,32 @@ private:
CUniquePtr<sqlite3_close_v2, SQLITE_OK> Connection;
public:
Database(llvm::StringRef Path) {
enum class OpenMode {
ReadWrite,
ReadOnly
};
Database(llvm::StringRef Path, OpenMode Mode) {
sqlite3 *Ptr = nullptr;
int Result;
if (Path == ":memory:")
if (Path == ":memory:") {
Result = sqlite3_open_v2("",
&Ptr,
SQLITE_OPEN_MEMORY | SQLITE_OPEN_PRIVATECACHE,
NULL);
else
} else if (Mode == OpenMode::ReadOnly) {
std::string URI = ("file:" + Path.str() + "?immutable=1");
Result = sqlite3_open_v2(URI.c_str(),
&Ptr,
SQLITE_OPEN_READONLY | SQLITE_OPEN_URI,
NULL);
} else {
Result = sqlite3_open_v2(Path.str().c_str(),
&Ptr,
SQLITE_OPEN_READWRITE,
NULL);
}
revng_assert(Result == SQLITE_OK);
Connection.reset(Ptr);
@@ -84,7 +84,8 @@ private:
sqlite::Database Database;
public:
PrototypeDatabase(llvm::StringRef Path) : Database(Path) {}
PrototypeDatabase(llvm::StringRef Path) :
Database(Path, sqlite::Database::OpenMode::ReadOnly) {}
/// Find a platform by its exact name.
/// Returns -1 if no platform with that name exists.
@@ -17,5 +17,5 @@ commands:
- type: source
filter: db-invariants
command: |-
sqlite3 "$SOURCES_ROOT/share/revng/prototypes.sqlite" < "$INPUT"
sqlite3 -cmd ".open \"file:$SOURCES_ROOT/share/revng/prototypes.sqlite?immutable=1\"" < "$INPUT"
| FileCheck "$INPUT"