From e9c28f4df85d4ac63c1b4c4e0315edc1108fd75d Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Sun, 29 Aug 2021 19:19:39 +0800 Subject: [PATCH] Merge pull request #115 from code-mm/postgres-concatenate-operator Postgres concatenate operator --- src/backend/postgres/query.rs | 7 ++++--- src/expr.rs | 26 ++++++++++++++++++++++++++ src/types.rs | 2 ++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/backend/postgres/query.rs b/src/backend/postgres/query.rs index 9824ba8f8..6b40e7861 100644 --- a/src/backend/postgres/query.rs +++ b/src/backend/postgres/query.rs @@ -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), } } diff --git a/src/expr.rs b/src/expr.rs index 5fbbc1ce2..98a817236 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -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(self, expr: T) -> SimpleExpr + where + T: Into, + { + self.bin_oper(BinOper::Concatenate, expr.into()) + } + pub(crate) fn func(func: Function) -> Self { let mut expr = Expr::new(); expr.func = Some(func); diff --git a/src/types.rs b/src/types.rs index ecee23039..351fdda99 100644 --- a/src/types.rs +++ b/src/types.rs @@ -118,6 +118,8 @@ pub enum BinOper { Contains, #[cfg(feature = "backend-postgres")] Contained, + #[cfg(feature = "backend-postgres")] + Concatenate, } /// Logical chain operator