DB integration ACTUALLY WORKS wow! 🎉
This commit is contained in:
parent
7d06ba9404
commit
2534cd2730
1 changed files with 27 additions and 22 deletions
49
src/db.rs
49
src/db.rs
|
@ -1,12 +1,10 @@
|
|||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::files;
|
||||
use rusqlite::{params, Connection, Result};
|
||||
|
||||
use chrono::FixedOffset;
|
||||
use rusqlite::{ Connection, Result};
|
||||
use super::files::*;
|
||||
use super::net::*;
|
||||
use chrono::Utc; //Maybe use a different time?
|
||||
use chrono::DateTime;
|
||||
|
||||
/*
|
||||
Cache is the in-memory database
|
||||
|
@ -24,7 +22,7 @@ fn get_db_path() -> PathBuf {
|
|||
get_data_directory().join(DB_LOCATION)
|
||||
}
|
||||
|
||||
const feeds_table_create: &str = "CREATE TABLE IF NOT EXISTS 'feeds' (
|
||||
const FEEDS_TABLE_CREATE: &str = "CREATE TABLE IF NOT EXISTS 'feeds' (
|
||||
'feedID' INTEGER NOT NULL,
|
||||
'title' TEXT NOT NULL,
|
||||
'description' TEXT,
|
||||
|
@ -35,12 +33,12 @@ const feeds_table_create: &str = "CREATE TABLE IF NOT EXISTS 'feeds' (
|
|||
PRIMARY KEY('feedID')
|
||||
);";
|
||||
|
||||
const feeds_index_create: &str = "CREATE INDEX IF NOT EXISTS 'subscribed_feeds_idx' ON 'feeds' (
|
||||
const FEEDS_INDEX_CREATE: &str = "CREATE INDEX IF NOT EXISTS 'subscribed_feeds_idx' ON 'feeds' (
|
||||
'feedID' ASC
|
||||
) WHERE 'subscribed' = 1;";
|
||||
|
||||
/* */
|
||||
const items_table_create: &str = "CREATE TABLE IF NOT EXISTS 'items' (
|
||||
const ITEMS_TABLE_CREATE: &str = "CREATE TABLE IF NOT EXISTS 'items' (
|
||||
'itemID' INTEGER NOT NULL,
|
||||
'title' TEXT NOT NULL,
|
||||
'icon' BLOB,
|
||||
|
@ -50,17 +48,17 @@ const items_table_create: &str = "CREATE TABLE IF NOT EXISTS 'items' (
|
|||
'read' INTEGER DEFAULT 0,
|
||||
PRIMARY KEY('itemID')
|
||||
);";
|
||||
const items_index_create: &str = "CREATE INDEX IF NOT EXISTS 'items_idx' on 'items'('itemID' ASC);";
|
||||
const ITEMS_INDEX_CREATE: &str = "CREATE INDEX IF NOT EXISTS 'items_idx' on 'items'('itemID' ASC);";
|
||||
|
||||
pub fn initialize() {
|
||||
let path = get_db_path();
|
||||
println!("Database at {} initialized", path.as_os_str().display());
|
||||
let conn = Connection::open(path).unwrap();
|
||||
conn.execute(feeds_table_create, []).unwrap();
|
||||
conn.execute(feeds_index_create, []).unwrap();
|
||||
conn.execute(items_table_create, []).unwrap();
|
||||
conn.execute(items_index_create, []).unwrap();
|
||||
conn.close();
|
||||
conn.execute(FEEDS_TABLE_CREATE, []).unwrap();
|
||||
conn.execute(FEEDS_INDEX_CREATE, []).unwrap();
|
||||
conn.execute(ITEMS_TABLE_CREATE, []).unwrap();
|
||||
conn.execute(ITEMS_INDEX_CREATE, []).unwrap();
|
||||
conn.close().unwrap();
|
||||
println!("Database Initialized.")
|
||||
}
|
||||
|
||||
|
@ -74,7 +72,7 @@ pub fn add_feed(url: &str) {
|
|||
[feed.title, url.to_owned(), feed.description, time],
|
||||
)
|
||||
.unwrap();
|
||||
conn.close();
|
||||
conn.close().unwrap();
|
||||
store_items(new_feed);
|
||||
}
|
||||
|
||||
|
@ -92,7 +90,7 @@ pub fn store_items(feed: rss::Channel) {
|
|||
)
|
||||
.unwrap();
|
||||
});
|
||||
conn.close();
|
||||
conn.close().unwrap();
|
||||
}
|
||||
|
||||
pub fn return_item() -> String {
|
||||
|
@ -116,13 +114,20 @@ pub fn return_item() -> String {
|
|||
}
|
||||
|
||||
pub struct Feed {
|
||||
pub feedID: u8,
|
||||
pub feed_id: u8,
|
||||
pub title: String,
|
||||
pub description: Option<String>,
|
||||
pub icon: Option<String>,
|
||||
pub url: String,
|
||||
pub subscribed: bool,
|
||||
pub last_updated: Option<String>,
|
||||
pub last_updated: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
fn time_string_conversion(str: String) -> Option<DateTime<Utc>>{
|
||||
match DateTime::parse_from_rfc2822(&str) {
|
||||
Ok(dt) => {Some(dt.to_utc())},
|
||||
Err(_) => {None}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_feeds() -> Vec<Feed> {
|
||||
|
@ -131,13 +136,13 @@ pub fn get_feeds() -> Vec<Feed> {
|
|||
let rows: Result<Vec<Feed>> = stmt
|
||||
.query_map([], |row| {
|
||||
Ok(Feed {
|
||||
feedID: row.get(0).unwrap(),
|
||||
feed_id: row.get(0).unwrap(),
|
||||
title: row.get(1).unwrap(),
|
||||
description: row.get(2).unwrap(),
|
||||
icon: row.get(3).unwrap(),
|
||||
url: row.get(4).unwrap(),
|
||||
subscribed: row.get::<_,bool>(5).unwrap(),
|
||||
last_updated: row.get(6).unwrap(),
|
||||
last_updated:time_string_conversion(row.get(6).unwrap()),
|
||||
})
|
||||
}).unwrap().collect();
|
||||
match rows {
|
||||
|
@ -167,8 +172,8 @@ pub fn update_feeds() {
|
|||
let url = feed.unwrap().url.clone();
|
||||
urls.push(url);
|
||||
}
|
||||
stmt.finalize();
|
||||
conn.close();
|
||||
stmt.finalize().unwrap();
|
||||
conn.close().unwrap();
|
||||
|
||||
for u in urls {
|
||||
store_items(load_rss(&u).unwrap());
|
||||
|
|
Loading…
Reference in a new issue