Skip to content

Commit 644d347

Browse files
cursoragentlovasoa
andcommitted
Add dbms_name method to Connection trait
Co-authored-by: contact <[email protected]>
1 parent 6a200a5 commit 644d347

File tree

7 files changed

+64
-36
lines changed

7 files changed

+64
-36
lines changed

sqlx-core/src/any/connection/mod.rs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -87,34 +87,6 @@ impl AnyConnection {
8787
pub fn private_get_mut(&mut self) -> &mut AnyConnectionKind {
8888
&mut self.0
8989
}
90-
91-
/// Returns the runtime DBMS name for this connection.
92-
///
93-
/// For most built-in drivers this returns a well-known constant string:
94-
/// - Postgres -> "PostgreSQL"
95-
/// - MySQL -> "MySQL"
96-
/// - SQLite -> "SQLite"
97-
/// - MSSQL -> "Microsoft SQL Server"
98-
///
99-
/// For ODBC, this queries the driver at runtime via `SQL_DBMS_NAME`.
100-
pub async fn dbms_name(&mut self) -> Result<String, Error> {
101-
match &mut self.0 {
102-
#[cfg(feature = "postgres")]
103-
AnyConnectionKind::Postgres(_) => Ok("PostgreSQL".to_string()),
104-
105-
#[cfg(feature = "mysql")]
106-
AnyConnectionKind::MySql(_) => Ok("MySQL".to_string()),
107-
108-
#[cfg(feature = "sqlite")]
109-
AnyConnectionKind::Sqlite(_) => Ok("SQLite".to_string()),
110-
111-
#[cfg(feature = "mssql")]
112-
AnyConnectionKind::Mssql(_) => Ok("Microsoft SQL Server".to_string()),
113-
114-
#[cfg(feature = "odbc")]
115-
AnyConnectionKind::Odbc(conn) => conn.dbms_name().await,
116-
}
117-
}
11890
}
11991

12092
macro_rules! delegate_to {
@@ -264,6 +236,25 @@ impl Connection for AnyConnection {
264236
fn should_flush(&self) -> bool {
265237
delegate_to!(self.should_flush())
266238
}
239+
240+
fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>> {
241+
match &mut self.0 {
242+
#[cfg(feature = "postgres")]
243+
AnyConnectionKind::Postgres(conn) => conn.dbms_name(),
244+
245+
#[cfg(feature = "mysql")]
246+
AnyConnectionKind::MySql(conn) => conn.dbms_name(),
247+
248+
#[cfg(feature = "sqlite")]
249+
AnyConnectionKind::Sqlite(conn) => conn.dbms_name(),
250+
251+
#[cfg(feature = "mssql")]
252+
AnyConnectionKind::Mssql(conn) => conn.dbms_name(),
253+
254+
#[cfg(feature = "odbc")]
255+
AnyConnectionKind::Odbc(conn) => conn.dbms_name(),
256+
}
257+
}
267258
}
268259

269260
#[cfg(feature = "postgres")]

sqlx-core/src/connection.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,25 @@ pub trait Connection: Send {
102102
Box::pin(async move { Ok(()) })
103103
}
104104

105+
/// Returns the name of the Database Management System (DBMS) this connection
106+
/// is talking to.
107+
///
108+
/// This is intended to be compatible with the ODBC `SQL_DBMS_NAME` info value
109+
/// and should generally return the same string as an ODBC driver for the same
110+
/// database. This makes it easier to write conditional SQL or feature probes
111+
/// that work across both native and ODBC connections.
112+
///
113+
/// Typical return values include:
114+
/// - "PostgreSQL"
115+
/// - "MySQL"
116+
/// - "SQLite"
117+
/// - "Microsoft SQL Server" (includes Azure SQL)
118+
/// - ODBC: the exact string reported by the driver via `SQL_DBMS_NAME`
119+
///
120+
/// Implementations for built-in drivers return a well-known constant string,
121+
/// while the ODBC driver queries the underlying driver at runtime.
122+
fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>>;
123+
105124
#[doc(hidden)]
106125
fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>>;
107126

sqlx-core/src/mssql/connection/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,8 @@ impl Connection for MssqlConnection {
8181
fn should_flush(&self) -> bool {
8282
!self.stream.wbuf.is_empty()
8383
}
84+
85+
fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>> {
86+
futures_util::future::ready(Ok("Microsoft SQL Server".to_string())).boxed()
87+
}
8488
}

sqlx-core/src/mysql/connection/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,8 @@ impl Connection for MySqlConnection {
107107
{
108108
Transaction::begin(self)
109109
}
110+
111+
fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>> {
112+
futures_util::future::ready(Ok("MySQL".to_string())).boxed()
113+
}
110114
}

sqlx-core/src/odbc/connection/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,7 @@ impl OdbcConnection {
9595
})
9696
}
9797

98-
/// Returns the name of the actual Database Management System (DBMS) this
99-
/// connection is talking to as reported by the ODBC driver.
100-
pub async fn dbms_name(&mut self) -> Result<String, Error> {
101-
self.with_conn("dbms_name", move |conn| {
102-
Ok(conn.database_management_system_name()?)
103-
})
104-
.await
105-
}
98+
// (dbms_name moved to the Connection trait implementation)
10699

107100
pub(crate) async fn ping_blocking(&mut self) -> Result<(), Error> {
108101
self.with_conn("ping", move |conn| {
@@ -237,6 +230,15 @@ impl Connection for OdbcConnection {
237230
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
238231
Box::pin(self.clear_cached_statements())
239232
}
233+
234+
fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>> {
235+
Box::pin(async move {
236+
self.with_conn("dbms_name", move |conn| {
237+
Ok(conn.database_management_system_name()?)
238+
})
239+
.await
240+
})
241+
}
240242
}
241243

242244
// moved helpers to connection/inner.rs

sqlx-core/src/postgres/connection/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,8 @@ impl Connection for PgConnection {
205205
fn should_flush(&self) -> bool {
206206
!self.stream.wbuf.is_empty()
207207
}
208+
209+
fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>> {
210+
futures_util::future::ready(Ok("PostgreSQL".to_string())).boxed()
211+
}
208212
}

sqlx-core/src/sqlite/connection/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ impl Connection for SqliteConnection {
202202
fn should_flush(&self) -> bool {
203203
false
204204
}
205+
206+
fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>> {
207+
Box::pin(async move { Ok("SQLite".to_string()) })
208+
}
205209
}
206210

207211
impl LockedSqliteHandle<'_> {

0 commit comments

Comments
 (0)