Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 19 additions & 28 deletions sqlx-core/src/any/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,34 +87,6 @@ impl AnyConnection {
pub fn private_get_mut(&mut self) -> &mut AnyConnectionKind {
&mut self.0
}

/// Returns the runtime DBMS name for this connection.
///
/// For most built-in drivers this returns a well-known constant string:
/// - Postgres -> "PostgreSQL"
/// - MySQL -> "MySQL"
/// - SQLite -> "SQLite"
/// - MSSQL -> "Microsoft SQL Server"
///
/// For ODBC, this queries the driver at runtime via `SQL_DBMS_NAME`.
pub async fn dbms_name(&mut self) -> Result<String, Error> {
match &mut self.0 {
#[cfg(feature = "postgres")]
AnyConnectionKind::Postgres(_) => Ok("PostgreSQL".to_string()),

#[cfg(feature = "mysql")]
AnyConnectionKind::MySql(_) => Ok("MySQL".to_string()),

#[cfg(feature = "sqlite")]
AnyConnectionKind::Sqlite(_) => Ok("SQLite".to_string()),

#[cfg(feature = "mssql")]
AnyConnectionKind::Mssql(_) => Ok("Microsoft SQL Server".to_string()),

#[cfg(feature = "odbc")]
AnyConnectionKind::Odbc(conn) => conn.dbms_name().await,
}
}
}

macro_rules! delegate_to {
Expand Down Expand Up @@ -264,6 +236,25 @@ impl Connection for AnyConnection {
fn should_flush(&self) -> bool {
delegate_to!(self.should_flush())
}

fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>> {
match &mut self.0 {
#[cfg(feature = "postgres")]
AnyConnectionKind::Postgres(conn) => conn.dbms_name(),

#[cfg(feature = "mysql")]
AnyConnectionKind::MySql(conn) => conn.dbms_name(),

#[cfg(feature = "sqlite")]
AnyConnectionKind::Sqlite(conn) => conn.dbms_name(),

#[cfg(feature = "mssql")]
AnyConnectionKind::Mssql(conn) => conn.dbms_name(),

#[cfg(feature = "odbc")]
AnyConnectionKind::Odbc(conn) => conn.dbms_name(),
}
}
}

#[cfg(feature = "postgres")]
Expand Down
19 changes: 19 additions & 0 deletions sqlx-core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,25 @@ pub trait Connection: Send {
Box::pin(async move { Ok(()) })
}

/// Returns the name of the Database Management System (DBMS) this connection
/// is talking to.
///
/// This is intended to be compatible with the ODBC `SQL_DBMS_NAME` info value
/// and should generally return the same string as an ODBC driver for the same
/// database. This makes it easier to write conditional SQL or feature probes
/// that work across both native and ODBC connections.
///
/// Typical return values include:
/// - "PostgreSQL"
/// - "MySQL"
/// - "SQLite"
/// - "Microsoft SQL Server" (includes Azure SQL)
/// - ODBC: the exact string reported by the driver via `SQL_DBMS_NAME`
///
/// Implementations for built-in drivers return a well-known constant string,
/// while the ODBC driver queries the underlying driver at runtime.
fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>>;

#[doc(hidden)]
fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>>;

Expand Down
4 changes: 4 additions & 0 deletions sqlx-core/src/mssql/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,8 @@ impl Connection for MssqlConnection {
fn should_flush(&self) -> bool {
!self.stream.wbuf.is_empty()
}

fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>> {
futures_util::future::ready(Ok("Microsoft SQL Server".to_string())).boxed()
}
}
4 changes: 4 additions & 0 deletions sqlx-core/src/mysql/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,8 @@ impl Connection for MySqlConnection {
{
Transaction::begin(self)
}

fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>> {
futures_util::future::ready(Ok("MySQL".to_string())).boxed()
}
}
18 changes: 10 additions & 8 deletions sqlx-core/src/odbc/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,7 @@ impl OdbcConnection {
})
}

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

pub(crate) async fn ping_blocking(&mut self) -> Result<(), Error> {
self.with_conn("ping", move |conn| {
Expand Down Expand Up @@ -237,6 +230,15 @@ impl Connection for OdbcConnection {
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
Box::pin(self.clear_cached_statements())
}

fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>> {
Box::pin(async move {
self.with_conn("dbms_name", move |conn| {
Ok(conn.database_management_system_name()?)
})
.await
})
}
}

// moved helpers to connection/inner.rs
4 changes: 4 additions & 0 deletions sqlx-core/src/postgres/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,8 @@ impl Connection for PgConnection {
fn should_flush(&self) -> bool {
!self.stream.wbuf.is_empty()
}

fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>> {
futures_util::future::ready(Ok("PostgreSQL".to_string())).boxed()
}
}
4 changes: 4 additions & 0 deletions sqlx-core/src/sqlite/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ impl Connection for SqliteConnection {
fn should_flush(&self) -> bool {
false
}

fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>> {
Box::pin(async move { Ok("SQLite".to_string()) })
}
}

impl LockedSqliteHandle<'_> {
Expand Down
Loading