From f914cc300aa89b67503d35390e63ef29caf04a4e Mon Sep 17 00:00:00 2001 From: xxchan Date: Mon, 2 Dec 2024 01:59:35 +0800 Subject: [PATCH] feat: add bitwise and/or operators (#841) Signed-off-by: xxchan --- src/backend/query_builder.rs | 2 ++ src/expr.rs | 66 ++++++++++++++++++++++++++++++++++++ src/types.rs | 2 ++ 3 files changed, 70 insertions(+) diff --git a/src/backend/query_builder.rs b/src/backend/query_builder.rs index 8d80a7d1..e6ee7811 100644 --- a/src/backend/query_builder.rs +++ b/src/backend/query_builder.rs @@ -589,6 +589,8 @@ pub trait QueryBuilder: BinOper::As => "AS", BinOper::Escape => "ESCAPE", BinOper::Custom(raw) => raw, + BinOper::BitAnd => "&", + BinOper::BitOr => "|", #[allow(unreachable_patterns)] _ => unimplemented!(), } diff --git a/src/expr.rs b/src/expr.rs index 06cf08ed..70c3c7a7 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1299,6 +1299,72 @@ pub trait ExprTrait: Sized { /// ); /// ``` fn unary(self, o: UnOper) -> SimpleExpr; + + /// Express a bitwise AND operation. + /// + /// # Examples + /// + /// ``` + /// use sea_query::{tests_cfg::*, *}; + /// + /// let query = Query::select() + /// .columns([Char::Character, Char::SizeW, Char::SizeH]) + /// .from(Char::Table) + /// .and_where(1.bit_and(1).eq(1)) + /// .to_owned(); + /// + /// assert_eq!( + /// query.to_string(MysqlQueryBuilder), + /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE (1 & 1) = 1"# + /// ); + /// assert_eq!( + /// query.to_string(PostgresQueryBuilder), + /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE (1 & 1) = 1"# + /// ); + /// assert_eq!( + /// query.to_string(SqliteQueryBuilder), + /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE (1 & 1) = 1"# + /// ); + /// ``` + fn bit_and(self, right: R) -> SimpleExpr + where + R: Into, + { + ExprTrait::binary(self, BinOper::BitAnd, right) + } + + /// Express a bitwise OR operation. + /// + /// # Examples + /// + /// ``` + /// use sea_query::{tests_cfg::*, *}; + /// + /// let query = Query::select() + /// .columns([Char::Character, Char::SizeW, Char::SizeH]) + /// .from(Char::Table) + /// .and_where(1.bit_or(1).eq(1)) + /// .to_owned(); + /// + /// assert_eq!( + /// query.to_string(MysqlQueryBuilder), + /// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE (1 | 1) = 1"# + /// ); + /// assert_eq!( + /// query.to_string(PostgresQueryBuilder), + /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE (1 | 1) = 1"# + /// ); + /// assert_eq!( + /// query.to_string(SqliteQueryBuilder), + /// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE (1 | 1) = 1"# + /// ); + /// ``` + fn bit_or(self, right: R) -> SimpleExpr + where + R: Into, + { + ExprTrait::binary(self, BinOper::BitOr, right) + } } /// This generic implementation covers all expression types, diff --git a/src/types.rs b/src/types.rs index 43d9a1de..5ef4d112 100644 --- a/src/types.rs +++ b/src/types.rs @@ -181,6 +181,8 @@ pub enum BinOper { Mul, Div, Mod, + BitAnd, + BitOr, LShift, RShift, As,