Skip to content

Commit

Permalink
JsonB and Json cell types (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
rebasedming authored and kysshsy committed Jan 12, 2025
1 parent 3749ec2 commit 5ece843
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
20 changes: 13 additions & 7 deletions supabase-wrappers/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use pgrx::{
datum::Uuid,
fcinfo,
pg_sys::{self, BuiltinOid, Datum, Oid},
varlena_to_byte_slice, AllocatedByRust, AnyNumeric, FromDatum, IntoDatum, JsonB, PgBuiltInOids,
PgOid,
varlena_to_byte_slice, AllocatedByRust, AnyNumeric, FromDatum, IntoDatum, Json, JsonB,
PgBuiltInOids, PgOid,
};
use std::collections::HashMap;
use std::ffi::CStr;
Expand Down Expand Up @@ -55,8 +55,9 @@ pub enum Cell {
Time(Time),
Timestamp(Timestamp),
Timestamptz(TimestampWithTimeZone),
Json(Json),
JsonB(JsonB),
Interval(Interval),
Json(JsonB),
Bytea(*mut pg_sys::varlena),
Uuid(Uuid),
BoolArray(Vec<Option<bool>>),
Expand Down Expand Up @@ -84,8 +85,8 @@ impl Clone for Cell {
Cell::Time(v) => Cell::Time(*v),
Cell::Timestamp(v) => Cell::Timestamp(*v),
Cell::Timestamptz(v) => Cell::Timestamptz(*v),
Cell::Interval(v) => Cell::Interval(*v),
Cell::Json(v) => Cell::Json(JsonB(v.0.clone())),
Cell::Json(v) => Cell::Json(Json(v.0.clone())),
Cell::JsonB(v) => Cell::JsonB(JsonB(v.0.clone())),
Cell::Interval(v) => Cell::Interval(v.clone()),
Cell::Bytea(v) => Cell::Bytea(*v as *mut pg_sys::varlena),
Cell::Uuid(v) => Cell::Uuid(v.clone()),
Expand Down Expand Up @@ -176,6 +177,7 @@ impl fmt::Display for Cell {
)
},
Cell::Json(v) => write!(f, "{:?}", v),
Cell::JsonB(v) => write!(f, "{:?}", v),
Cell::Interval(v) => unsafe {
let i = fcinfo::direct_function_call_as_datum(
pg_sys::interval_out,
Expand Down Expand Up @@ -245,6 +247,7 @@ impl IntoDatum for Cell {
Cell::Timestamptz(v) => v.into_datum(),
Cell::Interval(v) => v.into_datum(),
Cell::Json(v) => v.into_datum(),
Cell::JsonB(v) => v.into_datum(),
Cell::Interval(v) => v.into_datum(),
Cell::Bytea(v) => Some(Datum::from(v as *mut pg_sys::varlena)),
Cell::Uuid(v) => v.into_datum(),
Expand Down Expand Up @@ -277,7 +280,7 @@ impl IntoDatum for Cell {
|| other == pg_sys::TIMEOID
|| other == pg_sys::TIMESTAMPOID
|| other == pg_sys::TIMESTAMPTZOID
|| other == pg_sys::INTERVALOID
|| other == pg_sys::JSONOID
|| other == pg_sys::JSONBOID
|| other == pg_sys::INTERVALOID
|| other == pg_sys::BYTEAOID
Expand Down Expand Up @@ -339,8 +342,11 @@ impl FromDatum for Cell {
PgOid::BuiltIn(PgBuiltInOids::INTERVALOID) => {
Interval::from_datum(datum, is_null).map(Cell::Interval)
}
PgOid::BuiltIn(PgBuiltInOids::JSONOID) => {
Json::from_datum(datum, is_null).map(Cell::Json)
}
PgOid::BuiltIn(PgBuiltInOids::JSONBOID) => {
JsonB::from_datum(datum, is_null).map(Cell::Json)
JsonB::from_datum(datum, is_null).map(Cell::JsonB)
}
PgOid::BuiltIn(PgBuiltInOids::BYTEAOID) => {
Some(Cell::Bytea(datum.cast_mut_ptr::<pg_sys::varlena>()))
Expand Down
3 changes: 3 additions & 0 deletions supabase-wrappers/src/qual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ pub(crate) unsafe fn form_array_from_datum(
PgOid::BuiltIn(PgBuiltInOids::TIMESTAMPTZARRAYOID) => {
Cell::from_polymorphic_datum(datum, false, pg_sys::TIMESTAMPTZOID)
}
PgOid::BuiltIn(PgBuiltInOids::JSONARRAYOID) => {
Cell::from_polymorphic_datum(datum, false, pg_sys::JSONOID)
}
PgOid::BuiltIn(PgBuiltInOids::JSONBARRAYOID) => {
Cell::from_polymorphic_datum(datum, false, pg_sys::JSONBOID)
}
Expand Down

0 comments on commit 5ece843

Please sign in to comment.