File tree Expand file tree Collapse file tree 5 files changed +38
-1
lines changed Expand file tree Collapse file tree 5 files changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -85,6 +85,8 @@ pub enum BinaryOperator {
85
85
BitwiseOr ,
86
86
BitwiseAnd ,
87
87
BitwiseXor ,
88
+ /// MySQL [`DIV`](https://dev.mysql.com/doc/refman/8.0/en/arithmetic-functions.html) integer division
89
+ MyIntegerDivide ,
88
90
/// Support for custom operators (built by parsers outside this crate)
89
91
Custom ( String ) ,
90
92
PGBitwiseXor ,
@@ -124,6 +126,7 @@ impl fmt::Display for BinaryOperator {
124
126
BinaryOperator :: BitwiseOr => f. write_str ( "|" ) ,
125
127
BinaryOperator :: BitwiseAnd => f. write_str ( "&" ) ,
126
128
BinaryOperator :: BitwiseXor => f. write_str ( "^" ) ,
129
+ BinaryOperator :: MyIntegerDivide => f. write_str ( "DIV" ) ,
127
130
BinaryOperator :: Custom ( s) => f. write_str ( s) ,
128
131
BinaryOperator :: PGBitwiseXor => f. write_str ( "#" ) ,
129
132
BinaryOperator :: PGBitwiseShiftLeft => f. write_str ( "<<" ) ,
Original file line number Diff line number Diff line change 10
10
// See the License for the specific language governing permissions and
11
11
// limitations under the License.
12
12
13
- use crate :: dialect:: Dialect ;
13
+ #[ cfg( not( feature = "std" ) ) ]
14
+ use alloc:: boxed:: Box ;
15
+
16
+ use crate :: {
17
+ ast:: { BinaryOperator , Expr } ,
18
+ dialect:: Dialect ,
19
+ keywords:: Keyword ,
20
+ } ;
14
21
15
22
/// [MySQL](https://www.mysql.com/)
16
23
#[ derive( Debug ) ]
@@ -35,4 +42,22 @@ impl Dialect for MySqlDialect {
35
42
fn is_delimited_identifier_start ( & self , ch : char ) -> bool {
36
43
ch == '`'
37
44
}
45
+
46
+ fn parse_infix (
47
+ & self ,
48
+ parser : & mut crate :: parser:: Parser ,
49
+ expr : & crate :: ast:: Expr ,
50
+ _precedence : u8 ,
51
+ ) -> Option < Result < crate :: ast:: Expr , crate :: parser:: ParserError > > {
52
+ // Parse DIV as an operator
53
+ if parser. parse_keyword ( Keyword :: DIV ) {
54
+ Some ( Ok ( Expr :: BinaryOp {
55
+ left : Box :: new ( expr. clone ( ) ) ,
56
+ op : BinaryOperator :: MyIntegerDivide ,
57
+ right : Box :: new ( parser. parse_expr ( ) . unwrap ( ) ) ,
58
+ } ) )
59
+ } else {
60
+ None
61
+ }
62
+ }
38
63
}
Original file line number Diff line number Diff line change @@ -211,6 +211,7 @@ define_keywords!(
211
211
DISCONNECT ,
212
212
DISTINCT ,
213
213
DISTRIBUTE ,
214
+ DIV ,
214
215
DO ,
215
216
DOUBLE ,
216
217
DOW ,
Original file line number Diff line number Diff line change @@ -1982,6 +1982,8 @@ impl<'a> Parser<'a> {
1982
1982
const AND_PREC : u8 = 10 ;
1983
1983
const OR_PREC : u8 = 5 ;
1984
1984
1985
+ const DIV_OP_PREC : u8 = 40 ;
1986
+
1985
1987
/// Get the precedence of the next token
1986
1988
pub fn get_next_precedence ( & self ) -> Result < u8 , ParserError > {
1987
1989
// allow the dialect to override precedence logic
@@ -2031,6 +2033,7 @@ impl<'a> Parser<'a> {
2031
2033
Token :: Word ( w) if w. keyword == Keyword :: ILIKE => Ok ( Self :: LIKE_PREC ) ,
2032
2034
Token :: Word ( w) if w. keyword == Keyword :: SIMILAR => Ok ( Self :: LIKE_PREC ) ,
2033
2035
Token :: Word ( w) if w. keyword == Keyword :: OPERATOR => Ok ( Self :: BETWEEN_PREC ) ,
2036
+ Token :: Word ( w) if w. keyword == Keyword :: DIV => Ok ( Self :: DIV_OP_PREC ) ,
2034
2037
Token :: Eq
2035
2038
| Token :: Lt
2036
2039
| Token :: LtEq
Original file line number Diff line number Diff line change @@ -1407,3 +1407,8 @@ fn parse_string_introducers() {
1407
1407
mysql ( ) . one_statement_parses_to ( "SELECT _utf8mb4'abc'" , "SELECT _utf8mb4 'abc'" ) ;
1408
1408
mysql ( ) . verified_stmt ( "SELECT _binary 'abc', _utf8mb4 'abc'" ) ;
1409
1409
}
1410
+
1411
+ #[ test]
1412
+ fn parse_div_infix ( ) {
1413
+ mysql ( ) . verified_stmt ( r#"SELECT 5 DIV 2"# ) ;
1414
+ }
You can’t perform that action at this time.
0 commit comments