Skip to content

Commit cedc9da

Browse files
davidcornukriswuollett
authored andcommitted
feat: Unify Debug implementations across PgRow, MySqlRow and SqliteRow (launchbadge#3890)
* Introduce `debug_row` function * Use `debug_row` to implement `Debug` for `SqliteRow` * Use `debug_row` in `PgRow`'s `Debug` implementation * Match `MySqlRow`'s `Debug` implementation
1 parent e7cd057 commit cedc9da

File tree

4 files changed

+47
-25
lines changed

4 files changed

+47
-25
lines changed

sqlx-core/src/row.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use crate::column::ColumnIndex;
1+
use crate::column::{Column, ColumnIndex};
22
use crate::database::Database;
33
use crate::decode::Decode;
44
use crate::error::{mismatched_types, Error};
55

6+
use crate::type_checking::TypeChecking;
67
use crate::type_info::TypeInfo;
78
use crate::types::Type;
89
use crate::value::ValueRef;
@@ -176,3 +177,32 @@ pub trait Row: Unpin + Send + Sync + 'static {
176177
where
177178
I: ColumnIndex<Self>;
178179
}
180+
181+
pub fn debug_row<R>(row: &R, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
182+
where
183+
R: Row,
184+
usize: ColumnIndex<R>,
185+
<R as Row>::Database: TypeChecking,
186+
{
187+
write!(f, "{} ", std::any::type_name::<R>())?;
188+
189+
let mut debug_map = f.debug_map();
190+
191+
for column in row.columns().iter() {
192+
match row.try_get_raw(column.ordinal()) {
193+
Ok(value) => {
194+
debug_map.entry(
195+
&column.name(),
196+
&<R as Row>::Database::fmt_value_debug(
197+
&<<R as Row>::Database as Database>::ValueRef::to_owned(&value),
198+
),
199+
);
200+
}
201+
Err(error) => {
202+
debug_map.entry(&column.name(), &format!("decode error: {error:?}"));
203+
}
204+
}
205+
}
206+
207+
debug_map.finish()
208+
}

sqlx-mysql/src/row.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::HashMap;
99
use crate::{protocol, MySql, MySqlColumn, MySqlValueFormat, MySqlValueRef};
1010

1111
/// Implementation of [`Row`] for MySQL.
12-
#[derive(Debug)]
1312
pub struct MySqlRow {
1413
pub(crate) row: protocol::Row,
1514
pub(crate) format: MySqlValueFormat,
@@ -49,3 +48,9 @@ impl ColumnIndex<MySqlRow> for &'_ str {
4948
.copied()
5049
}
5150
}
51+
52+
impl std::fmt::Debug for MySqlRow {
53+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54+
debug_row(self, f)
55+
}
56+
}

sqlx-postgres/src/row.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ use crate::message::DataRow;
44
use crate::statement::PgStatementMetadata;
55
use crate::value::PgValueFormat;
66
use crate::{PgColumn, PgValueRef, Postgres};
7+
use sqlx_core::row::debug_row;
78
pub(crate) use sqlx_core::row::Row;
8-
use sqlx_core::type_checking::TypeChecking;
9-
use sqlx_core::value::ValueRef;
10-
use std::fmt::Debug;
119
use std::sync::Arc;
1210

1311
/// Implementation of [`Row`] for PostgreSQL.
@@ -51,25 +49,8 @@ impl ColumnIndex<PgRow> for &'_ str {
5149
}
5250
}
5351

54-
impl Debug for PgRow {
52+
impl std::fmt::Debug for PgRow {
5553
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56-
write!(f, "PgRow ")?;
57-
58-
let mut debug_map = f.debug_map();
59-
for (index, column) in self.columns().iter().enumerate() {
60-
match self.try_get_raw(index) {
61-
Ok(value) => {
62-
debug_map.entry(
63-
&column.name,
64-
&Postgres::fmt_value_debug(&<PgValueRef as ValueRef>::to_owned(&value)),
65-
);
66-
}
67-
Err(error) => {
68-
debug_map.entry(&column.name, &format!("decode error: {error:?}"));
69-
}
70-
}
71-
}
72-
73-
debug_map.finish()
54+
debug_row(self, f)
7455
}
7556
}

sqlx-sqlite/src/row.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::Arc;
55
use sqlx_core::column::ColumnIndex;
66
use sqlx_core::error::Error;
77
use sqlx_core::ext::ustr::UStr;
8-
use sqlx_core::row::Row;
8+
use sqlx_core::row::{debug_row, Row};
99
use sqlx_core::HashMap;
1010

1111
use crate::statement::StatementHandle;
@@ -77,6 +77,12 @@ impl ColumnIndex<SqliteRow> for &'_ str {
7777
}
7878
}
7979

80+
impl std::fmt::Debug for SqliteRow {
81+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82+
debug_row(self, f)
83+
}
84+
}
85+
8086
// #[cfg(feature = "any")]
8187
// impl From<SqliteRow> for crate::any::AnyRow {
8288
// #[inline]

0 commit comments

Comments
 (0)