Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ast/operator.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -85,6 +85,8 @@ pub enum BinaryOperator {
BitwiseOr,
BitwiseAnd,
BitwiseXor,
// MySQL & Sqlite use `REGEXP` as an infix operator
Regexp,
PGBitwiseXor,
PGBitwiseShiftLeft,
PGBitwiseShiftRight,
Expand DownExpand Up@@ -122,6 +124,7 @@ impl fmt::Display for BinaryOperator {
BinaryOperator::BitwiseOr => f.write_str("|"),
BinaryOperator::BitwiseAnd => f.write_str("&"),
BinaryOperator::BitwiseXor => f.write_str("^"),
BinaryOperator::Regexp => f.write_str("REGEXP"),
BinaryOperator::PGBitwiseXor => f.write_str("#"),
BinaryOperator::PGBitwiseShiftLeft => f.write_str("<<"),
BinaryOperator::PGBitwiseShiftRight => f.write_str(">>"),
Expand Down
27 changes: 26 additions & 1 deletion src/dialect/mysql.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,7 +10,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::dialect::Dialect;
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;

use crate::{
ast::{BinaryOperator, Expr},
dialect::Dialect,
keywords::Keyword,
};

/// [MySQL](https://www.mysql.com/)
#[derive(Debug)]
Expand All@@ -35,4 +42,22 @@ impl Dialect for MySqlDialect {
fn is_delimited_identifier_start(&self, ch: char) -> bool {
ch == '`'
}

fn parse_infix(
&self,
_parser: &mut crate::parser::Parser,
_expr: &crate::ast::Expr,
_precedence: u8,
) -> Option<Result<crate::ast::Expr, crate::parser::ParserError>> {
// Parse REGEXP as an operator
if _parser.parse_keyword(Keyword::REGEXP) {
Some(Ok(Expr::BinaryOp {
left: Box::new(_expr.clone()),
op: BinaryOperator::Regexp,
right: Box::new(_parser.parse_expr().unwrap()),
}))
} else {
None
}
}
}
23 changes: 22 additions & 1 deletion src/dialect/sqlite.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,7 +10,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::ast::Statement;
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;

use crate::ast::{BinaryOperator, Expr, Statement};
use crate::dialect::Dialect;
use crate::keywords::Keyword;
use crate::parser::{Parser, ParserError};
Expand DownExpand Up@@ -47,4 +50,22 @@ impl Dialect for SQLiteDialect {
None
}
}

fn parse_infix(
&self,
_parser: &mut crate::parser::Parser,
_expr: &crate::ast::Expr,
_precedence: u8,
) -> Option<Result<crate::ast::Expr, crate::parser::ParserError>> {
// Parse REGEXP as an operator
if _parser.parse_keyword(Keyword::REGEXP) {
Some(Ok(Expr::BinaryOp {
left: Box::new(_expr.clone()),
op: BinaryOperator::Regexp,
right: Box::new(_parser.parse_expr().unwrap()),
}))
} else {
None
}
}
}
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -473,6 +473,7 @@ define_keywords!(
REFERENCES,
REFERENCING,
REGCLASS,
REGEXP,
REGR_AVGX,
REGR_AVGY,
REGR_COUNT,
Expand Down
1 change: 1 addition & 0 deletions src/parser.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -2029,6 +2029,7 @@ impl<'a> Parser<'a> {
Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(Self::LIKE_PREC),
Token::Word(w) if w.keyword == Keyword::SIMILAR => Ok(Self::LIKE_PREC),
Token::Word(w) if w.keyword == Keyword::OPERATOR => Ok(Self::BETWEEN_PREC),
Token::Word(w) if w.keyword == Keyword::REGEXP => Ok(Self::LIKE_PREC),
Token::Eq
| Token::Lt
| Token::LtEq
Expand Down
5 changes: 5 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -1407,3 +1407,8 @@ fn parse_string_introducers() {
mysql().one_statement_parses_to("SELECT _utf8mb4'abc'", "SELECT _utf8mb4 'abc'");
mysql().verified_stmt("SELECT _binary 'abc', _utf8mb4 'abc'");
}

#[test]
fn parse_regexp_infix() {
mysql().verified_stmt(r#"SELECT 'Michael!' REGEXP '.*'"#);
}
5 changes: 5 additions & 0 deletions tests/sqlparser_sqlite.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -256,3 +256,8 @@ fn sqlite_and_generic() -> TestedDialects {
options: None,
}
}

#[test]
fn parse_regexp_infix() {
sqlite().verified_stmt(r#"SELECT 'Michael!' REGEXP '.*'"#);
}