Skip to main content

drizzle_mysql/traits/
column.rs

1use drizzle_core::SQLColumn;
2
3use crate::values::MySQLValue;
4
5/// A generated column belonging to a MySQL table.
6pub trait MySQLColumn<'a>: SQLColumn<'a, MySQLValue<'a>> {
7    /// Backtick-quoted column identifier for const DDL generation.
8    const DDL_NAME: &'static str;
9
10    /// Whether the column uses MySQL's `AUTO_INCREMENT` behavior.
11    const AUTO_INCREMENT: bool = false;
12
13    /// Effective character set declared by the column or its table.
14    const CHARSET: Option<&'static str> = None;
15
16    /// Effective collation declared by the column or its table.
17    const COLLATE: Option<&'static str> = None;
18}
19
20/// Marker for a column that MySQL can index directly.
21///
22/// `TEXT` and `BLOB` families require a prefix length, while `JSON` requires an
23/// indexed generated scalar column. Those capabilities are intentionally kept
24/// out of this marker.
25#[diagnostic::on_unimplemented(
26    message = "`{Self}` cannot be used directly in a MySQL index",
27    note = "use a bounded character/binary column, or index a generated scalar column for JSON"
28)]
29pub trait MySQLIndexColumn {}
30
31impl<'a, T> MySQLColumn<'a> for &T
32where
33    T: MySQLColumn<'a>,
34    for<'r> &'r T: SQLColumn<'a, MySQLValue<'a>>,
35{
36    const DDL_NAME: &'static str = T::DDL_NAME;
37    const AUTO_INCREMENT: bool = T::AUTO_INCREMENT;
38    const CHARSET: Option<&'static str> = T::CHARSET;
39    const COLLATE: Option<&'static str> = T::COLLATE;
40}
41
42#[doc(hidden)]
43pub const fn optional_str_eq(left: Option<&str>, right: Option<&str>) -> bool {
44    match (left, right) {
45        (None, None) => true,
46        (Some(left), Some(right)) => str_eq(left, right),
47        _ => false,
48    }
49}
50
51const fn str_eq(left: &str, right: &str) -> bool {
52    let left = left.as_bytes();
53    let right = right.as_bytes();
54    if left.len() != right.len() {
55        return false;
56    }
57    let mut index = 0;
58    while index < left.len() {
59        if left[index] != right[index] {
60            return false;
61        }
62        index += 1;
63    }
64    true
65}