diff --git a/sqlx-core/src/row.rs b/sqlx-core/src/row.rs index fe0b75ebb0..6077b9a640 100644 --- a/sqlx-core/src/row.rs +++ b/sqlx-core/src/row.rs @@ -1,8 +1,9 @@ -use crate::column::ColumnIndex; +use crate::column::{Column, ColumnIndex}; use crate::database::Database; use crate::decode::Decode; use crate::error::{mismatched_types, Error}; +use crate::type_checking::TypeChecking; use crate::type_info::TypeInfo; use crate::types::Type; use crate::value::ValueRef; @@ -176,3 +177,32 @@ pub trait Row: Unpin + Send + Sync + 'static { where I: ColumnIndex; } + +pub fn debug_row(row: &R, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result +where + R: Row, + usize: ColumnIndex, + ::Database: TypeChecking, +{ + write!(f, "{} ", std::any::type_name::())?; + + let mut debug_map = f.debug_map(); + + for column in row.columns().iter() { + match row.try_get_raw(column.ordinal()) { + Ok(value) => { + debug_map.entry( + &column.name(), + &::Database::fmt_value_debug( + &<::Database as Database>::ValueRef::to_owned(&value), + ), + ); + } + Err(error) => { + debug_map.entry(&column.name(), &format!("decode error: {error:?}")); + } + } + } + + debug_map.finish() +} diff --git a/sqlx-mysql/src/row.rs b/sqlx-mysql/src/row.rs index e7191366e6..7bed7c7726 100644 --- a/sqlx-mysql/src/row.rs +++ b/sqlx-mysql/src/row.rs @@ -9,7 +9,6 @@ use crate::HashMap; use crate::{protocol, MySql, MySqlColumn, MySqlValueFormat, MySqlValueRef}; /// Implementation of [`Row`] for MySQL. -#[derive(Debug)] pub struct MySqlRow { pub(crate) row: protocol::Row, pub(crate) format: MySqlValueFormat, @@ -49,3 +48,9 @@ impl ColumnIndex for &'_ str { .copied() } } + +impl std::fmt::Debug for MySqlRow { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + debug_row(self, f) + } +} diff --git a/sqlx-postgres/src/row.rs b/sqlx-postgres/src/row.rs index f9e43bb9c6..5240a50be0 100644 --- a/sqlx-postgres/src/row.rs +++ b/sqlx-postgres/src/row.rs @@ -4,10 +4,8 @@ use crate::message::DataRow; use crate::statement::PgStatementMetadata; use crate::value::PgValueFormat; use crate::{PgColumn, PgValueRef, Postgres}; +use sqlx_core::row::debug_row; pub(crate) use sqlx_core::row::Row; -use sqlx_core::type_checking::TypeChecking; -use sqlx_core::value::ValueRef; -use std::fmt::Debug; use std::sync::Arc; /// Implementation of [`Row`] for PostgreSQL. @@ -51,25 +49,8 @@ impl ColumnIndex for &'_ str { } } -impl Debug for PgRow { +impl std::fmt::Debug for PgRow { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "PgRow ")?; - - let mut debug_map = f.debug_map(); - for (index, column) in self.columns().iter().enumerate() { - match self.try_get_raw(index) { - Ok(value) => { - debug_map.entry( - &column.name, - &Postgres::fmt_value_debug(&::to_owned(&value)), - ); - } - Err(error) => { - debug_map.entry(&column.name, &format!("decode error: {error:?}")); - } - } - } - - debug_map.finish() + debug_row(self, f) } } diff --git a/sqlx-sqlite/src/row.rs b/sqlx-sqlite/src/row.rs index 92fc455819..3cb47efec8 100644 --- a/sqlx-sqlite/src/row.rs +++ b/sqlx-sqlite/src/row.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use sqlx_core::column::ColumnIndex; use sqlx_core::error::Error; use sqlx_core::ext::ustr::UStr; -use sqlx_core::row::Row; +use sqlx_core::row::{debug_row, Row}; use sqlx_core::HashMap; use crate::statement::StatementHandle; @@ -77,6 +77,12 @@ impl ColumnIndex for &'_ str { } } +impl std::fmt::Debug for SqliteRow { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + debug_row(self, f) + } +} + // #[cfg(feature = "any")] // impl From for crate::any::AnyRow { // #[inline]