diff options
| author | Teddy Wing | 2022-03-19 04:06:15 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2022-03-19 04:06:15 +0100 | 
| commit | 1b467a73e02c89b4b6b63957d6f68c620c6fcb44 (patch) | |
| tree | 00641f551aa7f62ccfef460e286b783ad66560ff | |
| parent | d78addd46aa7633346c47dd310c972ef0e9029fa (diff) | |
| download | yaqlite-1b467a73e02c89b4b6b63957d6f68c620c6fcb44.tar.bz2 | |
select(): Change `column_names` to `Vec<String>`
Change `column_names` from `Vec<yaml_rust::Yaml::String>` to
`Vec<String>`. 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.
| -rw-r--r-- | src/select.rs | 41 | 
1 files changed, 36 insertions, 5 deletions
| 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<yaml_rust::Yaml> = stmt.column_names() -        .iter() -        .map(|col| yaml_rust::Yaml::String((*col).to_owned())) -        .collect(); +    // let column_names: Vec<yaml_rust::Yaml> = 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<String> = 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<yaml_rust::Yaml> = 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);      } | 
