Skip to content

Commit

Permalink
Improve Cell format (#6)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
7 people committed Jan 12, 2025
1 parent 07ce529 commit b0ff56a
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions supabase-wrappers/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use pgrx::prelude::{Date, Interval, Time, Timestamp, TimestampWithTimeZone};
use pgrx::{
datum::Uuid,
fcinfo,
pg_sys::{self, bytea, BuiltinOid, Datum, Oid},
AllocatedByRust, AnyNumeric, FromDatum, IntoDatum, JsonB, PgBuiltInOids, PgOid,
pg_sys::{self, BuiltinOid, Datum, Oid},
varlena_to_byte_slice, AllocatedByRust, AnyNumeric, FromDatum, IntoDatum, JsonB, PgBuiltInOids,
PgOid,
};
use std::collections::HashMap;
use std::ffi::CStr;
Expand Down Expand Up @@ -172,16 +173,6 @@ impl fmt::Display for Cell {
.expect("timestamptz should be a valid string")
)
},
Cell::Interval(v) => write!(f, "{}", v),
Cell::TimestampTz(v) => unsafe {
let ts = fcinfo::direct_function_call_as_datum(
pg_sys::timestamptz_out,
&[(*v).into_datum()],
)
.unwrap();
let ts_cstr = CStr::from_ptr(ts.cast_mut_ptr());
write!(f, "'{}'", ts_cstr.to_str().unwrap())
},
Cell::Json(v) => write!(f, "{:?}", v),
Cell::Interval(v) => unsafe {
let i = fcinfo::direct_function_call_as_datum(
Expand All @@ -192,19 +183,44 @@ impl fmt::Display for Cell {
let i_cstr = CStr::from_ptr(i.cast_mut_ptr());
write!(f, "'{}'", i_cstr.to_str().unwrap())
},
Cell::Bytea(v) => write!(f, "{:?}", v),
Cell::Uuid(v) => write!(f, "{:?}", v),
Cell::BoolArray(v) => write!(f, "{:?}", v),
Cell::StringArray(v) => write!(f, "{:?}", v),
Cell::I16Array(v) => write!(f, "{:?}", v),
Cell::I32Array(v) => write!(f, "{:?}", v),
Cell::I64Array(v) => write!(f, "{:?}", v),
Cell::F32Array(v) => write!(f, "{:?}", v),
Cell::F64Array(v) => write!(f, "{:?}", v),
Cell::Bytea(v) => {
let byte_u8 = unsafe { varlena_to_byte_slice(*v) };
let hex = byte_u8
.iter()
.map(|b| format!("{:02X}", b))
.collect::<Vec<String>>()
.join("");
write!(f, r#"\x{}"#, hex)
}
Cell::Uuid(v) => {
write!(f, "'{}'", v)
}
Cell::BoolArray(v) => write_array(v, f),
Cell::StringArray(v) => write_array(v, f),
Cell::I16Array(v) => write_array(v, f),
Cell::I32Array(v) => write_array(v, f),
Cell::I64Array(v) => write_array(v, f),
Cell::F32Array(v) => write_array(v, f),
Cell::F64Array(v) => write_array(v, f),
}
}
}

fn write_array<T: std::fmt::Display>(
array: &[Option<T>],
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
let res = array
.iter()
.map(|e| match e {
Some(val) => format!("{}", val),
None => "null".to_owned(),
})
.collect::<Vec<String>>()
.join(",");
write!(f, "[{}]", res)
}

impl IntoDatum for Cell {
fn into_datum(self) -> Option<Datum> {
match self {
Expand Down

0 comments on commit b0ff56a

Please sign in to comment.