pgrx/datum/into.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC.
//LICENSE
//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc.
//LICENSE
//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <contact@pgcentral.org>
//LICENSE
//LICENSE All rights reserved.
//LICENSE
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
//! for converting primitive types into Datums
//!
//! Primitive types can never be null, so we do a direct
//! cast of the primitive type to pg_sys::Datum
use crate::{pg_sys, rust_regtypein, set_varsize_4b, PgBox, PgOid, WhoAllocated};
use core::fmt::Display;
use pgrx_pg_sys::panic::ErrorReportable;
use std::{
any::Any,
ffi::{CStr, CString},
ptr::addr_of_mut,
str,
};
/// Convert a Rust type into a `pg_sys::Datum`.
///
/// Default implementations are provided for the common Rust types.
///
/// If implementing this, also implement `FromDatum` for the reverse
/// conversion.
///
/// Note that any conversions that need to allocate memory (ie, for a `varlena *` representation
/// of a Rust type, that memory **must** be allocated within a [`PgMemoryContexts`](crate::PgMemoryContexts).
pub trait IntoDatum {
fn into_datum(self) -> Option<pg_sys::Datum>;
fn type_oid() -> pg_sys::Oid;
fn composite_type_oid(&self) -> Option<pg_sys::Oid> {
None
}
/// Is a Datum of this type compatible with another Postgres type?
///
/// An example of this are the Postgres `text` and `varchar` types, which are both
/// technically compatible from a Rust type perspective. They're both represented in Rust as
/// `String` (or `&str`), but the underlying Postgres types are different.
///
/// If implementing this yourself, you likely want to follow a pattern like this:
///
/// ```rust,no_run
/// # use pgrx::*;
/// # #[repr(transparent)]
/// # struct FooType(String);
/// # impl pgrx::IntoDatum for FooType {
/// fn is_compatible_with(other: pg_sys::Oid) -> bool {
/// // first, if our type is the other type, then we're compatible
/// Self::type_oid() == other
///
/// // and here's the other type we're compatible with
/// || other == pg_sys::VARCHAROID
/// }
///
/// # fn into_datum(self) -> Option<pg_sys::Datum> {
/// # todo!()
/// # }
/// #
/// # fn type_oid() -> pg_sys::Oid {
/// # pg_sys::TEXTOID
/// # }
/// # }
/// ```
#[inline]
fn is_compatible_with(other: pg_sys::Oid) -> bool {
Self::type_oid() == other
}
}
/// for supporting NULL as the None value of an `Option<T>`
impl<T> IntoDatum for Option<T>
where
T: IntoDatum,
{
fn into_datum(self) -> Option<pg_sys::Datum> {
match self {
Some(t) => t.into_datum(),
None => None,
}
}
fn type_oid() -> pg_sys::Oid {
T::type_oid()
}
}
impl<T, E> IntoDatum for Result<T, E>
where
T: IntoDatum,
E: Any + Display,
{
/// Returns The `Option<pg_sys::Datum>` representation of this Result's `Ok` variant.
///
/// ## Panics
///
/// If this Result represents an error, then that error is raised as a Postgres ERROR, using
/// the [`ERRCODE_DATA_EXCEPTION`] error code.
///
/// If we detect that the `Err()` variant contains [ErrorReport], then we
/// directly raise that as the error. This enables users to set a specific "sql error code"
/// for a returned error, along with providing the HINT and DETAIL lines of the error.
///
/// [ErrorReport]: pg_sys::panic::ErrorReport
/// [`ERRCODE_DATA_EXCEPTION`]: pg_sys::errcodes::PgSqlErrorCode::ERRCODE_DATA_EXCEPTION
#[inline]
fn into_datum(self) -> Option<pg_sys::Datum> {
self.unwrap_or_report().into_datum()
}
#[inline]
fn type_oid() -> pg_sys::Oid {
T::type_oid()
}
}
/// for bool
impl IntoDatum for bool {
#[inline]
fn into_datum(self) -> Option<pg_sys::Datum> {
Some(pg_sys::Datum::from(if self { 1 } else { 0 }))
}
fn type_oid() -> pg_sys::Oid {
pg_sys::BOOLOID
}
}
/// for "char"
impl IntoDatum for i8 {
fn into_datum(self) -> Option<pg_sys::Datum> {
Some(pg_sys::Datum::from(self))
}
fn type_oid() -> pg_sys::Oid {
pg_sys::CHAROID
}
}
/// for smallint
impl IntoDatum for i16 {
#[inline]
fn into_datum(self) -> Option<pg_sys::Datum> {
Some(pg_sys::Datum::from(self))
}
fn type_oid() -> pg_sys::Oid {
pg_sys::INT2OID
}
fn is_compatible_with(other: pg_sys::Oid) -> bool {
Self::type_oid() == other || i8::type_oid() == other
}
}
/// for integer
impl IntoDatum for i32 {
#[inline]
fn into_datum(self) -> Option<pg_sys::Datum> {
Some(pg_sys::Datum::from(self))
}
fn type_oid() -> pg_sys::Oid {
pg_sys::INT4OID
}
fn is_compatible_with(other: pg_sys::Oid) -> bool {
Self::type_oid() == other || i8::type_oid() == other || i16::type_oid() == other
}
}
/// for bigint
impl IntoDatum for i64 {
#[inline]
fn into_datum(self) -> Option<pg_sys::Datum> {
Some(pg_sys::Datum::from(self))
}
fn type_oid() -> pg_sys::Oid {
pg_sys::INT8OID
}
fn is_compatible_with(other: pg_sys::Oid) -> bool {
Self::type_oid() == other
|| i8::type_oid() == other
|| i16::type_oid() == other
|| i32::type_oid() == other
|| i64::type_oid() == other
}
}
/// for real
impl IntoDatum for f32 {
#[inline]
fn into_datum(self) -> Option<pg_sys::Datum> {
Some(self.to_bits().into())
}
fn type_oid() -> pg_sys::Oid {
pg_sys::FLOAT4OID
}
}
/// for double precision
impl IntoDatum for f64 {
#[inline]
fn into_datum(self) -> Option<pg_sys::Datum> {
Some(self.to_bits().into())
}
fn type_oid() -> pg_sys::Oid {
pg_sys::FLOAT8OID
}
}
impl IntoDatum for pg_sys::Oid {
#[inline]
fn into_datum(self) -> Option<pg_sys::Datum> {
if self == pg_sys::Oid::INVALID {
None
} else {
Some(pg_sys::Datum::from(self.as_u32()))
}
}
#[inline]
fn type_oid() -> pg_sys::Oid {
pg_sys::OIDOID
}
}
impl IntoDatum for PgOid {
#[inline]
fn into_datum(self) -> Option<pg_sys::Datum> {
match self {
PgOid::Invalid => None,
oid => Some(oid.value().into()),
}
}
fn type_oid() -> pg_sys::Oid {
pg_sys::OIDOID
}
}
// for text, varchar
macro_rules! impl_into_datum_str {
($t:ty) => {
impl IntoDatum for $t {
#[inline]
fn into_datum(self) -> Option<$crate::pg_sys::Datum> {
self.as_bytes().into_datum()
}
#[inline]
fn type_oid() -> pg_sys::Oid {
pg_sys::TEXTOID
}
#[inline]
fn is_compatible_with(other: pg_sys::Oid) -> bool {
Self::type_oid() == other || other == pg_sys::VARCHAROID
}
}
};
}
impl_into_datum_str!(String);
impl_into_datum_str!(&String);
impl_into_datum_str!(&str);
impl IntoDatum for char {
#[inline]
fn into_datum(self) -> Option<pg_sys::Datum> {
let mut buf = [0; 4];
self.encode_utf8(&mut buf);
unsafe {
// SAFETY: The buffer contains only valid UTF8 data
// coming from the encode_utf8 method used above.
let len = self.len_utf8();
str::from_utf8_unchecked(&buf[..len]).into_datum()
}
}
fn type_oid() -> pg_sys::Oid {
pg_sys::VARCHAROID
}
#[inline]
fn is_compatible_with(other: pg_sys::Oid) -> bool {
Self::type_oid() == other || other == pg_sys::VARCHAROID
}
}
// for cstring
macro_rules! impl_into_datum_c_str {
($t:ty) => {
impl IntoDatum for $t {
#[inline]
fn into_datum(self) -> Option<$crate::pg_sys::Datum> {
unsafe {
// SAFETY: A `CStr` has already been validated to be a non-null pointer to a null-terminated
// "char *", and it won't ever overlap with a newly palloc'd block of memory. Using
// `to_bytes_with_nul()` ensures that we'll never try to palloc zero bytes -- it'll at
// least always be 1 byte to hold the null terminator for the empty string.
//
// This is akin to Postgres' `pg_sys::pstrdup` or even `pg_sys::pnstrdup` functions, but
// doing the copy ourselves allows us to elide the "strlen" or "strnlen" operations those
// functions need to do; the byte slice returned from `to_bytes_with_nul` knows its length.
let src = self.as_ref().to_bytes_with_nul();
let dst = $crate::pg_sys::palloc(src.len());
dst.copy_from(src.as_ptr().cast(), src.len());
Some(dst.into())
}
}
#[inline]
fn type_oid() -> pg_sys::Oid {
pg_sys::CSTRINGOID
}
}
};
}
impl_into_datum_c_str!(CString);
impl_into_datum_c_str!(&CString);
impl_into_datum_c_str!(&CStr);
/// for bytea
impl<'a> IntoDatum for &'a [u8] {
/// # Panics
///
/// This function will panic if the string being converted to a datum
// is longer than 1 GiB including 4 bytes used for a header.
#[inline]
fn into_datum(self) -> Option<pg_sys::Datum> {
let len = self.len().saturating_add(pg_sys::VARHDRSZ);
assert!(len < (u32::MAX as usize >> 2));
unsafe {
// SAFETY: palloc gives us a valid pointer and if there's not enough memory it'll raise an error
let varlena = pg_sys::palloc(len) as *mut pg_sys::varlena;
// SAFETY: `varlena` can properly cast into a `varattrib_4b` and all of what it contains is properly
// allocated thanks to our call to `palloc` above
let varattrib_4b: *mut _ =
&mut varlena.cast::<pg_sys::varattrib_4b>().as_mut().unwrap_unchecked().va_4byte;
// This is the same as Postgres' `#define SET_VARSIZE_4B` (which have over in
// `pgrx/src/varlena.rs`), however we're asserting above that the input string
// isn't too big for a Postgres varlena, since it's limited to 32 bits and,
// in reality, it's a quarter that length, but this is good enough
set_varsize_4b(varlena, len as i32);
// SAFETY: src and dest pointers are valid, exactly `self.len()` bytes long,
// and the `dest` was freshly allocated, thus non-overlapping
std::ptr::copy_nonoverlapping(
self.as_ptr(),
addr_of_mut!((*varattrib_4b).va_data).cast::<u8>(),
self.len(),
);
Some(pg_sys::Datum::from(varlena))
}
}
#[inline]
fn type_oid() -> pg_sys::Oid {
pg_sys::BYTEAOID
}
}
impl IntoDatum for Vec<u8> {
#[inline]
fn into_datum(self) -> Option<pg_sys::Datum> {
(&self[..]).into_datum()
}
#[inline]
fn type_oid() -> pg_sys::Oid {
pg_sys::BYTEAOID
}
}
/// for VOID
impl IntoDatum for () {
#[inline]
fn into_datum(self) -> Option<pg_sys::Datum> {
// VOID isn't very useful, but Postgres represents it as a non-null Datum with a zero value
Some(pg_sys::Datum::from(0))
}
fn type_oid() -> pg_sys::Oid {
pg_sys::VOIDOID
}
}
/// for user types
impl<T, AllocatedBy: WhoAllocated> IntoDatum for PgBox<T, AllocatedBy> {
#[inline]
fn into_datum(self) -> Option<pg_sys::Datum> {
if self.is_null() {
None
} else {
Some(self.into_pg().into())
}
}
fn type_oid() -> pg_sys::Oid {
rust_regtypein::<T>()
}
}
impl IntoDatum for pg_sys::Datum {
fn into_datum(self) -> Option<pg_sys::Datum> {
Some(self)
}
fn type_oid() -> pg_sys::Oid {
pg_sys::INT8OID
}
}