From 122b1611882d4670369812dd1d2bccbd6416daea Mon Sep 17 00:00:00 2001 From: Brahm Lower Date: Fri, 12 Jan 2024 06:32:43 -0800 Subject: [PATCH] apply method for select builder (#730) --- src/query/select.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/query/select.rs b/src/query/select.rs index 4501e5a43..dd39862a8 100644 --- a/src/query/select.rs +++ b/src/query/select.rs @@ -214,6 +214,44 @@ impl SelectStatement { self } + /// Construct part of the select statement in another function. + /// + /// # Examples + /// + /// ``` + /// use sea_query::{tests_cfg::*, *}; + /// + /// let common_expr = |q: &mut SelectStatement| { + /// q.and_where(Expr::col(Char::FontId).eq(5)); + /// }; + /// + /// let query = Query::select() + /// .column(Char::Character) + /// .from(Char::Table) + /// .apply(common_expr) + /// .to_owned(); + /// + /// assert_eq!( + /// query.to_string(MysqlQueryBuilder), + /// r#"SELECT `character` FROM `character` WHERE `font_id` = 5"# + /// ); + /// assert_eq!( + /// query.to_string(PostgresQueryBuilder), + /// r#"SELECT "character" FROM "character" WHERE "font_id" = 5"# + /// ); + /// assert_eq!( + /// query.to_string(SqliteQueryBuilder), + /// r#"SELECT "character" FROM "character" WHERE "font_id" = 5"# + /// ); + /// ``` + pub fn apply(&mut self, func: F) -> &mut Self + where + F: FnOnce(&mut Self), + { + func(self); + self + } + /// Clear the select list pub fn clear_selects(&mut self) -> &mut Self { self.selects = Vec::new();