sea_query/extension/mysql/select.rs
1use super::{IndexHint, IndexHintScope, IndexHintType};
2use crate::{IntoIden, SelectStatement};
3
4pub trait MySqlSelectStatementExt {
5 fn use_index<I>(&mut self, index: I, scope: IndexHintScope) -> &mut Self
6 where
7 I: IntoIden;
8
9 fn force_index<I>(&mut self, index: I, scope: IndexHintScope) -> &mut Self
10 where
11 I: IntoIden;
12
13 fn ignore_index<I>(&mut self, index: I, scope: IndexHintScope) -> &mut Self
14 where
15 I: IntoIden;
16}
17
18impl MySqlSelectStatementExt for SelectStatement {
19 /// Use index hint for MySQL
20 ///
21 /// Give the optimizer information about how to choose indexes during query processing.
22 /// See [MySQL reference manual for Index Hints](https://dev.mysql.com/doc/refman/8.0/en/index-hints.html)
23 ///
24 /// # Examples
25 ///
26 /// ```
27 /// use sea_query::{extension::mysql::*, tests_cfg::*, *};
28 ///
29 /// let query = Query::select()
30 /// .from(Char::Table)
31 /// .use_index(IndexName::new("IDX_123456"), IndexHintScope::All)
32 /// .column(Char::SizeW)
33 /// .to_owned();
34 ///
35 /// assert_eq!(
36 /// query.to_string(MysqlQueryBuilder),
37 /// r#"SELECT `size_w` FROM `character` USE INDEX (`IDX_123456`)"#
38 /// );
39 /// ```
40 fn use_index<I>(&mut self, index: I, scope: IndexHintScope) -> &mut Self
41 where
42 I: IntoIden,
43 {
44 self.index_hints.push(IndexHint {
45 index: index.into_iden(),
46 r#type: IndexHintType::Use,
47 scope,
48 });
49 self
50 }
51
52 /// Force index hint for MySQL
53 ///
54 /// Give the optimizer information about how to choose indexes during query processing.
55 /// See [MySQL reference manual for Index Hints](https://dev.mysql.com/doc/refman/8.0/en/index-hints.html)
56 ///
57 /// # Examples
58 ///
59 /// ```
60 /// use sea_query::{extension::mysql::*, tests_cfg::*, *};
61 ///
62 /// let query = Query::select()
63 /// .from(Char::Table)
64 /// .force_index(IndexName::new("IDX_123456"), IndexHintScope::All)
65 /// .column(Char::SizeW)
66 /// .to_owned();
67 ///
68 /// assert_eq!(
69 /// query.to_string(MysqlQueryBuilder),
70 /// r#"SELECT `size_w` FROM `character` FORCE INDEX (`IDX_123456`)"#
71 /// );
72 /// ```
73 fn force_index<I>(&mut self, index: I, scope: IndexHintScope) -> &mut Self
74 where
75 I: IntoIden,
76 {
77 self.index_hints.push(IndexHint {
78 index: index.into_iden(),
79 r#type: IndexHintType::Force,
80 scope,
81 });
82 self
83 }
84
85 /// Ignore index hint for MySQL
86 ///
87 /// Give the optimizer information about how to choose indexes during query processing.
88 /// See [MySQL reference manual for Index Hints](https://dev.mysql.com/doc/refman/8.0/en/index-hints.html)
89 ///
90 /// # Examples
91 ///
92 /// ```
93 /// use sea_query::{extension::mysql::*, tests_cfg::*, *};
94 ///
95 /// let query = Query::select()
96 /// .from(Char::Table)
97 /// .ignore_index(IndexName::new("IDX_123456"), IndexHintScope::All)
98 /// .column(Char::SizeW)
99 /// .to_owned();
100 ///
101 /// assert_eq!(
102 /// query.to_string(MysqlQueryBuilder),
103 /// r#"SELECT `size_w` FROM `character` IGNORE INDEX (`IDX_123456`)"#
104 /// )
105 /// ```
106 fn ignore_index<I>(&mut self, index: I, scope: IndexHintScope) -> &mut Self
107 where
108 I: IntoIden,
109 {
110 self.index_hints.push(IndexHint {
111 index: index.into_iden(),
112 r#type: IndexHintType::Ignore,
113 scope,
114 });
115 self
116 }
117}