From 9f8315fc22d4198119a3e914e489abf6935b7cf4 Mon Sep 17 00:00:00 2001 From: Brahm Lower Date: Mon, 29 Jan 2024 02:33:14 -0800 Subject: [PATCH] insert builder supports values from iterator (#739) --- src/query/insert.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/query/insert.rs b/src/query/insert.rs index 93ca39e31..b82f53611 100644 --- a/src/query/insert.rs +++ b/src/query/insert.rs @@ -278,6 +278,44 @@ impl InsertStatement { self.values(values).unwrap() } + /// Add rows to be inserted from an iterator, variation of [`InsertStatement::values_panic`]. + /// + /// # Examples + /// + /// ``` + /// use sea_query::{tests_cfg::*, *}; + /// + /// let rows = vec![[2.1345.into(), "24B".into()], [5.15.into(), "12A".into()]]; + /// + /// let query = Query::insert() + /// .into_table(Glyph::Table) + /// .columns([Glyph::Aspect, Glyph::Image]) + /// .values_from_panic(rows) + /// .to_owned(); + /// + /// assert_eq!( + /// query.to_string(MysqlQueryBuilder), + /// r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (2.1345, '24B'), (5.15, '12A')"# + /// ); + /// assert_eq!( + /// query.to_string(PostgresQueryBuilder), + /// r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2.1345, '24B'), (5.15, '12A')"# + /// ); + /// assert_eq!( + /// query.to_string(SqliteQueryBuilder), + /// r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2.1345, '24B'), (5.15, '12A')"# + /// ); + /// ``` + pub fn values_from_panic(&mut self, values_iter: impl IntoIterator) -> &mut Self + where + I: IntoIterator, + { + values_iter.into_iter().for_each(|values| { + self.values_panic(values); + }); + self + } + /// ON CONFLICT expression /// /// # Examples