MrTwoc

MrTwoc

Hello!

[4]Todo-rs-Sqlite

Previous chapter content

Project address: https://github.com/MrTwoc/todo-rs

The previous chapter implemented tabular output of tasks, but the beautification effect was not very good.

This time, the JSON file storing tasks has been changed to use SQLite for data storage, and the task levels have been changed from a fixed few levels to u8 levels, allowing for a larger hierarchical space.

Additionally, a user management feature based on SQLite has been added to prepare for a possible future change of the project structure to a client-server architecture.

A TOML configuration file has been added, currently containing only one parameter: if_login. If set to false, it skips login, with the default being local login.

In the future, if support for PostgreSQL is needed, the connection address can be set in the configuration file.

Current challenges encountered:

  1. Batch status updates for tasks in SQLite
  2. Task editing in SQLite

For task editing, the parameters are matched using a match statement during iteration, and each matching task is updated one by one. Each matching task is replaced with a statement like:

conn.execute(
  "UPDATE tasks SET description = ? WHERE id = ?",
  (value, task_id),
)?;

For batch updates of tasks, the IDs are grouped into an array using iter and collect, and then the status is modified for each task during iteration:

pub fn sql_update_status(args: &[&str]) -> Result<(), Box<dyn std::error::Error>> {
    let status = args[1];
    let ids: Vec<u32> = args[2..]
        .iter()
        .map(|s| s.parse())
        .collect::<Result<_, _>>()?;
    let conn = get_conn()?;
    for id in ids {
        conn.execute(
            "UPDATE tasks SET task_status = ? WHERE id = ?",
            (status, id),
        )?;
    }
    info!("[sqlite] Task status updated successfully");
    println!("Task updated");
    Ok(())
}

Unimplemented content:

Integrate sorting algorithms

Integrate searching algorithms

Hide the application to run in the system tray

Change task deletion to a status change for tasks, where deleted tasks will have their status changed from 0 to 1, rather than being directly deleted.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.