Skip to content

Commit

Permalink
feat: add bitwise and/or operators (#841)
Browse files Browse the repository at this point in the history
Signed-off-by: xxchan <[email protected]>
  • Loading branch information
xxchan authored Dec 1, 2024
1 parent 6771326 commit f914cc3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
}
Expand Down
66 changes: 66 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R>(self, right: R) -> SimpleExpr
where
R: Into<SimpleExpr>,
{
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<R>(self, right: R) -> SimpleExpr
where
R: Into<SimpleExpr>,
{
ExprTrait::binary(self, BinOper::BitOr, right)
}
}

/// This generic implementation covers all expression types,
Expand Down
2 changes: 2 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ pub enum BinOper {
Mul,
Div,
Mod,
BitAnd,
BitOr,
LShift,
RShift,
As,
Expand Down

0 comments on commit f914cc3

Please sign in to comment.