Skip to content

Commit 282f1dd

Browse files
cursoragentlovasoa
andcommitted
Refactor ConnectOptions to use FromStr and update auth plugin names
Co-authored-by: contact <[email protected]>
1 parent 3920eb8 commit 282f1dd

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

sqlx-core/src/connection.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ pub trait Connection: Send {
116116
fn connect(url: &str) -> BoxFuture<'static, Result<Self, Error>>
117117
where
118118
Self: Sized,
119+
Self::Options: FromStr<Err = Error>,
119120
{
120121
let options = url.parse();
121122

@@ -161,19 +162,28 @@ impl LogSettings {
161162
pub trait ConnectOptions: Sized + Send + Sync + 'static {
162163
type Connection: Connection;
163164

164-
fn from_url(url: &str) -> Result<Self, Error>;
165+
fn from_url(url: &str) -> Result<Self, Error>
166+
where
167+
Self: std::str::FromStr<Err = Error>,
168+
{
169+
Self::from_str(url)
170+
}
165171

166172
fn connect(&self) -> BoxFuture<'_, Result<Self::Connection, Error>>;
167173

168-
fn connect_with(options: &Self) -> BoxFuture<'_, Result<Self::Connection, Error>>;
174+
fn connect_with(options: &Self) -> BoxFuture<'_, Result<Self::Connection, Error>> {
175+
options.connect()
176+
}
169177

170-
fn from_env() -> Result<Self, Error> {
171-
let options = Self::from_url(&std::env::var("DATABASE_URL")?)?;
172-
Box::pin(async move { Self::connect_with(&options?).await })
178+
fn from_env() -> Result<Self, Error>
179+
where
180+
Self: std::str::FromStr<Err = Error>,
181+
{
182+
let url = std::env::var("DATABASE_URL").map_err(Error::config)?;
183+
Self::from_str(&url)
173184
}
174185

175-
fn create_pool(
176-
&self,
177-
options: crate::pool::PoolOptions<Self::Connection>,
178-
) -> Result<crate::pool::Pool<Self::Connection>, Error>;
186+
fn log_statements(&mut self, level: LevelFilter) -> &mut Self;
187+
188+
fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) -> &mut Self;
179189
}

sqlx-core/src/mysql/protocol/connect/handshake.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,7 @@ fn test_decode_handshake_mysql_8_0_18() {
133133
assert_eq!(p.server_default_collation, 255);
134134
assert!(p.status.contains(Status::SERVER_STATUS_AUTOCOMMIT));
135135

136-
assert!(matches!(
137-
p.auth_plugin,
138-
Some(AuthPlugin::CachingSha2Password)
139-
));
136+
assert!(matches!(p.auth_plugin, Some(AuthPlugin::CachingSha2)));
140137

141138
assert_eq!(
142139
&*p.auth_plugin_data.into_iter().collect::<Vec<_>>(),
@@ -186,10 +183,7 @@ fn test_decode_handshake_mariadb_10_4_7() {
186183

187184
assert_eq!(p.server_default_collation, 8);
188185
assert!(p.status.contains(Status::SERVER_STATUS_AUTOCOMMIT));
189-
assert!(matches!(
190-
p.auth_plugin,
191-
Some(AuthPlugin::MySqlNativePassword)
192-
));
186+
assert!(matches!(p.auth_plugin, Some(AuthPlugin::MySqlNative)));
193187

194188
assert_eq!(
195189
&*p.auth_plugin_data.into_iter().collect::<Vec<_>>(),

sqlx-core/src/pool/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,10 @@ impl<DB: Database> Pool<DB> {
265265
/// For production applications, you'll likely want to make at least few tweaks.
266266
///
267267
/// See [`PoolOptions::new()`] for details.
268-
pub async fn connect(url: &str) -> Result<Self, Error> {
268+
pub async fn connect(url: &str) -> Result<Self, Error>
269+
where
270+
<DB::Connection as Connection>::Options: std::str::FromStr<Err = Error>,
271+
{
269272
PoolOptions::<DB>::new().connect(url).await
270273
}
271274

@@ -298,7 +301,10 @@ impl<DB: Database> Pool<DB> {
298301
/// For production applications, you'll likely want to make at least few tweaks.
299302
///
300303
/// See [`PoolOptions::new()`] for details.
301-
pub fn connect_lazy(url: &str) -> Result<Self, Error> {
304+
pub fn connect_lazy(url: &str) -> Result<Self, Error>
305+
where
306+
<DB::Connection as Connection>::Options: std::str::FromStr<Err = Error>,
307+
{
302308
PoolOptions::<DB>::new().connect_lazy(url)
303309
}
304310

sqlx-core/src/pool/options.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,10 @@ impl<DB: Database> PoolOptions<DB> {
428428
/// * MySQL: [`MySqlConnectOptions`][crate::mysql::MySqlConnectOptions]
429429
/// * SQLite: [`SqliteConnectOptions`][crate::sqlite::SqliteConnectOptions]
430430
/// * MSSQL: [`MssqlConnectOptions`][crate::mssql::MssqlConnectOptions]
431-
pub async fn connect(self, url: &str) -> Result<Pool<DB>, Error> {
431+
pub async fn connect(self, url: &str) -> Result<Pool<DB>, Error>
432+
where
433+
<DB::Connection as Connection>::Options: std::str::FromStr<Err = Error>,
434+
{
432435
self.connect_with(url.parse()?).await
433436
}
434437

@@ -471,7 +474,10 @@ impl<DB: Database> PoolOptions<DB> {
471474
/// * MySQL: [`MySqlConnectOptions`][crate::mysql::MySqlConnectOptions]
472475
/// * SQLite: [`SqliteConnectOptions`][crate::sqlite::SqliteConnectOptions]
473476
/// * MSSQL: [`MssqlConnectOptions`][crate::mssql::MssqlConnectOptions]
474-
pub fn connect_lazy(self, url: &str) -> Result<Pool<DB>, Error> {
477+
pub fn connect_lazy(self, url: &str) -> Result<Pool<DB>, Error>
478+
where
479+
<DB::Connection as Connection>::Options: std::str::FromStr<Err = Error>,
480+
{
475481
Ok(self.connect_lazy_with(url.parse()?))
476482
}
477483

0 commit comments

Comments
 (0)