Skip to content

Commit d209c60

Browse files
committed
feat(sqlite): add read_only to SqliteConnectionOptions
1 parent 3c33566 commit d209c60

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::ptr::{null, null_mut};
44
use libsqlite3_sys::{
55
sqlite3_busy_timeout, sqlite3_extended_result_codes, sqlite3_open_v2, SQLITE_OK,
66
SQLITE_OPEN_CREATE, SQLITE_OPEN_MEMORY, SQLITE_OPEN_NOMUTEX, SQLITE_OPEN_PRIVATECACHE,
7-
SQLITE_OPEN_READWRITE,
7+
SQLITE_OPEN_READONLY, SQLITE_OPEN_READWRITE,
88
};
99
use sqlx_rt::blocking;
1010

@@ -33,8 +33,14 @@ pub(super) async fn establish(options: &SqliteConnectOptions) -> Result<SqliteCo
3333
// By default, we connect to an in-memory database.
3434
// [SQLITE_OPEN_NOMUTEX] will instruct [sqlite3_open_v2] to return an error if it
3535
// cannot satisfy our wish for a thread-safe, lock-free connection object
36-
let mut flags =
37-
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_PRIVATECACHE;
36+
37+
let mut flags = SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_PRIVATECACHE;
38+
39+
flags |= if options.read_only {
40+
SQLITE_OPEN_READONLY
41+
} else {
42+
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
43+
};
3844

3945
if options.in_memory {
4046
flags |= SQLITE_OPEN_MEMORY;

sqlx-core/src/sqlite/options.rs

+9
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ impl FromStr for SqliteJournalMode {
6060
pub struct SqliteConnectOptions {
6161
pub(crate) filename: PathBuf,
6262
pub(crate) in_memory: bool,
63+
pub(crate) read_only: bool,
6364
pub(crate) journal_mode: SqliteJournalMode,
6465
pub(crate) foreign_keys: bool,
6566
pub(crate) statement_cache_capacity: usize,
@@ -76,6 +77,7 @@ impl SqliteConnectOptions {
7677
Self {
7778
filename: PathBuf::from(":memory:"),
7879
in_memory: false,
80+
read_only: false,
7981
foreign_keys: true,
8082
statement_cache_capacity: 100,
8183
journal_mode: SqliteJournalMode::Wal,
@@ -99,6 +101,13 @@ impl SqliteConnectOptions {
99101
self
100102
}
101103

104+
/// Sets the [access mode](https://www.sqlite.org/c3ref/open.html) to open the database
105+
/// for read-only access.
106+
pub fn read_only(mut self, read_only: bool) -> Self {
107+
self.read_only = read_only;
108+
self
109+
}
110+
102111
/// Sets the capacity of the connection's statement cache in a number of stored
103112
/// distinct statements. Caching is handled using LRU, meaning when the
104113
/// amount of queries hits the defined limit, the oldest statement will get

0 commit comments

Comments
 (0)