|
1 | 1 | use futures_core::future::BoxFuture;
|
2 | 2 | use futures_intrusive::sync::MutexGuard;
|
3 | 3 | use futures_util::future;
|
4 |
| -use libsqlite3_sys::sqlite3; |
| 4 | +use libsqlite3_sys::{sqlite3, sqlite3_progress_handler}; |
5 | 5 | use sqlx_core::common::StatementCache;
|
6 | 6 | use sqlx_core::error::Error;
|
7 | 7 | use sqlx_core::transaction::Transaction;
|
8 | 8 | use std::cmp::Ordering;
|
9 | 9 | use std::fmt::{self, Debug, Formatter};
|
10 | 10 | use std::ptr::NonNull;
|
| 11 | +use std::os::raw::{c_int, c_void}; |
11 | 12 |
|
12 | 13 | use crate::connection::establish::EstablishParams;
|
13 | 14 | use crate::connection::worker::ConnectionWorker;
|
@@ -89,6 +90,45 @@ impl SqliteConnection {
|
89 | 90 |
|
90 | 91 | Ok(LockedSqliteHandle { guard })
|
91 | 92 | }
|
| 93 | + |
| 94 | + /// Sets a progress handler that is invoked periodically during long running calls. If the progress callback |
| 95 | + /// returns `false`, then the operation is interrupted. |
| 96 | + /// |
| 97 | + /// `num_ops` is the approximate number of [virtual machine instructions](https://www.sqlite.org/opcode.html) |
| 98 | + /// that are evaluated between successive invocations of the callback. If `num_ops` is less than one then the |
| 99 | + /// progress handler is disabled. |
| 100 | + /// |
| 101 | + /// Only a single progress handler may be defined at one time per database connection; setting a new progress |
| 102 | + /// handler cancels the old one. |
| 103 | + /// |
| 104 | + /// The progress handler callback must not do anything that will modify the database connection that invoked |
| 105 | + /// the progress handler. Note that sqlite3_prepare_v2() and sqlite3_step() both modify their database connections |
| 106 | + /// in this context. |
| 107 | + pub async fn set_progress_handler<F>(&mut self, num_ops: i32, callback: F) |
| 108 | + where |
| 109 | + F: FnMut() -> bool + Send + 'static, |
| 110 | + { |
| 111 | + unsafe { |
| 112 | + let callback = Box::new(callback); |
| 113 | + if let Ok(mut lock_conn) = self.lock_handle().await { |
| 114 | + sqlite3_progress_handler( |
| 115 | + lock_conn.as_raw_handle().as_mut(), |
| 116 | + num_ops, |
| 117 | + Some(progress_callback::<F>), |
| 118 | + &*callback as *const F as *mut F as *mut _, |
| 119 | + ); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /// Removes a previously set progress handler on a database connection. |
| 125 | + pub async fn remove_progress_handler(&mut self) { |
| 126 | + unsafe { |
| 127 | + if let Ok(mut lock_conn) = self.lock_handle().await { |
| 128 | + sqlite3_progress_handler(lock_conn.as_raw_handle().as_mut(), 0, None, 0 as *mut _); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
92 | 132 | }
|
93 | 133 |
|
94 | 134 | impl Debug for SqliteConnection {
|
@@ -172,6 +212,21 @@ impl Connection for SqliteConnection {
|
172 | 212 | }
|
173 | 213 | }
|
174 | 214 |
|
| 215 | +/// Implements a C binding to a progress callback. The function returns `0` if the |
| 216 | +/// user-provided callback returns `true`, and `1` otherwise to signal an interrupt. |
| 217 | +extern "C" fn progress_callback<F>(callback: *mut c_void) -> c_int |
| 218 | +where |
| 219 | + F: FnMut() -> bool, |
| 220 | +{ |
| 221 | + unsafe { |
| 222 | + if (*(callback as *mut F))() { |
| 223 | + 0 |
| 224 | + } else { |
| 225 | + 1 |
| 226 | + } |
| 227 | + } |
| 228 | +} |
| 229 | + |
175 | 230 | impl LockedSqliteHandle<'_> {
|
176 | 231 | /// Returns the underlying sqlite3* connection handle.
|
177 | 232 | ///
|
|
0 commit comments