use std::error::Error as StdError;
use prax_query::row::{RowError, RowRef, into_row_error};
use tokio_postgres::Row;
use tokio_postgres::types::{FromSql, Kind, Type};
struct AnyText(String);
impl<'a> FromSql<'a> for AnyText {
fn from_sql(_ty: &Type, raw: &'a [u8]) -> Result<Self, Box<dyn StdError + Sync + Send>> {
Ok(AnyText(std::str::from_utf8(raw)?.to_owned()))
}
fn accepts(_ty: &Type) -> bool {
true
}
}
struct NullProbe;
impl<'a> FromSql<'a> for NullProbe {
fn from_sql(_ty: &Type, _raw: &'a [u8]) -> Result<Self, Box<dyn StdError + Sync + Send>> {
Ok(NullProbe)
}
fn accepts(_ty: &Type) -> bool {
true
}
}
#[repr(transparent)]
pub struct PgRow(Row);
impl PgRow {
pub fn into_inner(self) -> Row {
self.0
}
}
impl std::ops::Deref for PgRow {
type Target = Row;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<Row> for PgRow {
fn from(row: Row) -> Self {
PgRow(row)
}
}
impl RowRef for PgRow {
fn get_i32(&self, c: &str) -> Result<i32, RowError> {
into_row_error(c, self.try_get::<_, i32>(c))
}
fn get_i32_opt(&self, c: &str) -> Result<Option<i32>, RowError> {
into_row_error(c, self.try_get::<_, Option<i32>>(c))
}
fn get_i64(&self, c: &str) -> Result<i64, RowError> {
into_row_error(c, self.try_get::<_, i64>(c))
}
fn get_i64_opt(&self, c: &str) -> Result<Option<i64>, RowError> {
into_row_error(c, self.try_get::<_, Option<i64>>(c))
}
fn get_f64(&self, c: &str) -> Result<f64, RowError> {
into_row_error(c, self.try_get::<_, f64>(c))
}
fn get_f64_opt(&self, c: &str) -> Result<Option<f64>, RowError> {
into_row_error(c, self.try_get::<_, Option<f64>>(c))
}
fn get_bool(&self, c: &str) -> Result<bool, RowError> {
into_row_error(c, self.try_get::<_, bool>(c))
}
fn get_bool_opt(&self, c: &str) -> Result<Option<bool>, RowError> {
into_row_error(c, self.try_get::<_, Option<bool>>(c))
}
fn get_str(&self, c: &str) -> Result<&str, RowError> {
into_row_error(c, self.try_get::<_, &str>(c))
}
fn get_str_opt(&self, c: &str) -> Result<Option<&str>, RowError> {
into_row_error(c, self.try_get::<_, Option<&str>>(c))
}
fn get_string(&self, c: &str) -> Result<String, RowError> {
let columns = self.0.columns();
if let Some(col) = columns.iter().find(|col| col.name() == c) {
let ty = col.type_();
if *ty == Type::UUID {
return into_row_error(c, self.try_get::<_, ::uuid::Uuid>(c))
.map(|u| u.to_string());
}
if matches!(ty.kind(), Kind::Enum(_)) {
return into_row_error(c, self.try_get::<_, AnyText>(c)).map(|t| t.0);
}
}
into_row_error(c, self.try_get::<_, &str>(c)).map(|s| s.to_string())
}
fn get_string_opt(&self, c: &str) -> Result<Option<String>, RowError> {
let columns = self.0.columns();
if let Some(col) = columns.iter().find(|col| col.name() == c) {
let ty = col.type_();
if *ty == Type::UUID {
return into_row_error(c, self.try_get::<_, Option<::uuid::Uuid>>(c))
.map(|opt| opt.map(|u| u.to_string()));
}
if matches!(ty.kind(), Kind::Enum(_)) {
return into_row_error(c, self.try_get::<_, Option<AnyText>>(c))
.map(|opt| opt.map(|t| t.0));
}
}
into_row_error(c, self.try_get::<_, Option<&str>>(c)).map(|opt| opt.map(|s| s.to_string()))
}
fn is_null(&self, c: &str) -> Result<bool, RowError> {
into_row_error(c, self.try_get::<_, Option<NullProbe>>(c)).map(|opt| opt.is_none())
}
fn get_bytes(&self, c: &str) -> Result<&[u8], RowError> {
into_row_error(c, self.try_get::<_, &[u8]>(c))
}
fn get_bytes_opt(&self, c: &str) -> Result<Option<&[u8]>, RowError> {
into_row_error(c, self.try_get::<_, Option<&[u8]>>(c))
}
fn get_datetime_utc(&self, c: &str) -> Result<chrono::DateTime<chrono::Utc>, RowError> {
into_row_error(c, self.try_get::<_, chrono::DateTime<chrono::Utc>>(c))
}
fn get_datetime_utc_opt(
&self,
c: &str,
) -> Result<Option<chrono::DateTime<chrono::Utc>>, RowError> {
into_row_error(
c,
self.try_get::<_, Option<chrono::DateTime<chrono::Utc>>>(c),
)
}
fn get_naive_datetime(&self, c: &str) -> Result<chrono::NaiveDateTime, RowError> {
into_row_error(c, self.try_get::<_, chrono::NaiveDateTime>(c))
}
fn get_naive_datetime_opt(&self, c: &str) -> Result<Option<chrono::NaiveDateTime>, RowError> {
into_row_error(c, self.try_get::<_, Option<chrono::NaiveDateTime>>(c))
}
fn get_naive_date(&self, c: &str) -> Result<chrono::NaiveDate, RowError> {
into_row_error(c, self.try_get::<_, chrono::NaiveDate>(c))
}
fn get_naive_date_opt(&self, c: &str) -> Result<Option<chrono::NaiveDate>, RowError> {
into_row_error(c, self.try_get::<_, Option<chrono::NaiveDate>>(c))
}
fn get_naive_time(&self, c: &str) -> Result<chrono::NaiveTime, RowError> {
into_row_error(c, self.try_get::<_, chrono::NaiveTime>(c))
}
fn get_naive_time_opt(&self, c: &str) -> Result<Option<chrono::NaiveTime>, RowError> {
into_row_error(c, self.try_get::<_, Option<chrono::NaiveTime>>(c))
}
fn get_uuid(&self, c: &str) -> Result<uuid::Uuid, RowError> {
into_row_error(c, self.try_get::<_, uuid::Uuid>(c))
}
fn get_uuid_opt(&self, c: &str) -> Result<Option<uuid::Uuid>, RowError> {
into_row_error(c, self.try_get::<_, Option<uuid::Uuid>>(c))
}
fn get_json(&self, c: &str) -> Result<serde_json::Value, RowError> {
into_row_error(c, self.try_get::<_, serde_json::Value>(c))
}
fn get_json_opt(&self, c: &str) -> Result<Option<serde_json::Value>, RowError> {
into_row_error(c, self.try_get::<_, Option<serde_json::Value>>(c))
}
fn get_decimal(&self, c: &str) -> Result<rust_decimal::Decimal, RowError> {
Err(RowError::TypeConversion {
column: c.to_string(),
message: "decimal columns require tokio-postgres with \
`with-rust_decimal-*` feature, which this workspace does not \
currently enable. Cast NUMERIC columns to TEXT in your SQL \
(e.g. amount::text) and decode as String, or upgrade \
tokio-postgres."
.to_string(),
})
}
fn get_decimal_opt(&self, c: &str) -> Result<Option<rust_decimal::Decimal>, RowError> {
Err(RowError::TypeConversion {
column: c.to_string(),
message: "decimal columns require tokio-postgres with \
`with-rust_decimal-*` feature, which this workspace does not \
currently enable. Cast NUMERIC columns to TEXT in your SQL \
(e.g. amount::text) and decode as String, or upgrade \
tokio-postgres."
.to_string(),
})
}
}