use crate::storage::schema::Value;
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PgOid {
Bool = 16,
Bytea = 17,
Int8 = 20,
Int2 = 21,
Int4 = 23,
Text = 25,
Oid = 26,
Json = 114,
Float4 = 700,
Float8 = 701,
Unknown = 705,
Varchar = 1043,
Date = 1082,
Time = 1083,
Timestamp = 1114,
TimestampTz = 1184,
Numeric = 1700,
Uuid = 2950,
Jsonb = 3802,
}
impl PgOid {
pub fn as_u32(self) -> u32 {
self as u32
}
pub fn from_value(value: &Value) -> Self {
match value {
Value::Null => PgOid::Text,
Value::Boolean(_) => PgOid::Bool,
Value::Integer(_) => PgOid::Int8,
Value::UnsignedInteger(_) => PgOid::Int8,
Value::BigInt(_) => PgOid::Int8,
Value::Float(_) => PgOid::Float8,
Value::Text(_) => PgOid::Text,
Value::Blob(_) => PgOid::Bytea,
Value::Json(_) => PgOid::Jsonb,
Value::Uuid(_) => PgOid::Uuid,
Value::Date(_) => PgOid::Date,
Value::Timestamp(_) => PgOid::TimestampTz,
Value::TimestampMs(_) => PgOid::TimestampTz,
_ => PgOid::Text,
}
}
}
pub fn value_to_pg_wire_bytes(value: &Value) -> Option<Vec<u8>> {
match value {
Value::Null => None,
Value::Boolean(b) => Some((if *b { "t" } else { "f" }).as_bytes().to_vec()),
Value::Integer(n) => Some(n.to_string().into_bytes()),
Value::UnsignedInteger(n) => Some(n.to_string().into_bytes()),
Value::BigInt(n) => Some(n.to_string().into_bytes()),
Value::Float(f) => Some(f.to_string().into_bytes()),
Value::Text(s) => Some(s.as_bytes().to_vec()),
Value::Blob(b) => {
let mut out = Vec::with_capacity(2 + b.len() * 2);
out.extend_from_slice(b"\\x");
for byte in b {
out.extend_from_slice(format!("{byte:02x}").as_bytes());
}
Some(out)
}
Value::Json(bytes) => Some(bytes.clone()),
other => Some(other.to_string().into_bytes()),
}
}