diff options
| author | Teddy Wing | 2022-03-20 01:31:45 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2022-03-20 01:31:45 +0100 | 
| commit | f30a4dafa5736c58bda54569ec9f46e6fdb2e200 (patch) | |
| tree | 161f52d74139a2e2721bf7b3110a78d32bc63a5b /src | |
| parent | 2a0182af66a64756eaa88d64e44a18ee5fb94bce (diff) | |
| download | yaqlite-f30a4dafa5736c58bda54569ec9f46e6fdb2e200.tar.bz2 | |
select(): Allow columns to be excluded from YAML output hash
Instead of only excluding the primary key column, exclude any columns in
the given list. This allows us to pass a list of excluded columns on the
command line.
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 2 | ||||
| -rw-r--r-- | src/select.rs | 23 | 
2 files changed, 15 insertions, 10 deletions
| diff --git a/src/main.rs b/src/main.rs index c21dee4..0fe52aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,12 +85,14 @@ fn main() {                      &table_name,                      &pk,                      &record_id, +                    &exclude_column,                  ).unwrap(),                  None => yaqlite::select(                      &dbconn,                      &table_name,                      &record_id, +                    &exclude_column,                  ).unwrap(),              }; diff --git a/src/select.rs b/src/select.rs index 0ddc9db..fb1b977 100644 --- a/src/select.rs +++ b/src/select.rs @@ -1,22 +1,29 @@ -pub fn select( +pub fn select<C>(      dbconn: &rusqlite::Connection,      table_name: &str,      record_id: &str, -) -> Result<yaml_rust::Yaml, crate::Error> { +    exclude_columns: &[C], +) -> Result<yaml_rust::Yaml, crate::Error> +where C: AsRef<str> + PartialEq<String> +{      select_by_column(          dbconn,          table_name,          &crate::sqlite::table_primary_key_column(dbconn, table_name)?,          record_id, +        exclude_columns,      )  } -pub fn select_by_column( +pub fn select_by_column<C>(      dbconn: &rusqlite::Connection,      table_name: &str,      primary_key_column: &str,      record_id: &str, -) -> Result<yaml_rust::Yaml, crate::Error> { +    exclude_columns: &[C], +) -> Result<yaml_rust::Yaml, crate::Error> +where C: AsRef<str> + PartialEq<String> +{      use crate::yaml::Yaml;      let mut stmt = dbconn.prepare( @@ -46,12 +53,8 @@ pub fn select_by_column(              let mut data = yaml_rust::yaml::Hash::new();              for (i, column) in column_names.iter().enumerate() { -                // TODO: Exclude "id" column separately in a subsequent "exclude -                // columns" step. - -                // Don't include the primary key column in the resulting hash as -                // it should not be editable. -                if column == primary_key_column { +                // Don't include excluded columns in the resulting hash. +                if exclude_columns.iter().any(|c| c == column) {                      continue                  } | 
