Skip to content

Commit 922c13a

Browse files
committed
restructure docs
1 parent 463c0b2 commit 922c13a

File tree

2 files changed

+26
-66
lines changed

2 files changed

+26
-66
lines changed

README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
3232
let 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.
3737
let 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();
6363
let 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+

src/lib.rs

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,5 @@
11
#![deny(missing_docs)]
2-
//! Derive [`FromRow`] to generate a mapping between a struct and postgres rows.
3-
//!
4-
//! This crate works with [postgres](<https://docs.rs/postgres>) by default.
5-
//!
6-
//! ```toml
7-
//! [dependencies]
8-
//! postgres_from_row = "0.4.0"
9-
//! ```
10-
//!
11-
//! If you want to use it with [tokio-postgres](<https://docs.rs/tokio-postgres>), enable it like so:
12-
//!
13-
//! ```toml
14-
//! [dependencies]
15-
//! postgres_from_row = { version = "0.4.0", default_features = false, features = ["tokio-postgres"] }
16-
//! ```
17-
//! # Examples
18-
//! ```rust
19-
//! use postgres_from_row::FromRow;
20-
//!
21-
//! #[derive(FromRow)]
22-
//! struct Todo {
23-
//! todo_id: i32,
24-
//! text: String,
25-
//! author_id: i32,
26-
//! }
27-
//!
28-
//! let row = client.query_one("SELECT todo_id, text, author_id FROM todos").unwrap();
29-
//!
30-
//! // Pass a row with the correct columns.
31-
//! let todo = Todo::from_row(&row);
32-
//!
33-
//! let row = client.query_one("SELECT foo FROM bar").unwrap();
34-
//!
35-
//! // Use `try_from_row` if the operation could fail.
36-
//! let todo = Todo::try_from_row(&row);
37-
//! assert!(todo.is_err());
38-
//! ```
39-
//!
40-
//! Each field need's to implement [`postgres::types::FromSql`], as this will be used to convert a
41-
//! single column to the specified type. If you want to override this behavior and delegate it to a
42-
//! nested structure that also implements [`FromRow`], use `#[from_row(flatten)]`:
43-
//!
44-
//! ```rust
45-
//! use postgres_from_row::FromRow;
46-
//!
47-
//! #[derive(FromRow)]
48-
//! struct Todo {
49-
//! todo_id: i32,
50-
//! text: String,
51-
//! #[from_row(flatten)]
52-
//! author: User
53-
//! }
54-
//!
55-
//! #[derive(FromRow)]
56-
//! struct User {
57-
//! user_id: i32,
58-
//! username: String
59-
//! }
60-
//!
61-
//! 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 todo = Todo::from_row(&row);
63-
//! ```
64-
//!
2+
#![doc = include_str!("../README.md")]
653

664
#[cfg(feature = "postgres")]
675
mod active_postgres {

0 commit comments

Comments
 (0)