summaryrefslogtreecommitdiff
path: root/tables/src/main.rs
blob: 4f552a6e9fff74d5d84f929fcb28bced869f6694 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use anyhow::Context;
use sqlx::{migrate::MigrateDatabase, Row};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let database_url = std::env::args()
        .nth(1)
        .context("You should provide path to the database")?
        .parse::<String>()?;

    if !sqlx::Sqlite::database_exists(&database_url).await? {
        return Err(anyhow::Error::msg(format!(
            "Database {database_url} not found"
        )));
    }
    let db = sqlx::SqlitePool::connect(&database_url).await?;
    let g0 = sqlx::query("SELECT size FROM graphs LIMIT 1")
        .fetch_one(&db)
        .await?;
    let vert_num = g0.get::<i32, &str>("size") as usize;
    println!("Size of graphs: {vert_num}");
    let mut res = vec![vec![0; vert_num + 1]; vert_num + 1];
    let graphs = sqlx::query("SELECT ind_dom, forced_geod FROM graphs")
        .fetch_all(&db)
        .await?;
    let mut cnt = 0;
    for row in graphs.into_iter() {
        let ind_dom = row.get::<i32, &str>("ind_dom") as usize;
        let forced_geod = row.get::<i32, &str>("forced_geod") as usize;
        res[ind_dom][forced_geod] += 1;
        cnt += 1;
    }

    println!("Number of rows: {cnt}");

    let mut table = Vec::new();
    table.push("\\begin{table}[H]\n".to_string());
    table.push("  \\centering\n".to_string());
    table.push(format!(
        "  \\caption{{Результаты вычислений для графов с {} вершинами}}\n",
        vert_num
    ));
    table.push("  \\begin{tabular}{".to_string());
    for _ in 0..=vert_num + 1 {
        table.push("|c".to_string());
    }
    table.push("|}\n".to_string());
    table.push("  \\hline\n".to_string());

    table.push("    $i(G)$ \\textbackslash{} $fg(G)$ ".to_string());
    for i in 0..=vert_num {
        table.push(format!("& {i} "));
    }
    table.push("\\\\ \\hline\n".to_string());
    for i in 0..=vert_num {
        table.push("    ".to_string());
        table.push(format!("{i} "));
        for j in 0..=vert_num {
            table.push(format!("& {} ", res[i][j]));
        }
        table.push(" \\\\ \\hline\n".to_string());
    }
    table.push("  \\end{tabular}\n".to_string());
    table.push("\\end{table}\n".to_string());

    println!("{}", table.join(""));

    db.close().await;

    Ok(())
}