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