use super::{Names, PostgresData};
use amadeus_types::{Bson, Date, Decimal, Enum, Group, Json, List, Map, Time, Timestamp, Value};
use chrono::NaiveDate;
use std::{
error::Error, fmt::{self, Display}, hash::Hash
};
impl PostgresData for Vec<u8> {
fn query(f: &mut fmt::Formatter, name: Option<&Names<'_>>) -> fmt::Result {
name.unwrap().fmt(f)
}
fn decode(
_type_: &::postgres::types::Type, _buf: Option<&[u8]>,
) -> Result<Self, Box<dyn Error + Sync + Send>> {
unimplemented!()
}
}
impl PostgresData for Bson {
fn query(f: &mut fmt::Formatter, name: Option<&Names<'_>>) -> fmt::Result {
name.unwrap().fmt(f)
}
fn decode(
_type_: &::postgres::types::Type, _buf: Option<&[u8]>,
) -> Result<Self, Box<dyn Error + Sync + Send>> {
unimplemented!()
}
}
impl PostgresData for Json {
fn query(f: &mut fmt::Formatter, name: Option<&Names<'_>>) -> fmt::Result {
name.unwrap().fmt(f)
}
fn decode(
_type_: &::postgres::types::Type, _buf: Option<&[u8]>,
) -> Result<Self, Box<dyn Error + Sync + Send>> {
unimplemented!()
}
}
impl PostgresData for Enum {
fn query(f: &mut fmt::Formatter, name: Option<&Names<'_>>) -> fmt::Result {
name.unwrap().fmt(f)
}
fn decode(
_type_: &::postgres::types::Type, _buf: Option<&[u8]>,
) -> Result<Self, Box<dyn Error + Sync + Send>> {
unimplemented!()
}
}
macro_rules! impl_parquet_record_array {
($i:tt) => {
impl PostgresData for [u8; $i] {
fn query(f: &mut fmt::Formatter, name: Option<&Names<'_>>) -> fmt::Result {
name.unwrap().fmt(f)
}
fn decode(
_type_: &::postgres::types::Type, _buf: Option<&[u8]>,
) -> Result<Self, Box<dyn Error + Sync + Send>> {
unimplemented!()
}
}
impl PostgresData for Box<[u8; $i]> {
fn decode(
_type_: &::postgres::types::Type, _buf: Option<&[u8]>,
) -> Result<Self, Box<dyn Error + Sync + Send>> {
unimplemented!()
}
}
};
}
impl_parquet_record_array!(0);
impl_parquet_record_array!(1);
impl_parquet_record_array!(2);
impl_parquet_record_array!(3);
impl_parquet_record_array!(4);
impl_parquet_record_array!(5);
impl_parquet_record_array!(6);
impl_parquet_record_array!(7);
impl_parquet_record_array!(8);
impl_parquet_record_array!(9);
impl_parquet_record_array!(10);
impl_parquet_record_array!(11);
impl_parquet_record_array!(12);
impl_parquet_record_array!(13);
impl_parquet_record_array!(14);
impl_parquet_record_array!(15);
impl_parquet_record_array!(16);
impl_parquet_record_array!(17);
impl_parquet_record_array!(18);
impl_parquet_record_array!(19);
impl_parquet_record_array!(20);
impl_parquet_record_array!(21);
impl_parquet_record_array!(22);
impl_parquet_record_array!(23);
impl_parquet_record_array!(24);
impl_parquet_record_array!(25);
impl_parquet_record_array!(26);
impl_parquet_record_array!(27);
impl_parquet_record_array!(28);
impl_parquet_record_array!(29);
impl_parquet_record_array!(30);
impl_parquet_record_array!(31);
impl_parquet_record_array!(32);
macro_rules! impl_parquet_record_tuple {
($len:tt $($t:ident $i:tt)*) => (
impl<$($t,)*> PostgresData for ($($t,)*) where $($t: PostgresData,)* {
fn query(_f: &mut fmt::Formatter, _name: Option<&Names<'_>>) -> fmt::Result {
unimplemented!()
}
fn decode(_type_: &::postgres::types::Type, _buf: Option<&[u8]>) -> Result<Self, Box<dyn Error + Sync + Send>> {
unimplemented!()
}
}
);
}
impl_parquet_record_tuple!(0);
impl_parquet_record_tuple!(1 A 0);
impl_parquet_record_tuple!(2 A 0 B 1);
impl_parquet_record_tuple!(3 A 0 B 1 C 2);
impl_parquet_record_tuple!(4 A 0 B 1 C 2 D 3);
impl_parquet_record_tuple!(5 A 0 B 1 C 2 D 3 E 4);
impl_parquet_record_tuple!(6 A 0 B 1 C 2 D 3 E 4 F 5);
impl_parquet_record_tuple!(7 A 0 B 1 C 2 D 3 E 4 F 5 G 6);
impl_parquet_record_tuple!(8 A 0 B 1 C 2 D 3 E 4 F 5 G 6 H 7);
impl_parquet_record_tuple!(9 A 0 B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8);
impl_parquet_record_tuple!(10 A 0 B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8 J 9);
impl_parquet_record_tuple!(11 A 0 B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8 J 9 K 10);
impl_parquet_record_tuple!(12 A 0 B 1 C 2 D 3 E 4 F 5 G 6 H 7 I 8 J 9 K 10 L 11);
impl PostgresData for Decimal {
fn query(f: &mut fmt::Formatter, name: Option<&Names<'_>>) -> fmt::Result {
name.unwrap().fmt(f)
}
fn decode(
_type_: &::postgres::types::Type, _buf: Option<&[u8]>,
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
unimplemented!()
}
}
impl PostgresData for Group {
fn query(_f: &mut fmt::Formatter, _name: Option<&Names<'_>>) -> fmt::Result {
unimplemented!()
}
fn decode(
_type_: &::postgres::types::Type, _buf: Option<&[u8]>,
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
unimplemented!()
}
}
impl<T> PostgresData for List<T>
where
T: PostgresData,
{
fn query(_f: &mut fmt::Formatter, _name: Option<&Names<'_>>) -> fmt::Result {
unimplemented!()
}
fn decode(
_type_: &::postgres::types::Type, _buf: Option<&[u8]>,
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
unimplemented!()
}
}
impl<K, V> PostgresData for Map<K, V>
where
K: Hash + Eq + PostgresData,
V: PostgresData,
{
fn query(_f: &mut fmt::Formatter, _name: Option<&Names<'_>>) -> fmt::Result {
unimplemented!()
}
fn decode(
_type_: &::postgres::types::Type, _buf: Option<&[u8]>,
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
unimplemented!()
}
}
impl PostgresData for Date {
fn query(f: &mut fmt::Formatter, name: Option<&Names<'_>>) -> fmt::Result {
name.unwrap().fmt(f)
}
fn decode(
type_: &::postgres::types::Type, buf: Option<&[u8]>,
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
if !<NaiveDate as ::postgres::types::FromSql>::accepts(type_) {
return Err(Into::into("invalid type"));
}
<NaiveDate as ::postgres::types::FromSql>::from_sql(
type_,
buf.ok_or_else(|| Box::new(::postgres::types::WasNull))?,
)
.map(Into::into)
}
}
impl PostgresData for Time {
fn query(f: &mut fmt::Formatter, name: Option<&Names<'_>>) -> fmt::Result {
name.unwrap().fmt(f)
}
fn decode(
_type_: &::postgres::types::Type, _buf: Option<&[u8]>,
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
unimplemented!()
}
}
impl PostgresData for Timestamp {
fn query(f: &mut fmt::Formatter, name: Option<&Names<'_>>) -> fmt::Result {
name.unwrap().fmt(f)
}
fn decode(
_type_: &::postgres::types::Type, _buf: Option<&[u8]>,
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
unimplemented!()
}
}
impl PostgresData for Value {
fn query(_f: &mut fmt::Formatter, _name: Option<&Names<'_>>) -> fmt::Result {
unimplemented!()
}
fn decode(
_type_: &::postgres::types::Type, _buf: Option<&[u8]>,
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
unimplemented!()
}
}
macro_rules! impl_data_for_record {
($($t:ty : $pt:ty),*) => (
$(
#[allow(clippy::use_self)]
impl PostgresData for $t {
fn query(f: &mut fmt::Formatter, name: Option<&Names<'_>>) -> fmt::Result {
name.unwrap().fmt(f)
}
fn decode(type_: &::postgres::types::Type, buf: Option<&[u8]>) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
if !<$pt as ::postgres::types::FromSql>::accepts(type_) {
return Err(Into::into("invalid type"));
}
#[allow(trivial_numeric_casts)]
<$pt as ::postgres::types::FromSql>::from_sql(type_, buf.ok_or_else(||Box::new(::postgres::types::WasNull))?).map(|x|x as Self)
}
}
)*
);
}
impl_data_for_record!(
bool: bool,
u8: i8,
i8: i8,
u16: i16,
i16: i16,
u32: i32,
i32: i32,
u64: i64,
i64: i64,
f32: f32,
f64: f64,
String: String
);
impl<T> PostgresData for Option<T>
where
T: PostgresData,
{
fn query(f: &mut fmt::Formatter, name: Option<&Names<'_>>) -> fmt::Result {
T::query(f, name)
}
fn decode(
type_: &::postgres::types::Type, buf: Option<&[u8]>,
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
match buf {
Some(buf) => T::decode(type_, Some(buf)).map(Some),
None => Ok(None),
}
}
}