Add support for serialized threading mode to sqlite (#1514)

* Add support for serialized threading mode

* Typos

* Fix build
This commit is contained in:
Lucille Blumire 2021-11-03 01:07:56 +00:00 committed by GitHub
parent d25ab07f21
commit 4ada6ac7e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View File

@ -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;
@ -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

View File

@ -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 {
@ -109,6 +110,7 @@ impl SqliteConnectOptions {
log_settings: Default::default(),
immutable: false,
pragmas,
serialized: false,
}
}
@ -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
}
}