Skip to content

Commit 4ada6ac

Browse files
authored
Add support for serialized threading mode to sqlite (#1514)
* Add support for serialized threading mode * Typos * Fix build
1 parent d25ab07 commit 4ada6ac

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use crate::{
77
};
88
use libsqlite3_sys::{
99
sqlite3_busy_timeout, sqlite3_extended_result_codes, sqlite3_open_v2, SQLITE_OK,
10-
SQLITE_OPEN_CREATE, SQLITE_OPEN_MEMORY, SQLITE_OPEN_NOMUTEX, SQLITE_OPEN_PRIVATECACHE,
11-
SQLITE_OPEN_READONLY, SQLITE_OPEN_READWRITE, SQLITE_OPEN_SHAREDCACHE,
10+
SQLITE_OPEN_CREATE, SQLITE_OPEN_FULLMUTEX, SQLITE_OPEN_MEMORY, SQLITE_OPEN_NOMUTEX,
11+
SQLITE_OPEN_PRIVATECACHE, SQLITE_OPEN_READONLY, SQLITE_OPEN_READWRITE, SQLITE_OPEN_SHAREDCACHE,
1212
};
1313
use sqlx_rt::blocking;
1414
use std::io;
@@ -33,7 +33,11 @@ pub(crate) async fn establish(options: &SqliteConnectOptions) -> Result<SqliteCo
3333
// [SQLITE_OPEN_NOMUTEX] will instruct [sqlite3_open_v2] to return an error if it
3434
// cannot satisfy our wish for a thread-safe, lock-free connection object
3535

36-
let mut flags = SQLITE_OPEN_NOMUTEX;
36+
let mut flags = if options.serialized {
37+
SQLITE_OPEN_FULLMUTEX
38+
} else {
39+
SQLITE_OPEN_NOMUTEX
40+
};
3741

3842
flags |= if options.read_only {
3943
SQLITE_OPEN_READONLY

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

+12
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub struct SqliteConnectOptions {
6161
pub(crate) log_settings: LogSettings,
6262
pub(crate) immutable: bool,
6363
pub(crate) pragmas: IndexMap<Cow<'static, str>, Cow<'static, str>>,
64+
pub(crate) serialized: bool,
6465
}
6566

6667
impl Default for SqliteConnectOptions {
@@ -109,6 +110,7 @@ impl SqliteConnectOptions {
109110
log_settings: Default::default(),
110111
immutable: false,
111112
pragmas,
113+
serialized: false,
112114
}
113115
}
114116

@@ -234,4 +236,14 @@ impl SqliteConnectOptions {
234236
self.immutable = immutable;
235237
self
236238
}
239+
240+
/// Sets the [threading mode](https://www.sqlite.org/threadsafe.html) for the database connection.
241+
///
242+
/// The default setting is `false` corersponding to using `OPEN_NOMUTEX`, if `true` then `OPEN_FULLMUTEX`.
243+
///
244+
/// See [open](https://www.sqlite.org/c3ref/open.html) for more details.
245+
pub fn serialized(mut self, serialized: bool) -> Self {
246+
self.serialized = serialized;
247+
self
248+
}
237249
}

0 commit comments

Comments
 (0)