reddb_server/wire/postgres/
types.rs1use crate::storage::schema::Value;
13
14#[allow(dead_code)]
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum PgOid {
18 Bool = 16,
19 Bytea = 17,
20 Int8 = 20,
21 Int2 = 21,
22 Int4 = 23,
23 Text = 25,
24 Oid = 26,
25 Json = 114,
26 Float4 = 700,
27 Float8 = 701,
28 Unknown = 705,
29 Varchar = 1043,
30 Date = 1082,
31 Time = 1083,
32 Timestamp = 1114,
33 TimestampTz = 1184,
34 Numeric = 1700,
35 Uuid = 2950,
36 Jsonb = 3802,
37}
38
39impl PgOid {
40 pub fn as_u32(self) -> u32 {
41 self as u32
42 }
43
44 pub fn from_value(value: &Value) -> Self {
47 match value {
48 Value::Null => PgOid::Text,
49 Value::Boolean(_) => PgOid::Bool,
50 Value::Integer(_) => PgOid::Int8,
51 Value::UnsignedInteger(_) => PgOid::Int8,
52 Value::BigInt(_) => PgOid::Int8,
53 Value::Float(_) => PgOid::Float8,
54 Value::Text(_) => PgOid::Text,
55 Value::Blob(_) => PgOid::Bytea,
56 Value::Json(_) => PgOid::Jsonb,
57 Value::Uuid(_) => PgOid::Uuid,
58 Value::Date(_) => PgOid::Date,
59 Value::Timestamp(_) => PgOid::TimestampTz,
60 Value::TimestampMs(_) => PgOid::TimestampTz,
61 _ => PgOid::Text,
63 }
64 }
65}
66
67pub fn value_to_pg_wire_bytes(value: &Value) -> Option<Vec<u8>> {
74 match value {
75 Value::Null => None,
76 Value::Boolean(b) => Some((if *b { "t" } else { "f" }).as_bytes().to_vec()),
77 Value::Integer(n) => Some(n.to_string().into_bytes()),
78 Value::UnsignedInteger(n) => Some(n.to_string().into_bytes()),
79 Value::BigInt(n) => Some(n.to_string().into_bytes()),
80 Value::Float(f) => Some(f.to_string().into_bytes()),
81 Value::Text(s) => Some(s.as_bytes().to_vec()),
82 Value::Blob(b) => {
83 let mut out = Vec::with_capacity(2 + b.len() * 2);
85 out.extend_from_slice(b"\\x");
86 for byte in b {
87 out.extend_from_slice(format!("{byte:02x}").as_bytes());
88 }
89 Some(out)
90 }
91 Value::Json(bytes) => Some(bytes.clone()),
92 other => Some(other.to_string().into_bytes()),
96 }
97}