From f30a4dafa5736c58bda54569ec9f46e6fdb2e200 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 20 Mar 2022 01:31:45 +0100 Subject: 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. --- src/main.rs | 2 ++ 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( dbconn: &rusqlite::Connection, table_name: &str, record_id: &str, -) -> Result { + exclude_columns: &[C], +) -> Result +where C: AsRef + PartialEq +{ 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( dbconn: &rusqlite::Connection, table_name: &str, primary_key_column: &str, record_id: &str, -) -> Result { + exclude_columns: &[C], +) -> Result +where C: AsRef + PartialEq +{ 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 } -- cgit v1.2.3