@@ -26,12 +26,12 @@ struct Todo {
2626 author_id : i32 ,
2727}
2828
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 ();
3030
3131// Pass a row with the correct columns.
3232let todo = Todo :: from_row (& row );
3333
34- let row = client . query_one (" SELECT foo FROM bar" ). unwrap ();
34+ let row = client . query_one (" SELECT foo FROM bar" , & [] ). unwrap ();
3535
3636// Use `try_from_row` if the operation could fail.
3737let todo = Todo :: try_from_row (& row );
@@ -59,7 +59,29 @@ struct User {
5959 username : String
6060}
6161
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 ();
6363let todo = Todo :: from_row (& row );
6464```
6565
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