aboutsummaryrefslogtreecommitdiff
path: root/src/db/mod.rs
blob: 434d74ac3da762c78edc92538fb17ae04c929640 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use anyhow::Result;
use rusqlite::{params, Connection};
use rusqlite_migration::{Migrations, M};

use crate::model::DbEntry;
use crate::util::time;
use crate::{
    model::{Card, Question},
    space_repetition,
    util::serialization,
};

pub fn init(database: String) -> Result<Connection> {
    let mut conn = Connection::open(database)?;
    let migrations = Migrations::new(vec![
        M::up(include_str!("sql/1-init.sql")),
        M::up(include_str!("sql/2-primary-key-question-responses.sql")),
        M::up(include_str!("sql/3-drop-deck-read.sql")),
    ]);
    migrations.to_latest(&mut conn)?;
    Ok(conn)
}

pub fn all(conn: &Connection) -> Result<Vec<DbEntry>> {
    let mut stmt = conn.prepare("SELECT question, responses, deleted FROM cards")?;

    let res: Result<Vec<DbEntry>, rusqlite::Error> = stmt
        .query_map([], |row| {
            let responses: String = row.get(1)?;
            Ok(DbEntry {
                question: row.get(0)?,
                responses: serialization::line_to_words(&responses),
                deleted: row.get(2)?,
            })
        })?
        .collect();

    Ok(res?)
}

pub fn insert(conn: &mut Connection, questions: &Vec<Question>) -> Result<()> {
    let now = time::seconds_since_unix_epoch()?;
    let state = serde_json::to_string(&space_repetition::init())?;

    let tx = conn.transaction()?;
    for Question {
        question,
        responses,
    } in questions
    {
        let responses = serialization::words_to_line(responses);
        tx.execute(
            "
            INSERT INTO cards (question, responses, state, created, ready)
            VALUES (?, ?, ?, ?, ?)
            ",
            params![question, responses, state, now, now],
        )?;
    }
    tx.commit()?;
    Ok(())
}

pub fn delete(conn: &mut Connection, questions: &Vec<Question>) -> Result<()> {
    let now = time::seconds_since_unix_epoch()?;

    let tx = conn.transaction()?;
    for Question {
        question,
        responses,
    } in questions
    {
        let responses = serialization::words_to_line(responses);
        tx.execute(
            "UPDATE cards SET deleted = ? WHERE question = ? AND responses = ?",
            params![now, question, responses],
        )?;
    }
    tx.commit()?;
    Ok(())
}

pub fn undelete(conn: &mut Connection, questions: &Vec<Question>) -> Result<()> {
    let tx = conn.transaction()?;
    for Question {
        question,
        responses,
    } in questions
    {
        let responses = serialization::words_to_line(responses);
        tx.execute(
            "UPDATE cards SET deleted = NULL WHERE question = ? AND responses = ?",
            params![question, responses],
        )?;
    }
    tx.commit()?;
    Ok(())
}

pub fn pick_random_ready(conn: &Connection) -> Option<Card> {
    let now = time::seconds_since_unix_epoch().ok()?;

    let mut stmt = conn
        .prepare(
            "
        SELECT question, responses, state, ready
        FROM cards
        WHERE deleted IS NULL AND ready <= ?
        ORDER BY RANDOM()
        LIMIT 1
        ",
        )
        .ok()?;

    let mut rows = stmt.query([now]).ok()?;
    let row = rows.next().ok()??;
    let state_str: String = row.get(2).ok()?;
    let responses_str: String = row.get(1).ok()?;

    Some(Card {
        question: row.get(0).ok()?,
        responses: serialization::line_to_words(&responses_str),
        state: serde_json::from_str(&state_str).ok()?,
        ready: row.get(3).ok()?,
    })
}

pub fn next_ready(conn: &Connection) -> Option<u64> {
    let mut stmt = conn
        .prepare(
            "
        SELECT ready 
        FROM cards 
        WHERE deleted IS NULL
        ORDER BY ready
        LIMIT 1
    ",
        )
        .ok()?;

    let mut rows = stmt.query([]).ok()?;
    let row = rows.next().ok()??;
    row.get(0).ok()?
}

pub fn count_available(conn: &Connection) -> Option<i32> {
    let now = time::seconds_since_unix_epoch().ok()?;
    let mut stmt = conn
        .prepare("SELECT COUNT(*) FROM cards WHERE ready <= ? AND deleted IS NULL")
        .ok()?;

    let mut rows = stmt.query([now]).ok()?;
    let row = rows.next().ok()??;
    row.get(0).ok()?
}

pub fn update(conn: &Connection, question: &str, state: &space_repetition::State) -> Result<()> {
    let now = time::seconds_since_unix_epoch()?;
    let ready = now + state.get_interval_seconds();
    let state_str = serde_json::to_string(state)?;

    conn.execute(
        "
        UPDATE cards
        SET state = ?, updated = ?, ready = ?
        WHERE question = ?
        ",
        params![state_str, now, ready, question],
    )?;

    Ok(())
}