Skip to content

Commit

Permalink
change 'user_view' to 'text_view'; change get_last_undo() to get_reco…
Browse files Browse the repository at this point in the history
…rd_to_redo() and get_last_change() to get_record_to_undo()
  • Loading branch information
lmcmicu committed Nov 3, 2023
1 parent 5e9e993 commit 93aa011
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion scripts/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def export_data(cursor, is_sqlite, args):

# Fetch the rows from the table and write them to a corresponding TSV file in the
# output directory:
cursor.execute(f'SELECT {select} FROM "{table}_user_view" ORDER BY "row_number"')
cursor.execute(f'SELECT {select} FROM "{table}_text_view" ORDER BY "row_number"')
colnames = [d[0] for d in cursor.description]
rows = map(lambda r: dict(zip(colnames, r)), cursor)
fieldnames = [c for c in colnames if c != "row_number"]
Expand Down
40 changes: 19 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,15 +838,14 @@ fn get_sql_for_standard_view(table: &str, pool: &AnyPool) -> (String, String) {
}

/// Given the tables configuration map, the name of a table and a database connection pool,
/// generate SQL for creating a more user-friendly version of the view that is generated by
/// generate SQL for creating a more user-friendly version of the view than the one generated by
/// [get_sql_for_standard_view()]. Unlike the standard view generated by that function, the view
/// generated by this function (called my_table_user_view) always shows all of the values of every
/// column in the table, even when those values contain errors. Also unlike the standard view, the
/// datatypes of all columns in the user view are TEXT (this is what makes it possible to always
/// display error values). Like the function for generating a standard view, the SQL generated by
/// this function is in the form of a tuple of Strings, with the first string being a SQL statement
/// generated by this function (called my_table_text_view) always shows all of the values (which are
/// all rendered as text) of every column in the table, even when those values contain SQL datatype
/// errors. Like the function for generating a standard view, the SQL generated by this function is
/// returned in the form of a tuple of Strings, with the first string being a SQL statement
/// for dropping the view, and the second string being a SQL statement for creating it.
fn get_sql_for_user_view(
fn get_sql_for_text_view(
tables_config: &mut SerdeMap,
table: &str,
pool: &AnyPool,
Expand All @@ -867,10 +866,10 @@ fn get_sql_for_user_view(
.and_then(|t| Some(t.collect::<Vec<_>>()))
.unwrap();

// Add a second "user view" such that the datatypes of all values are TEXT and appear
// Add a second "text view" such that the datatypes of all values are TEXT and appear
// directly in their corresponsing columns (rather than as NULLs) even when they have
// errors.
let mut drop_view_sql = format!(r#"DROP VIEW IF EXISTS "{}_user_view""#, table);
// SQL datatype errors.
let mut drop_view_sql = format!(r#"DROP VIEW IF EXISTS "{}_text_view""#, table);
if pool.any_kind() == AnyKind::Postgres {
drop_view_sql.push_str(" CASCADE");
}
Expand Down Expand Up @@ -928,7 +927,7 @@ fn get_sql_for_user_view(
};

let create_view_sql = format!(
r#"CREATE VIEW "{table}_user_view" AS
r#"CREATE VIEW "{table}_text_view" AS
SELECT {outer_columns}
FROM (
SELECT {inner_columns}
Expand Down Expand Up @@ -1087,12 +1086,12 @@ pub async fn configure_db(
}

let (drop_view_sql, create_view_sql) = get_sql_for_standard_view(&table_name, pool);
let (drop_user_view_sql, create_user_view_sql) =
get_sql_for_user_view(tables_config, &table_name, pool);
table_statements.push(drop_user_view_sql);
let (drop_text_view_sql, create_text_view_sql) =
get_sql_for_text_view(tables_config, &table_name, pool);
table_statements.push(drop_text_view_sql);
table_statements.push(drop_view_sql);
table_statements.push(create_view_sql);
table_statements.push(create_user_view_sql);
table_statements.push(create_text_view_sql);

setup_statements.insert(table_name.to_string(), table_statements);
}
Expand Down Expand Up @@ -2047,9 +2046,8 @@ async fn switch_undone_state(
Ok(())
}

/// Given a database pool fetch the last database change represented by the last row inserted to the
/// history table that has not been undone.
pub async fn get_last_change(pool: &AnyPool) -> Result<Option<AnyRow>, sqlx::Error> {
/// Given a database pool fetch the last row inserted to the history table that has not been undone.
pub async fn get_record_to_undo(pool: &AnyPool) -> Result<Option<AnyRow>, sqlx::Error> {
// Look in the history table, get the row with the greatest ID, get the row number,
// from, and to, and determine whether the last operation was a delete, insert, or update.
let is_clause = if pool.any_kind() == AnyKind::Sqlite {
Expand All @@ -2070,7 +2068,7 @@ pub async fn get_last_change(pool: &AnyPool) -> Result<Option<AnyRow>, sqlx::Err

/// Given a database pool fetch the row in the history table that has been most recently marked as
/// undone.
pub async fn get_last_undo(pool: &AnyPool) -> Result<Option<AnyRow>, sqlx::Error> {
pub async fn get_record_to_redo(pool: &AnyPool) -> Result<Option<AnyRow>, sqlx::Error> {
// Look in the history table, get the row with the greatest ID, get the row number,
// from, and to, and determine whether the last operation was a delete, insert, or update.
let is_not_clause = if pool.any_kind() == AnyKind::Sqlite {
Expand Down Expand Up @@ -2100,7 +2098,7 @@ pub async fn undo(
pool: &AnyPool,
undo_user: &str,
) -> Result<(), sqlx::Error> {
let last_change = match get_last_change(pool).await? {
let last_change = match get_record_to_undo(pool).await? {
None => {
eprintln!("WARN: Nothing to undo.");
return Ok(());
Expand Down Expand Up @@ -2196,7 +2194,7 @@ pub async fn redo(
pool: &AnyPool,
redo_user: &str,
) -> Result<(), sqlx::Error> {
let last_undo = match get_last_undo(pool).await? {
let last_undo = match get_record_to_redo(pool).await? {
None => {
eprintln!("WARN: Nothing to redo.");
return Ok(());
Expand Down

0 comments on commit 93aa011

Please sign in to comment.