feat: 完善 db 的内容

This commit is contained in:
2025-07-30 23:46:33 +08:00
parent 9ba719a9ea
commit 1f378cd15f
12 changed files with 89 additions and 41 deletions

View File

@@ -0,0 +1 @@
pub mod sqlite;

View File

@@ -0,0 +1,31 @@
mod sql_line;
use log::warn;
use r2d2::{Pool};
use r2d2_sqlite::SqliteConnectionManager;
use crate::utils::sql::sqlite::sql_line::{generate_sql_line};
#[derive(Debug)]
pub struct SqliteDB {
pub connection_pool: Pool<SqliteConnectionManager>,
}
impl SqliteDB {
pub fn new(address: &str) -> Option<Self> {
let manager = SqliteConnectionManager::file(address);
match Pool::builder().build(manager) {
Ok(pool) => Some(SqliteDB {
connection_pool: pool
}),
Err(e) => {
warn!("Failed to create connection pool: {}", e);
None
}
}
}
pub fn create_table(&self) {
let conn = self.connection_pool.get().unwrap();
let line = generate_sql_line();
conn.execute(line.get("create_table").unwrap(), []).expect(&format!("Failed to create db table: {:?}", self));
}
}

View File

@@ -0,0 +1,15 @@
use std::collections::HashMap;
pub fn generate_sql_line() -> HashMap<&'static str, &'static str> {
let mut line = HashMap::new();
line.insert("create_table", "
CREATE TABLE IF NOT EXISTS gotify (
id INTEGER PRIMARY KEY,
appid INTEGER,
message TEXT,
title TEXT,
priority INTEGER,
date TEXT
");
line
}