Implement the save_all method.
This commit is contained in:
@@ -62,30 +62,27 @@ impl LibraryItemSqliteRepository {
|
||||
)
|
||||
}
|
||||
|
||||
fn save_all(&self, entities: Vec<LibraryItemEntity>) -> Result<(), Error> {
|
||||
pub fn save_all(&self, entities: Vec<LibraryItemEntity>) -> Result<(), Error> {
|
||||
let database_connection = self.get_connection();
|
||||
|
||||
/*
|
||||
Expected query if there is one single item in "entities":
|
||||
INSERT INTO library_item (id, name, type) VALUES (?1, ?2, ?3);
|
||||
|
||||
Expected query if there are two items in "entities":
|
||||
INSERT INTO library_item (id, name, type) VALUES (?1, ?2, ?3), ($4, $5, $6);
|
||||
*/
|
||||
let mut query = String::from("INSERT INTO library_item (id, name, type) VALUES ");
|
||||
let mut params: Vec<rusqlite::types::Value> = Vec::new();
|
||||
|
||||
for (i, entity) in entities.iter().enumerate() {
|
||||
if i > 0 {
|
||||
query.push_str(", ");
|
||||
}
|
||||
query.push_str(&format!("(${}, ${}, ${})", i * 3 + 1, i * 3 + 2, i * 3 + 3));
|
||||
params.push(entity.id.to_string().into());
|
||||
params.push(entity.name.clone().into());
|
||||
params.push(entity.r#type.into());
|
||||
}
|
||||
|
||||
let mut prepared_statement = database_connection
|
||||
.prepare(
|
||||
"INSERT INTO library_item (
|
||||
id,
|
||||
name,
|
||||
type,
|
||||
) VALUES (?1, ?2, ?3);",
|
||||
)
|
||||
.prepare(&query)
|
||||
.expect("Unable to prepare statement for user saving…");
|
||||
prepared_statement.execute(rusqlite::params![
|
||||
entity.id.to_string(),
|
||||
entity.name,
|
||||
entity.r#type
|
||||
]).expect("Unable to insert the library items.");
|
||||
|
||||
prepared_statement.execute(rusqlite::params_from_iter(params))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user