@@ -26,12 +26,12 @@ struct Todo {
26
26
author_id : i32 ,
27
27
}
28
28
29
- let row = client . query_one (" SELECT todo_id, text, author_id FROM todos" ). unwrap ();
29
+ let row = client . query_one (" SELECT todo_id, text, author_id FROM todos" , & [] ). unwrap ();
30
30
31
31
// Pass a row with the correct columns.
32
32
let todo = Todo :: from_row (& row );
33
33
34
- let row = client . query_one (" SELECT foo FROM bar" ). unwrap ();
34
+ let row = client . query_one (" SELECT foo FROM bar" , & [] ). unwrap ();
35
35
36
36
// Use `try_from_row` if the operation could fail.
37
37
let todo = Todo :: try_from_row (& row );
@@ -59,7 +59,29 @@ struct User {
59
59
username : String
60
60
}
61
61
62
- let row = client . query_one (" SELECT todo_id, text, user_id, username FROM todos t, users u WHERE t.author_id = u.user_id" ). unwrap ();
62
+ let row = client . query_one (" SELECT todo_id, text, user_id, username FROM todos t, users u WHERE t.author_id = u.user_id" , & [] ). unwrap ();
63
63
let todo = Todo :: from_row (& row );
64
64
```
65
65
66
+ If a the struct contains a field with a name that differs from the name of the sql column, you can use the ` #[from_row(rename = "..")] ` attribute.
67
+
68
+ When a field in your struct has a type ` T ` that doesn't implement ` FromSql ` or ` FromRow ` but
69
+ it does impement ` T: From<C> ` or ` T: TryFrom<c> ` , and ` C ` does implment ` FromSql ` or ` FromRow `
70
+ you can use ` #[from_row(from = "C")] ` or ` #[from_row(try_from = "C")] ` . This will use type ` C ` to extract it from the row and
71
+ then finally converts it into ` T ` .
72
+
73
+ ``` rust
74
+
75
+ struct Todo {
76
+ // If the postgres column is named `todo_id`.
77
+ #[from_row(rename = " todo_id" )]
78
+ id : i32 ,
79
+ // If the postgres column is `VARCHAR`, it will be decoded to `String`,
80
+ // using `FromSql` and then converted to `Vec<u8>` using `std::convert::From`.
81
+ #[from_row(from = " String" )]
82
+ todo : Vec <u8 >
83
+ }
84
+
85
+ ```
86
+
87
+
0 commit comments