Skip to content

Commit a464c57

Browse files
committed
update
1 parent 5925ae3 commit a464c57

File tree

2 files changed

+55
-24
lines changed

2 files changed

+55
-24
lines changed

crates/rs-tauri-vue/src-tauri/src/main.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use window_shadows::set_shadow;
1313

1414
use command::calc::backend_add;
1515

16-
use crate::ctx::AppContext;
16+
use crate::{ctx::AppContext, util::app_cfg::get_local_sqlite_url};
1717

1818
mod command;
1919
mod config;
@@ -94,6 +94,14 @@ fn main() {
9494
})
9595
.invoke_handler(tauri::generate_handler![backend_add, command::add_todo,])
9696
.setup(|app| {
97+
// todo x: init app
98+
99+
tauri::async_runtime::spawn(async move {
100+
// A loop that takes output from the async process and sends it
101+
// to the webview via a Tauri Event
102+
init_app().await;
103+
});
104+
97105
#[cfg(debug_assertions)]
98106
{
99107
let main_window = app.get_window("main").unwrap();
@@ -115,7 +123,7 @@ fn main() {
115123
Ok(())
116124
})
117125
.plugin(TauriSql::default().add_migrations(
118-
"sqlite:app.db",
126+
get_local_sqlite_url(Some("app.db")).as_str(),
119127
vec![Migration {
120128
version: 1,
121129
description: "create todo",
@@ -127,9 +135,11 @@ fn main() {
127135
.expect("error while running tauri application");
128136
}
129137

130-
fn init_app() {
138+
async fn init_app() {
131139
// init sqlite
132-
AppContext::global();
140+
AppContext::global().await;
141+
142+
info!("init app context");
133143
}
134144

135145
pub trait WindowExt {

crates/rs-tauri-vue/src-tauri/src/storage/sql/todo.rs

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use sqlx::{Acquire, Executor, Statement};
1+
use sqlx::{Acquire, Executor, Row, Statement};
22
use tracing::info;
33

44
use crate::{proto::TodoEntity, storage::db::SqlConn};
@@ -13,17 +13,17 @@ impl TodoSqlScope {
1313
}
1414

1515
pub async fn add_todo(&self, todo: &TodoEntity) -> anyhow::Result<i64> {
16-
let mut conn = (self.g).conn.acquire().await?;
16+
let mut conn = self.g.conn.acquire().await?;
1717

1818
// Insert the task, then obtain the ID of this row
19-
let id = sqlx::query!(
19+
let id = sqlx::query(
2020
r#"
2121
INSERT INTO todos ( description, title )
2222
VALUES ( ?1, ?2 )
2323
"#,
24-
todo.description,
25-
todo.title
2624
)
25+
.bind(&todo.description)
26+
.bind(&todo.title)
2727
.execute(&mut conn)
2828
.await?
2929
.last_insert_rowid();
@@ -33,28 +33,49 @@ impl TodoSqlScope {
3333
Ok(id)
3434
}
3535

36+
// pub async fn add_todo2(&self, todo: &TodoEntity) -> anyhow::Result<i64> {
37+
// let mut conn = self.g.conn.acquire().await?;
38+
//
39+
// // Insert the task, then obtain the ID of this row
40+
// let id = sqlx::query!(
41+
// r#"
42+
// INSERT INTO todos ( description, title )
43+
// VALUES ( ?1, ?2 )
44+
// "#,
45+
// todo.description,
46+
// todo.title
47+
// )
48+
// .execute(&mut conn)
49+
// .await?
50+
// .last_insert_rowid();
51+
//
52+
// info!("add todo: {:?}", todo);
53+
//
54+
// Ok(id)
55+
// }
56+
3657
pub async fn list_todo(&self) -> anyhow::Result<Vec<TodoEntity>> {
3758
let mut conn = self.g.conn.acquire().await?;
3859

39-
let rows = sqlx::query!(
60+
let rows = sqlx::query(
4061
r#"
4162
SELECT id, description, title, done, completed
4263
FROM todos
4364
ORDER BY id
44-
"#
65+
"#,
4566
)
4667
.fetch_all(&mut conn)
4768
.await?;
4869

4970
let mut todos = Vec::new();
5071
for row in rows {
51-
println!("row: {:?}", row);
72+
// println!("row: {:?}", row);
5273
todos.push(TodoEntity {
53-
id: row.id,
54-
description: row.description,
55-
title: row.title,
56-
done: row.done,
57-
completed: row.completed,
74+
id: row.get(0),
75+
description: row.get(1),
76+
title: row.get(2),
77+
done: row.get(3),
78+
completed: row.get(4),
5879
});
5980
}
6081

@@ -64,22 +85,22 @@ impl TodoSqlScope {
6485
pub async fn get_todo(&self, id: i64) -> anyhow::Result<TodoEntity> {
6586
let mut conn = self.g.conn.acquire().await?;
6687

67-
let row = sqlx::query!(
88+
let row = sqlx::query(
6889
r#"SELECT id, title, description, completed FROM todos WHERE id =
6990
?"#,
70-
id
7191
)
92+
.bind(id)
7293
.fetch_one(&mut conn)
7394
.await?;
7495

75-
println!("get_todo row: {:?}", row);
96+
// println!("get_todo row: {:?}", row);
7697

7798
Ok(TodoEntity {
78-
id: row.id,
79-
description: row.description,
80-
title: row.title,
99+
id: row.get("id"),
100+
description: row.get("description"),
101+
title: row.get("title"),
81102
done: false,
82-
completed: row.completed,
103+
completed: row.get("completed"),
83104
})
84105
}
85106
}

0 commit comments

Comments
 (0)