-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added insert multiple rows support (#8)
* Added insert multiple rows support * Updated inserts to using Vec * Renamed Insert values property * Updated insert_many method documentation * Fixed different value types issue * Reset main.rs * Created eloquent_sql_row macro * Added insert columns validator * Formatting --------- Co-authored-by: Tjardo <[email protected]>
- Loading branch information
Showing
7 changed files
with
141 additions
and
20 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
eloquent_core/src/checks/cannot_insert_with_different_columns.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use crate::{error::EloquentError, PerformChecks, QueryBuilder}; | ||
|
||
pub struct CannotInsertWithDifferentColumns; | ||
|
||
impl PerformChecks for CannotInsertWithDifferentColumns { | ||
fn check(builder: &QueryBuilder) -> Result<(), EloquentError> { | ||
if builder.inserts.is_empty() { | ||
return Ok(()); | ||
} | ||
|
||
let column_count = builder | ||
.inserts | ||
.first() | ||
.map(|insert| insert.values.len()) | ||
.unwrap_or(0); | ||
|
||
let inconsistent_row = builder | ||
.inserts | ||
.iter() | ||
.find(|insert| insert.values.len() != column_count); | ||
|
||
if inconsistent_row.is_some() { | ||
return Err(EloquentError::InconsistentInsertColumns); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{eloquent_sql_row, error::EloquentError, QueryBuilder, ToSql}; | ||
|
||
#[test] | ||
fn test_cannot_insert_with_different_columns() { | ||
let result = QueryBuilder::new() | ||
.table("users") | ||
.insert_many(vec![ | ||
eloquent_sql_row! { | ||
"name" => "Alice", | ||
"email" => "[email protected]", | ||
"is_active" => true, | ||
}, | ||
eloquent_sql_row! { | ||
"name" => "Bob", | ||
"email" => "[email protected]", | ||
"age" => 22, | ||
"is_active" => false, | ||
}, | ||
]) | ||
.to_sql(); | ||
|
||
match result { | ||
Err(EloquentError::InconsistentInsertColumns) => (), | ||
Err(_error) => panic!(), | ||
Ok(_value) => panic!(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,58 @@ impl QueryBuilder { | |
/// ); | ||
/// ``` | ||
pub fn insert(mut self, column: &str, value: impl ToSql + 'static) -> Self { | ||
self.inserts.push(Insert { | ||
column: column.to_string(), | ||
value: Box::new(value), | ||
}); | ||
self.add_insert(column, Box::new(value)); | ||
|
||
self | ||
} | ||
|
||
/// Insert single or multiple rows into the table. | ||
/// | ||
/// ``` | ||
/// use eloquent_core::{QueryBuilder, ToSql, eloquent_sql_row}; | ||
/// | ||
/// let rows = vec![ | ||
/// eloquent_sql_row! { | ||
/// "name" => "Alice", | ||
/// "email" => "[email protected]", | ||
/// "age" => 21, | ||
/// "is_active" => true, | ||
/// }, | ||
/// eloquent_sql_row! { | ||
/// "name" => "Bob", | ||
/// "email" => "[email protected]", | ||
/// "age" => 22, | ||
/// "is_active" => false, | ||
/// }, | ||
/// ]; | ||
/// let query = QueryBuilder::new() | ||
/// .table("users") | ||
/// .insert_many(rows); | ||
/// | ||
/// assert_eq!( | ||
/// query.sql().unwrap(), | ||
/// "INSERT INTO users (name, email, age, is_active) VALUES ('Alice', '[email protected]', 21, true), ('Bob', '[email protected]', 22, false)" | ||
/// ); | ||
/// ``` | ||
pub fn insert_many(mut self, rows: Vec<Vec<(&str, Box<dyn ToSql>)>>) -> Self { | ||
rows.into_iter().for_each(|row| self.add_row(row)); | ||
|
||
self | ||
} | ||
|
||
fn add_insert(&mut self, column: &str, value: Box<dyn ToSql>) { | ||
if let Some(insert) = self.inserts.iter_mut().find(|i| i.column == column) { | ||
insert.values.push(value); | ||
} else { | ||
self.inserts.push(Insert { | ||
column: column.to_string(), | ||
values: vec![value], | ||
}); | ||
} | ||
} | ||
|
||
fn add_row(&mut self, row: Vec<(&str, Box<dyn ToSql>)>) { | ||
row.into_iter() | ||
.for_each(|(column, value)| self.add_insert(column, value)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters