1
- use sqlx:: { Acquire , Executor , Statement } ;
1
+ use sqlx:: { Acquire , Executor , Row , Statement } ;
2
2
use tracing:: info;
3
3
4
4
use crate :: { proto:: TodoEntity , storage:: db:: SqlConn } ;
@@ -13,17 +13,17 @@ impl TodoSqlScope {
13
13
}
14
14
15
15
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 ?;
17
17
18
18
// Insert the task, then obtain the ID of this row
19
- let id = sqlx:: query! (
19
+ let id = sqlx:: query (
20
20
r#"
21
21
INSERT INTO todos ( description, title )
22
22
VALUES ( ?1, ?2 )
23
23
"# ,
24
- todo. description,
25
- todo. title
26
24
)
25
+ . bind ( & todo. description )
26
+ . bind ( & todo. title )
27
27
. execute ( & mut conn)
28
28
. await ?
29
29
. last_insert_rowid ( ) ;
@@ -33,28 +33,49 @@ impl TodoSqlScope {
33
33
Ok ( id)
34
34
}
35
35
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
+
36
57
pub async fn list_todo ( & self ) -> anyhow:: Result < Vec < TodoEntity > > {
37
58
let mut conn = self . g . conn . acquire ( ) . await ?;
38
59
39
- let rows = sqlx:: query! (
60
+ let rows = sqlx:: query (
40
61
r#"
41
62
SELECT id, description, title, done, completed
42
63
FROM todos
43
64
ORDER BY id
44
- "#
65
+ "# ,
45
66
)
46
67
. fetch_all ( & mut conn)
47
68
. await ?;
48
69
49
70
let mut todos = Vec :: new ( ) ;
50
71
for row in rows {
51
- println ! ( "row: {:?}" , row) ;
72
+ // println!("row: {:?}", row);
52
73
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 ) ,
58
79
} ) ;
59
80
}
60
81
@@ -64,22 +85,22 @@ impl TodoSqlScope {
64
85
pub async fn get_todo ( & self , id : i64 ) -> anyhow:: Result < TodoEntity > {
65
86
let mut conn = self . g . conn . acquire ( ) . await ?;
66
87
67
- let row = sqlx:: query! (
88
+ let row = sqlx:: query (
68
89
r#"SELECT id, title, description, completed FROM todos WHERE id =
69
90
?"# ,
70
- id
71
91
)
92
+ . bind ( id)
72
93
. fetch_one ( & mut conn)
73
94
. await ?;
74
95
75
- println ! ( "get_todo row: {:?}" , row) ;
96
+ // println!("get_todo row: {:?}", row);
76
97
77
98
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" ) ,
81
102
done : false ,
82
- completed : row. completed ,
103
+ completed : row. get ( " completed" ) ,
83
104
} )
84
105
}
85
106
}
0 commit comments