@@ -12,6 +12,7 @@ pub(crate) use handle::{ConnectionHandle, ConnectionHandleRaw};
12
12
use crate :: common:: StatementCache ;
13
13
use crate :: connection:: { Connection , LogSettings } ;
14
14
use crate :: error:: Error ;
15
+ use crate :: executor:: Executor ;
15
16
use crate :: sqlite:: connection:: establish:: EstablishParams ;
16
17
use crate :: sqlite:: connection:: worker:: ConnectionWorker ;
17
18
use crate :: sqlite:: statement:: VirtualStatement ;
@@ -39,6 +40,8 @@ mod worker;
39
40
/// You can explicitly call [`.close()`][Self::close] to ensure the database is closed successfully
40
41
/// or get an error otherwise.
41
42
pub struct SqliteConnection {
43
+ optimize_on_close : bool ,
44
+ analysis_limit : Option < u32 > ,
42
45
pub ( crate ) worker : ConnectionWorker ,
43
46
pub ( crate ) row_channel_size : usize ,
44
47
}
@@ -70,6 +73,8 @@ impl SqliteConnection {
70
73
let params = EstablishParams :: from_options ( options) ?;
71
74
let worker = ConnectionWorker :: establish ( params) . await ?;
72
75
Ok ( Self {
76
+ optimize_on_close : options. optimize_on_close ,
77
+ analysis_limit : options. analysis_limit ,
73
78
worker,
74
79
row_channel_size : options. row_channel_size ,
75
80
} )
@@ -146,6 +151,14 @@ impl Connection for SqliteConnection {
146
151
147
152
fn close ( mut self ) -> BoxFuture < ' static , Result < ( ) , Error > > {
148
153
Box :: pin ( async move {
154
+ if self . optimize_on_close {
155
+ let mut pragma_string = String :: new ( ) ;
156
+ if let Some ( limit) = self . analysis_limit {
157
+ pragma_string. push_str ( & format ! ( "PRAGMA analysis_limit = {}; " , limit) ) ;
158
+ }
159
+ pragma_string. push_str ( "PRAGMA optimize;" ) ;
160
+ self . execute ( & * pragma_string) . await ?;
161
+ }
149
162
let shutdown = self . worker . shutdown ( ) ;
150
163
// Drop the statement worker, which should
151
164
// cover all references to the connection handle outside of the worker thread
0 commit comments