Commit d446d876 authored by Chunchi Che's avatar Chunchi Che

add CardsDB

parent cbc4e08d
Pipeline #18097 failed with stages
in 3 minutes and 50 seconds
use std::ops::{Deref, DerefMut};
use diesel::{Connection, SqliteConnection};
pub enum DbConn {
......@@ -11,6 +13,24 @@ impl DbConn {
}
}
impl Deref for DbConn {
type Target = SqliteConnection;
fn deref(&self) -> &Self::Target {
match self {
DbConn::SqliteConn(conn) => conn,
}
}
}
impl DerefMut for DbConn {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
DbConn::SqliteConn(conn) => conn,
}
}
}
#[cfg(test)]
mod tests {
use super::DbConn;
......@@ -18,7 +38,7 @@ mod tests {
#[test]
fn test_establish_sqlite() {
let workspace = env!("CARGO_MANIFEST_DIR");
let db_url = format!("{}/ygopro-database/locales/zh-CN/cards.db", workspace);
let db_url = format!("{}/ygopro-database/locales/zh-CN/cards.cdb", workspace);
let _conn = DbConn::establish_sqlite(&db_url).unwrap();
}
}
mod model;
mod schema;
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
use std::collections::HashMap;
use crate::infra::DbConn;
/// `cards.cdb`业务层模块
pub struct CardsDB {
conn: DbConn,
}
impl CardsDB {
/// 创建`CardsDB`实例
///
/// TODO: 这里更好得做法应该是由基建层维护db连接池,
/// 业务方通过取到db连接来做业务逻辑
pub fn new() -> anyhow::Result<Self> {
let workspace = env!("CARGO_MANIFEST_DIR");
let db_url = format!("{}/ygopro-database/locales/zh-CN/cards.cdb", workspace);
let conn = DbConn::establish_sqlite(&db_url)?;
Ok(Self { conn })
}
/// 通过`id`获取`Card`元数据
pub fn select_card_datas(
&mut self,
ids: &[i64],
) -> anyhow::Result<HashMap<i64, model::CardDatas>> {
use schema::datas;
let conn = &mut *self.conn;
let records: Vec<model::CardDatas> =
datas::table.filter(datas::id.eq_any(ids)).load(conn)?;
let records = records
.into_iter()
.map(|record| (record.id, record))
.collect();
Ok(records)
}
/// 通过`id`获取`Card`文本数据
pub fn select_card_texts(
&mut self,
ids: &[i64],
) -> anyhow::Result<HashMap<i64, model::CardTexts>> {
use schema::texts;
let conn = &mut *self.conn;
let records: Vec<model::CardTexts> =
texts::table.filter(texts::id.eq_any(ids)).load(conn)?;
let records = records
.into_iter()
.map(|record| (record.id, record))
.collect();
Ok(records)
}
}
#[cfg(test)]
mod tests {
use super::{
model::{CardDatas, CardTexts},
CardsDB,
};
#[test]
fn test_select_card_datas() {
let mut db = CardsDB::new().unwrap();
let id = 2333365;
let data = db.select_card_datas(&[id]).unwrap().remove(&id).unwrap();
assert_eq!(
data,
CardDatas {
id: 2333365,
ot: 3,
alias: 0,
setcode: 66,
type_: 33,
atk: 2000,
def: 2000,
level: 4,
race: 1,
attribute: 16,
category: 65536
}
)
}
#[test]
fn test_select_card_texts() {
let mut db = CardsDB::new().unwrap();
let id = 2333365;
let text = db.select_card_texts(&[id]).unwrap().remove(&id).unwrap();
assert_eq!(
text,
CardTexts {
id: 2333365,
name: "极星将 提尔".into(),
desc: Some(
"场上没有这张卡以外的名字带有「极星」的怪兽表侧表示存在的场合,这张卡破坏。\
只要这张卡在场上表侧表示存在,对方不能选择「极星将 \
提尔」以外的名字带有「极星」的怪兽作为攻击对象。"
.into()
),
str1: Some("".into()),
str2: Some("".into()),
str3: Some("".into()),
str4: Some("".into()),
str5: Some("".into()),
str6: Some("".into()),
str7: Some("".into()),
str8: Some("".into()),
str9: Some("".into()),
str10: Some("".into()),
str11: Some("".into()),
str12: Some("".into()),
str13: Some("".into()),
str14: Some("".into()),
str15: Some("".into()),
str16: Some("".into())
}
)
}
}
use diesel_derives::Queryable;
#[derive(Queryable, Clone, Debug)]
#[derive(Queryable, Clone, Debug, Default, PartialEq, Eq)]
#[diesel(table_name = datas)]
pub struct CradDatas {
pub struct CardDatas {
pub id: i64,
pub ot: i32,
pub alias: i32,
......@@ -17,11 +17,12 @@ pub struct CradDatas {
}
// TODO: 这里字段应该命名得更清晰一点
#[derive(Queryable, Clone, Debug)]
#[derive(Queryable, Clone, Debug, Default, PartialEq, Eq)]
#[diesel(table_name = texts)]
pub struct CradTexts {
pub struct CardTexts {
pub id: i64,
pub name: String,
pub desc: Option<String>,
pub str1: Option<String>,
pub str2: Option<String>,
pub str3: Option<String>,
......
......@@ -4,6 +4,7 @@ diesel::table! {
ot -> Integer,
alias -> Integer,
setcode -> Integer,
#[sql_name = "type"]
type_ -> Integer,
atk -> Integer,
def -> Integer,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment