aboutsummaryrefslogtreecommitdiff
path: root/src/db/db.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db/db.rs')
-rw-r--r--src/db/db.rs44
1 files changed, 38 insertions, 6 deletions
diff --git a/src/db/db.rs b/src/db/db.rs
index 30ea1da..b42da3f 100644
--- a/src/db/db.rs
+++ b/src/db/db.rs
@@ -19,13 +19,23 @@ pub fn init(database: String) -> Result<Connection> {
Ok(conn)
}
+pub fn last_deck_read(conn: &Connection) -> Option<u64> {
+ let mut stmt = conn
+ .prepare("SELECT deck_read FROM cards ORDER BY deck_read DESC LIMIT 1")
+ .ok()?;
+
+ let mut rows = stmt.query([]).ok()?;
+ let row = rows.next().ok()??;
+ row.get(0).ok()?
+}
+
/// Synchronize the DB with the deck:
///
/// - insert new cards,
/// - keep existing cards,
/// - hide unused cards (keep state in case the card is added back afterward).
pub fn synchronize(conn: &Connection, entries: Vec<Entry>) -> Result<()> {
- let now = time::now()?;
+ let now = time::seconds_since_unix_epoch()?;
let state = serde_json::to_string(&space_repetition::init())?;
@@ -76,19 +86,21 @@ fn delete_read_before(conn: &Connection, t: u64) -> Result<()> {
}
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
+ WHERE deleted IS NULL AND ready <= ?
ORDER BY RANDOM()
LIMIT 1
",
)
.ok()?;
- let mut rows = stmt.query([]).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()?;
@@ -101,9 +113,29 @@ pub fn pick_random_ready(conn: &Connection) -> Option<Card> {
})
}
+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::now().ok()?;
- let mut stmt = conn.prepare("SELECT COUNT(*) FROM cards WHERE ready <= ? AND deleted IS NULL").ok()?;
+ 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()??;
@@ -111,7 +143,7 @@ pub fn count_available(conn: &Connection) -> Option<i32> {
}
pub fn update(conn: &Connection, question: &String, state: &space_repetition::State) -> Result<()> {
- let now = time::now()?;
+ let now = time::seconds_since_unix_epoch()?;
let ready = now + state.get_interval_seconds();
let state_str = serde_json::to_string(state)?;