zero-mysql 0.6.0

A high-performance MySQL client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use std::os::raw as libc;
use std::sync::Arc;

use diesel::QueryResult;
use diesel::mysql::MysqlType;
use diesel::mysql::data_types::{MysqlTime, MysqlTimestampType};

use crate::constant::{ColumnFlags, ColumnType};
use crate::error::eyre;
use crate::protocol::BinaryRowPayload;
use crate::protocol::command::ColumnDefinition;
use crate::protocol::primitive::read_string_lenenc;
use crate::protocol::response::OkPayloadBytes;
use crate::protocol::r#trait::BinaryResultSetHandler;

use super::row::ZeroMysqlRow;

pub struct ColumnInfo {
    pub name: String,
    pub column_type: MysqlType,
}

pub struct Cursor {
    columns: Arc<[ColumnInfo]>,
    rows: Vec<Vec<Option<Vec<u8>>>>,
    current: usize,
}

impl Cursor {
    pub(in crate::diesel) fn new(
        columns: Arc<[ColumnInfo]>,
        rows: Vec<Vec<Option<Vec<u8>>>>,
    ) -> Self {
        Self {
            columns,
            rows,
            current: 0,
        }
    }
}

impl Iterator for Cursor {
    type Item = QueryResult<ZeroMysqlRow>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current >= self.rows.len() {
            return None;
        }
        let idx = self.current;
        self.current += 1;
        let values = std::mem::take(&mut self.rows[idx]);
        Some(Ok(ZeroMysqlRow {
            columns: Arc::clone(&self.columns),
            values,
        }))
    }
}

pub(in crate::diesel) struct CollectRawHandler {
    pub columns: Vec<ColumnInfo>,
    pub rows: Vec<Vec<Option<Vec<u8>>>>,
}

impl CollectRawHandler {
    pub fn new() -> Self {
        Self {
            columns: Vec::new(),
            rows: Vec::new(),
        }
    }
}

/// Map zero-mysql ColumnType + ColumnFlags to diesel MysqlType.
fn to_mysql_type(col_type: ColumnType, flags: ColumnFlags) -> MysqlType {
    let unsigned = flags.contains(ColumnFlags::UNSIGNED_FLAG);
    match col_type {
        ColumnType::MYSQL_TYPE_TINY => {
            if unsigned {
                MysqlType::UnsignedTiny
            } else {
                MysqlType::Tiny
            }
        }
        ColumnType::MYSQL_TYPE_SHORT | ColumnType::MYSQL_TYPE_YEAR => {
            if unsigned {
                MysqlType::UnsignedShort
            } else {
                MysqlType::Short
            }
        }
        ColumnType::MYSQL_TYPE_LONG | ColumnType::MYSQL_TYPE_INT24 => {
            if unsigned {
                MysqlType::UnsignedLong
            } else {
                MysqlType::Long
            }
        }
        ColumnType::MYSQL_TYPE_LONGLONG => {
            if unsigned {
                MysqlType::UnsignedLongLong
            } else {
                MysqlType::LongLong
            }
        }
        ColumnType::MYSQL_TYPE_FLOAT => MysqlType::Float,
        ColumnType::MYSQL_TYPE_DOUBLE => MysqlType::Double,
        ColumnType::MYSQL_TYPE_DECIMAL | ColumnType::MYSQL_TYPE_NEWDECIMAL => MysqlType::Numeric,
        ColumnType::MYSQL_TYPE_DATE | ColumnType::MYSQL_TYPE_NEWDATE => MysqlType::Date,
        ColumnType::MYSQL_TYPE_TIME | ColumnType::MYSQL_TYPE_TIME2 => MysqlType::Time,
        ColumnType::MYSQL_TYPE_DATETIME | ColumnType::MYSQL_TYPE_DATETIME2 => MysqlType::DateTime,
        ColumnType::MYSQL_TYPE_TIMESTAMP | ColumnType::MYSQL_TYPE_TIMESTAMP2 => {
            MysqlType::Timestamp
        }
        ColumnType::MYSQL_TYPE_BIT => MysqlType::Bit,
        ColumnType::MYSQL_TYPE_ENUM => MysqlType::Enum,
        ColumnType::MYSQL_TYPE_SET => MysqlType::Set,
        ColumnType::MYSQL_TYPE_TINY_BLOB
        | ColumnType::MYSQL_TYPE_MEDIUM_BLOB
        | ColumnType::MYSQL_TYPE_LONG_BLOB
        | ColumnType::MYSQL_TYPE_BLOB => MysqlType::Blob,
        ColumnType::MYSQL_TYPE_VARCHAR
        | ColumnType::MYSQL_TYPE_VAR_STRING
        | ColumnType::MYSQL_TYPE_STRING
        | ColumnType::MYSQL_TYPE_JSON
        | ColumnType::MYSQL_TYPE_GEOMETRY
        | ColumnType::MYSQL_TYPE_TYPED_ARRAY
        | ColumnType::MYSQL_TYPE_NULL => MysqlType::String,
    }
}

/// Convert wire-format date/time bytes to a `MysqlTime` struct, then return its raw bytes.
///
/// Diesel's `FromSql` for date/time types expects the raw bytes of a C `MYSQL_TIME` struct,
/// which is different from the compact MySQL binary protocol wire format.
fn wire_datetime_to_bytes(data: &[u8], col_type: ColumnType) -> Vec<u8> {
    let time = match col_type {
        ColumnType::MYSQL_TYPE_DATE | ColumnType::MYSQL_TYPE_NEWDATE => {
            let (year, month, day) = if let Some(d) = data.first_chunk::<4>() {
                (
                    u16::from_le_bytes([d[0], d[1]]) as libc::c_uint,
                    d[2] as libc::c_uint,
                    d[3] as libc::c_uint,
                )
            } else {
                (0, 0, 0)
            };
            MysqlTime::new(
                year,
                month,
                day,
                0,
                0,
                0,
                0,
                false,
                MysqlTimestampType::MYSQL_TIMESTAMP_DATE,
                0,
            )
        }
        ColumnType::MYSQL_TYPE_TIME | ColumnType::MYSQL_TYPE_TIME2 => {
            let (neg, hours, minutes, seconds, usec) = match data.len() {
                0 => (false, 0u32, 0u32, 0u32, 0u64),
                8 => {
                    if let Some(d) = data.first_chunk::<8>() {
                        let neg = d[0] != 0;
                        let days = u32::from_le_bytes([d[1], d[2], d[3], d[4]]);
                        (neg, days * 24 + d[5] as u32, d[6] as u32, d[7] as u32, 0)
                    } else {
                        (false, 0, 0, 0, 0)
                    }
                }
                12 => {
                    if let Some(d) = data.first_chunk::<12>() {
                        let neg = d[0] != 0;
                        let days = u32::from_le_bytes([d[1], d[2], d[3], d[4]]);
                        let usec = u32::from_le_bytes([d[8], d[9], d[10], d[11]]) as u64;
                        (neg, days * 24 + d[5] as u32, d[6] as u32, d[7] as u32, usec)
                    } else {
                        (false, 0, 0, 0, 0)
                    }
                }
                _ => (false, 0, 0, 0, 0),
            };
            MysqlTime::new(
                0,
                0,
                0,
                hours,
                minutes,
                seconds,
                usec as libc::c_ulong,
                neg,
                MysqlTimestampType::MYSQL_TIMESTAMP_TIME,
                0,
            )
        }
        // DATETIME, TIMESTAMP
        _ => {
            let (year, month, day, hour, minute, second, usec) = match data.len() {
                0 => (0u32, 0u32, 0u32, 0u32, 0u32, 0u32, 0u64),
                4 => {
                    if let Some(d) = data.first_chunk::<4>() {
                        (
                            u16::from_le_bytes([d[0], d[1]]) as u32,
                            d[2] as u32,
                            d[3] as u32,
                            0,
                            0,
                            0,
                            0,
                        )
                    } else {
                        (0, 0, 0, 0, 0, 0, 0)
                    }
                }
                7 => {
                    if let Some(d) = data.first_chunk::<7>() {
                        (
                            u16::from_le_bytes([d[0], d[1]]) as u32,
                            d[2] as u32,
                            d[3] as u32,
                            d[4] as u32,
                            d[5] as u32,
                            d[6] as u32,
                            0,
                        )
                    } else {
                        (0, 0, 0, 0, 0, 0, 0)
                    }
                }
                11 => {
                    if let Some(d) = data.first_chunk::<11>() {
                        (
                            u16::from_le_bytes([d[0], d[1]]) as u32,
                            d[2] as u32,
                            d[3] as u32,
                            d[4] as u32,
                            d[5] as u32,
                            d[6] as u32,
                            u32::from_le_bytes([d[7], d[8], d[9], d[10]]) as u64,
                        )
                    } else {
                        (0, 0, 0, 0, 0, 0, 0)
                    }
                }
                _ => (0, 0, 0, 0, 0, 0, 0),
            };
            let timestamp_type = match col_type {
                ColumnType::MYSQL_TYPE_TIMESTAMP | ColumnType::MYSQL_TYPE_TIMESTAMP2 => {
                    MysqlTimestampType::MYSQL_TIMESTAMP_DATETIME
                }
                _ => MysqlTimestampType::MYSQL_TIMESTAMP_DATETIME,
            };
            MysqlTime::new(
                year,
                month,
                day,
                hour,
                minute,
                second,
                usec as libc::c_ulong,
                false,
                timestamp_type,
                0,
            )
        }
    };

    mysql_time_to_bytes(&time)
}

#[expect(unsafe_code)]
fn mysql_time_to_bytes(time: &MysqlTime) -> Vec<u8> {
    let size = std::mem::size_of::<MysqlTime>();
    let mut bytes = vec![0u8; size];
    // SAFETY: MysqlTime is a repr(C) struct with no padding requirements beyond alignment.
    // We copy the raw bytes into a Vec<u8> for diesel's MysqlValue consumption.
    unsafe {
        std::ptr::copy_nonoverlapping(
            time as *const MysqlTime as *const u8,
            bytes.as_mut_ptr(),
            size,
        );
    }
    bytes
}

impl BinaryResultSetHandler for CollectRawHandler {
    fn no_result_set(&mut self, _ok: OkPayloadBytes) -> crate::error::Result<()> {
        Ok(())
    }

    fn resultset_start(&mut self, cols: &[ColumnDefinition<'_>]) -> crate::error::Result<()> {
        self.columns = cols
            .iter()
            .map(|c| {
                let col_type = c.tail.column_type()?;
                let flags = c.tail.flags()?;
                Ok(ColumnInfo {
                    name: String::from_utf8_lossy(c.name_alias).into_owned(),
                    column_type: to_mysql_type(col_type, flags),
                })
            })
            .collect::<crate::error::Result<Vec<_>>>()?;
        Ok(())
    }

    fn row(
        &mut self,
        cols: &[ColumnDefinition<'_>],
        row: BinaryRowPayload<'_>,
    ) -> crate::error::Result<()> {
        let null_bitmap = row.null_bitmap();
        let mut data = row.values();
        let mut values = Vec::with_capacity(self.columns.len());

        for (i, col) in cols.iter().enumerate() {
            if null_bitmap.is_null(i) {
                values.push(None);
                continue;
            }

            let col_type = col.tail.column_type()?;

            match col_type {
                ColumnType::MYSQL_TYPE_NULL => {
                    values.push(None);
                }

                // 1-byte integer
                ColumnType::MYSQL_TYPE_TINY => {
                    let (chunk, rest) = data.split_first_chunk::<1>().ok_or_else(|| {
                        crate::error::Error::LibraryBug(eyre!("truncated TINY column"))
                    })?;
                    values.push(Some(chunk.to_vec()));
                    data = rest;
                }

                // 2-byte integer
                ColumnType::MYSQL_TYPE_SHORT | ColumnType::MYSQL_TYPE_YEAR => {
                    let (chunk, rest) = data.split_first_chunk::<2>().ok_or_else(|| {
                        crate::error::Error::LibraryBug(eyre!("truncated SHORT column"))
                    })?;
                    values.push(Some(chunk.to_vec()));
                    data = rest;
                }

                // 4-byte integer/float
                ColumnType::MYSQL_TYPE_INT24
                | ColumnType::MYSQL_TYPE_LONG
                | ColumnType::MYSQL_TYPE_FLOAT => {
                    let (chunk, rest) = data.split_first_chunk::<4>().ok_or_else(|| {
                        crate::error::Error::LibraryBug(eyre!("truncated LONG column"))
                    })?;
                    values.push(Some(chunk.to_vec()));
                    data = rest;
                }

                // 8-byte integer/double
                ColumnType::MYSQL_TYPE_LONGLONG | ColumnType::MYSQL_TYPE_DOUBLE => {
                    let (chunk, rest) = data.split_first_chunk::<8>().ok_or_else(|| {
                        crate::error::Error::LibraryBug(eyre!("truncated LONGLONG column"))
                    })?;
                    values.push(Some(chunk.to_vec()));
                    data = rest;
                }

                // Date/time: variable-length wire format → MysqlTime struct bytes
                ColumnType::MYSQL_TYPE_DATE
                | ColumnType::MYSQL_TYPE_NEWDATE
                | ColumnType::MYSQL_TYPE_DATETIME
                | ColumnType::MYSQL_TYPE_DATETIME2
                | ColumnType::MYSQL_TYPE_TIMESTAMP
                | ColumnType::MYSQL_TYPE_TIMESTAMP2
                | ColumnType::MYSQL_TYPE_TIME
                | ColumnType::MYSQL_TYPE_TIME2 => {
                    let (&len_byte, payload) = data.split_first().ok_or_else(|| {
                        crate::error::Error::LibraryBug(eyre!("truncated datetime length"))
                    })?;
                    let len = len_byte as usize;
                    let (dt_data, rest) = payload.split_at_checked(len).ok_or_else(|| {
                        crate::error::Error::LibraryBug(eyre!("truncated datetime payload"))
                    })?;
                    values.push(Some(wire_datetime_to_bytes(dt_data, col_type)));
                    data = rest;
                }

                // Length-encoded string/blob/decimal
                _ => {
                    let (bytes, rest) = read_string_lenenc(data)?;
                    values.push(Some(bytes.to_vec()));
                    data = rest;
                }
            }
        }

        self.rows.push(values);
        Ok(())
    }

    fn resultset_end(&mut self, _eof: OkPayloadBytes) -> crate::error::Result<()> {
        Ok(())
    }
}