Skip to content

Commit e2ce463

Browse files
authored
doc: make it clear that ConnectOptions types impl FromStr (#2574)
1 parent 3fbc3a3 commit e2ce463

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

sqlx-mysql/src/options/mod.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ pub use ssl_mode::MySqlSslMode;
1818
/// mysql://[host][/database][?properties]
1919
/// ```
2020
///
21+
/// This type also implements [`FromStr`][std::str::FromStr] so you can parse it from a string
22+
/// containing a connection URL and then further adjust options if necessary (see example below).
23+
///
2124
/// ## Properties
2225
///
2326
/// |Parameter|Default|Description|
@@ -30,13 +33,10 @@ pub use ssl_mode::MySqlSslMode;
3033
/// # Example
3134
///
3235
/// ```rust,no_run
33-
/// # use sqlx_core::error::Error;
34-
/// # use sqlx_core::connection::{Connection, ConnectOptions};
35-
/// # use sqlx_core::mysql::{MySqlConnectOptions, MySqlConnection, MySqlSslMode};
36-
/// #
37-
/// # fn main() {
38-
/// # #[cfg(feature = "_rt")]
39-
/// # sqlx::__rt::test_block_on(async move {
36+
/// # async fn example() -> sqlx::Result<()> {
37+
/// use sqlx::{Connection, ConnectOptions};
38+
/// use sqlx::mysql::{MySqlConnectOptions, MySqlConnection, MySqlPool, MySqlSslMode};
39+
///
4040
/// // URL connection string
4141
/// let conn = MySqlConnection::connect("mysql://root:password@localhost/db").await?;
4242
///
@@ -47,8 +47,16 @@ pub use ssl_mode::MySqlSslMode;
4747
/// .password("password")
4848
/// .database("db")
4949
/// .connect().await?;
50-
/// # Result::<(), Error>::Ok(())
51-
/// # }).unwrap();
50+
///
51+
/// // Modifying options parsed from a string
52+
/// let mut opts: MySqlConnectOptions = "mysql://root:password@localhost/db".parse()?;
53+
///
54+
/// // Change the log verbosity level for queries.
55+
/// // Information about SQL queries is logged at `DEBUG` level by default.
56+
/// opts.log_statements(log::LevelFilter::Trace);
57+
///
58+
/// let pool = MySqlPool::connect_with(&opts).await?;
59+
/// # Ok(())
5260
/// # }
5361
/// ```
5462
#[derive(Debug, Clone)]

sqlx-postgres/src/options/mod.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ mod ssl_mode;
2323
/// postgresql://[user[:password]@][host][:port][/dbname][?param1=value1&...]
2424
/// ```
2525
///
26+
/// This type also implements [`FromStr`][std::str::FromStr] so you can parse it from a string
27+
/// containing a connection URL and then further adjust options if necessary (see example below).
28+
///
2629
/// ## Parameters
2730
///
2831
/// |Parameter|Default|Description|
@@ -55,13 +58,10 @@ mod ssl_mode;
5558
/// # Example
5659
///
5760
/// ```rust,no_run
58-
/// # use sqlx_core::error::Error;
59-
/// # use sqlx_core::connection::{Connection, ConnectOptions};
60-
/// # use sqlx_core::postgres::{PgConnectOptions, PgConnection, PgSslMode};
61-
/// #
62-
/// # fn main() {
63-
/// # #[cfg(feature = "_rt")]
64-
/// # sqlx::__rt::test_block_on(async move {
61+
/// use sqlx::{Connection, ConnectOptions};
62+
/// use sqlx::postgres::{PgConnectOptions, PgConnection, PgPool, PgSslMode};
63+
///
64+
/// # async fn example() -> sqlx::Result<()> {
6565
/// // URL connection string
6666
/// let conn = PgConnection::connect("postgres://localhost/mydb").await?;
6767
///
@@ -72,9 +72,17 @@ mod ssl_mode;
7272
/// .username("secret-user")
7373
/// .password("secret-password")
7474
/// .ssl_mode(PgSslMode::Require)
75-
/// .connect().await?;
76-
/// # Result::<(), Error>::Ok(())
77-
/// # }).unwrap();
75+
/// .connect()
76+
/// .await?;
77+
///
78+
/// // Modifying options parsed from a string
79+
/// let mut opts: PgConnectOptions = "postgres://localhost/mydb".parse()?;
80+
///
81+
/// // Change the log verbosity level for queries.
82+
/// // Information about SQL queries is logged at `DEBUG` level by default.
83+
/// opts.log_statements(log::LevelFilter::Trace);
84+
///
85+
/// let pool = PgPool::connect_with(&opts).await?;
7886
/// # }
7987
/// ```
8088
#[derive(Debug, Clone)]

sqlx-sqlite/src/options/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ use sqlx_core::IndexMap;
2525
/// A value of `SqliteConnectOptions` can be parsed from a connection URL,
2626
/// as described by [SQLite](https://www.sqlite.org/uri.html).
2727
///
28+
/// This type also implements [`FromStr`][std::str::FromStr] so you can parse it from a string
29+
/// containing a connection URL and then further adjust options if necessary (see example below).
30+
///
2831
/// | URL | Description |
2932
/// | -- | -- |
3033
/// `sqlite::memory:` | Open an in-memory database. |
@@ -36,20 +39,17 @@ use sqlx_core::IndexMap;
3639
/// # Example
3740
///
3841
/// ```rust,no_run
39-
/// # use sqlx_core::connection::ConnectOptions;
40-
/// # use sqlx_core::error::Error;
41-
/// # use sqlx_sqlite::{SqliteConnectOptions, SqliteJournalMode};
42+
/// # async fn example() -> sqlx::Result<()> {
43+
/// use sqlx::ConnectOptions;
44+
/// use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode};
4245
/// use std::str::FromStr;
4346
///
44-
/// # fn main() {
45-
/// # #[cfg(feature = "_rt-async-std")]
46-
/// # sqlx::__rt::test_block_on(async move {
4747
/// let conn = SqliteConnectOptions::from_str("sqlite://data.db")?
4848
/// .journal_mode(SqliteJournalMode::Wal)
4949
/// .read_only(true)
5050
/// .connect().await?;
51-
/// # Result::<(), Error>::Ok(())
52-
/// # }).unwrap();
51+
/// #
52+
/// # Ok(())
5353
/// # }
5454
/// ```
5555
#[derive(Clone, Debug)]

0 commit comments

Comments
 (0)