@@ -2,10 +2,15 @@ use crate::column::ColumnIndex;
2
2
use crate :: error:: Error ;
3
3
use crate :: message:: DataRow ;
4
4
use crate :: statement:: PgStatementMetadata ;
5
+ use crate :: type_info:: PgType ;
6
+ use crate :: types:: * ;
5
7
use crate :: value:: PgValueFormat ;
6
8
use crate :: { PgColumn , PgValueRef , Postgres } ;
9
+ use std:: fmt:: { Debug , DebugMap } ;
7
10
use std:: sync:: Arc ;
8
11
12
+ use sqlx_core:: decode:: Decode ;
13
+ use sqlx_core:: ext:: ustr:: UStr ;
9
14
pub ( crate ) use sqlx_core:: row:: Row ;
10
15
11
16
/// Implementation of [`Row`] for PostgreSQL.
@@ -48,3 +53,105 @@ impl ColumnIndex<PgRow> for &'_ str {
48
53
. map ( |v| * v)
49
54
}
50
55
}
56
+
57
+ impl Debug for PgRow {
58
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
59
+ write ! ( f, "PgRow: " ) ?;
60
+
61
+ let mut debug_map = f. debug_map ( ) ;
62
+ for ( index, column) in self . columns ( ) . iter ( ) . enumerate ( ) {
63
+ add_debug_entry ( & mut debug_map, self , index, column) ;
64
+ }
65
+
66
+ debug_map. finish ( )
67
+ }
68
+ }
69
+
70
+ macro_rules! debug_types {
71
+ { $( $enum: ident:: $variant: ident => $type: ty) ,* } => {
72
+ fn add_debug_entry(
73
+ debug_map: & mut DebugMap <' _, ' _>,
74
+ row: & PgRow ,
75
+ index: usize ,
76
+ column: & PgColumn ) {
77
+ let name = & column. name;
78
+ match row. try_get_raw( index) {
79
+ Ok ( value) => {
80
+ match column. type_info. 0 {
81
+ $(
82
+ $enum:: $variant => add_decoded_entry:: <$type>( debug_map, value, name) ,
83
+ ) *
84
+ _ => add_raw_entry( debug_map, value, name)
85
+ }
86
+ }
87
+ _ => {
88
+ debug_map. entry( name, & "NOT FOUND" ) ;
89
+ }
90
+ }
91
+
92
+ }
93
+ }
94
+ }
95
+
96
+ fn add_decoded_entry < ' r , T : Decode < ' r , Postgres > + Debug > (
97
+ debug_map : & mut DebugMap < ' _ , ' _ > ,
98
+ value : PgValueRef < ' r > ,
99
+ name : & UStr ,
100
+ ) {
101
+ match T :: decode ( value. clone ( ) ) {
102
+ Ok ( decoded_value) => {
103
+ debug_map. entry ( name, & decoded_value) ;
104
+ }
105
+ _ => {
106
+ add_raw_entry ( debug_map, value, name) ;
107
+ }
108
+ } ;
109
+ }
110
+
111
+ fn add_raw_entry ( debug_map : & mut DebugMap < ' _ , ' _ > , value : PgValueRef , name : & UStr ) {
112
+ match value. format {
113
+ PgValueFormat :: Text => debug_map. entry ( name, & value. as_str ( ) . unwrap_or_default ( ) ) ,
114
+ PgValueFormat :: Binary => debug_map. entry ( name, & value. as_bytes ( ) . unwrap_or_default ( ) ) ,
115
+ } ;
116
+ }
117
+
118
+ debug_types ! {
119
+ PgType :: Money => PgMoney ,
120
+ PgType :: MoneyArray => Vec <PgMoney >,
121
+ PgType :: Void => ( ) ,
122
+ PgType :: Bool => bool ,
123
+ PgType :: BoolArray => Vec <bool >,
124
+ PgType :: Float4 => f32 ,
125
+ PgType :: Float4Array => Vec <f32 >,
126
+ PgType :: Float8 => f64 ,
127
+ PgType :: Int4Range => PgRange <i32 >,
128
+ PgType :: Int8Range => PgRange <i64 >,
129
+ PgType :: Text => String ,
130
+ PgType :: TextArray => Vec <String >,
131
+ PgType :: Bpchar => String ,
132
+ PgType :: BpcharArray => Vec <String >,
133
+ PgType :: Name => String ,
134
+ PgType :: NameArray => Vec <String >,
135
+ PgType :: Varchar => String ,
136
+ PgType :: VarcharArray => Vec <String >,
137
+ PgType :: Interval => PgInterval ,
138
+ PgType :: IntervalArray => Vec <PgInterval >,
139
+ PgType :: Oid => Oid ,
140
+ PgType :: OidArray => Vec <Oid >,
141
+ PgType :: Char => i8 ,
142
+ PgType :: CharArray => Vec <i8 >,
143
+ PgType :: Int2 => i16 ,
144
+ PgType :: Int2Array => Vec <i16 >,
145
+ PgType :: Int4 => i32 ,
146
+ PgType :: Int4Array => Vec <i32 >,
147
+ PgType :: Int8 => i64 ,
148
+ PgType :: Int8Array => Vec <i64 >,
149
+ PgType :: Timestamp => i64 ,
150
+ PgType :: TimestampArray => Vec <i64 >,
151
+ PgType :: Time => i64 ,
152
+ PgType :: TimeArray => Vec <i64 >,
153
+ PgType :: Timestamptz => i64 ,
154
+ PgType :: TimestamptzArray => Vec <i64 >,
155
+ PgType :: Date => i32 ,
156
+ PgType :: DateArray => Vec <i32 >
157
+ }
0 commit comments