Skip to content
Open
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
4 changes: 4 additions & 0 deletions src/ast/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ pub enum BinaryOperator {
Match,
/// REGEXP operator, e.g. `a REGEXP b` (SQLite-specific)
Regexp,
/// GLOB operator, e.g. `a GLOB b` (SQLite-specific)
/// See <https://www.sqlite.org/lang_expr.html#the_like_glob_regexp_match_and_extract_operators>
Glob,
/// Support for custom operators (such as Postgres custom operators)
Custom(String),
/// Bitwise XOR, e.g. `a # b` (PostgreSQL-specific)
Expand Down Expand Up @@ -357,6 +360,7 @@ impl fmt::Display for BinaryOperator {
BinaryOperator::MyIntegerDivide => f.write_str("DIV"),
BinaryOperator::Match => f.write_str("MATCH"),
BinaryOperator::Regexp => f.write_str("REGEXP"),
BinaryOperator::Glob => f.write_str("GLOB"),
BinaryOperator::Custom(s) => f.write_str(s),
BinaryOperator::PGBitwiseXor => f.write_str("#"),
BinaryOperator::PGBitwiseShiftLeft => f.write_str("<<"),
Expand Down
2 changes: 2 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ pub trait Dialect: Debug + Any {
Token::Word(w) if w.keyword == Keyword::RLIKE => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::REGEXP => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::MATCH => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::GLOB => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::SIMILAR => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::MEMBER => Ok(p!(Like)),
Token::Word(w)
Expand All @@ -859,6 +860,7 @@ pub trait Dialect: Debug + Any {
Token::Word(w) if w.keyword == Keyword::RLIKE => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::REGEXP => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::MATCH => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::GLOB => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::SIMILAR => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::MEMBER => Ok(p!(Like)),
Token::Word(w) if w.keyword == Keyword::OPERATOR => Ok(p!(Between)),
Expand Down
3 changes: 2 additions & 1 deletion src/dialect/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ impl Dialect for SQLiteDialect {
expr: &crate::ast::Expr,
_precedence: u8,
) -> Option<Result<crate::ast::Expr, ParserError>> {
// Parse MATCH and REGEXP as operators
// Parse MATCH, REGEXP and GLOB as operators
// See <https://www.sqlite.org/lang_expr.html#the_like_glob_regexp_match_and_extract_operators>
for (keyword, op) in [
(Keyword::REGEXP, BinaryOperator::Regexp),
(Keyword::MATCH, BinaryOperator::Match),
(Keyword::GLOB, BinaryOperator::Glob),
] {
if parser.parse_keyword(keyword) {
let left = Box::new(expr.clone());
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ define_keywords!(
GET,
GIN,
GIST,
GLOB,
GLOBAL,
GRANT,
GRANTED,
Expand Down
18 changes: 18 additions & 0 deletions tests/sqlparser_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,24 @@ fn test_regexp_operator() {
assert!(sqlite().parse_sql_statements("SELECT 1 MATCH").is_err());
}

#[test]
fn test_glob_operator() {
assert_eq!(
sqlite().verified_expr("col GLOB 'pattern'"),
Expr::BinaryOp {
op: BinaryOperator::Glob,
left: Box::new(Expr::Identifier(Ident::new("col"))),
right: Box::new(Expr::Value(
(Value::SingleQuotedString("pattern".to_string())).with_empty_span()
))
}
);
sqlite().verified_only_select(r#"SELECT count(*) FROM files WHERE name GLOB '*.txt'"#);

// Should return an error, not panic
assert!(sqlite().parse_sql_statements("SELECT 1 GLOB").is_err());
}

#[test]
fn test_update_delete_limit() {
match sqlite().verified_stmt("UPDATE foo SET bar = 1 LIMIT 99") {
Expand Down