ankurah_storage_postgres/value.rs
1// use tokio_postgres::types::ToSql;
2
3use ankurah_core::property::PropertyValue;
4
5#[derive(Debug)]
6pub enum PGValue {
7 // Boolean(bool),
8 Bytea(Vec<u8>),
9 CharacterVarying(String),
10 SmallInt(i16),
11 Integer(i32),
12 BigInt(i64),
13 // Text(String),
14 // Timestamp(chrono::DateTime<chrono::Utc>),
15}
16
17impl PGValue {
18 pub fn postgres_type(&self) -> &'static str {
19 match *self {
20 PGValue::CharacterVarying(_) => "varchar",
21 PGValue::SmallInt(_) => "int2",
22 PGValue::Integer(_) => "int4",
23 PGValue::BigInt(_) => "int8",
24 PGValue::Bytea(_) => "bytea",
25 }
26 }
27}
28
29impl From<PropertyValue> for PGValue {
30 fn from(property: PropertyValue) -> Self {
31 match property {
32 PropertyValue::String(string) => PGValue::CharacterVarying(string),
33 PropertyValue::I16(integer) => PGValue::SmallInt(integer),
34 PropertyValue::I32(integer) => PGValue::Integer(integer),
35 PropertyValue::I64(integer) => PGValue::BigInt(integer),
36 PropertyValue::Object(items) => PGValue::Bytea(items),
37 PropertyValue::Binary(items) => PGValue::Bytea(items),
38 }
39 }
40}
41
42// impl ToSql for PGValue {
43// fn to_sql(
44// &self,
45// ty: &tokio_postgres::types::Type,
46// out: &mut tokio_postgres::types::private::BytesMut,
47// ) -> Result<tokio_postgres::types::IsNull, Box<dyn std::error::Error + Sync + Send>>
48// where
49// Self: Sized,
50// {
51// match self {
52// PGValue::CharacterVarying(v) => v.to_sql(ty, out),
53// PGValue::BigInt(v) => v.to_sql(ty, out),
54// PGValue::Bytea(v) => v.to_sql(ty, out),
55// }
56// }
57
58// fn accepts(ty: &tokio_postgres::types::Type) -> bool
59// where Self: Sized {
60// match self {
61// PGValue::CharacterVarying(v) => v.accepts(ty),
62// PGValue::BigInt(v) => v.accepts(ty),
63// PGValue::Bytea(v) => v.accepts(ty),
64// }
65// }
66
67// fn to_sql_checked(
68// &self,
69// ty: &tokio_postgres::types::Type,
70// out: &mut tokio_postgres::types::private::BytesMut,
71// ) -> Result<tokio_postgres::types::IsNull, Box<dyn std::error::Error + Sync + Send>> {
72// todo!()
73// }
74// }