@@ -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,16 +55,11 @@ 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 ,
62
+ pub ( crate ) pragmas : IndexMap < String , String > ,
66
63
}
67
64
68
65
impl Default for SqliteConnectOptions {
@@ -73,21 +70,43 @@ impl Default for SqliteConnectOptions {
73
70
74
71
impl SqliteConnectOptions {
75
72
pub fn new ( ) -> Self {
73
+ // set default pragmas
74
+ let mut pragmas = IndexMap :: new ( ) ;
75
+
76
+ let locking_mode: SqliteLockingMode = Default :: default ( ) ;
77
+ let auto_vacuum: SqliteAutoVacuum = Default :: default ( ) ;
78
+
79
+ // page_size must be set before any other action on the database.
80
+ pragmas. insert ( "page_size" . into ( ) , 4096 . to_string ( ) ) ;
81
+
82
+ // Note that locking_mode should be set before journal_mode; see
83
+ // https://www.sqlite.org/wal.html#use_of_wal_without_shared_memory .
84
+ pragmas. insert ( "locking_mode" . into ( ) , locking_mode. as_str ( ) . into ( ) ) ;
85
+
86
+ pragmas. insert (
87
+ "journal_mode" . into ( ) ,
88
+ SqliteJournalMode :: Wal . as_str ( ) . into ( ) ,
89
+ ) ;
90
+
91
+ pragmas. insert ( "foreign_keys" . into ( ) , "ON" . into ( ) ) ;
92
+
93
+ pragmas. insert (
94
+ "synchronous" . into ( ) ,
95
+ SqliteSynchronous :: Full . as_str ( ) . into ( ) ,
96
+ ) ;
97
+
98
+ pragmas. insert ( "auto_vacuum" . into ( ) , auto_vacuum. as_str ( ) . into ( ) ) ;
99
+
76
100
Self {
77
101
filename : Cow :: Borrowed ( Path :: new ( ":memory:" ) ) ,
78
102
in_memory : false ,
79
103
read_only : false ,
80
104
create_if_missing : false ,
81
- foreign_keys : true ,
82
105
shared_cache : false ,
83
106
statement_cache_capacity : 100 ,
84
- journal_mode : SqliteJournalMode :: Wal ,
85
- locking_mode : Default :: default ( ) ,
86
107
busy_timeout : Duration :: from_secs ( 5 ) ,
87
108
log_settings : Default :: default ( ) ,
88
- synchronous : SqliteSynchronous :: Full ,
89
- auto_vacuum : Default :: default ( ) ,
90
- page_size : 4096 ,
109
+ pragmas,
91
110
}
92
111
}
93
112
@@ -101,7 +120,10 @@ impl SqliteConnectOptions {
101
120
///
102
121
/// By default, this is enabled.
103
122
pub fn foreign_keys ( mut self , on : bool ) -> Self {
104
- self . foreign_keys = on;
123
+ self . pragmas . insert (
124
+ "foreign_keys" . into ( ) ,
125
+ ( if on { "ON" } else { "OFF" } ) . into ( ) ,
126
+ ) ;
105
127
self
106
128
}
107
129
@@ -118,15 +140,17 @@ impl SqliteConnectOptions {
118
140
/// The default journal mode is WAL. For most use cases this can be significantly faster but
119
141
/// there are [disadvantages](https://www.sqlite.org/wal.html).
120
142
pub fn journal_mode ( mut self , mode : SqliteJournalMode ) -> Self {
121
- self . journal_mode = mode;
143
+ self . pragmas
144
+ . insert ( "journal_mode" . into ( ) , mode. as_str ( ) . into ( ) ) ;
122
145
self
123
146
}
124
147
125
148
/// Sets the [locking mode](https://www.sqlite.org/pragma.html#pragma_locking_mode) for the database connection.
126
149
///
127
150
/// The default locking mode is NORMAL.
128
151
pub fn locking_mode ( mut self , mode : SqliteLockingMode ) -> Self {
129
- self . locking_mode = mode;
152
+ self . pragmas
153
+ . insert ( "locking_mode" . into ( ) , mode. as_str ( ) . into ( ) ) ;
130
154
self
131
155
}
132
156
@@ -171,23 +195,31 @@ impl SqliteConnectOptions {
171
195
/// The default synchronous settings is FULL. However, if durability is not a concern,
172
196
/// then NORMAL is normally all one needs in WAL mode.
173
197
pub fn synchronous ( mut self , synchronous : SqliteSynchronous ) -> Self {
174
- self . synchronous = synchronous;
198
+ self . pragmas
199
+ . insert ( "synchronous" . into ( ) , synchronous. as_str ( ) . into ( ) ) ;
175
200
self
176
201
}
177
202
178
203
/// Sets the [auto_vacuum](https://www.sqlite.org/pragma.html#pragma_auto_vacuum) setting for the database connection.
179
204
///
180
205
/// The default auto_vacuum setting is NONE.
181
206
pub fn auto_vacuum ( mut self , auto_vacuum : SqliteAutoVacuum ) -> Self {
182
- self . auto_vacuum = auto_vacuum;
207
+ self . pragmas
208
+ . insert ( "auto_vacuum" . into ( ) , auto_vacuum. as_str ( ) . into ( ) ) ;
209
+ self
210
+ }
211
+
212
+ /// Sets custom initial pragmas for the database connection.
213
+ pub fn pragma ( mut self , key : & str , value : & str ) -> Self {
214
+ self . pragmas . insert ( key. into ( ) , value. into ( ) ) ;
183
215
self
184
216
}
185
217
186
218
/// Sets the [page_size](https://www.sqlite.org/pragma.html#pragma_page_size) setting for the database connection.
187
219
///
188
220
/// The default page_size setting is 4096.
189
221
pub fn page_size ( mut self , page_size : u32 ) -> Self {
190
- self . page_size = page_size;
222
+ self . pragmas . insert ( " page_size" . into ( ) , page_size. to_string ( ) ) ;
191
223
self
192
224
}
193
225
}
0 commit comments