From 6ad224dc4e6b8b4dd1255c2df0470b24f22d9e27 Mon Sep 17 00:00:00 2001 From: Michael Cuffaro Date: Fri, 6 Sep 2024 14:00:17 -0400 Subject: [PATCH] add back get_column_format() and correct typo --- src/valve.rs | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/src/valve.rs b/src/valve.rs index cf07045..06e2159 100644 --- a/src/valve.rs +++ b/src/valve.rs @@ -1981,12 +1981,7 @@ impl Valve { .collect::>(); let (options, _) = normalize_options(&options, 0)?; - let path = row - .try_get::<&str, &str>("path") - .ok() - .ok_or(ValveError::InputError( - "No column \"table\" found in row".to_string(), - ))?; + let path = row.try_get::<&str, &str>("path").ok().unwrap_or_default(); let path = match save_dir { Some(save_dir) => { if !options.contains("save") { @@ -2278,6 +2273,46 @@ impl Valve { } } + /// Given a table name and the name of a column in that table, find (in the database) the value + /// of the optional configuration parameter called 'format' and return it, or an empty string + /// if no format parameter has been configured or if none has been defined for that column. + /// The format parameter indicates how values of the given column are to be formatted when + /// saving a table to an external file. + pub async fn get_column_format_from_db(&self, table: &str, column: &str) -> Result { + if !self + .config + .table + .get("datatype") + .and_then(|t| Some(t.column.clone())) + .and_then(|c| Some(c.keys().cloned().collect::>())) + .and_then(|v| Some(v.contains(&"format".to_string()))) + .expect("Could not find column configrations for the datatype table") + { + Ok("".to_string()) + } else { + let sql = format!( + r#"SELECT d."format" + FROM "column" c, "datatype" d + WHERE c."table" = '{}' + AND c."column" = '{}' + AND c."datatype" = d."datatype""#, + table, column + ); + let rows = sqlx_query(&sql).fetch_all(&self.pool).await?; + if rows.len() > 1 { + panic!( + "Multiple entries corresponding to '{}' in datatype table", + column + ); + } + let format_string = rows[0] + .try_get::<&str, &str>("format") + .ok() + .unwrap_or_default(); + Ok(format_string.to_string()) + } + } + /// Given a table name, a row number, and a transaction through which to access the database, /// search for and return the row number of the row that is marked as previous to the given row. pub async fn get_previous_row(&self, table: &str, row: &u32) -> Result {