2018-08-15 09:07:17 +00:00
|
|
|
//! Traits, utilities, and a macro for easy database connection pooling.
|
|
|
|
//!
|
2018-07-21 22:11:08 +00:00
|
|
|
//! # Overview
|
|
|
|
//!
|
|
|
|
//! This module provides traits, utilities, and a procedural macro that allows
|
|
|
|
//! you to easily connect your Rocket application to databases through
|
|
|
|
//! connection pools. A _database connection pool_ is a data structure that
|
|
|
|
//! maintains active database connections for later use in the application.
|
|
|
|
//! This implementation of connection pooling support is based on
|
2018-08-15 09:07:17 +00:00
|
|
|
//! [`r2d2`] and exposes connections through [request guards]. Databases are
|
2018-07-21 22:11:08 +00:00
|
|
|
//! individually configured through Rocket's regular configuration mechanisms: a
|
|
|
|
//! `Rocket.toml` file, environment variables, or procedurally.
|
|
|
|
//!
|
|
|
|
//! Connecting your Rocket application to a database using this library occurs
|
|
|
|
//! in three simple steps:
|
|
|
|
//!
|
|
|
|
//! 1. Configure your databases in `Rocket.toml`.
|
2018-08-15 09:07:17 +00:00
|
|
|
//! (see [Configuration](#configuration))
|
2018-07-21 22:11:08 +00:00
|
|
|
//! 2. Associate a request guard type and fairing with each database.
|
2018-08-15 09:07:17 +00:00
|
|
|
//! (see [Guard Types](#guard-types))
|
2018-07-21 22:11:08 +00:00
|
|
|
//! 3. Use the request guard to retrieve a connection in a handler.
|
2018-08-15 09:07:17 +00:00
|
|
|
//! (see [Handlers](#handlers))
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! For a list of supported databases, see [Provided Databases](#provided). This
|
|
|
|
//! support can be easily extended by implementing the [`Poolable`] trait. See
|
|
|
|
//! [Extending](#extending) for more.
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
|
|
|
//! ## Example
|
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! Before using this library, the feature corresponding to your database type
|
|
|
|
//! in `rocket_contrib` must be enabled:
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
|
|
|
//! ```toml
|
|
|
|
//! [dependencies.rocket_contrib]
|
2019-05-13 23:18:48 +00:00
|
|
|
//! version = "0.5.0-dev"
|
2018-07-21 22:11:08 +00:00
|
|
|
//! default-features = false
|
2018-08-15 09:07:17 +00:00
|
|
|
//! features = ["diesel_sqlite_pool"]
|
2018-07-21 22:11:08 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! See [Provided](#provided) for a list of supported database and their
|
|
|
|
//! associated feature name.
|
|
|
|
//!
|
2018-07-21 22:11:08 +00:00
|
|
|
//! In `Rocket.toml` or the equivalent via environment variables:
|
|
|
|
//!
|
|
|
|
//! ```toml
|
|
|
|
//! [global.databases]
|
|
|
|
//! sqlite_logs = { url = "/path/to/database.sqlite" }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! In your application's source code, one-time:
|
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! ```rust
|
2018-10-09 11:16:57 +00:00
|
|
|
//! #![feature(proc_macro_hygiene, decl_macro)]
|
|
|
|
//!
|
|
|
|
//! #[macro_use] extern crate rocket;
|
|
|
|
//! #[macro_use] extern crate rocket_contrib;
|
|
|
|
//!
|
|
|
|
//! # #[cfg(feature = "diesel_sqlite_pool")]
|
|
|
|
//! # mod test {
|
|
|
|
//! use rocket_contrib::databases::diesel;
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
|
|
|
//! #[database("sqlite_logs")]
|
|
|
|
//! struct LogsDbConn(diesel::SqliteConnection);
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! rocket::ignite()
|
|
|
|
//! .attach(LogsDbConn::fairing())
|
|
|
|
//! .launch();
|
|
|
|
//! }
|
2018-10-09 11:16:57 +00:00
|
|
|
//! # } fn main() {}
|
2018-07-21 22:11:08 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Whenever a connection to the database is needed:
|
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! ```rust
|
2018-10-06 04:56:46 +00:00
|
|
|
//! # #![feature(proc_macro_hygiene, decl_macro)]
|
2018-08-15 09:07:17 +00:00
|
|
|
//! #
|
2018-10-05 04:44:42 +00:00
|
|
|
//! # #[macro_use] extern crate rocket;
|
2018-10-09 11:16:57 +00:00
|
|
|
//! # #[macro_use] extern crate rocket_contrib;
|
2018-08-15 09:07:17 +00:00
|
|
|
//! #
|
2018-10-09 11:16:57 +00:00
|
|
|
//! # #[cfg(feature = "diesel_sqlite_pool")]
|
|
|
|
//! # mod test {
|
|
|
|
//! # use rocket_contrib::databases::diesel;
|
2018-08-15 09:07:17 +00:00
|
|
|
//! #
|
|
|
|
//! # #[database("sqlite_logs")]
|
|
|
|
//! # struct LogsDbConn(diesel::SqliteConnection);
|
|
|
|
//! #
|
|
|
|
//! # type Logs = ();
|
2019-06-13 02:17:59 +00:00
|
|
|
//! # type Result<T> = std::result::Result<T, ()>;
|
2018-08-15 09:07:17 +00:00
|
|
|
//! #
|
2018-07-21 22:11:08 +00:00
|
|
|
//! #[get("/logs/<id>")]
|
2018-08-15 09:07:17 +00:00
|
|
|
//! fn get_logs(conn: LogsDbConn, id: usize) -> Result<Logs> {
|
|
|
|
//! # /*
|
2018-12-11 06:20:28 +00:00
|
|
|
//! Logs::by_id(&*conn, id)
|
2018-08-15 09:07:17 +00:00
|
|
|
//! # */
|
|
|
|
//! # Ok(())
|
2018-07-21 22:11:08 +00:00
|
|
|
//! }
|
2018-10-09 11:16:57 +00:00
|
|
|
//! # } fn main() {}
|
2018-07-21 22:11:08 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! # Usage
|
|
|
|
//!
|
|
|
|
//! ## Configuration
|
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! Databases can be configured via various mechanisms: `Rocket.toml`,
|
|
|
|
//! procedurally via `rocket::custom()`, or via environment variables.
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! ### `Rocket.toml`
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! To configure a database via `Rocket.toml`, add a table for each database
|
|
|
|
//! to the `databases` table where the key is a name of your choice. The table
|
|
|
|
//! should have a `url` key and, optionally, a `pool_size` key. This looks as
|
|
|
|
//! follows:
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
|
|
|
//! ```toml
|
2018-12-11 06:20:28 +00:00
|
|
|
//! # Option 1:
|
2018-07-21 22:11:08 +00:00
|
|
|
//! [global.databases]
|
2018-08-15 09:07:17 +00:00
|
|
|
//! sqlite_db = { url = "db.sqlite" }
|
|
|
|
//!
|
2018-12-11 06:20:28 +00:00
|
|
|
//! # Option 2:
|
2019-03-11 09:40:01 +00:00
|
|
|
//! [global.databases.my_db]
|
|
|
|
//! url = "mysql://root:root@localhost/my_db"
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
2018-12-11 06:20:28 +00:00
|
|
|
//! # With a `pool_size` key:
|
2018-08-15 09:07:17 +00:00
|
|
|
//! [global.databases]
|
|
|
|
//! sqlite_db = { url = "db.sqlite", pool_size = 20 }
|
2018-07-21 22:11:08 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! The table _requires_ one key:
|
|
|
|
//!
|
|
|
|
//! * `url` - the URl to the database
|
|
|
|
//!
|
|
|
|
//! Additionally, all configurations accept the following _optional_ keys:
|
|
|
|
//!
|
|
|
|
//! * `pool_size` - the size of the pool, i.e., the number of connections to
|
|
|
|
//! pool (defaults to the configured number of workers)
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! Additional options may be required or supported by other adapters.
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! ### Procedurally
|
|
|
|
//!
|
|
|
|
//! Databases can also be configured procedurally database via
|
|
|
|
//! `rocket::custom()`. The example below does just this:
|
|
|
|
//!
|
|
|
|
//! ```rust
|
2018-07-21 22:11:08 +00:00
|
|
|
//! extern crate rocket;
|
|
|
|
//!
|
2018-10-09 11:16:57 +00:00
|
|
|
//! # #[cfg(feature = "diesel_sqlite_pool")]
|
|
|
|
//! # mod test {
|
2018-07-21 22:11:08 +00:00
|
|
|
//! use std::collections::HashMap;
|
|
|
|
//! use rocket::config::{Config, Environment, Value};
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! let mut database_config = HashMap::new();
|
|
|
|
//! let mut databases = HashMap::new();
|
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! // This is the same as the following TOML:
|
|
|
|
//! // my_db = { url = "database.sqlite" }
|
2018-07-21 22:11:08 +00:00
|
|
|
//! database_config.insert("url", Value::from("database.sqlite"));
|
|
|
|
//! databases.insert("my_db", Value::from(database_config));
|
|
|
|
//!
|
|
|
|
//! let config = Config::build(Environment::Development)
|
|
|
|
//! .extra("databases", databases)
|
|
|
|
//! .finalize()
|
|
|
|
//! .unwrap();
|
|
|
|
//!
|
|
|
|
//! rocket::custom(config).launch();
|
|
|
|
//! }
|
2018-10-09 11:16:57 +00:00
|
|
|
//! # } fn main() {}
|
2018-07-21 22:11:08 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! ### Environment Variables
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! Lastly, databases can be configured via environment variables by specifying
|
|
|
|
//! the `databases` table as detailed in the [Environment Variables
|
|
|
|
//! configuration
|
2019-05-13 23:18:48 +00:00
|
|
|
//! guide](https://rocket.rs/v0.5/guide/configuration/#environment-variables):
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
|
|
|
//! ```bash
|
2019-02-07 18:21:07 +00:00
|
|
|
//! ROCKET_DATABASES='{my_db={url="db.sqlite"}}'
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Multiple databases can be specified in the `ROCKET_DATABASES` environment variable
|
|
|
|
//! as well by comma separating them:
|
|
|
|
//!
|
|
|
|
//! ```bash
|
|
|
|
//! ROCKET_DATABASES='{my_db={url="db.sqlite"},my_pg_db={url="postgres://root:root@localhost/my_pg_db"}}'
|
2018-07-21 22:11:08 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! ## Guard Types
|
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! Once a database has been configured, the `#[database]` attribute can be used
|
|
|
|
//! to tie a type in your application to a configured database. The database
|
|
|
|
//! attributes accepts a single string parameter that indicates the name of the
|
|
|
|
//! database. This corresponds to the database name set as the database's
|
|
|
|
//! configuration key.
|
|
|
|
//!
|
|
|
|
//! The attribute can only be applied to unit-like structs with one type. The
|
|
|
|
//! internal type of the structure must implement [`Poolable`].
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! # extern crate rocket;
|
2018-10-09 11:16:57 +00:00
|
|
|
//! # #[macro_use] extern crate rocket_contrib;
|
|
|
|
//! # #[cfg(feature = "diesel_sqlite_pool")]
|
|
|
|
//! # mod test {
|
|
|
|
//! use rocket_contrib::databases::diesel;
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
|
|
|
//! #[database("my_db")]
|
|
|
|
//! struct MyDatabase(diesel::SqliteConnection);
|
2018-10-09 11:16:57 +00:00
|
|
|
//! # }
|
2018-07-21 22:11:08 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
2019-02-07 18:21:07 +00:00
|
|
|
//! Other databases can be used by specifying their respective [`Poolable`]
|
|
|
|
//! type:
|
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! # extern crate rocket;
|
|
|
|
//! # #[macro_use] extern crate rocket_contrib;
|
|
|
|
//! # #[cfg(feature = "postgres_pool")]
|
|
|
|
//! # mod test {
|
|
|
|
//! use rocket_contrib::databases::postgres;
|
|
|
|
//!
|
|
|
|
//! #[database("my_pg_db")]
|
|
|
|
//! struct MyPgDatabase(postgres::Connection);
|
|
|
|
//! # }
|
|
|
|
//! ```
|
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! The macro generates a [`FromRequest`] implementation for the decorated type,
|
|
|
|
//! allowing the type to be used as a request guard. This implementation
|
|
|
|
//! retrieves a connection from the database pool or fails with a
|
|
|
|
//! `Status::ServiceUnavailable` if no connections are available. The macro also
|
2019-06-13 02:17:59 +00:00
|
|
|
//! generates an implementation of the [`Deref`](std::ops::Deref) trait with
|
2018-08-15 09:07:17 +00:00
|
|
|
//! the internal `Poolable` type as the target.
|
|
|
|
//!
|
|
|
|
//! The macro will also generate two inherent methods on the decorated type:
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! * `fn fairing() -> impl Fairing`
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! Returns a fairing that initializes the associated database connection
|
|
|
|
//! pool.
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! * `fn get_one(&Rocket) -> Option<Self>`
|
|
|
|
//!
|
|
|
|
//! Retrieves a connection from the configured pool. Returns `Some` as long
|
|
|
|
//! as `Self::fairing()` has been attached and there is at least one
|
|
|
|
//! connection in the pool.
|
|
|
|
//!
|
|
|
|
//! The fairing returned from the generated `fairing()` method _must_ be
|
|
|
|
//! attached for the request guard implementation to succeed. Putting the pieces
|
|
|
|
//! together, a use of the `#[database]` attribute looks as follows:
|
|
|
|
//!
|
|
|
|
//! ```rust
|
2018-07-21 22:11:08 +00:00
|
|
|
//! # extern crate rocket;
|
2018-10-09 11:16:57 +00:00
|
|
|
//! # #[macro_use] extern crate rocket_contrib;
|
2018-07-21 22:11:08 +00:00
|
|
|
//! #
|
2018-10-09 11:16:57 +00:00
|
|
|
//! # #[cfg(feature = "diesel_sqlite_pool")]
|
|
|
|
//! # mod test {
|
2018-07-21 22:11:08 +00:00
|
|
|
//! # use std::collections::HashMap;
|
|
|
|
//! # use rocket::config::{Config, Environment, Value};
|
|
|
|
//! #
|
2018-10-09 11:16:57 +00:00
|
|
|
//! use rocket_contrib::databases::diesel;
|
2018-08-15 09:07:17 +00:00
|
|
|
//!
|
2018-07-21 22:11:08 +00:00
|
|
|
//! #[database("my_db")]
|
|
|
|
//! struct MyDatabase(diesel::SqliteConnection);
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! # let mut db_config = HashMap::new();
|
|
|
|
//! # let mut databases = HashMap::new();
|
|
|
|
//! #
|
|
|
|
//! # db_config.insert("url", Value::from("database.sqlite"));
|
|
|
|
//! # db_config.insert("pool_size", Value::from(10));
|
|
|
|
//! # databases.insert("my_db", Value::from(db_config));
|
|
|
|
//! #
|
|
|
|
//! # let config = Config::build(Environment::Development)
|
|
|
|
//! # .extra("databases", databases)
|
|
|
|
//! # .finalize()
|
|
|
|
//! # .unwrap();
|
|
|
|
//! #
|
|
|
|
//! rocket::custom(config)
|
2018-08-15 09:07:17 +00:00
|
|
|
//! .attach(MyDatabase::fairing())
|
2018-07-21 22:11:08 +00:00
|
|
|
//! .launch();
|
|
|
|
//! }
|
2018-10-09 11:16:57 +00:00
|
|
|
//! # } fn main() {}
|
2018-07-21 22:11:08 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! ## Handlers
|
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! Finally, simply use your type as a request guard in a handler to retrieve a
|
|
|
|
//! connection to a given database:
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! ```rust
|
2018-10-06 04:56:46 +00:00
|
|
|
//! # #![feature(proc_macro_hygiene, decl_macro)]
|
2018-08-15 09:07:17 +00:00
|
|
|
//! #
|
2018-10-05 04:44:42 +00:00
|
|
|
//! # #[macro_use] extern crate rocket;
|
2018-10-09 11:16:57 +00:00
|
|
|
//! # #[macro_use] extern crate rocket_contrib;
|
|
|
|
//! #
|
|
|
|
//! # #[cfg(feature = "diesel_sqlite_pool")]
|
|
|
|
//! # mod test {
|
|
|
|
//! # use rocket_contrib::databases::diesel;
|
2018-08-15 09:07:17 +00:00
|
|
|
//! #[database("my_db")]
|
|
|
|
//! struct MyDatabase(diesel::SqliteConnection);
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
|
|
|
//! #[get("/")]
|
|
|
|
//! fn my_handler(conn: MyDatabase) {
|
2018-10-09 11:16:57 +00:00
|
|
|
//! // ...
|
2018-07-21 22:11:08 +00:00
|
|
|
//! }
|
2018-10-09 11:16:57 +00:00
|
|
|
//! # }
|
2018-07-21 22:11:08 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! The generated `Deref` implementation allows easy access to the inner
|
|
|
|
//! connection type:
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! ```rust
|
2018-10-06 04:56:46 +00:00
|
|
|
//! # #![feature(proc_macro_hygiene, decl_macro)]
|
2018-08-15 09:07:17 +00:00
|
|
|
//! #
|
2018-10-05 04:44:42 +00:00
|
|
|
//! # #[macro_use] extern crate rocket;
|
2018-10-09 11:16:57 +00:00
|
|
|
//! # #[macro_use] extern crate rocket_contrib;
|
2018-08-15 09:07:17 +00:00
|
|
|
//! #
|
2018-10-09 11:16:57 +00:00
|
|
|
//! # #[cfg(feature = "diesel_sqlite_pool")]
|
|
|
|
//! # mod test {
|
|
|
|
//! # use rocket_contrib::databases::diesel;
|
2018-08-15 09:07:17 +00:00
|
|
|
//! # type Data = ();
|
|
|
|
//! #[database("my_db")]
|
|
|
|
//! struct MyDatabase(diesel::SqliteConnection);
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! fn load_from_db(conn: &diesel::SqliteConnection) -> Data {
|
|
|
|
//! // Do something with connection, return some data.
|
|
|
|
//! # ()
|
|
|
|
//! }
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! #[get("/")]
|
|
|
|
//! fn my_handler(conn: MyDatabase) -> Data {
|
2018-12-11 06:20:28 +00:00
|
|
|
//! load_from_db(&*conn)
|
2018-08-15 09:07:17 +00:00
|
|
|
//! }
|
2018-10-09 11:16:57 +00:00
|
|
|
//! # }
|
2018-08-15 09:07:17 +00:00
|
|
|
//! ```
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
|
|
|
//! # Database Support
|
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! Built-in support is provided for many popular databases and drivers. Support
|
|
|
|
//! can be easily extended by [`Poolable`] implementations.
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
|
|
|
//! ## Provided
|
|
|
|
//!
|
2018-08-15 09:07:17 +00:00
|
|
|
//! The list below includes all presently supported database adapters and their
|
|
|
|
//! corresponding [`Poolable`] type.
|
|
|
|
//!
|
2018-10-06 13:25:17 +00:00
|
|
|
//! | Kind | Driver | `Poolable` Type | Feature |
|
2018-08-15 09:07:17 +00:00
|
|
|
//! |----------|-----------------------|--------------------------------|------------------------|
|
|
|
|
//! | MySQL | [Diesel] | [`diesel::MysqlConnection`] | `diesel_mysql_pool` |
|
2018-10-06 13:25:17 +00:00
|
|
|
//! | MySQL | [`rust-mysql-simple`] | [`mysql::Conn`] | `mysql_pool` |
|
2018-08-15 09:07:17 +00:00
|
|
|
//! | Postgres | [Diesel] | [`diesel::PgConnection`] | `diesel_postgres_pool` |
|
|
|
|
//! | Postgres | [Rust-Postgres] | [`postgres::Connection`] | `postgres_pool` |
|
|
|
|
//! | Sqlite | [Diesel] | [`diesel::SqliteConnection`] | `diesel_sqlite_pool` |
|
2018-10-06 13:25:17 +00:00
|
|
|
//! | Sqlite | [Rustqlite] | [`rusqlite::Connection`] | `sqlite_pool` |
|
2018-08-15 09:07:17 +00:00
|
|
|
//! | Neo4j | [`rusted_cypher`] | [`rusted_cypher::GraphClient`] | `cypher_pool` |
|
|
|
|
//! | Redis | [`redis-rs`] | [`redis::Connection`] | `redis_pool` |
|
2018-11-28 20:01:21 +00:00
|
|
|
//! | MongoDB | [`mongodb`] | [`mongodb::db::Database`] | `mongodb_pool` |
|
2019-01-04 16:19:09 +00:00
|
|
|
//! | Memcache | [`memcache`] | [`memcache::Client`] | `memcache_pool` |
|
2018-08-15 09:07:17 +00:00
|
|
|
//!
|
|
|
|
//! [Diesel]: https://diesel.rs
|
|
|
|
//! [`redis::Connection`]: https://docs.rs/redis/0.9.0/redis/struct.Connection.html
|
|
|
|
//! [`rusted_cypher::GraphClient`]: https://docs.rs/rusted_cypher/1.1.0/rusted_cypher/graph/struct.GraphClient.html
|
2019-01-27 15:52:03 +00:00
|
|
|
//! [`rusqlite::Connection`]: https://docs.rs/rusqlite/0.16.0/rusqlite/struct.Connection.html
|
2018-08-15 09:07:17 +00:00
|
|
|
//! [`diesel::SqliteConnection`]: http://docs.diesel.rs/diesel/prelude/struct.SqliteConnection.html
|
|
|
|
//! [`postgres::Connection`]: https://docs.rs/postgres/0.15.2/postgres/struct.Connection.html
|
|
|
|
//! [`diesel::PgConnection`]: http://docs.diesel.rs/diesel/pg/struct.PgConnection.html
|
|
|
|
//! [`mysql::conn`]: https://docs.rs/mysql/14.0.0/mysql/struct.Conn.html
|
|
|
|
//! [`diesel::MysqlConnection`]: http://docs.diesel.rs/diesel/mysql/struct.MysqlConnection.html
|
|
|
|
//! [`redis-rs`]: https://github.com/mitsuhiko/redis-rs
|
|
|
|
//! [`rusted_cypher`]: https://github.com/livioribeiro/rusted-cypher
|
2018-10-06 13:25:17 +00:00
|
|
|
//! [Rustqlite]: https://github.com/jgallagher/rusqlite
|
2018-08-15 09:07:17 +00:00
|
|
|
//! [Rust-Postgres]: https://github.com/sfackler/rust-postgres
|
|
|
|
//! [`rust-mysql-simple`]: https://github.com/blackbeam/rust-mysql-simple
|
|
|
|
//! [`diesel::PgConnection`]: http://docs.diesel.rs/diesel/pg/struct.PgConnection.html
|
2018-11-28 20:01:21 +00:00
|
|
|
//! [`mongodb`]: https://github.com/mongodb-labs/mongo-rust-driver-prototype
|
|
|
|
//! [`mongodb::db::Database`]: https://docs.rs/mongodb/0.3.12/mongodb/db/type.Database.html
|
2019-01-04 16:19:09 +00:00
|
|
|
//! [`memcache`]: https://github.com/aisk/rust-memcache
|
|
|
|
//! [`memcache::Client`]: https://docs.rs/memcache/0.11.0/memcache/struct.Client.html
|
2018-08-15 09:07:17 +00:00
|
|
|
//!
|
2018-07-21 22:11:08 +00:00
|
|
|
//! The above table lists all the supported database adapters in this library.
|
|
|
|
//! In order to use particular `Poolable` type that's included in this library,
|
2018-08-15 09:07:17 +00:00
|
|
|
//! you must first enable the feature listed in the "Feature" column. The
|
|
|
|
//! interior type of your decorated database type should match the type in the
|
|
|
|
//! "`Poolable` Type" column.
|
2018-07-21 22:11:08 +00:00
|
|
|
//!
|
|
|
|
//! ## Extending
|
|
|
|
//!
|
|
|
|
//! Extending Rocket's support to your own custom database adapter (or other
|
2018-08-15 09:07:17 +00:00
|
|
|
//! database-like struct that can be pooled by `r2d2`) is as easy as
|
|
|
|
//! implementing the [`Poolable`] trait. See the documentation for [`Poolable`]
|
|
|
|
//! for more details on how to implement it.
|
2018-10-06 13:25:17 +00:00
|
|
|
//!
|
2018-10-07 00:24:11 +00:00
|
|
|
//! [`FromRequest`]: rocket::request::FromRequest
|
|
|
|
//! [request guards]: rocket::request::FromRequest
|
2018-10-06 13:25:17 +00:00
|
|
|
//! [`Poolable`]: databases::Poolable
|
2018-07-21 22:11:08 +00:00
|
|
|
|
|
|
|
pub extern crate r2d2;
|
|
|
|
|
2018-10-07 00:24:11 +00:00
|
|
|
#[cfg(any(feature = "diesel_sqlite_pool",
|
|
|
|
feature = "diesel_postgres_pool",
|
|
|
|
feature = "diesel_mysql_pool"))]
|
|
|
|
pub extern crate diesel;
|
|
|
|
|
2018-07-21 22:11:08 +00:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::fmt::{self, Display, Formatter};
|
|
|
|
use std::marker::{Send, Sized};
|
|
|
|
|
|
|
|
use rocket::config::{self, Value};
|
|
|
|
|
|
|
|
use self::r2d2::ManageConnection;
|
|
|
|
|
2018-10-07 00:24:11 +00:00
|
|
|
#[doc(hidden)] pub use rocket_contrib_codegen::*;
|
2018-07-21 22:11:08 +00:00
|
|
|
|
2018-10-07 00:24:11 +00:00
|
|
|
#[cfg(feature = "postgres_pool")] pub extern crate postgres;
|
|
|
|
#[cfg(feature = "postgres_pool")] pub extern crate r2d2_postgres;
|
2018-07-21 22:11:08 +00:00
|
|
|
|
2018-10-07 00:24:11 +00:00
|
|
|
#[cfg(feature = "mysql_pool")] pub extern crate mysql;
|
|
|
|
#[cfg(feature = "mysql_pool")] pub extern crate r2d2_mysql;
|
2018-07-21 22:11:08 +00:00
|
|
|
|
2018-10-07 00:24:11 +00:00
|
|
|
#[cfg(feature = "sqlite_pool")] pub extern crate rusqlite;
|
|
|
|
#[cfg(feature = "sqlite_pool")] pub extern crate r2d2_sqlite;
|
2018-07-21 22:11:08 +00:00
|
|
|
|
2018-10-07 00:24:11 +00:00
|
|
|
#[cfg(feature = "cypher_pool")] pub extern crate rusted_cypher;
|
|
|
|
#[cfg(feature = "cypher_pool")] pub extern crate r2d2_cypher;
|
2018-07-21 22:11:08 +00:00
|
|
|
|
2018-10-07 00:24:11 +00:00
|
|
|
#[cfg(feature = "redis_pool")] pub extern crate redis;
|
|
|
|
#[cfg(feature = "redis_pool")] pub extern crate r2d2_redis;
|
2018-07-21 22:11:08 +00:00
|
|
|
|
2018-11-28 20:01:21 +00:00
|
|
|
#[cfg(feature = "mongodb_pool")] pub extern crate mongodb;
|
|
|
|
#[cfg(feature = "mongodb_pool")] pub extern crate r2d2_mongodb;
|
|
|
|
|
2019-01-04 16:19:09 +00:00
|
|
|
#[cfg(feature = "memcache_pool")] pub extern crate memcache;
|
|
|
|
#[cfg(feature = "memcache_pool")] pub extern crate r2d2_memcache;
|
|
|
|
|
2018-08-15 09:07:17 +00:00
|
|
|
/// A structure representing a particular database configuration.
|
2018-07-21 22:11:08 +00:00
|
|
|
///
|
|
|
|
/// For the following configuration:
|
|
|
|
///
|
|
|
|
/// ```toml
|
2018-08-15 09:07:17 +00:00
|
|
|
/// [global.databases.my_database]
|
2018-08-16 19:20:23 +00:00
|
|
|
/// url = "postgres://root:root@localhost/my_database"
|
2018-07-21 22:11:08 +00:00
|
|
|
/// pool_size = 10
|
|
|
|
/// certs = "sample_cert.pem"
|
|
|
|
/// key = "key.pem"
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The following structure would be generated after calling
|
2018-08-15 09:07:17 +00:00
|
|
|
/// [`database_config`]`("my_database", &config)`:
|
2018-07-21 22:11:08 +00:00
|
|
|
///
|
2018-08-15 09:07:17 +00:00
|
|
|
/// ```rust,ignore
|
2018-07-21 22:11:08 +00:00
|
|
|
/// DatabaseConfig {
|
|
|
|
/// url: "dummy_db.sqlite",
|
|
|
|
/// pool_size: 10,
|
|
|
|
/// extras: {
|
|
|
|
/// "certs": String("certs.pem"),
|
2018-08-15 09:07:17 +00:00
|
|
|
/// "key": String("key.pem"),
|
|
|
|
/// },
|
2018-07-21 22:11:08 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub struct DatabaseConfig<'a> {
|
|
|
|
/// The connection URL specified in the Rocket configuration.
|
|
|
|
pub url: &'a str,
|
|
|
|
/// The size of the pool to be initialized. Defaults to the number of
|
|
|
|
/// Rocket workers.
|
|
|
|
pub pool_size: u32,
|
|
|
|
/// Any extra options that are included in the configuration, **excluding**
|
|
|
|
/// the url and pool_size.
|
|
|
|
pub extras: BTreeMap<String, Value>,
|
|
|
|
}
|
|
|
|
|
2018-08-15 09:07:17 +00:00
|
|
|
/// A wrapper around `r2d2::Error`s or a custom database error type.
|
2018-07-21 22:11:08 +00:00
|
|
|
///
|
2018-08-15 09:07:17 +00:00
|
|
|
/// This type is only relevant to implementors of the [`Poolable`] trait. See
|
|
|
|
/// the [`Poolable`] documentation for more information on how to use this type.
|
2018-07-21 22:11:08 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum DbError<T> {
|
|
|
|
/// The custom error type to wrap alongside `r2d2::Error`.
|
|
|
|
Custom(T),
|
|
|
|
/// The error returned by an r2d2 pool.
|
|
|
|
PoolError(r2d2::Error),
|
|
|
|
}
|
|
|
|
|
2018-08-15 09:07:17 +00:00
|
|
|
/// Error returned on invalid database configurations.
|
2018-07-21 22:11:08 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
2018-10-07 00:24:11 +00:00
|
|
|
pub enum ConfigError {
|
2018-08-15 09:07:17 +00:00
|
|
|
/// The `databases` configuration key is missing or is empty.
|
2018-07-21 22:11:08 +00:00
|
|
|
MissingTable,
|
2018-08-15 09:07:17 +00:00
|
|
|
/// The requested database configuration key is missing from the active
|
2018-07-21 22:11:08 +00:00
|
|
|
/// configuration.
|
|
|
|
MissingKey,
|
2018-08-15 09:07:17 +00:00
|
|
|
/// The configuration associated with the key isn't a
|
2019-06-13 02:17:59 +00:00
|
|
|
/// [`Table`](rocket::config::Table).
|
2018-07-21 22:11:08 +00:00
|
|
|
MalformedConfiguration,
|
2018-08-15 09:07:17 +00:00
|
|
|
/// The required `url` key is missing.
|
2018-07-21 22:11:08 +00:00
|
|
|
MissingUrl,
|
2018-08-15 09:07:17 +00:00
|
|
|
/// The value for `url` isn't a string.
|
2018-07-21 22:11:08 +00:00
|
|
|
MalformedUrl,
|
2018-08-15 09:07:17 +00:00
|
|
|
/// The `pool_size` exceeds `u32::max_value()` or is negative.
|
2018-07-21 22:11:08 +00:00
|
|
|
InvalidPoolSize(i64),
|
|
|
|
}
|
|
|
|
|
2018-08-15 09:07:17 +00:00
|
|
|
/// Retrieves the database configuration for the database named `name`.
|
2018-07-21 22:11:08 +00:00
|
|
|
///
|
2018-08-15 09:07:17 +00:00
|
|
|
/// This function is primarily used by the code generated by the `#[database]`
|
|
|
|
/// attribute.
|
2018-07-21 22:11:08 +00:00
|
|
|
///
|
2018-08-15 09:07:17 +00:00
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// Consider the following configuration:
|
2018-07-21 22:11:08 +00:00
|
|
|
///
|
|
|
|
/// ```toml
|
2018-08-15 09:07:17 +00:00
|
|
|
/// [global.databases]
|
2018-07-21 22:11:08 +00:00
|
|
|
/// my_db = { url = "db/db.sqlite", pool_size = 25 }
|
|
|
|
/// my_other_db = { url = "mysql://root:root@localhost/database" }
|
|
|
|
/// ```
|
|
|
|
///
|
2018-08-15 09:07:17 +00:00
|
|
|
/// The following example uses `database_config` to retrieve the configurations
|
|
|
|
/// for the `my_db` and `my_other_db` databases:
|
2018-07-21 22:11:08 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate rocket;
|
|
|
|
/// # extern crate rocket_contrib;
|
|
|
|
/// #
|
|
|
|
/// # use std::{collections::BTreeMap, mem::drop};
|
|
|
|
/// # use rocket::{fairing::AdHoc, config::{Config, Environment, Value}};
|
2018-10-07 00:24:11 +00:00
|
|
|
/// use rocket_contrib::databases::{database_config, ConfigError};
|
2018-07-21 22:11:08 +00:00
|
|
|
///
|
|
|
|
/// # let mut databases = BTreeMap::new();
|
|
|
|
/// #
|
|
|
|
/// # let mut my_db = BTreeMap::new();
|
|
|
|
/// # my_db.insert("url".to_string(), Value::from("db/db.sqlite"));
|
|
|
|
/// # my_db.insert("pool_size".to_string(), Value::from(25));
|
|
|
|
/// #
|
|
|
|
/// # let mut my_other_db = BTreeMap::new();
|
2018-08-15 09:07:17 +00:00
|
|
|
/// # my_other_db.insert("url".to_string(),
|
|
|
|
/// # Value::from("mysql://root:root@localhost/database"));
|
2018-07-21 22:11:08 +00:00
|
|
|
/// #
|
|
|
|
/// # databases.insert("my_db".to_string(), Value::from(my_db));
|
|
|
|
/// # databases.insert("my_other_db".to_string(), Value::from(my_other_db));
|
|
|
|
/// #
|
2018-08-15 09:07:17 +00:00
|
|
|
/// # let config = Config::build(Environment::Development)
|
|
|
|
/// # .extra("databases", databases)
|
|
|
|
/// # .expect("custom config okay");
|
2018-07-21 22:11:08 +00:00
|
|
|
/// #
|
2018-08-15 09:07:17 +00:00
|
|
|
/// # rocket::custom(config).attach(AdHoc::on_attach("Testing", |rocket| {
|
|
|
|
/// # {
|
|
|
|
/// let config = database_config("my_db", rocket.config()).unwrap();
|
2018-07-21 22:11:08 +00:00
|
|
|
/// assert_eq!(config.url, "db/db.sqlite");
|
|
|
|
/// assert_eq!(config.pool_size, 25);
|
|
|
|
///
|
2018-08-15 09:07:17 +00:00
|
|
|
/// let other_config = database_config("my_other_db", rocket.config()).unwrap();
|
2018-07-21 22:11:08 +00:00
|
|
|
/// assert_eq!(other_config.url, "mysql://root:root@localhost/database");
|
|
|
|
///
|
2018-08-15 09:07:17 +00:00
|
|
|
/// let error = database_config("invalid_db", rocket.config()).unwrap_err();
|
2018-10-07 00:24:11 +00:00
|
|
|
/// assert_eq!(error, ConfigError::MissingKey);
|
2018-08-15 09:07:17 +00:00
|
|
|
/// # }
|
2018-07-21 22:11:08 +00:00
|
|
|
/// #
|
|
|
|
/// # Ok(rocket)
|
|
|
|
/// # }));
|
|
|
|
/// ```
|
|
|
|
pub fn database_config<'a>(
|
|
|
|
name: &str,
|
|
|
|
from: &'a config::Config
|
2018-10-07 00:24:11 +00:00
|
|
|
) -> Result<DatabaseConfig<'a>, ConfigError> {
|
2018-07-21 22:11:08 +00:00
|
|
|
// Find the first `databases` config that's a table with a key of 'name'
|
|
|
|
// equal to `name`.
|
|
|
|
let connection_config = from.get_table("databases")
|
2018-10-07 00:24:11 +00:00
|
|
|
.map_err(|_| ConfigError::MissingTable)?
|
2018-07-21 22:11:08 +00:00
|
|
|
.get(name)
|
2018-10-07 00:24:11 +00:00
|
|
|
.ok_or(ConfigError::MissingKey)?
|
2018-07-21 22:11:08 +00:00
|
|
|
.as_table()
|
2018-10-07 00:24:11 +00:00
|
|
|
.ok_or(ConfigError::MalformedConfiguration)?;
|
2018-07-21 22:11:08 +00:00
|
|
|
|
|
|
|
let maybe_url = connection_config.get("url")
|
2018-10-07 00:24:11 +00:00
|
|
|
.ok_or(ConfigError::MissingUrl)?;
|
2018-07-21 22:11:08 +00:00
|
|
|
|
2018-10-07 00:24:11 +00:00
|
|
|
let url = maybe_url.as_str().ok_or(ConfigError::MalformedUrl)?;
|
2018-07-21 22:11:08 +00:00
|
|
|
|
|
|
|
let pool_size = connection_config.get("pool_size")
|
|
|
|
.and_then(Value::as_integer)
|
|
|
|
.unwrap_or(from.workers as i64);
|
|
|
|
|
|
|
|
if pool_size < 1 || pool_size > u32::max_value() as i64 {
|
2018-10-07 00:24:11 +00:00
|
|
|
return Err(ConfigError::InvalidPoolSize(pool_size));
|
2018-07-21 22:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut extras = connection_config.clone();
|
|
|
|
extras.remove("url");
|
|
|
|
extras.remove("pool_size");
|
|
|
|
|
|
|
|
Ok(DatabaseConfig { url, pool_size: pool_size as u32, extras: extras })
|
|
|
|
}
|
|
|
|
|
2018-10-07 00:24:11 +00:00
|
|
|
impl<'a> Display for ConfigError {
|
2019-06-13 02:17:59 +00:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
2018-07-21 22:11:08 +00:00
|
|
|
match self {
|
2018-10-07 00:24:11 +00:00
|
|
|
ConfigError::MissingTable => {
|
2018-07-21 22:11:08 +00:00
|
|
|
write!(f, "A table named `databases` was not found for this configuration")
|
|
|
|
},
|
2018-10-07 00:24:11 +00:00
|
|
|
ConfigError::MissingKey => {
|
2018-07-21 22:11:08 +00:00
|
|
|
write!(f, "An entry in the `databases` table was not found for this key")
|
|
|
|
},
|
2018-10-07 00:24:11 +00:00
|
|
|
ConfigError::MalformedConfiguration => {
|
2018-07-21 22:11:08 +00:00
|
|
|
write!(f, "The configuration for this database is malformed")
|
|
|
|
}
|
2018-10-07 00:24:11 +00:00
|
|
|
ConfigError::MissingUrl => {
|
2018-07-21 22:11:08 +00:00
|
|
|
write!(f, "The connection URL is missing for this database")
|
|
|
|
},
|
2018-10-07 00:24:11 +00:00
|
|
|
ConfigError::MalformedUrl => {
|
2018-07-21 22:11:08 +00:00
|
|
|
write!(f, "The specified connection URL is malformed")
|
|
|
|
},
|
2018-10-07 00:24:11 +00:00
|
|
|
ConfigError::InvalidPoolSize(invalid_size) => {
|
2018-07-21 22:11:08 +00:00
|
|
|
write!(f, "'{}' is not a valid value for `pool_size`", invalid_size)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-15 09:07:17 +00:00
|
|
|
/// Trait implemented by `r2d2`-based database adapters.
|
2018-07-21 22:11:08 +00:00
|
|
|
///
|
|
|
|
/// # Provided Implementations
|
|
|
|
///
|
2018-08-15 09:07:17 +00:00
|
|
|
/// Implementations of `Poolable` are provided for the following types:
|
2018-07-21 22:11:08 +00:00
|
|
|
///
|
2018-08-15 09:07:17 +00:00
|
|
|
/// * `diesel::MysqlConnection`
|
|
|
|
/// * `diesel::PgConnection`
|
|
|
|
/// * `diesel::SqliteConnection`
|
|
|
|
/// * `postgres::Connection`
|
|
|
|
/// * `mysql::Conn`
|
|
|
|
/// * `rusqlite::Connection`
|
|
|
|
/// * `rusted_cypher::GraphClient`
|
|
|
|
/// * `redis::Connection`
|
2018-07-21 22:11:08 +00:00
|
|
|
///
|
|
|
|
/// # Implementation Guide
|
|
|
|
///
|
|
|
|
/// As a r2d2-compatible database (or other resource) adapter provider,
|
|
|
|
/// implementing `Poolable` in your own library will enable Rocket users to
|
2018-08-15 09:07:17 +00:00
|
|
|
/// consume your adapter with its built-in connection pooling support.
|
2018-07-21 22:11:08 +00:00
|
|
|
///
|
|
|
|
/// ## Example
|
|
|
|
///
|
2018-08-15 09:07:17 +00:00
|
|
|
/// Consider a library `foo` with the following types:
|
2018-07-21 22:11:08 +00:00
|
|
|
///
|
2018-08-15 09:07:17 +00:00
|
|
|
/// * `foo::ConnectionManager`, which implements [`r2d2::ManageConnection`]
|
|
|
|
/// * `foo::Connection`, the `Connection` associated type of
|
|
|
|
/// `foo::ConnectionManager`
|
|
|
|
/// * `foo::Error`, errors resulting from manager instantiation
|
2018-07-21 22:11:08 +00:00
|
|
|
///
|
2018-08-15 09:07:17 +00:00
|
|
|
/// In order for Rocket to generate the required code to automatically provision
|
|
|
|
/// a r2d2 connection pool into application state, the `Poolable` trait needs to
|
|
|
|
/// be implemented for the connection type. The following example implements
|
|
|
|
/// `Poolable` for `foo::Connection`:
|
2018-07-21 22:11:08 +00:00
|
|
|
///
|
2018-08-15 09:07:17 +00:00
|
|
|
/// ```rust
|
|
|
|
/// use rocket_contrib::databases::{r2d2, DbError, DatabaseConfig, Poolable};
|
|
|
|
/// # mod foo {
|
|
|
|
/// # use std::fmt;
|
2018-10-06 13:25:17 +00:00
|
|
|
/// # use rocket_contrib::databases::r2d2;
|
2018-08-15 09:07:17 +00:00
|
|
|
/// # #[derive(Debug)] pub struct Error;
|
2019-06-13 02:17:59 +00:00
|
|
|
/// # impl std::error::Error for Error { }
|
2018-08-15 09:07:17 +00:00
|
|
|
/// # impl fmt::Display for Error {
|
|
|
|
/// # fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { Ok(()) }
|
|
|
|
/// # }
|
|
|
|
/// #
|
|
|
|
/// # pub struct Connection;
|
|
|
|
/// # pub struct ConnectionManager;
|
|
|
|
/// #
|
2019-06-13 02:17:59 +00:00
|
|
|
/// # type Result<T> = std::result::Result<T, Error>;
|
2018-08-15 09:07:17 +00:00
|
|
|
/// #
|
2018-10-06 13:25:17 +00:00
|
|
|
/// # impl ConnectionManager {
|
|
|
|
/// # pub fn new(url: &str) -> Result<Self> { Err(Error) }
|
|
|
|
/// # }
|
|
|
|
/// #
|
2018-08-15 09:07:17 +00:00
|
|
|
/// # impl self::r2d2::ManageConnection for ConnectionManager {
|
|
|
|
/// # type Connection = Connection;
|
|
|
|
/// # type Error = Error;
|
|
|
|
/// # fn connect(&self) -> Result<Connection> { panic!(()) }
|
|
|
|
/// # fn is_valid(&self, _: &mut Connection) -> Result<()> { panic!() }
|
|
|
|
/// # fn has_broken(&self, _: &mut Connection) -> bool { panic!() }
|
|
|
|
/// # }
|
|
|
|
/// # }
|
|
|
|
/// #
|
|
|
|
/// impl Poolable for foo::Connection {
|
|
|
|
/// type Manager = foo::ConnectionManager;
|
2018-07-21 22:11:08 +00:00
|
|
|
/// type Error = DbError<foo::Error>;
|
|
|
|
///
|
|
|
|
/// fn pool(config: DatabaseConfig) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
|
2018-08-15 09:07:17 +00:00
|
|
|
/// let manager = foo::ConnectionManager::new(config.url)
|
2018-07-21 22:11:08 +00:00
|
|
|
/// .map_err(DbError::Custom)?;
|
|
|
|
///
|
2018-08-15 09:07:17 +00:00
|
|
|
/// r2d2::Pool::builder()
|
|
|
|
/// .max_size(config.pool_size)
|
|
|
|
/// .build(manager)
|
2018-07-21 22:11:08 +00:00
|
|
|
/// .map_err(DbError::PoolError)
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2018-08-15 09:07:17 +00:00
|
|
|
/// In this example, `ConnectionManager::new()` method returns a `foo::Error` on
|
|
|
|
/// failure. For convenience, the [`DbError`] enum is used to consolidate this
|
|
|
|
/// error type and the `r2d2::Error` type that can result from
|
|
|
|
/// `r2d2::Pool::builder()`.
|
2018-07-21 22:11:08 +00:00
|
|
|
///
|
2018-08-15 09:07:17 +00:00
|
|
|
/// In the event that a connection manager isn't fallible (as is the case with
|
|
|
|
/// Diesel's r2d2 connection manager, for instance), the associated error type
|
2018-07-21 22:11:08 +00:00
|
|
|
/// for the `Poolable` implementation can simply be `r2d2::Error` as this is the
|
2018-08-15 09:07:17 +00:00
|
|
|
/// only error that can be result. For more concrete example, consult Rocket's
|
|
|
|
/// existing implementations of [`Poolable`].
|
2018-07-21 22:11:08 +00:00
|
|
|
pub trait Poolable: Send + Sized + 'static {
|
|
|
|
/// The associated connection manager for the given connection type.
|
|
|
|
type Manager: ManageConnection<Connection=Self>;
|
|
|
|
/// The associated error type in the event that constructing the connection
|
2018-08-15 09:07:17 +00:00
|
|
|
/// manager and/or the connection pool fails.
|
2018-07-21 22:11:08 +00:00
|
|
|
type Error;
|
|
|
|
|
2018-08-15 09:07:17 +00:00
|
|
|
/// Creates an `r2d2` connection pool for `Manager::Connection`, returning
|
|
|
|
/// the pool on success.
|
2019-06-13 02:17:59 +00:00
|
|
|
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error>;
|
2018-07-21 22:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "diesel_sqlite_pool")]
|
|
|
|
impl Poolable for diesel::SqliteConnection {
|
|
|
|
type Manager = diesel::r2d2::ConnectionManager<diesel::SqliteConnection>;
|
|
|
|
type Error = r2d2::Error;
|
|
|
|
|
2019-06-13 02:17:59 +00:00
|
|
|
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
|
2018-07-21 22:11:08 +00:00
|
|
|
let manager = diesel::r2d2::ConnectionManager::new(config.url);
|
|
|
|
r2d2::Pool::builder().max_size(config.pool_size).build(manager)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 18:16:36 +00:00
|
|
|
#[cfg(feature = "diesel_postgres_pool")]
|
2018-07-21 22:11:08 +00:00
|
|
|
impl Poolable for diesel::PgConnection {
|
|
|
|
type Manager = diesel::r2d2::ConnectionManager<diesel::PgConnection>;
|
|
|
|
type Error = r2d2::Error;
|
|
|
|
|
2019-06-13 02:17:59 +00:00
|
|
|
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
|
2018-07-21 22:11:08 +00:00
|
|
|
let manager = diesel::r2d2::ConnectionManager::new(config.url);
|
|
|
|
r2d2::Pool::builder().max_size(config.pool_size).build(manager)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "diesel_mysql_pool")]
|
|
|
|
impl Poolable for diesel::MysqlConnection {
|
|
|
|
type Manager = diesel::r2d2::ConnectionManager<diesel::MysqlConnection>;
|
|
|
|
type Error = r2d2::Error;
|
|
|
|
|
2019-06-13 02:17:59 +00:00
|
|
|
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
|
2018-07-21 22:11:08 +00:00
|
|
|
let manager = diesel::r2d2::ConnectionManager::new(config.url);
|
|
|
|
r2d2::Pool::builder().max_size(config.pool_size).build(manager)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Come up with a way to handle TLS
|
|
|
|
#[cfg(feature = "postgres_pool")]
|
|
|
|
impl Poolable for postgres::Connection {
|
|
|
|
type Manager = r2d2_postgres::PostgresConnectionManager;
|
|
|
|
type Error = DbError<postgres::Error>;
|
|
|
|
|
2019-06-13 02:17:59 +00:00
|
|
|
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
|
2018-07-21 22:11:08 +00:00
|
|
|
let manager = r2d2_postgres::PostgresConnectionManager::new(config.url, r2d2_postgres::TlsMode::None)
|
|
|
|
.map_err(DbError::Custom)?;
|
|
|
|
|
|
|
|
r2d2::Pool::builder().max_size(config.pool_size).build(manager)
|
|
|
|
.map_err(DbError::PoolError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "mysql_pool")]
|
|
|
|
impl Poolable for mysql::Conn {
|
|
|
|
type Manager = r2d2_mysql::MysqlConnectionManager;
|
|
|
|
type Error = r2d2::Error;
|
|
|
|
|
2019-06-13 02:17:59 +00:00
|
|
|
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
|
2018-07-21 22:11:08 +00:00
|
|
|
let opts = mysql::OptsBuilder::from_opts(config.url);
|
|
|
|
let manager = r2d2_mysql::MysqlConnectionManager::new(opts);
|
|
|
|
r2d2::Pool::builder().max_size(config.pool_size).build(manager)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "sqlite_pool")]
|
|
|
|
impl Poolable for rusqlite::Connection {
|
|
|
|
type Manager = r2d2_sqlite::SqliteConnectionManager;
|
|
|
|
type Error = r2d2::Error;
|
|
|
|
|
2019-06-13 02:17:59 +00:00
|
|
|
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
|
2018-07-21 22:11:08 +00:00
|
|
|
let manager = r2d2_sqlite::SqliteConnectionManager::file(config.url);
|
|
|
|
|
|
|
|
r2d2::Pool::builder().max_size(config.pool_size).build(manager)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "cypher_pool")]
|
|
|
|
impl Poolable for rusted_cypher::GraphClient {
|
|
|
|
type Manager = r2d2_cypher::CypherConnectionManager;
|
|
|
|
type Error = r2d2::Error;
|
|
|
|
|
2019-06-13 02:17:59 +00:00
|
|
|
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
|
2018-07-21 22:11:08 +00:00
|
|
|
let manager = r2d2_cypher::CypherConnectionManager { url: config.url.to_string() };
|
|
|
|
r2d2::Pool::builder().max_size(config.pool_size).build(manager)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "redis_pool")]
|
|
|
|
impl Poolable for redis::Connection {
|
|
|
|
type Manager = r2d2_redis::RedisConnectionManager;
|
|
|
|
type Error = DbError<redis::RedisError>;
|
|
|
|
|
2019-06-13 02:17:59 +00:00
|
|
|
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
|
2018-07-21 22:11:08 +00:00
|
|
|
let manager = r2d2_redis::RedisConnectionManager::new(config.url).map_err(DbError::Custom)?;
|
|
|
|
r2d2::Pool::builder().max_size(config.pool_size).build(manager)
|
|
|
|
.map_err(DbError::PoolError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 20:01:21 +00:00
|
|
|
#[cfg(feature = "mongodb_pool")]
|
|
|
|
impl Poolable for mongodb::db::Database {
|
|
|
|
type Manager = r2d2_mongodb::MongodbConnectionManager;
|
|
|
|
type Error = DbError<mongodb::Error>;
|
|
|
|
|
2019-06-13 02:17:59 +00:00
|
|
|
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
|
2018-11-28 20:01:21 +00:00
|
|
|
let manager = r2d2_mongodb::MongodbConnectionManager::new_with_uri(config.url).map_err(DbError::Custom)?;
|
|
|
|
r2d2::Pool::builder().max_size(config.pool_size).build(manager).map_err(DbError::PoolError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 16:19:09 +00:00
|
|
|
#[cfg(feature = "memcache_pool")]
|
|
|
|
impl Poolable for memcache::Client {
|
|
|
|
type Manager = r2d2_memcache::MemcacheConnectionManager;
|
|
|
|
type Error = DbError<memcache::MemcacheError>;
|
|
|
|
|
2019-06-13 02:17:59 +00:00
|
|
|
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
|
2019-01-04 16:19:09 +00:00
|
|
|
let manager = r2d2_memcache::MemcacheConnectionManager::new(config.url);
|
|
|
|
r2d2::Pool::builder().max_size(config.pool_size).build(manager).map_err(DbError::PoolError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 22:11:08 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use rocket::{Config, config::{Environment, Value}};
|
2018-10-07 00:24:11 +00:00
|
|
|
use super::{ConfigError::*, database_config};
|
2018-07-21 22:11:08 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_database_entry_in_config_returns_error() {
|
|
|
|
let config = Config::build(Environment::Development)
|
|
|
|
.finalize()
|
|
|
|
.unwrap();
|
|
|
|
let database_config_result = database_config("dummy_db", &config);
|
|
|
|
|
2018-08-15 09:07:17 +00:00
|
|
|
assert_eq!(Err(MissingTable), database_config_result);
|
2018-07-21 22:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_matching_connection_returns_error() {
|
|
|
|
// Laboriously setup the config extras
|
|
|
|
let mut database_extra = BTreeMap::new();
|
|
|
|
let mut connection_config = BTreeMap::new();
|
|
|
|
connection_config.insert("url".to_string(), Value::from("dummy_db.sqlite"));
|
|
|
|
connection_config.insert("pool_size".to_string(), Value::from(10));
|
|
|
|
database_extra.insert("dummy_db".to_string(), Value::from(connection_config));
|
|
|
|
|
|
|
|
let config = Config::build(Environment::Development)
|
|
|
|
.extra("databases", database_extra)
|
|
|
|
.finalize()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let database_config_result = database_config("real_db", &config);
|
|
|
|
|
2018-08-15 09:07:17 +00:00
|
|
|
assert_eq!(Err(MissingKey), database_config_result);
|
2018-07-21 22:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn incorrectly_structured_config_returns_error() {
|
|
|
|
let mut database_extra = BTreeMap::new();
|
|
|
|
let connection_config = vec!["url", "dummy_db.slqite"];
|
|
|
|
database_extra.insert("dummy_db".to_string(), Value::from(connection_config));
|
|
|
|
|
|
|
|
let config = Config::build(Environment::Development)
|
|
|
|
.extra("databases", database_extra)
|
|
|
|
.finalize()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let database_config_result = database_config("dummy_db", &config);
|
|
|
|
|
2018-08-15 09:07:17 +00:00
|
|
|
assert_eq!(Err(MalformedConfiguration), database_config_result);
|
2018-07-21 22:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_connection_string_returns_error() {
|
|
|
|
let mut database_extra = BTreeMap::new();
|
|
|
|
let connection_config: BTreeMap<String, Value> = BTreeMap::new();
|
|
|
|
database_extra.insert("dummy_db", connection_config);
|
|
|
|
|
|
|
|
let config = Config::build(Environment::Development)
|
|
|
|
.extra("databases", database_extra)
|
|
|
|
.finalize()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let database_config_result = database_config("dummy_db", &config);
|
|
|
|
|
2018-08-15 09:07:17 +00:00
|
|
|
assert_eq!(Err(MissingUrl), database_config_result);
|
2018-07-21 22:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_connection_string_returns_error() {
|
|
|
|
let mut database_extra = BTreeMap::new();
|
|
|
|
let mut connection_config = BTreeMap::new();
|
|
|
|
connection_config.insert("url".to_string(), Value::from(42));
|
|
|
|
database_extra.insert("dummy_db", connection_config);
|
|
|
|
|
|
|
|
let config = Config::build(Environment::Development)
|
|
|
|
.extra("databases", database_extra)
|
|
|
|
.finalize()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let database_config_result = database_config("dummy_db", &config);
|
|
|
|
|
2018-08-15 09:07:17 +00:00
|
|
|
assert_eq!(Err(MalformedUrl), database_config_result);
|
2018-07-21 22:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn negative_pool_size_returns_error() {
|
|
|
|
let mut database_extra = BTreeMap::new();
|
|
|
|
let mut connection_config = BTreeMap::new();
|
|
|
|
connection_config.insert("url".to_string(), Value::from("dummy_db.sqlite"));
|
|
|
|
connection_config.insert("pool_size".to_string(), Value::from(-1));
|
|
|
|
database_extra.insert("dummy_db", connection_config);
|
|
|
|
|
|
|
|
let config = Config::build(Environment::Development)
|
|
|
|
.extra("databases", database_extra)
|
|
|
|
.finalize()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let database_config_result = database_config("dummy_db", &config);
|
|
|
|
|
2018-08-15 09:07:17 +00:00
|
|
|
assert_eq!(Err(InvalidPoolSize(-1)), database_config_result);
|
2018-07-21 22:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pool_size_beyond_u32_max_returns_error() {
|
|
|
|
let mut database_extra = BTreeMap::new();
|
|
|
|
let mut connection_config = BTreeMap::new();
|
2018-08-15 09:07:17 +00:00
|
|
|
let over_max = (u32::max_value()) as i64 + 1;
|
2018-07-21 22:11:08 +00:00
|
|
|
connection_config.insert("url".to_string(), Value::from("dummy_db.sqlite"));
|
2018-08-15 09:07:17 +00:00
|
|
|
connection_config.insert("pool_size".to_string(), Value::from(over_max));
|
2018-07-21 22:11:08 +00:00
|
|
|
database_extra.insert("dummy_db", connection_config);
|
|
|
|
|
|
|
|
let config = Config::build(Environment::Development)
|
|
|
|
.extra("databases", database_extra)
|
|
|
|
.finalize()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let database_config_result = database_config("dummy_db", &config);
|
|
|
|
|
|
|
|
// The size of `0` is an overflow wrap-around
|
2018-08-15 09:07:17 +00:00
|
|
|
assert_eq!(Err(InvalidPoolSize(over_max)), database_config_result);
|
2018-07-21 22:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn happy_path_database_config() {
|
|
|
|
let url = "dummy_db.sqlite";
|
|
|
|
let pool_size = 10;
|
|
|
|
|
|
|
|
let mut database_extra = BTreeMap::new();
|
|
|
|
let mut connection_config = BTreeMap::new();
|
|
|
|
connection_config.insert("url".to_string(), Value::from(url));
|
|
|
|
connection_config.insert("pool_size".to_string(), Value::from(pool_size));
|
|
|
|
database_extra.insert("dummy_db", connection_config);
|
|
|
|
|
|
|
|
let config = Config::build(Environment::Development)
|
|
|
|
.extra("databases", database_extra)
|
|
|
|
.finalize()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let database_config = database_config("dummy_db", &config).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(url, database_config.url);
|
|
|
|
assert_eq!(pool_size, database_config.pool_size);
|
|
|
|
assert_eq!(0, database_config.extras.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extras_do_not_contain_required_keys() {
|
|
|
|
let url = "dummy_db.sqlite";
|
|
|
|
let pool_size = 10;
|
|
|
|
|
|
|
|
let mut database_extra = BTreeMap::new();
|
|
|
|
let mut connection_config = BTreeMap::new();
|
|
|
|
connection_config.insert("url".to_string(), Value::from(url));
|
|
|
|
connection_config.insert("pool_size".to_string(), Value::from(pool_size));
|
|
|
|
database_extra.insert("dummy_db", connection_config);
|
|
|
|
|
|
|
|
let config = Config::build(Environment::Development)
|
|
|
|
.extra("databases", database_extra)
|
|
|
|
.finalize()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let database_config = database_config("dummy_db", &config).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(url, database_config.url);
|
|
|
|
assert_eq!(pool_size, database_config.pool_size);
|
|
|
|
assert_eq!(false, database_config.extras.contains_key("url"));
|
|
|
|
assert_eq!(false, database_config.extras.contains_key("pool_size"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extra_values_are_placed_in_extras_map() {
|
|
|
|
let url = "dummy_db.sqlite";
|
|
|
|
let pool_size = 10;
|
|
|
|
let tls_cert = "certs.pem";
|
|
|
|
let tls_key = "key.pem";
|
|
|
|
|
|
|
|
let mut database_extra = BTreeMap::new();
|
|
|
|
let mut connection_config = BTreeMap::new();
|
|
|
|
connection_config.insert("url".to_string(), Value::from(url));
|
|
|
|
connection_config.insert("pool_size".to_string(), Value::from(pool_size));
|
|
|
|
connection_config.insert("certs".to_string(), Value::from(tls_cert));
|
|
|
|
connection_config.insert("key".to_string(), Value::from(tls_key));
|
|
|
|
database_extra.insert("dummy_db", connection_config);
|
|
|
|
|
|
|
|
let config = Config::build(Environment::Development)
|
|
|
|
.extra("databases", database_extra)
|
|
|
|
.finalize()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let database_config = database_config("dummy_db", &config).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(url, database_config.url);
|
|
|
|
assert_eq!(pool_size, database_config.pool_size);
|
|
|
|
assert_eq!(true, database_config.extras.contains_key("certs"));
|
|
|
|
assert_eq!(true, database_config.extras.contains_key("key"));
|
|
|
|
|
|
|
|
println!("{:#?}", database_config);
|
|
|
|
}
|
|
|
|
}
|