Skip to content

[Breaking Change] Unify Debug implementations across PgRow, MySqlRow and SqliteRow #3890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 31 additions & 1 deletion sqlx-core/src/row.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -176,3 +177,32 @@ pub trait Row: Unpin + Send + Sync + 'static {
where
I: ColumnIndex<Self>;
}

pub fn debug_row<R>(row: &R, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
where
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this being pub but given that it's defined in the core crate and used in the database crates I don't think there's a better way.

Happy to either add docs or a #[doc(hidden)]

R: Row,
usize: ColumnIndex<R>,
<R as Row>::Database: TypeChecking,
{
write!(f, "{} ", std::any::type_name::<R>())?;

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(),
&<R as Row>::Database::fmt_value_debug(
&<<R as Row>::Database as Database>::ValueRef::to_owned(&value),
),
);
}
Err(error) => {
debug_map.entry(&column.name(), &format!("decode error: {error:?}"));
}
}
}

debug_map.finish()
}
7 changes: 6 additions & 1 deletion sqlx-mysql/src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -49,3 +48,9 @@ impl ColumnIndex<MySqlRow> for &'_ str {
.copied()
}
}

impl std::fmt::Debug for MySqlRow {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
debug_row(self, f)
}
}
25 changes: 3 additions & 22 deletions sqlx-postgres/src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -51,25 +49,8 @@ impl ColumnIndex<PgRow> 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(&<PgValueRef as ValueRef>::to_owned(&value)),
);
}
Err(error) => {
debug_map.entry(&column.name, &format!("decode error: {error:?}"));
}
}
}

debug_map.finish()
debug_row(self, f)
}
}
8 changes: 7 additions & 1 deletion sqlx-sqlite/src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,6 +77,12 @@ impl ColumnIndex<SqliteRow> 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<SqliteRow> for crate::any::AnyRow {
// #[inline]
Expand Down
Loading