| 1 |
//! Types related to database connections |
| 2 |
|
| 3 |
pub(crate) mod instrumentation; |
| 4 |
#[cfg(all( |
| 5 |
not(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"), |
| 6 |
any(feature = "sqlite", feature = "postgres", feature = "mysql") |
| 7 |
))] |
| 8 |
pub(crate) mod statement_cache; |
| 9 |
#[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")] |
| 10 |
pub mod statement_cache; |
| 11 |
mod transaction_manager; |
| 12 |
|
| 13 |
use crate::backend::Backend; |
| 14 |
use crate::expression::QueryMetadata; |
| 15 |
use crate::query_builder::{Query, QueryFragment, QueryId}; |
| 16 |
use crate::result::*; |
| 17 |
use crate::sql_types::TypeMetadata; |
| 18 |
use std::fmt::Debug; |
| 19 |
|
| 20 |
#[doc(inline)] |
| 21 |
pub use self::instrumentation::{ |
| 22 |
get_default_instrumentation, set_default_instrumentation, DebugQuery, Instrumentation, |
| 23 |
InstrumentationEvent, |
| 24 |
}; |
| 25 |
#[doc(inline)] |
| 26 |
pub use self::transaction_manager::{ |
| 27 |
AnsiTransactionManager, InTransactionStatus, TransactionDepthChange, TransactionManager, |
| 28 |
TransactionManagerStatus, ValidTransactionManagerStatus, |
| 29 |
}; |
| 30 |
|
| 31 |
#[diesel_derives::__diesel_public_if( |
| 32 |
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes" |
| 33 |
)] |
| 34 |
pub(crate) use self::private::ConnectionSealed; |
| 35 |
|
| 36 |
#[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")] |
| 37 |
pub use self::private::MultiConnectionHelper; |
| 38 |
|
| 39 |
#[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")] |
| 40 |
pub use self::instrumentation::{DynInstrumentation, StrQueryHelper}; |
| 41 |
|
| 42 |
#[cfg(all( |
| 43 |
not(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"), |
| 44 |
any(feature = "sqlite", feature = "postgres", feature = "mysql") |
| 45 |
))] |
| 46 |
pub(crate) use self::private::MultiConnectionHelper; |
| 47 |
|
| 48 |
/// Set cache size for a connection |
| 49 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 50 |
#[non_exhaustive] |
| 51 |
pub enum CacheSize { |
| 52 |
/// Caches all queries if possible |
| 53 |
Unbounded, |
| 54 |
/// Disable statement cache |
| 55 |
Disabled, |
| 56 |
} |
| 57 |
|
| 58 |
/// Perform simple operations on a backend. |
| 59 |
/// |
| 60 |
/// You should likely use [`Connection`] instead. |
| 61 |
pub trait SimpleConnection { |
| 62 |
/// Execute multiple SQL statements within the same string. |
| 63 |
/// |
| 64 |
/// This function is used to execute migrations, |
| 65 |
/// which may contain more than one SQL statement. |
| 66 |
fn batch_execute(&mut self, query: &str) -> QueryResult<()>; |
| 67 |
} |
| 68 |
|
| 69 |
#[doc(hidden)] |
| 70 |
#[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))] |
| 71 |
#[deprecated(note = "Directly use `LoadConnection::Cursor` instead")] |
| 72 |
pub type LoadRowIter<'conn, 'query, C, DB, B = DefaultLoadingMode> = |
| 73 |
<C as self::private::ConnectionHelperType<DB, B>>::Cursor<'conn, 'query>; |
| 74 |
|
| 75 |
/// A connection to a database |
| 76 |
/// |
| 77 |
/// This trait represents a database connection. It can be used to query the database through |
| 78 |
/// the query dsl provided by diesel, custom extensions or raw sql queries. |
| 79 |
/// |
| 80 |
/// # Implementing a custom connection |
| 81 |
/// |
| 82 |
/// There are several reasons why you would want to implement a custom connection implementation: |
| 83 |
/// |
| 84 |
/// * To wrap an existing connection for instrumentation purposes |
| 85 |
/// * To use a different underlying library to provide a connection implementation |
| 86 |
/// for already existing backends. |
| 87 |
/// * To add support for an unsupported database system |
| 88 |
/// |
| 89 |
/// Implementing a `Connection` in a third party crate requires |
| 90 |
/// enabling the |
| 91 |
/// `i-implement-a-third-party-backend-and-opt-into-breaking-changes` |
| 92 |
/// crate feature which grants access to some of diesel's implementation details. |
| 93 |
/// |
| 94 |
/// |
| 95 |
/// ## Wrapping an existing connection impl |
| 96 |
/// |
| 97 |
/// Wrapping an existing connection allows you to customize the implementation to |
| 98 |
/// add additional functionality, like for example instrumentation. For this use case |
| 99 |
/// you only need to implement `Connection`, [`LoadConnection`] and all super traits. |
| 100 |
/// You should forward any method call to the wrapped connection type. |
| 101 |
/// It is **important** to also forward any method where diesel provides a |
| 102 |
/// default implementation, as the wrapped connection implementation may |
| 103 |
/// contain a customized implementation. |
| 104 |
/// |
| 105 |
/// To allow the integration of your new connection type with other diesel features |
| 106 |
#[cfg_attr( |
| 107 |
feature = "r2d2", |
| 108 |
doc = "it may be useful to also implement [`R2D2Connection`](crate::r2d2::R2D2Connection)" |
| 109 |
)] |
| 110 |
#[cfg_attr( |
| 111 |
not(feature = "r2d2"), |
| 112 |
doc = "it may be useful to also implement `R2D2Connection`" |
| 113 |
)] |
| 114 |
/// and [`MigrationConnection`](crate::migration::MigrationConnection). |
| 115 |
/// |
| 116 |
/// ## Provide a new connection implementation for an existing backend |
| 117 |
/// |
| 118 |
/// Implementing a new connection based on an existing backend can enable the usage of |
| 119 |
/// other methods to connect to the database. One example here would be to replace |
| 120 |
/// the official diesel provided connection implementations with an implementation |
| 121 |
/// based on a pure rust connection crate. |
| 122 |
/// |
| 123 |
/// **It's important to use prepared statements to implement the following methods:** |
| 124 |
/// * [`LoadConnection::load`] |
| 125 |
/// * [`Connection::execute_returning_count`] |
| 126 |
/// |
| 127 |
/// For performance reasons it may also be meaningful to cache already prepared statements. |
| 128 |
#[cfg_attr( |
| 129 |
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes", |
| 130 |
doc = "See [`StatementCache`](self::statement_cache::StatementCache)" |
| 131 |
)] |
| 132 |
#[cfg_attr( |
| 133 |
not(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"), |
| 134 |
doc = "See `StatementCache`" |
| 135 |
)] |
| 136 |
/// for a helper type to implement prepared statement caching. |
| 137 |
#[cfg_attr( |
| 138 |
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes", |
| 139 |
doc = "The [statement_cache](self::statement_cache)" |
| 140 |
)] |
| 141 |
#[cfg_attr( |
| 142 |
not(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"), |
| 143 |
doc = "The statement_cache" |
| 144 |
)] |
| 145 |
/// module documentation contains details about efficient prepared statement caching |
| 146 |
/// based on diesels query builder. |
| 147 |
/// |
| 148 |
/// It is required to implement at least the following parts: |
| 149 |
/// |
| 150 |
/// * A row type that describes how to receive values form a database row. |
| 151 |
/// This type needs to implement [`Row`](crate::row::Row) |
| 152 |
/// * A field type that describes a database field value. |
| 153 |
/// This type needs to implement [`Field`](crate::row::Field) |
| 154 |
/// * A connection type that wraps the connection + |
| 155 |
/// the necessary state management. |
| 156 |
/// * Maybe a [`TransactionManager`] implementation matching |
| 157 |
/// the interface provided by the database connection crate. |
| 158 |
/// Otherwise the implementation used by the corresponding |
| 159 |
/// `Connection` in diesel can be reused. |
| 160 |
/// |
| 161 |
/// To allow the integration of your new connection type with other diesel features |
| 162 |
#[cfg_attr( |
| 163 |
feature = "r2d2", |
| 164 |
doc = "it may be useful to also implement [`R2D2Connection`](crate::r2d2::R2D2Connection)" |
| 165 |
)] |
| 166 |
#[cfg_attr( |
| 167 |
not(feature = "r2d2"), |
| 168 |
doc = "it may be useful to also implement `R2D2Connection`" |
| 169 |
)] |
| 170 |
/// and [`MigrationConnection`](crate::migration::MigrationConnection). |
| 171 |
/// |
| 172 |
/// The exact implementation of the `Connection` trait depends on the interface provided |
| 173 |
/// by the connection crate/library. A struct implementing `Connection` should |
| 174 |
#[cfg_attr( |
| 175 |
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes", |
| 176 |
doc = "likely contain a [`StatementCache`](self::statement_cache::StatementCache)" |
| 177 |
)] |
| 178 |
#[cfg_attr( |
| 179 |
not(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"), |
| 180 |
doc = "likely contain a `StatementCache`" |
| 181 |
)] |
| 182 |
/// to cache prepared statements efficiently. |
| 183 |
/// |
| 184 |
/// As implementations differ significantly between the supported backends |
| 185 |
/// we cannot give a one for all description here. Generally it's likely a |
| 186 |
/// good idea to follow the implementation of the corresponding connection |
| 187 |
/// in diesel at a high level to gain some idea how to implement your |
| 188 |
/// custom implementation. |
| 189 |
/// |
| 190 |
/// ## Implement support for an unsupported database system |
| 191 |
/// |
| 192 |
/// Additionally to anything mentioned in the previous section the following steps are required: |
| 193 |
/// |
| 194 |
/// * Implement a custom backend type. See the documentation of [`Backend`] for details |
| 195 |
/// * Implement appropriate [`FromSql`](crate::deserialize::FromSql)/ |
| 196 |
/// [`ToSql`](crate::serialize::ToSql) conversions. |
| 197 |
/// At least the following impls should be considered: |
| 198 |
/// * `i16`: `FromSql<SmallInt, YourBackend>` |
| 199 |
/// * `i32`: `FromSql<Integer, YourBackend>` |
| 200 |
/// * `i64`: `FromSql<BigInt, YourBackend>` |
| 201 |
/// * `f32`: `FromSql<Float, YourBackend>` |
| 202 |
/// * `f64`: `FromSql<Double, YourBackend>` |
| 203 |
/// * `bool`: `FromSql<Bool, YourBackend>` |
| 204 |
/// * `String`: `FromSql<Text, YourBackend>` |
| 205 |
/// * `Vec<u8>`: `FromSql<Binary, YourBackend>` |
| 206 |
/// * `i16`: `ToSql<SmallInt, YourBackend>` |
| 207 |
/// * `i32`: `ToSql<Integer, YourBackend>` |
| 208 |
/// * `i64`: `ToSql<BigInt, YourBackend>` |
| 209 |
/// * `f32`: `ToSql<Float, YourBackend>` |
| 210 |
/// * `f64`: `ToSql<Double, YourBackend>` |
| 211 |
/// * `bool`: `ToSql<Bool, YourBackend>` |
| 212 |
/// * `String`: `ToSql<Text, YourBackend>` |
| 213 |
/// * `Vec<u8>`: `ToSql<Binary, YourBackend>` |
| 214 |
/// * Maybe a [`TransactionManager`] implementation matching |
| 215 |
/// the interface provided by the database connection crate. |
| 216 |
/// Otherwise the implementation used by the corresponding |
| 217 |
/// `Connection` in diesel can be reused. |
| 218 |
/// |
| 219 |
/// As these implementations will vary depending on the backend being used, |
| 220 |
/// we cannot give concrete examples here. We recommend looking at our existing |
| 221 |
/// implementations to see how you can implement your own connection. |
| 222 |
pub trait Connection: SimpleConnection + Sized + Send |
| 223 |
where |
| 224 |
// This trait bound is there so that implementing a new connection is |
| 225 |
// gated behind the `i-implement-a-third-party-backend-and-opt-into-breaking-changes` |
| 226 |
// feature flag |
| 227 |
Self: ConnectionSealed, |
| 228 |
{ |
| 229 |
/// The backend this type connects to |
| 230 |
type Backend: Backend; |
| 231 |
|
| 232 |
/// The transaction manager implementation used by this connection |
| 233 |
#[diesel_derives::__diesel_public_if( |
| 234 |
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes" |
| 235 |
)] |
| 236 |
type TransactionManager: TransactionManager<Self>; |
| 237 |
|
| 238 |
/// Establishes a new connection to the database |
| 239 |
/// |
| 240 |
/// The argument to this method and the method's behavior varies by backend. |
| 241 |
/// See the documentation for that backend's connection class |
| 242 |
/// for details about what it accepts and how it behaves. |
| 243 |
fn establish(database_url: &str) -> ConnectionResult<Self>; |
| 244 |
|
| 245 |
/// Executes the given function inside of a database transaction |
| 246 |
/// |
| 247 |
/// This function executes the provided closure `f` inside a database |
| 248 |
/// transaction. If there is already an open transaction for the current |
| 249 |
/// connection savepoints will be used instead. The connection is committed if |
| 250 |
/// the closure returns `Ok(_)`, it will be rolled back if it returns `Err(_)`. |
| 251 |
/// For both cases the original result value will be returned from this function. |
| 252 |
/// |
| 253 |
/// If the transaction fails to commit due to a `SerializationFailure` or a |
| 254 |
/// `ReadOnlyTransaction` a rollback will be attempted. |
| 255 |
/// If the rollback fails, the error will be returned in a |
| 256 |
/// [`Error::RollbackErrorOnCommit`], |
| 257 |
/// from which you will be able to extract both the original commit error and |
| 258 |
/// the rollback error. |
| 259 |
/// In addition, the connection will be considered broken |
| 260 |
/// as it contains a uncommitted unabortable open transaction. Any further |
| 261 |
/// interaction with the transaction system will result in an returned error |
| 262 |
/// in this case. |
| 263 |
/// |
| 264 |
/// If the closure returns an `Err(_)` and the rollback fails the function |
| 265 |
/// will return that rollback error directly, and the transaction manager will |
| 266 |
/// be marked as broken as it contains a uncommitted unabortable open transaction. |
| 267 |
/// |
| 268 |
/// If a nested transaction fails to release the corresponding savepoint |
| 269 |
/// the error will be returned directly. |
| 270 |
/// |
| 271 |
/// # Example |
| 272 |
/// |
| 273 |
/// ```rust |
| 274 |
/// # include!("../doctest_setup.rs"); |
| 275 |
/// use diesel::result::Error; |
| 276 |
/// |
| 277 |
/// # fn main() { |
| 278 |
/// # run_test().unwrap(); |
| 279 |
/// # } |
| 280 |
/// # |
| 281 |
/// # fn run_test() -> QueryResult<()> { |
| 282 |
/// # use schema::users::dsl::*; |
| 283 |
/// # let conn = &mut establish_connection(); |
| 284 |
/// conn.transaction::<_, Error, _>(|conn| { |
| 285 |
/// diesel::insert_into(users) |
| 286 |
/// .values(name.eq("Ruby")) |
| 287 |
/// .execute(conn)?; |
| 288 |
/// |
| 289 |
/// let all_names = users.select(name).load::<String>(conn)?; |
| 290 |
/// assert_eq!(vec!["Sean", "Tess", "Ruby"], all_names); |
| 291 |
/// |
| 292 |
/// Ok(()) |
| 293 |
/// })?; |
| 294 |
/// |
| 295 |
/// conn.transaction::<(), _, _>(|conn| { |
| 296 |
/// diesel::insert_into(users) |
| 297 |
/// .values(name.eq("Pascal")) |
| 298 |
/// .execute(conn)?; |
| 299 |
/// |
| 300 |
/// let all_names = users.select(name).load::<String>(conn)?; |
| 301 |
/// assert_eq!(vec!["Sean", "Tess", "Ruby", "Pascal"], all_names); |
| 302 |
/// |
| 303 |
/// // If we want to roll back the transaction, but don't have an |
| 304 |
/// // actual error to return, we can return `RollbackTransaction`. |
| 305 |
/// Err(Error::RollbackTransaction) |
| 306 |
/// }); |
| 307 |
/// |
| 308 |
/// let all_names = users.select(name).load::<String>(conn)?; |
| 309 |
/// assert_eq!(vec!["Sean", "Tess", "Ruby"], all_names); |
| 310 |
/// # Ok(()) |
| 311 |
/// # } |
| 312 |
/// ``` |
| 313 |
fn transaction<T, E, F>(&mut self, f: F) -> Result<T, E> |
| 314 |
where |
| 315 |
F: FnOnce(&mut Self) -> Result<T, E>, |
| 316 |
E: From<Error>, |
| 317 |
{ |
| 318 |
Self::TransactionManager::transaction(self, f) |
| 319 |
} |
| 320 |
|
| 321 |
/// Creates a transaction that will never be committed. This is useful for |
| 322 |
/// tests. Panics if called while inside of a transaction or |
| 323 |
/// if called with a connection containing a broken transaction |
| 324 |
fn begin_test_transaction(&mut self) -> QueryResult<()> { |
| 325 |
match Self::TransactionManager::transaction_manager_status_mut(self) { |
| 326 |
TransactionManagerStatus::Valid(valid_status) => { |
| 327 |
assert_eq!(None, valid_status.transaction_depth()) |
| 328 |
} |
| 329 |
TransactionManagerStatus::InError => panic!("Transaction manager in error"), |
| 330 |
}; |
| 331 |
Self::TransactionManager::begin_transaction(self)?; |
| 332 |
// set the test transaction flag |
| 333 |
// to prevent that this connection gets dropped in connection pools |
| 334 |
// Tests commonly set the poolsize to 1 and use `begin_test_transaction` |
| 335 |
// to prevent modifications to the schema |
| 336 |
Self::TransactionManager::transaction_manager_status_mut(self).set_test_transaction_flag(); |
| 337 |
Ok(()) |
| 338 |
} |
| 339 |
|
| 340 |
/// Executes the given function inside a transaction, but does not commit |
| 341 |
/// it. Panics if the given function returns an error. |
| 342 |
/// |
| 343 |
/// # Example |
| 344 |
/// |
| 345 |
/// ```rust |
| 346 |
/// # include!("../doctest_setup.rs"); |
| 347 |
/// use diesel::result::Error; |
| 348 |
/// |
| 349 |
/// # fn main() { |
| 350 |
/// # run_test().unwrap(); |
| 351 |
/// # } |
| 352 |
/// # |
| 353 |
/// # fn run_test() -> QueryResult<()> { |
| 354 |
/// # use schema::users::dsl::*; |
| 355 |
/// # let conn = &mut establish_connection(); |
| 356 |
/// conn.test_transaction::<_, Error, _>(|conn| { |
| 357 |
/// diesel::insert_into(users) |
| 358 |
/// .values(name.eq("Ruby")) |
| 359 |
/// .execute(conn)?; |
| 360 |
/// |
| 361 |
/// let all_names = users.select(name).load::<String>(conn)?; |
| 362 |
/// assert_eq!(vec!["Sean", "Tess", "Ruby"], all_names); |
| 363 |
/// |
| 364 |
/// Ok(()) |
| 365 |
/// }); |
| 366 |
/// |
| 367 |
/// // Even though we returned `Ok`, the transaction wasn't committed. |
| 368 |
/// let all_names = users.select(name).load::<String>(conn)?; |
| 369 |
/// assert_eq!(vec!["Sean", "Tess"], all_names); |
| 370 |
/// # Ok(()) |
| 371 |
/// # } |
| 372 |
/// ``` |
| 373 |
fn test_transaction<T, E, F>(&mut self, f: F) -> T |
| 374 |
where |
| 375 |
F: FnOnce(&mut Self) -> Result<T, E>, |
| 376 |
E: Debug, |
| 377 |
{ |
| 378 |
let mut user_result = None; |
| 379 |
let _ = self.transaction::<(), _, _>(|conn| { |
| 380 |
user_result = Some(f(conn)); |
| 381 |
Err(Error::RollbackTransaction) |
| 382 |
}); |
| 383 |
user_result |
| 384 |
.expect("Transaction never executed") |
| 385 |
.unwrap_or_else(|e| panic!("Transaction did not succeed: {:?}", e)) |
| 386 |
} |
| 387 |
|
| 388 |
/// Execute a single SQL statements given by a query and return |
| 389 |
/// number of affected rows |
| 390 |
#[diesel_derives::__diesel_public_if( |
| 391 |
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes" |
| 392 |
)] |
| 393 |
fn execute_returning_count<T>(&mut self, source: &T) -> QueryResult<usize> |
| 394 |
where |
| 395 |
T: QueryFragment<Self::Backend> + QueryId; |
| 396 |
|
| 397 |
/// Get access to the current transaction state of this connection |
| 398 |
/// |
| 399 |
/// This function should be used from [`TransactionManager`] to access |
| 400 |
/// internally required state. |
| 401 |
#[diesel_derives::__diesel_public_if( |
| 402 |
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes" |
| 403 |
)] |
| 404 |
fn transaction_state( |
| 405 |
&mut self, |
| 406 |
) -> &mut <Self::TransactionManager as TransactionManager<Self>>::TransactionStateData; |
| 407 |
|
| 408 |
/// Get the instrumentation instance stored in this connection |
| 409 |
#[diesel_derives::__diesel_public_if( |
| 410 |
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes" |
| 411 |
)] |
| 412 |
fn instrumentation(&mut self) -> &mut dyn Instrumentation; |
| 413 |
|
| 414 |
/// Set a specific [`Instrumentation`] implementation for this connection |
| 415 |
fn set_instrumentation(&mut self, instrumentation: impl Instrumentation); |
| 416 |
|
| 417 |
/// Set the prepared statement cache size to [`CacheSize`] for this connection |
| 418 |
fn set_prepared_statement_cache_size(&mut self, size: CacheSize); |
| 419 |
} |
| 420 |
|
| 421 |
/// The specific part of a [`Connection`] which actually loads data from the database |
| 422 |
/// |
| 423 |
/// This is a separate trait to allow connection implementations to specify |
| 424 |
/// different loading modes via the generic parameter. |
| 425 |
pub trait LoadConnection<B = DefaultLoadingMode>: Connection { |
| 426 |
/// The cursor type returned by [`LoadConnection::load`] |
| 427 |
/// |
| 428 |
/// Users should handle this as opaque type that implements [`Iterator`] |
| 429 |
type Cursor<'conn, 'query>: Iterator< |
| 430 |
Item = QueryResult<<Self as LoadConnection<B>>::Row<'conn, 'query>>, |
| 431 |
> |
| 432 |
where |
| 433 |
Self: 'conn; |
| 434 |
|
| 435 |
/// The row type used as [`Iterator::Item`] for the iterator implementation |
| 436 |
/// of [`LoadConnection::Cursor`] |
| 437 |
type Row<'conn, 'query>: crate::row::Row<'conn, Self::Backend> |
| 438 |
where |
| 439 |
Self: 'conn; |
| 440 |
|
| 441 |
/// Executes a given query and returns any requested values |
| 442 |
/// |
| 443 |
/// This function executes a given query and returns the |
| 444 |
/// query result as given by the database. **Normal users |
| 445 |
/// should not use this function**. Use |
| 446 |
/// [`QueryDsl::load`](crate::QueryDsl) instead. |
| 447 |
/// |
| 448 |
/// This function is useful for people trying to build an alternative |
| 449 |
/// dsl on top of diesel. It returns an [`impl Iterator<Item = QueryResult<&impl Row<Self::Backend>>`](Iterator). |
| 450 |
/// This type can be used to iterate over all rows returned by the database. |
| 451 |
#[diesel_derives::__diesel_public_if( |
| 452 |
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes" |
| 453 |
)] |
| 454 |
fn load<'conn, 'query, T>( |
| 455 |
&'conn mut self, |
| 456 |
source: T, |
| 457 |
) -> QueryResult<Self::Cursor<'conn, 'query>> |
| 458 |
where |
| 459 |
T: Query + QueryFragment<Self::Backend> + QueryId + 'query, |
| 460 |
Self::Backend: QueryMetadata<T::SqlType>; |
| 461 |
} |
| 462 |
|
| 463 |
/// Describes a connection with an underlying [`crate::sql_types::TypeMetadata::MetadataLookup`] |
| 464 |
#[diesel_derives::__diesel_public_if( |
| 465 |
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes" |
| 466 |
)] |
| 467 |
pub trait WithMetadataLookup: Connection { |
| 468 |
/// Retrieves the underlying metadata lookup |
| 469 |
fn metadata_lookup(&mut self) -> &mut <Self::Backend as TypeMetadata>::MetadataLookup; |
| 470 |
} |
| 471 |
|
| 472 |
/// A variant of the [`Connection`](trait.Connection.html) trait that is |
| 473 |
/// usable with dynamic dispatch |
| 474 |
/// |
| 475 |
/// If you are looking for a way to use pass database connections |
| 476 |
/// for different database backends around in your application |
| 477 |
/// this trait won't help you much. Normally you should only |
| 478 |
/// need to use this trait if you are interacting with a connection |
| 479 |
/// passed to a [`Migration`](../migration/trait.Migration.html) |
| 480 |
pub trait BoxableConnection<DB: Backend>: SimpleConnection + std::any::Any { |
| 481 |
/// Maps the current connection to `std::any::Any` |
| 482 |
#[diesel_derives::__diesel_public_if( |
| 483 |
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes" |
| 484 |
)] |
| 485 |
fn as_any(&self) -> &dyn std::any::Any; |
| 486 |
|
| 487 |
#[doc(hidden)] |
| 488 |
fn as_any_mut(&mut self) -> &mut dyn std::any::Any; |
| 489 |
} |
| 490 |
|
| 491 |
impl<C> BoxableConnection<C::Backend> for C |
| 492 |
where |
| 493 |
C: Connection + std::any::Any, |
| 494 |
{ |
| 495 |
fn as_any(&self) -> &dyn std::any::Any { |
| 496 |
self |
| 497 |
} |
| 498 |
|
| 499 |
fn as_any_mut(&mut self) -> &mut dyn std::any::Any { |
| 500 |
self |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
/// The default loading mode provided by a [`Connection`]. |
| 505 |
/// |
| 506 |
/// Checkout the documentation of concrete connection types for details about |
| 507 |
/// supported loading modes. |
| 508 |
/// |
| 509 |
/// All types implementing [`Connection`] should provide at least |
| 510 |
/// a single [`LoadConnection<DefaultLoadingMode>`](self::LoadConnection) |
| 511 |
/// implementation. |
| 512 |
#[derive(Debug, Copy, Clone)] |
| 513 |
pub struct DefaultLoadingMode; |
| 514 |
|
| 515 |
impl<DB: Backend + 'static> dyn BoxableConnection<DB> { |
| 516 |
/// Downcast the current connection to a specific connection |
| 517 |
/// type. |
| 518 |
/// |
| 519 |
/// This will return `None` if the underlying |
| 520 |
/// connection does not match the corresponding |
| 521 |
/// type, otherwise a reference to the underlying connection is returned |
| 522 |
pub fn downcast_ref<T>(&self) -> Option<&T> |
| 523 |
where |
| 524 |
T: Connection<Backend = DB> + 'static, |
| 525 |
{ |
| 526 |
self.as_any().downcast_ref::<T>() |
| 527 |
} |
| 528 |
|
| 529 |
/// Downcast the current connection to a specific mutable connection |
| 530 |
/// type. |
| 531 |
/// |
| 532 |
/// This will return `None` if the underlying |
| 533 |
/// connection does not match the corresponding |
| 534 |
/// type, otherwise a mutable reference to the underlying connection is returned |
| 535 |
pub fn downcast_mut<T>(&mut self) -> Option<&mut T> |
| 536 |
where |
| 537 |
T: Connection<Backend = DB> + 'static, |
| 538 |
{ |
| 539 |
self.as_any_mut().downcast_mut::<T>() |
| 540 |
} |
| 541 |
|
| 542 |
/// Check if the current connection is |
| 543 |
/// a specific connection type |
| 544 |
pub fn is<T>(&self) -> bool |
| 545 |
where |
| 546 |
T: Connection<Backend = DB> + 'static, |
| 547 |
{ |
| 548 |
self.as_any().is::<T>() |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
// These traits are considered private for different reasons: |
| 553 |
// |
| 554 |
// `ConnectionSealed` to control who can implement `Connection`, |
| 555 |
// so that we can later change the `Connection` trait |
| 556 |
// |
| 557 |
// `MultiConnectionHelper` is a workaround needed for the |
| 558 |
// `MultiConnection` derive. We might stabilize this trait with |
| 559 |
// the corresponding derive |
| 560 |
// |
| 561 |
// `ConnectionHelperType` as a workaround for the `LoadRowIter` |
| 562 |
// type def. That trait should not be used by any user outside of diesel, |
| 563 |
// it purely exists for backward compatibility reasons. |
| 564 |
pub(crate) mod private { |
| 565 |
|
| 566 |
/// This trait restricts who can implement `Connection` |
| 567 |
#[cfg_attr( |
| 568 |
diesel_docsrs, |
| 569 |
doc(cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")) |
| 570 |
)] |
| 571 |
pub trait ConnectionSealed {} |
| 572 |
|
| 573 |
/// This trait provides helper methods to convert a database lookup type |
| 574 |
/// to/from an `std::any::Any` reference. This is used internally by the `#[derive(MultiConnection)]` |
| 575 |
/// implementation |
| 576 |
#[cfg_attr( |
| 577 |
diesel_docsrs, |
| 578 |
doc(cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")) |
| 579 |
)] |
| 580 |
pub trait MultiConnectionHelper: super::Connection { |
| 581 |
/// Convert the lookup type to any |
| 582 |
fn to_any<'a>( |
| 583 |
lookup: &mut <Self::Backend as crate::sql_types::TypeMetadata>::MetadataLookup, |
| 584 |
) -> &mut (dyn std::any::Any + 'a); |
| 585 |
|
| 586 |
/// Get the lookup type from any |
| 587 |
fn from_any( |
| 588 |
lookup: &mut dyn std::any::Any, |
| 589 |
) -> Option<&mut <Self::Backend as crate::sql_types::TypeMetadata>::MetadataLookup>; |
| 590 |
} |
| 591 |
|
| 592 |
// These impls are only there for backward compatibility reasons |
| 593 |
// Remove them on the next breaking release |
| 594 |
#[allow(unreachable_pub)] // must be pub for the type def using this trait |
| 595 |
#[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))] |
| 596 |
pub trait ConnectionHelperType<DB, B>: super::LoadConnection<B, Backend = DB> { |
| 597 |
type Cursor<'conn, 'query> |
| 598 |
where |
| 599 |
Self: 'conn; |
| 600 |
} |
| 601 |
#[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))] |
| 602 |
impl<T, B> ConnectionHelperType<T::Backend, B> for T |
| 603 |
where |
| 604 |
T: super::LoadConnection<B>, |
| 605 |
{ |
| 606 |
type Cursor<'conn, 'query> |
| 607 |
= <T as super::LoadConnection<B>>::Cursor<'conn, 'query> |
| 608 |
where |
| 609 |
T: 'conn; |
| 610 |
} |
| 611 |
} |