sea_query/extension/mysql/
select.rs

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
use super::{IndexHint, IndexHintScope, IndexHintType};
use crate::{IntoIden, SelectStatement};

pub trait MySqlSelectStatementExt {
    fn use_index<I>(&mut self, index: I, scope: IndexHintScope) -> &mut Self
    where
        I: IntoIden;

    fn force_index<I>(&mut self, index: I, scope: IndexHintScope) -> &mut Self
    where
        I: IntoIden;

    fn ignore_index<I>(&mut self, index: I, scope: IndexHintScope) -> &mut Self
    where
        I: IntoIden;
}

impl MySqlSelectStatementExt for SelectStatement {
    /// Use index hint for MySQL
    ///
    /// Give the optimizer information about how to choose indexes during query processing.
    /// See [MySQL reference manual for Index Hints](https://dev.mysql.com/doc/refman/8.0/en/index-hints.html)
    ///
    /// # Examples
    ///
    /// ```
    /// use sea_query::{extension::mysql::*, tests_cfg::*, *};
    ///
    /// let query = Query::select()
    ///     .from(Char::Table)
    ///     .use_index(IndexName::new("IDX_123456"), IndexHintScope::All)
    ///     .column(Char::SizeW)
    ///     .to_owned();
    ///
    /// assert_eq!(
    ///     query.to_string(MysqlQueryBuilder),
    ///     r#"SELECT `size_w` FROM `character` USE INDEX (`IDX_123456`)"#
    /// );
    /// ```
    fn use_index<I>(&mut self, index: I, scope: IndexHintScope) -> &mut Self
    where
        I: IntoIden,
    {
        self.index_hints.push(IndexHint {
            index: index.into_iden(),
            r#type: IndexHintType::Use,
            scope,
        });
        self
    }

    /// Force index hint for MySQL
    ///
    /// Give the optimizer information about how to choose indexes during query processing.
    /// See [MySQL reference manual for Index Hints](https://dev.mysql.com/doc/refman/8.0/en/index-hints.html)
    ///
    /// # Examples
    ///
    /// ```
    /// use sea_query::{extension::mysql::*, tests_cfg::*, *};
    ///
    /// let query = Query::select()
    ///     .from(Char::Table)
    ///     .force_index(IndexName::new("IDX_123456"), IndexHintScope::All)
    ///     .column(Char::SizeW)
    ///     .to_owned();
    ///
    /// assert_eq!(
    ///     query.to_string(MysqlQueryBuilder),
    ///     r#"SELECT `size_w` FROM `character` FORCE INDEX (`IDX_123456`)"#
    /// );
    /// ```
    fn force_index<I>(&mut self, index: I, scope: IndexHintScope) -> &mut Self
    where
        I: IntoIden,
    {
        self.index_hints.push(IndexHint {
            index: index.into_iden(),
            r#type: IndexHintType::Force,
            scope,
        });
        self
    }

    /// Ignore index hint for MySQL
    ///
    /// Give the optimizer information about how to choose indexes during query processing.
    /// See [MySQL reference manual for Index Hints](https://dev.mysql.com/doc/refman/8.0/en/index-hints.html)
    ///
    /// # Examples
    ///
    /// ```
    /// use sea_query::{extension::mysql::*, tests_cfg::*, *};
    ///
    /// let query = Query::select()
    ///     .from(Char::Table)
    ///     .ignore_index(IndexName::new("IDX_123456"), IndexHintScope::All)
    ///     .column(Char::SizeW)
    ///     .to_owned();
    ///
    /// assert_eq!(
    ///     query.to_string(MysqlQueryBuilder),
    ///     r#"SELECT `size_w` FROM `character` IGNORE INDEX (`IDX_123456`)"#
    /// )
    /// ```
    fn ignore_index<I>(&mut self, index: I, scope: IndexHintScope) -> &mut Self
    where
        I: IntoIden,
    {
        self.index_hints.push(IndexHint {
            index: index.into_iden(),
            r#type: IndexHintType::Ignore,
            scope,
        });
        self
    }
}