use postgres_protocol::Oid;
#[derive(Debug, Clone)]
pub enum BinaryParam {
Int4(i32),
Text(String),
Bool(bool),
Int8(i64),
Float8(f64),
}
pub fn format_params<P>(params: P) -> (Vec<Oid>, Vec<Option<Vec<u8>>>)
where
P: IntoIterator<Item = Option<BinaryParam>>,
{
let mut param_types: Vec<Oid> = Vec::new();
let mut param_values = Vec::new();
for param in params {
match param {
Some(BinaryParam::Int4(val)) => {
param_types.push(23); param_values.push(Some(val.to_be_bytes().to_vec()));
}
Some(BinaryParam::Text(s)) => {
param_types.push(25); param_values.push(Some(s.into_bytes()));
}
Some(BinaryParam::Bool(val)) => {
param_types.push(16); param_values.push(Some(vec![val as u8]));
}
Some(BinaryParam::Int8(val)) => {
param_types.push(20); param_values.push(Some(val.to_be_bytes().to_vec()));
}
Some(BinaryParam::Float8(val)) => {
param_types.push(701); param_values.push(Some(val.to_be_bytes().to_vec()));
}
None => {
param_types.push(0); param_values.push(None);
}
}
}
(param_types, param_values)
}