Skip to content

Commit

Permalink
add back get_column_format() and correct typo
Browse files Browse the repository at this point in the history
  • Loading branch information
lmcmicu committed Sep 6, 2024
1 parent 804872e commit 6ad224d
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/valve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1981,12 +1981,7 @@ impl Valve {
.collect::<Vec<_>>();
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") {
Expand Down Expand Up @@ -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<String> {
if !self
.config
.table
.get("datatype")
.and_then(|t| Some(t.column.clone()))
.and_then(|c| Some(c.keys().cloned().collect::<Vec<_>>()))
.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<u32> {
Expand Down

0 comments on commit 6ad224d

Please sign in to comment.