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 Boolean(bool),
14 // Text(String),
15 // Timestamp(chrono::DateTime<chrono::Utc>),
16}
17
18impl PGValue {
19 pub fn postgres_type(&self) -> &'static str {
20 match *self {
21 PGValue::CharacterVarying(_) => "varchar",
22 PGValue::SmallInt(_) => "int2",
23 PGValue::Integer(_) => "int4",
24 PGValue::BigInt(_) => "int8",
25 PGValue::Bytea(_) => "bytea",
26 PGValue::Boolean(_) => "boolean",
27 }
28 }
29}
30
31impl From<PropertyValue> for PGValue {
32 fn from(property: PropertyValue) -> Self {
33 match property {
34 PropertyValue::String(string) => PGValue::CharacterVarying(string),
35 PropertyValue::I16(integer) => PGValue::SmallInt(integer),
36 PropertyValue::I32(integer) => PGValue::Integer(integer),
37 PropertyValue::I64(integer) => PGValue::BigInt(integer),
38 PropertyValue::Bool(bool) => PGValue::Boolean(bool),
39 PropertyValue::Object(items) => PGValue::Bytea(items),
40 PropertyValue::Binary(items) => PGValue::Bytea(items),
41 }
42 }
43}
44
45// impl ToSql for PGValue {
46// fn to_sql(
47// &self,
48// ty: &tokio_postgres::types::Type,
49// out: &mut tokio_postgres::types::private::BytesMut,
50// ) -> Result<tokio_postgres::types::IsNull, Box<dyn std::error::Error + Sync + Send>>
51// where
52// Self: Sized,
53// {
54// match self {
55// PGValue::CharacterVarying(v) => v.to_sql(ty, out),
56// PGValue::BigInt(v) => v.to_sql(ty, out),
57// PGValue::Bytea(v) => v.to_sql(ty, out),
58// }
59// }
60
61// fn accepts(ty: &tokio_postgres::types::Type) -> bool
62// where Self: Sized {
63// match self {
64// PGValue::CharacterVarying(v) => v.accepts(ty),
65// PGValue::BigInt(v) => v.accepts(ty),
66// PGValue::Bytea(v) => v.accepts(ty),
67// }
68// }
69
70// fn to_sql_checked(
71// &self,
72// ty: &tokio_postgres::types::Type,
73// out: &mut tokio_postgres::types::private::BytesMut,
74// ) -> Result<tokio_postgres::types::IsNull, Box<dyn std::error::Error + Sync + Send>> {
75// todo!()
76// }
77// }