Skip to content

Commit

Permalink
Merge pull request #115 from code-mm/postgres-concatenate-operator
Browse files Browse the repository at this point in the history
Postgres concatenate operator
  • Loading branch information
tyt2y3 committed Aug 29, 2021
1 parent b384d4d commit e9c28f4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/backend/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ impl QueryBuilder for PostgresQueryBuilder {

fn prepare_bin_oper(&self, bin_oper: &BinOper, sql: &mut SqlWriter, collector: &mut dyn FnMut(Value)) {
match bin_oper {
BinOper::Matches => write!(sql, "{}", "@@").unwrap(),
BinOper::Contains => write!(sql, "{}", "@>").unwrap(),
BinOper::Contained => write!(sql, "{}", "<@").unwrap(),
BinOper::Matches => write!(sql, "@@").unwrap(),
BinOper::Contains => write!(sql, "@>").unwrap(),
BinOper::Contained => write!(sql, "<@").unwrap(),
BinOper::Concatenate => write!(sql, "||").unwrap(),
_ => self.prepare_bin_oper_common(bin_oper, sql, collector),
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,32 @@ impl Expr {
self.bin_oper(BinOper::Contained, expr.into())
}

/// Express an postgres concatenate (`||`) expression.
///
/// # Examples
///
/// ```
/// use sea_query::{*, tests_cfg::*};
///
/// let query = Query::select()
/// .columns(vec![Font::Name, Font::Variant, Font::Language])
/// .from(Font::Table)
/// .and_where(Expr::val("a").concatenate(Expr::val("b")))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "name", "variant", "language" FROM "font" WHERE 'a' || 'b'"#
/// );
/// ```
#[cfg(feature = "backend-postgres")]
pub fn concatenate<T>(self, expr: T) -> SimpleExpr
where
T: Into<SimpleExpr>,
{
self.bin_oper(BinOper::Concatenate, expr.into())
}

pub(crate) fn func(func: Function) -> Self {
let mut expr = Expr::new();
expr.func = Some(func);
Expand Down
2 changes: 2 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ pub enum BinOper {
Contains,
#[cfg(feature = "backend-postgres")]
Contained,
#[cfg(feature = "backend-postgres")]
Concatenate,
}

/// Logical chain operator
Expand Down

0 comments on commit e9c28f4

Please sign in to comment.