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 Bytea(Vec<u8>),
8 CharacterVarying(String),
9 SmallInt(i16),
10 Integer(i32),
11 BigInt(i64),
12 Boolean(bool),
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 PGValue::Boolean(_) => "boolean",
26 }
27 }
28}
29
30impl From<PropertyValue> for PGValue {
31 fn from(property: PropertyValue) -> Self {
32 match property {
33 PropertyValue::String(string) => PGValue::CharacterVarying(string),
34 PropertyValue::I16(integer) => PGValue::SmallInt(integer),
35 PropertyValue::I32(integer) => PGValue::Integer(integer),
36 PropertyValue::I64(integer) => PGValue::BigInt(integer),
37 PropertyValue::Bool(bool) => PGValue::Boolean(bool),
38 PropertyValue::Object(items) => PGValue::Bytea(items),
39 PropertyValue::Binary(items) => PGValue::Bytea(items),
40 }
41 }
42}
43
44// impl ToSql for PGValue {
45// fn to_sql(
46// &self,
47// ty: &tokio_postgres::types::Type,
48// out: &mut tokio_postgres::types::private::BytesMut,
49// ) -> Result<tokio_postgres::types::IsNull, Box<dyn std::error::Error + Sync + Send>>
50// where
51// Self: Sized,
52// {
53// match self {
54// PGValue::CharacterVarying(v) => v.to_sql(ty, out),
55// PGValue::BigInt(v) => v.to_sql(ty, out),
56// PGValue::Bytea(v) => v.to_sql(ty, out),
57// }
58// }
59
60// fn accepts(ty: &tokio_postgres::types::Type) -> bool
61// where Self: Sized {
62// match self {
63// PGValue::CharacterVarying(v) => v.accepts(ty),
64// PGValue::BigInt(v) => v.accepts(ty),
65// PGValue::Bytea(v) => v.accepts(ty),
66// }
67// }
68
69// fn to_sql_checked(
70// &self,
71// ty: &tokio_postgres::types::Type,
72// out: &mut tokio_postgres::types::private::BytesMut,
73// ) -> Result<tokio_postgres::types::IsNull, Box<dyn std::error::Error + Sync + Send>> {
74// todo!()
75// }
76// }