Skip to content

Commit b0ff56a

Browse files
kysshsyromaingrxburmeciaimorremilapeyre
committed
Improve Cell format (#6)
Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: Romain Graux <[email protected]> Co-authored-by: Bo Lu <[email protected]> Co-authored-by: Bo Lu <[email protected]> Co-authored-by: Raminder Singh <[email protected]> Co-authored-by: Rémi Lapeyre <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent 07ce529 commit b0ff56a

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

supabase-wrappers/src/interface.rs

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use pgrx::prelude::{Date, Interval, Time, Timestamp, TimestampWithTimeZone};
88
use pgrx::{
99
datum::Uuid,
1010
fcinfo,
11-
pg_sys::{self, bytea, BuiltinOid, Datum, Oid},
12-
AllocatedByRust, AnyNumeric, FromDatum, IntoDatum, JsonB, PgBuiltInOids, PgOid,
11+
pg_sys::{self, BuiltinOid, Datum, Oid},
12+
varlena_to_byte_slice, AllocatedByRust, AnyNumeric, FromDatum, IntoDatum, JsonB, PgBuiltInOids,
13+
PgOid,
1314
};
1415
use std::collections::HashMap;
1516
use std::ffi::CStr;
@@ -172,16 +173,6 @@ impl fmt::Display for Cell {
172173
.expect("timestamptz should be a valid string")
173174
)
174175
},
175-
Cell::Interval(v) => write!(f, "{}", v),
176-
Cell::TimestampTz(v) => unsafe {
177-
let ts = fcinfo::direct_function_call_as_datum(
178-
pg_sys::timestamptz_out,
179-
&[(*v).into_datum()],
180-
)
181-
.unwrap();
182-
let ts_cstr = CStr::from_ptr(ts.cast_mut_ptr());
183-
write!(f, "'{}'", ts_cstr.to_str().unwrap())
184-
},
185176
Cell::Json(v) => write!(f, "{:?}", v),
186177
Cell::Interval(v) => unsafe {
187178
let i = fcinfo::direct_function_call_as_datum(
@@ -192,19 +183,44 @@ impl fmt::Display for Cell {
192183
let i_cstr = CStr::from_ptr(i.cast_mut_ptr());
193184
write!(f, "'{}'", i_cstr.to_str().unwrap())
194185
},
195-
Cell::Bytea(v) => write!(f, "{:?}", v),
196-
Cell::Uuid(v) => write!(f, "{:?}", v),
197-
Cell::BoolArray(v) => write!(f, "{:?}", v),
198-
Cell::StringArray(v) => write!(f, "{:?}", v),
199-
Cell::I16Array(v) => write!(f, "{:?}", v),
200-
Cell::I32Array(v) => write!(f, "{:?}", v),
201-
Cell::I64Array(v) => write!(f, "{:?}", v),
202-
Cell::F32Array(v) => write!(f, "{:?}", v),
203-
Cell::F64Array(v) => write!(f, "{:?}", v),
186+
Cell::Bytea(v) => {
187+
let byte_u8 = unsafe { varlena_to_byte_slice(*v) };
188+
let hex = byte_u8
189+
.iter()
190+
.map(|b| format!("{:02X}", b))
191+
.collect::<Vec<String>>()
192+
.join("");
193+
write!(f, r#"\x{}"#, hex)
194+
}
195+
Cell::Uuid(v) => {
196+
write!(f, "'{}'", v)
197+
}
198+
Cell::BoolArray(v) => write_array(v, f),
199+
Cell::StringArray(v) => write_array(v, f),
200+
Cell::I16Array(v) => write_array(v, f),
201+
Cell::I32Array(v) => write_array(v, f),
202+
Cell::I64Array(v) => write_array(v, f),
203+
Cell::F32Array(v) => write_array(v, f),
204+
Cell::F64Array(v) => write_array(v, f),
204205
}
205206
}
206207
}
207208

209+
fn write_array<T: std::fmt::Display>(
210+
array: &[Option<T>],
211+
f: &mut fmt::Formatter<'_>,
212+
) -> fmt::Result {
213+
let res = array
214+
.iter()
215+
.map(|e| match e {
216+
Some(val) => format!("{}", val),
217+
None => "null".to_owned(),
218+
})
219+
.collect::<Vec<String>>()
220+
.join(",");
221+
write!(f, "[{}]", res)
222+
}
223+
208224
impl IntoDatum for Cell {
209225
fn into_datum(self) -> Option<Datum> {
210226
match self {

0 commit comments

Comments
 (0)