diff options
author | Teddy Wing | 2022-03-20 01:46:27 +0100 |
---|---|---|
committer | Teddy Wing | 2022-03-20 01:48:32 +0100 |
commit | 09525eb8aae8801468a75b867361106f6f830e7c (patch) | |
tree | 9526332d13d602eb724eb574ce07f1c99b403a9d | |
parent | f30a4dafa5736c58bda54569ec9f46e6fdb2e200 (diff) | |
download | yaqlite-09525eb8aae8801468a75b867361106f6f830e7c.tar.bz2 |
select: Exclude primary key column unless given excluded columns
If no excluded columns are given, exclude the primary key column by
default. Otherwise, exclude only those columns in the `exclude_columns`
list.
-rw-r--r-- | src/main.rs | 6 | ||||
-rw-r--r-- | src/select.rs | 18 |
2 files changed, 16 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs index 0fe52aa..1d918d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,7 +32,7 @@ enum Command { record_id: String, #[clap(long)] - exclude_column: Vec<String>, + exclude_column: Option<Vec<String>>, }, } @@ -85,14 +85,14 @@ fn main() { &table_name, &pk, &record_id, - &exclude_column, + exclude_column.as_deref(), ).unwrap(), None => yaqlite::select( &dbconn, &table_name, &record_id, - &exclude_column, + exclude_column.as_deref(), ).unwrap(), }; diff --git a/src/select.rs b/src/select.rs index fb1b977..cc82cb5 100644 --- a/src/select.rs +++ b/src/select.rs @@ -2,7 +2,7 @@ pub fn select<C>( dbconn: &rusqlite::Connection, table_name: &str, record_id: &str, - exclude_columns: &[C], + exclude_columns: Option<&[C]>, ) -> Result<yaml_rust::Yaml, crate::Error> where C: AsRef<str> + PartialEq<String> { @@ -20,7 +20,7 @@ pub fn select_by_column<C>( table_name: &str, primary_key_column: &str, record_id: &str, - exclude_columns: &[C], + exclude_columns: Option<&[C]>, ) -> Result<yaml_rust::Yaml, crate::Error> where C: AsRef<str> + PartialEq<String> { @@ -53,9 +53,17 @@ where C: AsRef<str> + PartialEq<String> let mut data = yaml_rust::yaml::Hash::new(); for (i, column) in column_names.iter().enumerate() { - // Don't include excluded columns in the resulting hash. - if exclude_columns.iter().any(|c| c == column) { - continue + // Don't include excluded columns in the resulting hash. If + // `exclude_columns` is None, exclude the primary key column. + match exclude_columns { + Some(exclude_columns) => + if exclude_columns.iter().any(|c| c == column) { + continue + } + None => + if column == primary_key_column { + continue + } } let column_name = column.to_owned(); |