diff options
| author | Teddy Wing | 2022-03-19 00:37:32 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2022-03-19 00:37:32 +0100 | 
| commit | d78addd46aa7633346c47dd310c972ef0e9029fa (patch) | |
| tree | 794fc485f65b19a29e8162866c05de22f233d7fc /src | |
| parent | 052e0e9639e15a4cce5cb5f0ec6399ed195ebb90 (diff) | |
| download | yaqlite-d78addd46aa7633346c47dd310c972ef0e9029fa.tar.bz2 | |
select(): Build a `yaml_rust::yaml::Hash` for each row
Instead of my earlier `std::collections::HashMap`, build the type we
want to return directly in the row closure.
Required copying some data like the column names, but I don't see a way
around that. I suppose we could make `yaml_rust::Yaml`s from the column
headers in the row closure too instead of copying an existing column
header that we otherwise never use.
This nearly passes the test I have for this function, but it includes
the `id` column in the `Yaml` hash. The primary key column will need to
be removed.
Diffstat (limited to 'src')
| -rw-r--r-- | src/select.rs | 13 | ||||
| -rw-r--r-- | src/yaml/sql.rs | 6 | 
2 files changed, 16 insertions, 3 deletions
| diff --git a/src/select.rs b/src/select.rs index 6f82727..5b11fae 100644 --- a/src/select.rs +++ b/src/select.rs @@ -39,21 +39,28 @@ pub fn select(              //     data.push(row.get(i)?);              // } -            let mut data: HashMap<&yaml_rust::Yaml, Yaml> = HashMap::new(); +            // let mut data: HashMap<&yaml_rust::Yaml, Yaml> = HashMap::new(); +            let mut data = yaml_rust::yaml::Hash::new();              for (i, column) in column_names.iter().enumerate() { -                data.insert(&column, row.get(i)?); +                let column_value: Yaml = row.get(i)?; +                data.insert(column.clone(), column_value.into_inner());              }              Ok(data)          },      ).unwrap(); +    let mut row = None;      for row_result in rows { -        let row = row_result.unwrap(); +        row = Some(yaml_rust::Yaml::Hash(row_result.unwrap()));          dbg!(&row);      } +    if let Some(r) = row { +        return r; +    } +      // todo!();      yaml_rust::Yaml::Null  } diff --git a/src/yaml/sql.rs b/src/yaml/sql.rs index 49d83f7..ef72aae 100644 --- a/src/yaml/sql.rs +++ b/src/yaml/sql.rs @@ -16,6 +16,12 @@ pub(crate) struct Yaml<'a>(pub Cow<'a, yaml::Yaml>);  // impl From< +impl<'a> Yaml<'a> { +    pub fn into_inner(self) -> yaml::Yaml { +        self.0.into_owned() +    } +} +  impl<'a> rusqlite::ToSql for Yaml<'a> {      fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {          use rusqlite::types::ToSqlOutput; | 
