Skip to main content

mysql_replication/model/
schema.rs

1use crate::{Error, Result};
2use serde::{Deserialize, Serialize};
3use std::collections::HashSet;
4
5/// Metadata is ordered by INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION.
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7#[serde(deny_unknown_fields)]
8pub struct ColumnMeta {
9    pub name: String,
10    pub data_type: String,
11    pub column_type: String,
12    pub nullable: bool,
13    pub primary_key: bool,
14    pub charset: Option<String>,
15    pub octet_length: Option<u64>,
16    pub numeric_precision: Option<u32>,
17    pub numeric_scale: Option<u32>,
18    pub datetime_precision: Option<u32>,
19    pub extra: String,
20}
21
22#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
23#[serde(deny_unknown_fields)]
24pub struct TableMeta {
25    pub schema: String,
26    pub table: String,
27    pub columns: Vec<ColumnMeta>,
28}
29
30impl TableMeta {
31    pub fn validate(&self) -> Result<usize> {
32        if self.schema.is_empty()
33            || self.table.is_empty()
34            || self.columns.is_empty()
35            || self.columns.len() > 4096
36        {
37            return Err(Error::Schema(
38                "empty table name/columns or more than 4096 columns".into(),
39            ));
40        }
41        let mut names = HashSet::new();
42        for col in &self.columns {
43            if col.name.is_empty() || !names.insert(&col.name) {
44                return Err(Error::Schema("empty or duplicate column name".into()));
45            }
46            if col.extra.contains("VIRTUAL GENERATED")
47                || col.extra.contains("STORED GENERATED")
48                || col.extra.contains("INVISIBLE")
49            {
50                return Err(Error::Unsupported(format!(
51                    "{}.{}.{}: generated/invisible columns",
52                    self.schema, self.table, col.name
53                )));
54            }
55        }
56        let keys: Vec<_> = self
57            .columns
58            .iter()
59            .enumerate()
60            .filter(|(_, c)| c.primary_key)
61            .collect();
62        if keys.len() != 1 || keys[0].1.nullable {
63            return Err(Error::Schema(format!(
64                "{}.{} needs exactly one NOT NULL primary-key column",
65                self.schema, self.table
66            )));
67        }
68        key_decoder(keys[0].1)?;
69        Ok(keys[0].0)
70    }
71}
72
73pub(crate) type KeyDecoder = for<'a> fn(&'a [u8], &ColumnMeta) -> Result<crate::PrimaryKey<'a>>;
74
75pub(crate) fn key_decoder(col: &ColumnMeta) -> Result<KeyDecoder> {
76    let unsigned = col
77        .column_type
78        .to_ascii_lowercase()
79        .split_ascii_whitespace()
80        .any(|s| s == "unsigned");
81    Ok(match (col.data_type.as_str(), unsigned) {
82        ("tinyint", false) => integer::<1, false>,
83        ("tinyint", true) => integer::<1, true>,
84        ("smallint", false) => integer::<2, false>,
85        ("smallint", true) => integer::<2, true>,
86        ("mediumint", false) => integer::<3, false>,
87        ("mediumint", true) => integer::<3, true>,
88        ("int", false) => integer::<4, false>,
89        ("int", true) => integer::<4, true>,
90        ("bigint", false) => integer::<8, false>,
91        ("bigint", true) => integer::<8, true>,
92        ("decimal", _) => {
93            if !matches!((col.numeric_precision, col.numeric_scale), (Some(p), Some(s)) if p > 0 && p <= 65 && s <= p && s <= 30)
94            {
95                return Err(Error::Schema("invalid DECIMAL precision/scale".into()));
96            }
97            decimal
98        }
99        ("char" | "varchar", _) => match col.charset.as_deref() {
100            Some("utf8" | "utf8mb3" | "utf8mb4") => text,
101            Some("ascii") => ascii,
102            Some("latin1") => latin1,
103            other => return Err(Error::Unsupported(format!("primary-key charset {other:?}"))),
104        },
105        other => {
106            return Err(Error::Unsupported(format!(
107                "primary-key type {other:?}; use integer, DECIMAL, CHAR or VARCHAR"
108            )));
109        }
110    })
111}
112
113fn integer<'a, const N: usize, const UNSIGNED: bool>(
114    data: &'a [u8],
115    _: &ColumnMeta,
116) -> Result<crate::PrimaryKey<'a>> {
117    if data.len() != N {
118        return Err(crate::error::invalid("incorrect integer width"));
119    }
120    let mut bytes = [0; 8];
121    bytes[..N].copy_from_slice(data);
122    let value = u64::from_le_bytes(bytes);
123    Ok(if UNSIGNED {
124        crate::PrimaryKey::Unsigned(value)
125    } else {
126        let shift = (8 - N) * 8;
127        crate::PrimaryKey::Signed(((value << shift) as i64) >> shift)
128    })
129}
130
131fn text<'a>(data: &'a [u8], col: &ColumnMeta) -> Result<crate::PrimaryKey<'a>> {
132    let value = std::str::from_utf8(data)?;
133    // SQL CHAR retrieval removes trailing space padding.
134    let value = if col.data_type == "char" {
135        value.trim_end_matches(' ')
136    } else {
137        value
138    };
139    Ok(crate::PrimaryKey::Text(value.into()))
140}
141fn ascii<'a>(data: &'a [u8], col: &ColumnMeta) -> Result<crate::PrimaryKey<'a>> {
142    if !data.is_ascii() {
143        return Err(crate::error::invalid("non-ASCII primary key"));
144    }
145    text(data, col)
146}
147fn latin1<'a>(data: &'a [u8], col: &ColumnMeta) -> Result<crate::PrimaryKey<'a>> {
148    let value = encoding_rs::WINDOWS_1252
149        .decode_without_bom_handling_and_without_replacement(data)
150        .ok_or_else(|| crate::error::invalid("invalid latin1 primary key"))?;
151    Ok(crate::PrimaryKey::Text(if col.data_type == "char" {
152        value.trim_end_matches(' ').to_owned().into()
153    } else {
154        value
155    }))
156}
157fn decimal<'a>(data: &'a [u8], col: &ColumnMeta) -> Result<crate::PrimaryKey<'a>> {
158    use std::fmt::Write;
159    let precision =
160        col.numeric_precision
161            .ok_or_else(|| crate::error::invalid("missing decimal precision"))? as usize;
162    let scale = col
163        .numeric_scale
164        .ok_or_else(|| crate::error::invalid("missing decimal scale"))? as usize;
165    if precision == 0 || precision > 65 || scale > precision || scale > 30 {
166        return Err(crate::error::invalid("invalid decimal precision/scale"));
167    }
168    const BYTES: [usize; 10] = [0, 1, 1, 2, 2, 3, 3, 4, 4, 4];
169    let negative = data
170        .first()
171        .ok_or_else(|| crate::error::invalid("empty decimal"))?
172        & 0x80
173        == 0;
174    let mut first = true;
175    let mut cursor = crate::protocol::mysql::wire::Cursor::new(data);
176    let mut nonzero = false;
177    let mut group = |digits: usize| -> Result<u32> {
178        let mut value = 0u32;
179        for &raw in cursor.take(BYTES[digits])? {
180            let mut b = raw;
181            if first {
182                b ^= 0x80;
183                first = false;
184            }
185            if negative {
186                b = !b;
187            }
188            value = (value << 8) | u32::from(b);
189        }
190        if value >= 10u32.pow(digits as u32) {
191            return Err(crate::error::invalid("invalid packed decimal digit group"));
192        }
193        nonzero |= value != 0;
194        Ok(value)
195    };
196    let mut value = String::with_capacity(precision + 3);
197    if negative {
198        value.push('-');
199    }
200    let integral = precision - scale;
201    let mut started = false;
202    let groups = std::iter::once(integral % 9)
203        .filter(|&n| n != 0)
204        .chain(std::iter::repeat_n(9, integral / 9));
205    for digits in groups {
206        let n = group(digits)?;
207        if started {
208            write!(&mut value, "{n:09}")
209                .map_err(|_| crate::error::invalid("decimal formatting"))?;
210        } else if n != 0 {
211            write!(&mut value, "{n}").map_err(|_| crate::error::invalid("decimal formatting"))?;
212            started = true;
213        }
214    }
215    if !started {
216        value.push('0');
217    }
218    if scale != 0 {
219        value.push('.');
220        for digits in
221            std::iter::repeat_n(9, scale / 9).chain(std::iter::once(scale % 9).filter(|&n| n != 0))
222        {
223            let n = group(digits)?;
224            write!(&mut value, "{n:0digits$}")
225                .map_err(|_| crate::error::invalid("decimal formatting"))?;
226        }
227    }
228    if !cursor.remaining().is_empty() {
229        return Err(crate::error::invalid("extra decimal bytes"));
230    }
231    if negative && !nonzero {
232        value.remove(0);
233    }
234    Ok(crate::PrimaryKey::Decimal(value))
235}