2026-08-24 01:22:00 +03:00
|
|
|
use crate::fs::*;
|
2026-08-25 12:04:07 +03:00
|
|
|
use rusqlite::Connection;
|
2026-06-15 17:06:02 +03:00
|
|
|
|
|
|
|
|
pub fn create_db() {
|
2026-08-24 01:22:00 +03:00
|
|
|
let db_file = db_path();
|
2026-06-15 17:06:02 +03:00
|
|
|
if !db_file.exists() {
|
2026-08-24 23:10:23 +03:00
|
|
|
std::fs::File::create(&db_file).expect("Database file creation failed");
|
2026-08-24 23:42:58 +03:00
|
|
|
let connection = Connection::open(&db_file).expect("Connection open failed");
|
2026-08-25 12:04:07 +03:00
|
|
|
connection
|
|
|
|
|
.execute("PRAGMA foreign_keys = ON;", [])
|
|
|
|
|
.expect("Failed to set PRAGMA");
|
2026-06-15 17:06:02 +03:00
|
|
|
|
|
|
|
|
create_tables(&connection);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn create_tables(conn: &Connection) {
|
2026-08-24 23:42:58 +03:00
|
|
|
make_card_set(conn).expect("Failed to init table");
|
|
|
|
|
make_settings(conn).expect("Failed to init table");
|
|
|
|
|
make_word_group(conn).expect("Failed to init table");
|
|
|
|
|
make_words(conn).expect("Failed to init table");
|
|
|
|
|
make_card_stats(conn).expect("Failed to init table");
|
2026-08-28 17:44:14 +03:00
|
|
|
make_history(conn).expect("Failed to init table");
|
2026-06-15 17:06:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn make_settings(conn: &Connection) -> Result<(), rusqlite::Error> {
|
|
|
|
|
let query = include_str!("../../sql/settings.sql");
|
|
|
|
|
conn.execute(query, ())?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn make_card_set(conn: &Connection) -> Result<(), rusqlite::Error> {
|
|
|
|
|
let query = include_str!("../../sql/card_set.sql");
|
|
|
|
|
conn.execute(query, ())?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn make_word_group(conn: &Connection) -> Result<(), rusqlite::Error> {
|
|
|
|
|
let query = include_str!("../../sql/word_group.sql");
|
|
|
|
|
conn.execute(query, ())?;
|
|
|
|
|
conn.execute("insert into word_group (name) values (\"Слова\");", ())?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
fn make_words(conn: &Connection) -> Result<(), rusqlite::Error> {
|
|
|
|
|
let query = include_str!("../../sql/words.sql");
|
|
|
|
|
conn.execute(query, ())?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
fn make_card_stats(conn: &Connection) -> Result<(), rusqlite::Error> {
|
|
|
|
|
let query = include_str!("../../sql/card_stats.sql");
|
|
|
|
|
conn.execute(query, ())?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2026-08-13 09:06:22 +03:00
|
|
|
|
2026-08-28 17:44:14 +03:00
|
|
|
fn make_history(conn: &Connection) -> Result<(), rusqlite::Error> {
|
|
|
|
|
let query = include_str!("../../sql/history.sql");
|
|
|
|
|
conn.execute(query, ())?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-13 09:06:22 +03:00
|
|
|
pub fn default_connection() -> Connection {
|
2026-08-24 01:22:00 +03:00
|
|
|
let db_file = db_path();
|
2026-08-24 23:42:58 +03:00
|
|
|
let connection = Connection::open(db_file).expect("Failed to open database");
|
2026-08-25 12:04:07 +03:00
|
|
|
connection
|
|
|
|
|
.execute("PRAGMA foreign_keys = ON;", [])
|
|
|
|
|
.expect("Failed to set PRAGMA");
|
2026-08-13 09:06:22 +03:00
|
|
|
connection
|
2026-08-15 21:32:24 +03:00
|
|
|
}
|