Skip to content

Commit b4710af

Browse files
committed
update
1 parent 6e4a00e commit b4710af

File tree

5 files changed

+132
-149
lines changed

5 files changed

+132
-149
lines changed

β€Žcrates/rs-tauri-vue/Taskfile.ymlβ€Ž

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ tasks:
6767
test:
6868
cmds:
6969
- echo "test"
70-
- RUST_BACKTRACE=1 RUST_LOG=debug cargo test -- --nocapture
70+
# - task: clean:test
71+
- RUST_BACKTRACE=1 RUST_LOG=debug macro_backtrace=1 cargo test -- --nocapture
7172
dir: "src-tauri"
7273

7374
update:
@@ -81,6 +82,12 @@ tasks:
8182
- cargo clean
8283
dir: "src-tauri"
8384

85+
clean:test:
86+
cmds:
87+
- rm *.db
88+
dir: "src-tauri/tmp"
89+
ignore_error: true
90+
8491

8592
release:
8693
cmds:
Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,77 @@
1-
use crate::storage::AppStorage;
2-
31
pub struct AppService {
4-
pub storage: AppStorage,
2+
// pub storage: AppStorage,
53
}
64

75
impl AppService {
86
pub async fn new() -> Self {
9-
let storage = AppStorage::new().await;
10-
Self { storage }
7+
// let storage = AppStorage::new().await;
8+
// Self { storage }
9+
Self {}
1110
}
11+
12+
// pub async fn add_todo(&self, todo: &TodoEntity) -> anyhow::Result<i64> {
13+
// self.storage.sql.todo.add_todo(todo).await
14+
// }
15+
//
16+
// pub async fn get_todo(&self, id: i64) -> anyhow::Result<TodoEntity> {
17+
// self.storage.sql.todo.get_todo(id).await
18+
// }
1219
}
1320

1421
#[cfg(test)]
1522
mod test {
16-
use crate::proto::TodoEntity;
17-
1823
use super::*;
1924

20-
#[tokio::test]
21-
async fn test_new_service() {
22-
let service = AppService::new().await;
25+
// async fn setup() {
26+
// // let mut db = SqlConn::new("app.db").await;
27+
// let mut db = SqlConn::default().await;
28+
//
29+
// db.init_migrations().await;
30+
// }
2331

24-
let ret = service
25-
.storage
26-
.sql
27-
.todo
28-
.add_todo(&TodoEntity {
29-
id: 0,
30-
title: "add todo, by service".to_string(),
31-
description: "test desc".to_string(),
32-
done: false,
33-
completed: false,
34-
})
35-
.await
36-
.unwrap();
32+
// #[tokio::test]
33+
// async fn test_new_service() {
34+
// setup().await;
35+
//
36+
// let service = AppService::new().await;
37+
//
38+
// let ret = service
39+
// .storage
40+
// .sql
41+
// .todo
42+
// .add_todo(&TodoEntity {
43+
// id: 0,
44+
// title: "add todo, by service".to_string(),
45+
// description: "test desc".to_string(),
46+
// done: false,
47+
// completed: false,
48+
// })
49+
// .await
50+
// .unwrap();
51+
//
52+
// println!("service add todo: {}", ret);
53+
//
54+
// let todo = service.storage.sql.todo.get_todo(ret).await.unwrap();
55+
//
56+
// println!("service get todo: {:?}", todo);
57+
// }
3758

38-
println!("service add todo: {}", ret);
39-
40-
let todo = service.storage.sql.todo.get_todo(ret).await.unwrap();
41-
42-
println!("service get todo: {:?}", todo);
43-
}
59+
// #[tokio::test]
60+
// async fn test_service_add_todo() {
61+
// let service = AppService::new().await;
62+
// let ret = service
63+
// .add_todo(&TodoEntity {
64+
// id: 0,
65+
// title: "add todo, by service api".to_string(),
66+
// description: "test desc".to_string(),
67+
// done: false,
68+
// completed: false,
69+
// })
70+
// .await
71+
// .unwrap();
72+
// println!("service add todo: {}", ret);
73+
//
74+
// let todo = service.get_todo(ret).await.unwrap();
75+
// println!("service get todo: {:?}", todo);
76+
// }
4477
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
pub use kv::*;
22
pub use sql::*;
33

4-
use crate::storage::db::AppSqlStorage;
5-
64
pub mod kv;
75
pub mod sql;
86

97
pub struct AppStorage {
108
pub kv: AppKvStorage,
11-
pub sql: AppSqlStorage,
9+
// pub sql: AppSqlStorage,
1210
}
1311

1412
impl AppStorage {
1513
pub async fn new() -> Self {
1614
let kv = AppKvStorage::default();
17-
let sql = AppSqlStorage::new().await;
18-
Self { kv, sql }
15+
// let sql = AppSqlStorage::new().await;
16+
Self { kv }
1917
}
2018
}

β€Žcrates/rs-tauri-vue/src-tauri/src/storage/sql/db.rsβ€Ž

Lines changed: 40 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{env, str::FromStr};
1+
use std::str::FromStr;
22

33
use sqlx::{
44
migrate::Migrator,
@@ -21,6 +21,8 @@ impl AppSqlStorage {
2121
pub async fn new() -> Self {
2222
let g = SqlConn::default().await;
2323

24+
// g.init_migrations().await;
25+
2426
let todo = TodoSqlScope::new(g.clone());
2527
Self { g, todo }
2628
}
@@ -39,58 +41,13 @@ impl Clone for SqlConn {
3941

4042
impl SqlConn {
4143
pub async fn default() -> Self {
42-
let mut _root_dir = tauri::api::path::document_dir();
43-
44-
// sqlite:tmp/app.db
45-
let url = env::var("").unwrap_or(util::app_cfg::get_local_sqlite_url(None));
46-
println!("sqlite url: {}", url);
47-
48-
// todo x: δ»…η”¨δΊŽεˆ›ε»Ί db ζ–‡δ»Ά
49-
let _ = SqliteConnectOptions::from_str(&url)
50-
.unwrap()
51-
.create_if_missing(true)
52-
.connect()
53-
.await;
54-
55-
let conn = SqlitePoolOptions::new().min_connections(2).connect(&url).await;
56-
57-
match conn {
58-
Ok(conn) => {
59-
info!("sqlite conn: {:?}", conn);
60-
Self { conn }
61-
},
62-
Err(e) => {
63-
panic!("sqlite conn err: {:?}", e);
64-
},
65-
}
44+
let conn = new_sql_conn("app.db").await;
45+
Self { conn }
6646
}
6747

6848
pub async fn new(db_name: &str) -> Self {
69-
let mut _root_dir = tauri::api::path::document_dir();
70-
71-
// sqlite:tmp/app.db
72-
// let url = env::var("").unwrap_or(util::app_cfg::get_local_sqlite_url(Some(db_name)));
73-
let url = util::app_cfg::get_local_sqlite_url(Some(db_name));
74-
println!("sqlite url: {}", url);
75-
76-
// todo x: δ»…η”¨δΊŽεˆ›ε»Ί db ζ–‡δ»Ά
77-
let _ = SqliteConnectOptions::from_str(&url)
78-
.unwrap()
79-
.create_if_missing(true)
80-
.connect()
81-
.await;
82-
83-
let db = SqlitePoolOptions::new().min_connections(2).connect(&url).await;
84-
85-
match db {
86-
Ok(db) => {
87-
info!("connect to sqlite db success");
88-
Self { conn: db }
89-
},
90-
Err(e) => {
91-
panic!("db create error: {:?}", e);
92-
},
93-
}
49+
let conn = new_sql_conn(db_name).await;
50+
Self { conn }
9451
}
9552

9653
pub async fn init_migrations(&mut self) {
@@ -108,53 +65,39 @@ impl SqlConn {
10865
},
10966
}
11067
}
68+
}
11169

112-
// pub async fn add_todo2(&self, todo: &TodoEntity) -> anyhow::Result<i64> {
113-
// let mut conn = self.db.acquire().await?;
114-
//
115-
// // let id = sqlx::query!(r#""#).execute(&mut conn).await?.last_insert_rowid();
116-
//
117-
// let id = 2;
118-
// Ok(id)
119-
// }
120-
//
121-
// pub async fn get_todo(&self, id: i64) -> anyhow::Result<TodoEntity> {
122-
// let mut conn = self.db.acquire().await?;
123-
// let mut tx = conn.begin().await?;
124-
// let mut stmt = tx.prepare(r#"SELECT id, title, completed FROM todo WHERE id =
125-
// ?"#).await?; let mut rows = stmt.query(sqlx::params![id]).await?;
126-
// let row = rows.next().await.unwrap()?;
127-
// tx.commit().await?;
128-
//
129-
// Ok(Todo { id: row.get(0), title: row.get(1), completed: row.get(2) })
130-
// }
131-
//
132-
// pub async fn list_todos(&mut self) -> anyhow::Result<Vec<TodoEntity>> {
133-
// let mut conn = self.db.acquire().await?;
134-
// let mut tx = conn.begin().await?;
135-
// let mut stmt = tx.prepare(r#"SELECT id, title, completed FROM todo"#).await?;
136-
// let mut rows = stmt.query(sqlx::params![]).await?;
137-
//
138-
// let mut todos = Vec::new();
139-
// for row in rows {
140-
// todos.push(Todo { id: row.get(0), title: row.get(1), completed: row.get(2) });
141-
// }
142-
// tx.commit().await?;
143-
//
144-
// Ok(todos)
145-
// }
146-
//
147-
// pub async fn update_todo(&self, todo: &TodoEntity) -> anyhow::Result<bool> {
148-
// let mut conn = self.db.acquire().await?;
149-
// let mut tx = conn.begin().await?;
150-
// let mut stmt =
151-
// tx.prepare(r#"UPDATE todo SET title = ?, completed = ? WHERE id = ?"#).await?;
152-
// let rows_affected =
153-
// stmt.execute(sqlx::params![todo.title, todo.completed, todo.id]).await?;
154-
// tx.commit().await?;
155-
//
156-
// Ok(rows_affected > 0)
157-
// }
70+
async fn new_sql_conn(db_name: &str) -> SqlitePool {
71+
let mut _root_dir = tauri::api::path::document_dir();
72+
73+
// sqlite:tmp/app.db
74+
// let url = env::var("").unwrap_or(util::app_cfg::get_local_sqlite_url(Some(db_name)));
75+
let url = util::app_cfg::get_local_sqlite_url(Some(db_name));
76+
println!("sqlite url: {}", url);
77+
78+
// todo x: δ»…η”¨δΊŽεˆ›ε»Ί db ζ–‡δ»Ά
79+
let _ = SqliteConnectOptions::from_str(&url)
80+
.unwrap()
81+
.create_if_missing(true)
82+
.connect()
83+
.await
84+
.expect("connect to sqlite failed");
85+
86+
// connect pool
87+
let conn = SqlitePoolOptions::new().min_connections(2).connect(&url).await;
88+
89+
match conn {
90+
Ok(conn) => {
91+
// todo x: εˆε§‹εŒ–θ‘¨η»“ζž„
92+
sqlx::migrate!("./migrations").run(&conn).await.expect("migrate failed");
93+
94+
info!("connect to sqlite db success");
95+
conn
96+
},
97+
Err(e) => {
98+
panic!("connect to sqlite db failed: {}", e);
99+
},
100+
}
158101
}
159102

160103
#[cfg(test)]
@@ -166,11 +109,7 @@ mod test {
166109
#[tokio::test]
167110
async fn test_sqlite() {
168111
let mut db = SqlConn::default().await;
169-
db.init_migrations().await;
170-
171-
// let mut conn = db.db.acquire().await.unwrap();
172-
// let mut tx = conn.begin().await.unwrap();
173-
// // insert todo
112+
// db.init_migrations().await;
174113

175114
println!("test sqlite success");
176115
}

β€Žcrates/rs-tauri-vue/src-tauri/src/storage/sql/todo.rsβ€Ž

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ impl TodoSqlScope {
1717
// Insert the task, then obtain the ID of this row
1818
let id = sqlx::query!(
1919
r#"
20-
INSERT INTO todos ( description, title )
21-
VALUES ( ?1, ?2 )
22-
"#,
20+
INSERT INTO todos ( description, title )
21+
VALUES ( ?1, ?2 )
22+
"#,
2323
todo.description,
2424
todo.title
2525
)
@@ -35,10 +35,10 @@ impl TodoSqlScope {
3535

3636
let rows = sqlx::query!(
3737
r#"
38-
SELECT id, description, title, done, completed
39-
FROM todos
40-
ORDER BY id
41-
"#
38+
SELECT id, description, title, done, completed
39+
FROM todos
40+
ORDER BY id
41+
"#
4242
)
4343
.fetch_all(&mut conn)
4444
.await?;
@@ -61,10 +61,13 @@ ORDER BY id
6161
pub async fn get_todo(&self, id: i64) -> anyhow::Result<TodoEntity> {
6262
let mut conn = self.g.conn.acquire().await?;
6363

64-
let row =
65-
sqlx::query!(r#"SELECT id, title, description, completed FROM todos WHERE id = ?"#, id)
66-
.fetch_one(&mut conn)
67-
.await?;
64+
let row = sqlx::query!(
65+
r#"SELECT id, title, description, completed FROM todos WHERE id =
66+
?"#,
67+
id
68+
)
69+
.fetch_one(&mut conn)
70+
.await?;
6871

6972
println!("get_todo row: {:?}", row);
7073

@@ -85,8 +88,11 @@ mod test {
8588
use super::*;
8689

8790
async fn setup() -> TodoSqlScope {
88-
let mut db = SqlConn::new("test.db").await;
89-
db.init_migrations().await;
91+
let mut db = SqlConn::new("app.db").await;
92+
// let mut db = SqlConn::default().await;
93+
// db.init_migrations().await;
94+
95+
println!("πŸ’–πŸ’–πŸ’–πŸ’–πŸ’– setup");
9096

9197
TodoSqlScope::new(db)
9298
}

0 commit comments

Comments
Β (0)