feat: 完善 db 的内容
This commit is contained in:
1
packages/gotify-ws/src/utils/sql/mod.rs
Normal file
1
packages/gotify-ws/src/utils/sql/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod sqlite;
|
||||
31
packages/gotify-ws/src/utils/sql/sqlite/mod.rs
Normal file
31
packages/gotify-ws/src/utils/sql/sqlite/mod.rs
Normal 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));
|
||||
}
|
||||
}
|
||||
15
packages/gotify-ws/src/utils/sql/sqlite/sql_line.rs
Normal file
15
packages/gotify-ws/src/utils/sql/sqlite/sql_line.rs
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user