use std::borrow::Cow;
use crate::misc::raw::Either;
use super::{BinlogDumpFlags, ComBinlogDump, ComBinlogDumpGtid, Sid};
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct BinlogRequest<'a> {
server_id: u32,
use_gtid: bool,
flags: BinlogDumpFlags,
filename: Cow<'a, [u8]>,
pos: u64,
sids: Vec<Sid<'a>>,
}
impl<'a> BinlogRequest<'a> {
pub fn new(server_id: u32) -> Self {
Self {
server_id,
use_gtid: false,
flags: BinlogDumpFlags::empty(),
filename: Default::default(),
pos: 4,
sids: vec![],
}
}
pub fn server_id(&self) -> u32 {
self.server_id
}
pub fn use_gtid(&self) -> bool {
self.use_gtid
}
pub fn flags(&self) -> BinlogDumpFlags {
self.flags
}
pub fn filename_raw(&'a self) -> &'a [u8] {
self.filename.as_ref()
}
pub fn filename(&'a self) -> &'a [u8] {
self.filename.as_ref()
}
pub fn pos(&self) -> u64 {
self.pos
}
pub fn sids(&self) -> &[Sid<'_>] {
&self.sids
}
pub fn with_server_id(mut self, server_id: u32) -> Self {
self.server_id = server_id;
self
}
pub fn with_use_gtid(mut self, use_gtid: bool) -> Self {
self.use_gtid = use_gtid;
self
}
pub fn with_flags(mut self, flags: BinlogDumpFlags) -> Self {
self.flags = flags;
self
}
pub fn with_filename(mut self, filename: impl Into<Cow<'a, [u8]>>) -> Self {
self.filename = filename.into();
self
}
pub fn with_pos<T: Into<u64>>(mut self, pos: T) -> Self {
self.pos = pos.into();
self
}
pub fn with_sids<T>(mut self, sids: T) -> Self
where
T: IntoIterator<Item = Sid<'a>>,
{
self.sids = sids.into_iter().collect();
self
}
pub fn as_cmd(&self) -> Either<ComBinlogDump<'_>, ComBinlogDumpGtid<'_>> {
if self.use_gtid() {
let cmd = ComBinlogDumpGtid::new(self.server_id)
.with_pos(self.pos)
.with_flags(self.flags)
.with_filename(&*self.filename)
.with_sids(&*self.sids);
Either::Right(cmd)
} else {
let cmd = ComBinlogDump::new(self.server_id)
.with_pos(self.pos as u32)
.with_filename(&*self.filename)
.with_flags(self.flags & BinlogDumpFlags::BINLOG_DUMP_NON_BLOCK);
Either::Left(cmd)
}
}
}