mysql_common/packets/binlog_request.rs
1// Copyright (c) 2021 Anatoly Ikorsky
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9use std::borrow::Cow;
10
11use crate::misc::raw::Either;
12
13use super::{BinlogDumpFlags, ComBinlogDump, ComBinlogDumpGtid, Sid};
14
15/// Binlog request representation. Please consult MySql documentation.
16///
17/// This struct is a helper builder for [`ComBinlogDump`] and [`ComBinlogDumpGtid`].
18#[derive(Debug, Clone, Eq, PartialEq, Hash)]
19pub struct BinlogRequest<'a> {
20 /// Server id of a slave.
21 server_id: u32,
22 /// If true, then `COM_BINLOG_DUMP_GTID` will be used.
23 use_gtid: bool,
24 /// If `use_gtid` is `false`, then all flags except `BINLOG_DUMP_NON_BLOCK` will be truncated.
25 flags: BinlogDumpFlags,
26 /// Filename of the binlog on the master.
27 filename: Cow<'a, [u8]>,
28 /// Position in the binlog-file to start the stream with.
29 ///
30 /// If `use_gtid` is `false`, then the value will be truncated to u32.
31 pos: u64,
32 /// SID blocks. If `use_gtid` is `false`, then this value is ignored.
33 sids: Vec<Sid<'a>>,
34}
35
36impl<'a> BinlogRequest<'a> {
37 /// Creates new request with the given slave server id.
38 pub fn new(server_id: u32) -> Self {
39 Self {
40 server_id,
41 use_gtid: false,
42 flags: BinlogDumpFlags::empty(),
43 filename: Default::default(),
44 pos: 4,
45 sids: vec![],
46 }
47 }
48
49 /// Server id of a slave.
50 pub fn server_id(&self) -> u32 {
51 self.server_id
52 }
53
54 /// If true, then `COM_BINLOG_DUMP_GTID` will be used (defaults to `false`).
55 pub fn use_gtid(&self) -> bool {
56 self.use_gtid
57 }
58
59 /// If `use_gtid` is `false`, then all flags except `BINLOG_DUMP_NON_BLOCK` will be truncated
60 /// (defaults to empty).
61 pub fn flags(&self) -> BinlogDumpFlags {
62 self.flags
63 }
64
65 /// Filename of the binlog on the master (defaults to an empty string).
66 pub fn filename_raw(&'a self) -> &'a [u8] {
67 self.filename.as_ref()
68 }
69
70 /// Filename of the binlog on the master as a UTF-8 string (lossy converted)
71 /// (defaults to an empty string).
72 pub fn filename(&'a self) -> &'a [u8] {
73 self.filename.as_ref()
74 }
75
76 /// Position in the binlog-file to start the stream with (defaults to `4`).
77 ///
78 /// If `use_gtid` is `false`, then the value will be truncated to u32.
79 pub fn pos(&self) -> u64 {
80 self.pos
81 }
82
83 /// If `use_gtid` is `false`, then this value will be ignored (defaults to an empty vector).
84 pub fn sids(&self) -> &[Sid<'_>] {
85 &self.sids
86 }
87
88 /// Returns modified `self` with the given value of the `server_id` field.
89 pub fn with_server_id(mut self, server_id: u32) -> Self {
90 self.server_id = server_id;
91 self
92 }
93
94 /// Returns modified `self` with the given value of the `use_gtid` field.
95 pub fn with_use_gtid(mut self, use_gtid: bool) -> Self {
96 self.use_gtid = use_gtid;
97 self
98 }
99
100 /// Returns modified `self` with the given value of the `flags` field.
101 pub fn with_flags(mut self, flags: BinlogDumpFlags) -> Self {
102 self.flags = flags;
103 self
104 }
105
106 /// Returns modified `self` with the given value of the `filename` field.
107 pub fn with_filename(mut self, filename: impl Into<Cow<'a, [u8]>>) -> Self {
108 self.filename = filename.into();
109 self
110 }
111
112 /// Returns modified `self` with the given value of the `pos` field.
113 pub fn with_pos<T: Into<u64>>(mut self, pos: T) -> Self {
114 self.pos = pos.into();
115 self
116 }
117
118 /// Returns modified `self` with the given value of the `sid_blocks` field.
119 pub fn with_sids<T>(mut self, sids: T) -> Self
120 where
121 T: IntoIterator<Item = Sid<'a>>,
122 {
123 self.sids = sids.into_iter().collect();
124 self
125 }
126
127 pub fn as_cmd(&self) -> Either<ComBinlogDump<'_>, ComBinlogDumpGtid<'_>> {
128 if self.use_gtid() {
129 let cmd = ComBinlogDumpGtid::new(self.server_id)
130 .with_pos(self.pos)
131 .with_flags(self.flags)
132 .with_filename(&*self.filename)
133 .with_sids(&*self.sids);
134 Either::Right(cmd)
135 } else {
136 let cmd = ComBinlogDump::new(self.server_id)
137 .with_pos(self.pos as u32)
138 .with_filename(&*self.filename)
139 .with_flags(self.flags & BinlogDumpFlags::BINLOG_DUMP_NON_BLOCK);
140 Either::Left(cmd)
141 }
142 }
143}