Skip to content

Commit

Permalink
Add support for serialized threading mode to sqlite (#1514)
Browse files Browse the repository at this point in the history
* Add support for serialized threading mode

* Typos

* Fix build
  • Loading branch information
LLBlumire authored Nov 3, 2021
1 parent d25ab07 commit 4ada6ac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 7 additions & 3 deletions sqlx-core/src/sqlite/connection/establish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::{
};
use libsqlite3_sys::{
sqlite3_busy_timeout, sqlite3_extended_result_codes, sqlite3_open_v2, SQLITE_OK,
SQLITE_OPEN_CREATE, SQLITE_OPEN_MEMORY, SQLITE_OPEN_NOMUTEX, SQLITE_OPEN_PRIVATECACHE,
SQLITE_OPEN_READONLY, SQLITE_OPEN_READWRITE, SQLITE_OPEN_SHAREDCACHE,
SQLITE_OPEN_CREATE, SQLITE_OPEN_FULLMUTEX, SQLITE_OPEN_MEMORY, SQLITE_OPEN_NOMUTEX,
SQLITE_OPEN_PRIVATECACHE, SQLITE_OPEN_READONLY, SQLITE_OPEN_READWRITE, SQLITE_OPEN_SHAREDCACHE,
};
use sqlx_rt::blocking;
use std::io;
Expand All @@ -33,7 +33,11 @@ pub(crate) async fn establish(options: &SqliteConnectOptions) -> Result<SqliteCo
// [SQLITE_OPEN_NOMUTEX] will instruct [sqlite3_open_v2] to return an error if it
// cannot satisfy our wish for a thread-safe, lock-free connection object

let mut flags = SQLITE_OPEN_NOMUTEX;
let mut flags = if options.serialized {
SQLITE_OPEN_FULLMUTEX
} else {
SQLITE_OPEN_NOMUTEX
};

flags |= if options.read_only {
SQLITE_OPEN_READONLY
Expand Down
12 changes: 12 additions & 0 deletions sqlx-core/src/sqlite/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub struct SqliteConnectOptions {
pub(crate) log_settings: LogSettings,
pub(crate) immutable: bool,
pub(crate) pragmas: IndexMap<Cow<'static, str>, Cow<'static, str>>,
pub(crate) serialized: bool,
}

impl Default for SqliteConnectOptions {
Expand Down Expand Up @@ -109,6 +110,7 @@ impl SqliteConnectOptions {
log_settings: Default::default(),
immutable: false,
pragmas,
serialized: false,
}
}

Expand Down Expand Up @@ -234,4 +236,14 @@ impl SqliteConnectOptions {
self.immutable = immutable;
self
}

/// Sets the [threading mode](https://www.sqlite.org/threadsafe.html) for the database connection.
///
/// The default setting is `false` corersponding to using `OPEN_NOMUTEX`, if `true` then `OPEN_FULLMUTEX`.
///
/// See [open](https://www.sqlite.org/c3ref/open.html) for more details.
pub fn serialized(mut self, serialized: bool) -> Self {
self.serialized = serialized;
self
}
}

0 comments on commit 4ada6ac

Please sign in to comment.