Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
N
Neos-rs
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Neos-rs
Commits
d446d876
You need to sign in or sign up before continuing.
Commit
d446d876
authored
Nov 26, 2022
by
Chunchi Che
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add CardsDB
parent
cbc4e08d
Pipeline
#18097
failed with stages
in 3 minutes and 50 seconds
Changes
4
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
160 additions
and
5 deletions
+160
-5
src/infra/mod.rs
src/infra/mod.rs
+21
-1
src/service/cards/db/mod.rs
src/service/cards/db/mod.rs
+133
-0
src/service/cards/db/model.rs
src/service/cards/db/model.rs
+5
-4
src/service/cards/db/schema.rs
src/service/cards/db/schema.rs
+1
-0
No files found.
src/infra/mod.rs
View file @
d446d876
use
std
::
ops
::{
Deref
,
DerefMut
};
use
diesel
::{
Connection
,
SqliteConnection
};
use
diesel
::{
Connection
,
SqliteConnection
};
pub
enum
DbConn
{
pub
enum
DbConn
{
...
@@ -11,6 +13,24 @@ impl 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)]
#[cfg(test)]
mod
tests
{
mod
tests
{
use
super
::
DbConn
;
use
super
::
DbConn
;
...
@@ -18,7 +38,7 @@ mod tests {
...
@@ -18,7 +38,7 @@ mod tests {
#[test]
#[test]
fn
test_establish_sqlite
()
{
fn
test_establish_sqlite
()
{
let
workspace
=
env!
(
"CARGO_MANIFEST_DIR"
);
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.
c
db"
,
workspace
);
let
_
conn
=
DbConn
::
establish_sqlite
(
&
db_url
)
.unwrap
();
let
_
conn
=
DbConn
::
establish_sqlite
(
&
db_url
)
.unwrap
();
}
}
}
}
src/service/cards/db/mod.rs
View file @
d446d876
mod
model
;
mod
model
;
mod
schema
;
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
())
}
)
}
}
src/service/cards/db/model.rs
View file @
d446d876
use
diesel_derives
::
Queryable
;
use
diesel_derives
::
Queryable
;
#[derive(Queryable,
Clone,
Debug)]
#[derive(Queryable,
Clone,
Debug
,
Default,
PartialEq,
Eq
)]
#[diesel(table_name
=
datas)]
#[diesel(table_name
=
datas)]
pub
struct
C
ra
dDatas
{
pub
struct
C
ar
dDatas
{
pub
id
:
i64
,
pub
id
:
i64
,
pub
ot
:
i32
,
pub
ot
:
i32
,
pub
alias
:
i32
,
pub
alias
:
i32
,
...
@@ -17,11 +17,12 @@ pub struct CradDatas {
...
@@ -17,11 +17,12 @@ pub struct CradDatas {
}
}
// TODO: 这里字段应该命名得更清晰一点
// TODO: 这里字段应该命名得更清晰一点
#[derive(Queryable,
Clone,
Debug)]
#[derive(Queryable,
Clone,
Debug
,
Default,
PartialEq,
Eq
)]
#[diesel(table_name
=
texts)]
#[diesel(table_name
=
texts)]
pub
struct
C
ra
dTexts
{
pub
struct
C
ar
dTexts
{
pub
id
:
i64
,
pub
id
:
i64
,
pub
name
:
String
,
pub
name
:
String
,
pub
desc
:
Option
<
String
>
,
pub
str1
:
Option
<
String
>
,
pub
str1
:
Option
<
String
>
,
pub
str2
:
Option
<
String
>
,
pub
str2
:
Option
<
String
>
,
pub
str3
:
Option
<
String
>
,
pub
str3
:
Option
<
String
>
,
...
...
src/service/cards/db/schema.rs
View file @
d446d876
...
@@ -4,6 +4,7 @@ diesel::table! {
...
@@ -4,6 +4,7 @@ diesel::table! {
ot
->
Integer
,
ot
->
Integer
,
alias
->
Integer
,
alias
->
Integer
,
setcode
->
Integer
,
setcode
->
Integer
,
#[sql_name
=
"type"
]
type_
->
Integer
,
type_
->
Integer
,
atk
->
Integer
,
atk
->
Integer
,
def
->
Integer
,
def
->
Integer
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment