Skip to content

Commit e5f53df

Browse files
committed
Support the immutable option on SQLite connections
1 parent e33e451 commit e5f53df

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

sqlx-core/src/sqlite/connection/establish.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ pub(crate) async fn establish(options: &SqliteConnectOptions) -> Result<SqliteCo
2929
})?
3030
.to_owned();
3131

32-
filename.push('\0');
33-
3432
// By default, we connect to an in-memory database.
3533
// [SQLITE_OPEN_NOMUTEX] will instruct [sqlite3_open_v2] to return an error if it
3634
// cannot satisfy our wish for a thread-safe, lock-free connection object
@@ -55,6 +53,13 @@ pub(crate) async fn establish(options: &SqliteConnectOptions) -> Result<SqliteCo
5553
SQLITE_OPEN_PRIVATECACHE
5654
};
5755

56+
if options.immutable {
57+
filename = format!("file:{}?immutable=true", filename);
58+
flags |= libsqlite3_sys::SQLITE_OPEN_URI;
59+
}
60+
61+
filename.push('\0');
62+
5863
let busy_timeout = options.busy_timeout;
5964

6065
let handle = blocking!({

sqlx-core/src/sqlite/options/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub struct SqliteConnectOptions {
6363
pub(crate) synchronous: SqliteSynchronous,
6464
pub(crate) auto_vacuum: SqliteAutoVacuum,
6565
pub(crate) page_size: u32,
66+
pub(crate) immutable: bool,
6667
}
6768

6869
impl Default for SqliteConnectOptions {
@@ -88,6 +89,7 @@ impl SqliteConnectOptions {
8889
synchronous: SqliteSynchronous::Full,
8990
auto_vacuum: Default::default(),
9091
page_size: 4096,
92+
immutable: false,
9193
}
9294
}
9395

@@ -190,4 +192,9 @@ impl SqliteConnectOptions {
190192
self.page_size = page_size;
191193
self
192194
}
195+
196+
pub fn immutable(mut self, immutable: bool) -> Self {
197+
self.immutable = immutable;
198+
self
199+
}
193200
}

sqlx-core/src/sqlite/options/parse.rs

+14
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ impl FromStr for SqliteConnectOptions {
9494
}
9595
},
9696

97+
"immutable" => match &*value {
98+
"true" | "1" => {
99+
options.immutable = true;
100+
}
101+
"false" | "0" => {
102+
options.immutable = false;
103+
}
104+
_ => {
105+
return Err(Error::Configuration(
106+
format!("unknown value {:?} for `immutable`", value).into(),
107+
));
108+
}
109+
}
110+
97111
_ => {
98112
return Err(Error::Configuration(
99113
format!(

0 commit comments

Comments
 (0)