From 1b467a73e02c89b4b6b63957d6f68c620c6fcb44 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 19 Mar 2022 04:06:15 +0100 Subject: select(): Change `column_names` to `Vec` Change `column_names` from `Vec` to `Vec`. This way we don't have to clone the `yaml_rust::Yaml` value to insert into the `yaml_rust::yaml::Hash`. Had some issues getting this to work. I tried calling `stmt.column_names()` on its own, but this caused a borrow checker error, as that creates an immutable borrow, while the `stmt.query_map()` call is a mutable borrow, and we can't do both. Found this ticket that describes exactly the problem I was having: https://github.com/rusqlite/rusqlite/pull/523 Turns out I had the right idea to begin with: I needed to collect the column names into a `Vec`. But don't use a `Vec` of `yaml_rust::Yaml` values, because that requires excess cloning. --- src/select.rs | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/select.rs b/src/select.rs index 5b11fae..b1319e5 100644 --- a/src/select.rs +++ b/src/select.rs @@ -22,11 +22,18 @@ pub fn select( ).unwrap(); let column_count = stmt.column_count(); - let column_names: Vec = stmt.column_names() - .iter() - .map(|col| yaml_rust::Yaml::String((*col).to_owned())) - .collect(); + // let column_names: Vec = stmt.column_names() + // .iter() + // .map(|col| yaml_rust::Yaml::String((*col).to_owned())) + // .collect(); // dbg!(column_names); + // let column_names = stmt.column_names(); + + let column_names: Vec = stmt + .column_names() + .into_iter() + .map(String::from) + .collect(); let rows = stmt.query_map( rusqlite::named_params! { @@ -43,16 +50,40 @@ pub fn select( let mut data = yaml_rust::yaml::Hash::new(); for (i, column) in column_names.iter().enumerate() { + let column_name = column.to_string(); let column_value: Yaml = row.get(i)?; - data.insert(column.clone(), column_value.into_inner()); + + data.insert( + yaml_rust::Yaml::String(column_name), + column_value.into_inner(), + ); } + // let mut data: Vec = Vec::with_capacity(column_count); + // + // for i in 0..column_count { + // let column_value: Yaml = row.get(i)?; + // data.push(column_value.into_inner()); + // } + Ok(data) }, ).unwrap(); let mut row = None; for row_result in rows { + // let mut data = yaml_rust::yaml::Hash::new(); + // let column_values = row_result.unwrap(); + // + // for (i, column) in column_names.iter().enumerate() { + // data.insert( + // yaml_rust::Yaml::String(column.to_string()), + // column_values[i], + // ); + // } + // + // row = Some(yaml_rust::Yaml::Hash(data)); + row = Some(yaml_rust::Yaml::Hash(row_result.unwrap())); dbg!(&row); } -- cgit v1.2.3