diff --git a/sr_download/src/db_part.rs b/sr_download/src/db_part.rs index c13e1f9..b3ea63b 100644 --- a/sr_download/src/db_part.rs +++ b/sr_download/src/db_part.rs @@ -1,16 +1,18 @@ use blake3::Hasher; -use colored::Colorize; use sea_orm::{ - ActiveModelTrait, ColumnTrait, ConnectOptions, ConnectionTrait, Database, DatabaseConnection, - DbErr, EntityTrait, IntoActiveModel, ModelTrait, QueryFilter, QueryOrder, QuerySelect, - Statement, TransactionTrait, + ActiveModelTrait, ColumnTrait, ConnectionTrait, DatabaseConnection, EntityTrait, + IntoActiveModel, ModelTrait, QueryFilter, QuerySelect, Statement, TransactionTrait, }; use tracing::{event, Level}; -use crate::config::ConfigFile; use crate::model; use crate::model::sea_orm_active_enums::SaveType; -use migration::{Migrator, MigratorTrait, SaveId, FULL_DATA_VIEW, TEXT_DATA_MAX_LEN}; +use migration::{SaveId, FULL_DATA_VIEW, TEXT_DATA_MAX_LEN}; + +pub mod search; +pub mod utils; + +pub use utils::{connect, connect_server, migrate}; #[allow(unused)] #[derive(Debug, Clone)] @@ -79,96 +81,6 @@ impl DbData { } } -pub async fn connect(conf: &ConfigFile) -> anyhow::Result { - let mut opt = ConnectOptions::new(conf.db.url.clone()); - opt.max_connections(conf.db.max_connections) - .set_schema_search_path(conf.db.schema.clone()) - .sqlx_logging(conf.db.sqlx_logging); - event!(Level::INFO, "正在连接数据库"); - let db: DatabaseConnection = Database::connect(opt).await?; - db.ping().await?; - event!(Level::INFO, "{}", "已经连接数据库".blue()); - Ok(db) -} - -pub async fn connect_server(conf: &ConfigFile) -> anyhow::Result { - let mut opt = ConnectOptions::new(conf.db.url.clone()); - opt.max_connections(conf.serve.db_max_connect) - .set_schema_search_path(conf.db.schema.clone()) - .sqlx_logging(conf.db.sqlx_logging); - event!(Level::INFO, "服务器正在连接数据库"); - let db: DatabaseConnection = Database::connect(opt).await?; - db.ping().await?; - event!(Level::INFO, "{}", "服务器已经连接数据库".blue()); - Ok(db) -} - -pub async fn migrate(db: &DatabaseConnection) -> anyhow::Result<()> { - event!(Level::INFO, "Starting migration"); - Migrator::up(db, None).await?; - event!(Level::INFO, "Migration finished"); - Ok(()) -} - -/// 找到最大的数据的 id -pub async fn find_max_id(db: &DatabaseConnection) -> SaveId { - // SELECT save_id from main_data ORDER BY save_id DESC LIMIT 1 - // 我丢你老母, 有这时间写这个, 我都写完 sql 语句了 - let data: Result, DbErr> = model::main_data::Entity::find() - .order_by_desc(model::main_data::Column::SaveId) - .filter(model::main_data::Column::Len.gt(0)) - .filter(model::main_data::Column::SaveType.ne(SaveType::None)) - .select_only() - .column(model::main_data::Column::SaveId) - .limit(1) - .into_tuple() - .one(db) - .await; - match data { - Ok(model) => match model { - Some(model) => model as SaveId, - None => 0, - }, - Err(e) => { - event!(Level::WARN, "Error when find_max_id: {:?}", e); - 0 - } - } -} - -/// 找到最大的存档 -pub async fn find_max_save(db: &DatabaseConnection) -> Option { - // SELECT * from main_data ORDER BY save_id DESC WHERE save_type = save LIMIT 1 - let data = model::main_data::Entity::find() - .order_by_desc(model::main_data::Column::SaveId) - .filter(model::main_data::Column::SaveType.eq(SaveType::Save)) - .one(db) - .await; - match data { - Ok(model) => model.map(|model| model.into()), - Err(e) => { - event!(Level::WARN, "Error when find_max_save: {:?}", e); - None - } - } -} - -/// 找到最大的飞船 -pub async fn find_max_ship(db: &DatabaseConnection) -> Option { - // SELECT * from main_data ORDER BY save_id DESC WHERE save_type = ship LIMIT 1 - let data = model::main_data::Entity::find() - .order_by_desc(model::main_data::Column::SaveId) - .filter(model::main_data::Column::SaveType.eq(SaveType::Ship)) - .one(db) - .await; - match data { - Ok(model) => model.map(|model| model.into()), - Err(e) => { - event!(Level::WARN, "Error when find_max_ship: {:?}", e); - None - } - } -} /// 直接从数据库中查询数据, 这里数据库已经准备好了根据长度区分过的数据 /// 可以从 full view 里直接选数据 @@ -303,6 +215,7 @@ where blake_hash: hash, len: data_len as i64, short_data: None, + xml_tested: None, }; let long_data = model::long_data::Model { save_id: save_id as i32, @@ -321,6 +234,7 @@ where blake_hash: hash, len: data_len as i64, short_data: Some(data), + xml_tested: None, }; new_data.into_active_model().insert(db).await?; } diff --git a/sr_download/src/db_part/search.rs b/sr_download/src/db_part/search.rs new file mode 100644 index 0000000..7e3252d --- /dev/null +++ b/sr_download/src/db_part/search.rs @@ -0,0 +1,68 @@ +use sea_orm::{ColumnTrait, DatabaseConnection, DbErr, EntityTrait, QueryFilter, QueryOrder, QuerySelect}; +use tracing::{event, Level}; + +use crate::model; +use crate::model::sea_orm_active_enums::SaveType; +use migration::SaveId; + +use super::DbData; + +/// 找到最大的数据的 id +pub async fn max_id(db: &DatabaseConnection) -> SaveId { + // SELECT save_id from main_data ORDER BY save_id DESC LIMIT 1 + // 我丢你老母, 有这时间写这个, 我都写完 sql 语句了 + let data: Result, DbErr> = model::main_data::Entity::find() + .order_by_desc(model::main_data::Column::SaveId) + .filter(model::main_data::Column::Len.gt(0)) + .filter(model::main_data::Column::SaveType.ne(SaveType::None)) + .select_only() + .column(model::main_data::Column::SaveId) + .limit(1) + .into_tuple() + .one(db) + .await; + match data { + Ok(model) => match model { + Some(model) => model as SaveId, + None => 0, + }, + Err(e) => { + event!(Level::WARN, "Error when find_max_id: {:?}", e); + 0 + } + } +} + +/// 找到最大的存档 +pub async fn max_save(db: &DatabaseConnection) -> Option { + // SELECT * from main_data ORDER BY save_id DESC WHERE save_type = save LIMIT 1 + let data = model::main_data::Entity::find() + .order_by_desc(model::main_data::Column::SaveId) + .filter(model::main_data::Column::SaveType.eq(SaveType::Save)) + .one(db) + .await; + match data { + Ok(model) => model.map(|model| model.into()), + Err(e) => { + event!(Level::WARN, "Error when find_max_save: {:?}", e); + None + } + } +} + +/// 找到最大的飞船 +pub async fn max_ship(db: &DatabaseConnection) -> Option { + // SELECT * from main_data ORDER BY save_id DESC WHERE save_type = ship LIMIT 1 + let data = model::main_data::Entity::find() + .order_by_desc(model::main_data::Column::SaveId) + .filter(model::main_data::Column::SaveType.eq(SaveType::Ship)) + .one(db) + .await; + match data { + Ok(model) => model.map(|model| model.into()), + Err(e) => { + event!(Level::WARN, "Error when find_max_ship: {:?}", e); + None + } + } +} diff --git a/sr_download/src/db_part/utils.rs b/sr_download/src/db_part/utils.rs new file mode 100644 index 0000000..f4cac7b --- /dev/null +++ b/sr_download/src/db_part/utils.rs @@ -0,0 +1,37 @@ +use colored::Colorize; +use sea_orm::{ConnectOptions, Database, DatabaseConnection}; +use tracing::{event, Level}; + +use crate::config::ConfigFile; +use migration::{Migrator, MigratorTrait}; + +pub async fn connect(conf: &ConfigFile) -> anyhow::Result { + let mut opt = ConnectOptions::new(conf.db.url.clone()); + opt.max_connections(conf.db.max_connections) + .set_schema_search_path(conf.db.schema.clone()) + .sqlx_logging(conf.db.sqlx_logging); + event!(Level::INFO, "正在连接数据库"); + let db: DatabaseConnection = Database::connect(opt).await?; + db.ping().await?; + event!(Level::INFO, "{}", "已经连接数据库".blue()); + Ok(db) +} + +pub async fn connect_server(conf: &ConfigFile) -> anyhow::Result { + let mut opt = ConnectOptions::new(conf.db.url.clone()); + opt.max_connections(conf.serve.db_max_connect) + .set_schema_search_path(conf.db.schema.clone()) + .sqlx_logging(conf.db.sqlx_logging); + event!(Level::INFO, "服务器正在连接数据库"); + let db: DatabaseConnection = Database::connect(opt).await?; + db.ping().await?; + event!(Level::INFO, "{}", "服务器已经连接数据库".blue()); + Ok(db) +} + +pub async fn migrate(db: &DatabaseConnection) -> anyhow::Result<()> { + event!(Level::INFO, "Starting migration"); + Migrator::up(db, None).await?; + event!(Level::INFO, "Migration finished"); + Ok(()) +} diff --git a/sr_download/src/main.rs b/sr_download/src/main.rs index 470f03e..ce3efec 100644 --- a/sr_download/src/main.rs +++ b/sr_download/src/main.rs @@ -85,7 +85,7 @@ async fn serve_mode(mut stop_receiver: Receiver<()>) -> anyhow::Result<()> { let db_connect = db_part::connect(&conf).await?; db_part::migrate(&db_connect).await?; - let mut db_max_id = db_part::find_max_id(&db_connect).await; + let mut db_max_id = db_part::search::max_id(&db_connect).await; let mut web_waiter = None; if conf.serve.enable { diff --git a/sr_download/src/model/readme.md b/sr_download/src/model/readme.md new file mode 100644 index 0000000..2942081 --- /dev/null +++ b/sr_download/src/model/readme.md @@ -0,0 +1,9 @@ +# 数据格式部分 + +生成的, 别动 + +生成新的: + +```powershell +sea-orm-cli generate entity --database-url +``` diff --git a/sr_download/src/serve.rs b/sr_download/src/serve.rs index 813e71a..56d8ea2 100644 --- a/sr_download/src/serve.rs +++ b/sr_download/src/serve.rs @@ -195,10 +195,10 @@ async fn jump_to_dashboard_from_root() -> impl IntoResponse { const INFO_PAGE: &str = include_str!("info.html"); async fn dashboard_page(State(db): State) -> Html { - let max_id = db_part::find_max_id(&db).await; + let max_id = db_part::search::max_id(&db).await; let max_id_data = db_part::get_raw_data(max_id, &db).await.unwrap(); - let max_ship = db_part::find_max_ship(&db).await; - let max_save = db_part::find_max_save(&db).await; + let max_ship = db_part::search::max_ship(&db).await; + let max_save = db_part::search::max_save(&db).await; let mut page_content = INFO_PAGE .replace("|MAX_ID|", &max_id_data.save_id.to_string()) diff --git a/sr_download/src/serve/traits.rs b/sr_download/src/serve/traits.rs index 7947e98..aa00d9c 100644 --- a/sr_download/src/serve/traits.rs +++ b/sr_download/src/serve/traits.rs @@ -11,7 +11,7 @@ pub trait FromDb { impl FromDb for LastData { async fn from_db(db: &DatabaseConnection) -> Option { - let id = db_part::find_max_id(db).await; + let id = db_part::search::max_id(db).await; let data = db_part::get_raw_data(id, db).await?; Some(Self { save_id: data.save_id, @@ -24,7 +24,7 @@ impl FromDb for LastData { impl FromDb for LastSave { async fn from_db(db: &DatabaseConnection) -> Option { - let data = db_part::find_max_save(db).await?; + let data = db_part::search::max_save(db).await?; Some(Self { save_id: data.save_id, len: data.len, @@ -35,7 +35,7 @@ impl FromDb for LastSave { impl FromDb for LastShip { async fn from_db(db: &DatabaseConnection) -> Option { - let data = db_part::find_max_ship(db).await?; + let data = db_part::search::max_ship(db).await?; Some(Self { save_id: data.save_id, len: data.len,