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