@@ -14,6 +14,8 @@ pub use locking_mode::SqliteLockingMode;
14
14
use std:: { borrow:: Cow , time:: Duration } ;
15
15
pub use synchronous:: SqliteSynchronous ;
16
16
17
+ use indexmap:: IndexMap ;
18
+
17
19
/// Options and flags which can be used to configure a SQLite connection.
18
20
///
19
21
/// A value of `SqliteConnectOptions` can be parsed from a connection URI,
@@ -53,17 +55,12 @@ pub struct SqliteConnectOptions {
53
55
pub ( crate ) in_memory : bool ,
54
56
pub ( crate ) read_only : bool ,
55
57
pub ( crate ) create_if_missing : bool ,
56
- pub ( crate ) journal_mode : SqliteJournalMode ,
57
- pub ( crate ) locking_mode : SqliteLockingMode ,
58
- pub ( crate ) foreign_keys : bool ,
59
58
pub ( crate ) shared_cache : bool ,
60
59
pub ( crate ) statement_cache_capacity : usize ,
61
60
pub ( crate ) busy_timeout : Duration ,
62
61
pub ( crate ) log_settings : LogSettings ,
63
- pub ( crate ) synchronous : SqliteSynchronous ,
64
- pub ( crate ) auto_vacuum : SqliteAutoVacuum ,
65
- pub ( crate ) page_size : u32 ,
66
62
pub ( crate ) immutable : bool ,
63
+ pub ( crate ) pragmas : IndexMap < Cow < ' static , str > , Cow < ' static , str > > ,
67
64
}
68
65
69
66
impl Default for SqliteConnectOptions {
@@ -74,22 +71,44 @@ impl Default for SqliteConnectOptions {
74
71
75
72
impl SqliteConnectOptions {
76
73
pub fn new ( ) -> Self {
74
+ // set default pragmas
75
+ let mut pragmas: IndexMap < Cow < ' static , str > , Cow < ' static , str > > = IndexMap :: new ( ) ;
76
+
77
+ let locking_mode: SqliteLockingMode = Default :: default ( ) ;
78
+ let auto_vacuum: SqliteAutoVacuum = Default :: default ( ) ;
79
+
80
+ // page_size must be set before any other action on the database.
81
+ pragmas. insert ( "page_size" . into ( ) , "4096" . into ( ) ) ;
82
+
83
+ // Note that locking_mode should be set before journal_mode; see
84
+ // https://www.sqlite.org/wal.html#use_of_wal_without_shared_memory .
85
+ pragmas. insert ( "locking_mode" . into ( ) , locking_mode. as_str ( ) . into ( ) ) ;
86
+
87
+ pragmas. insert (
88
+ "journal_mode" . into ( ) ,
89
+ SqliteJournalMode :: Wal . as_str ( ) . into ( ) ,
90
+ ) ;
91
+
92
+ pragmas. insert ( "foreign_keys" . into ( ) , "ON" . into ( ) ) ;
93
+
94
+ pragmas. insert (
95
+ "synchronous" . into ( ) ,
96
+ SqliteSynchronous :: Full . as_str ( ) . into ( ) ,
97
+ ) ;
98
+
99
+ pragmas. insert ( "auto_vacuum" . into ( ) , auto_vacuum. as_str ( ) . into ( ) ) ;
100
+
77
101
Self {
78
102
filename : Cow :: Borrowed ( Path :: new ( ":memory:" ) ) ,
79
103
in_memory : false ,
80
104
read_only : false ,
81
105
create_if_missing : false ,
82
- foreign_keys : true ,
83
106
shared_cache : false ,
84
107
statement_cache_capacity : 100 ,
85
- journal_mode : SqliteJournalMode :: Wal ,
86
- locking_mode : Default :: default ( ) ,
87
108
busy_timeout : Duration :: from_secs ( 5 ) ,
88
109
log_settings : Default :: default ( ) ,
89
- synchronous : SqliteSynchronous :: Full ,
90
- auto_vacuum : Default :: default ( ) ,
91
- page_size : 4096 ,
92
110
immutable : false ,
111
+ pragmas,
93
112
}
94
113
}
95
114
@@ -103,7 +122,10 @@ impl SqliteConnectOptions {
103
122
///
104
123
/// By default, this is enabled.
105
124
pub fn foreign_keys ( mut self , on : bool ) -> Self {
106
- self . foreign_keys = on;
125
+ self . pragmas . insert (
126
+ "foreign_keys" . into ( ) ,
127
+ ( if on { "ON" } else { "OFF" } ) . into ( ) ,
128
+ ) ;
107
129
self
108
130
}
109
131
@@ -120,15 +142,17 @@ impl SqliteConnectOptions {
120
142
/// The default journal mode is WAL. For most use cases this can be significantly faster but
121
143
/// there are [disadvantages](https://www.sqlite.org/wal.html).
122
144
pub fn journal_mode ( mut self , mode : SqliteJournalMode ) -> Self {
123
- self . journal_mode = mode;
145
+ self . pragmas
146
+ . insert ( "journal_mode" . into ( ) , mode. as_str ( ) . into ( ) ) ;
124
147
self
125
148
}
126
149
127
150
/// Sets the [locking mode](https://www.sqlite.org/pragma.html#pragma_locking_mode) for the database connection.
128
151
///
129
152
/// The default locking mode is NORMAL.
130
153
pub fn locking_mode ( mut self , mode : SqliteLockingMode ) -> Self {
131
- self . locking_mode = mode;
154
+ self . pragmas
155
+ . insert ( "locking_mode" . into ( ) , mode. as_str ( ) . into ( ) ) ;
132
156
self
133
157
}
134
158
@@ -173,23 +197,36 @@ impl SqliteConnectOptions {
173
197
/// The default synchronous settings is FULL. However, if durability is not a concern,
174
198
/// then NORMAL is normally all one needs in WAL mode.
175
199
pub fn synchronous ( mut self , synchronous : SqliteSynchronous ) -> Self {
176
- self . synchronous = synchronous;
200
+ self . pragmas
201
+ . insert ( "synchronous" . into ( ) , synchronous. as_str ( ) . into ( ) ) ;
177
202
self
178
203
}
179
204
180
205
/// Sets the [auto_vacuum](https://www.sqlite.org/pragma.html#pragma_auto_vacuum) setting for the database connection.
181
206
///
182
207
/// The default auto_vacuum setting is NONE.
183
208
pub fn auto_vacuum ( mut self , auto_vacuum : SqliteAutoVacuum ) -> Self {
184
- self . auto_vacuum = auto_vacuum;
209
+ self . pragmas
210
+ . insert ( "auto_vacuum" . into ( ) , auto_vacuum. as_str ( ) . into ( ) ) ;
185
211
self
186
212
}
187
213
188
214
/// Sets the [page_size](https://www.sqlite.org/pragma.html#pragma_page_size) setting for the database connection.
189
215
///
190
216
/// The default page_size setting is 4096.
191
217
pub fn page_size ( mut self , page_size : u32 ) -> Self {
192
- self . page_size = page_size;
218
+ self . pragmas
219
+ . insert ( "page_size" . into ( ) , page_size. to_string ( ) . into ( ) ) ;
220
+ self
221
+ }
222
+
223
+ /// Sets custom initial pragma for the database connection.
224
+ pub fn pragma < K , V > ( mut self , key : K , value : V ) -> Self
225
+ where
226
+ K : Into < Cow < ' static , str > > ,
227
+ V : Into < Cow < ' static , str > > ,
228
+ {
229
+ self . pragmas . insert ( key. into ( ) , value. into ( ) ) ;
193
230
self
194
231
}
195
232
0 commit comments