1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
pub use postgres;
/// See the [Query] docs for details.
pub use Query;
/// See the [Statement] docs for details.
pub use Statement;
/// A `Statement` is a SQL statement that, unlike a [Query], does not return rows.
/// Instead, it returns the number of rows that have been affected by the
/// statement.
///
/// # Example
/// `Statement` can be derived like so:
/// ```no_run
/// #[derive(Statement)]
/// #[statement(sql = "DELETE FROM Person WHERE id = @id")]
/// struct DeletePerson {
/// // Define the statement's parameters
/// id: i32
/// }
/// ```
///
/// It then can be used like so:
/// ```no_run
/// # #[derive(Statement)]
/// # #[statement(sql = "DELETE FROM Person WHERE id = @id")]
/// # struct DeletePerson {
/// # id: i32
/// # }
/// fn main() -> Result<(), postgres::Error> {
/// let connection_string = std::env::var("POSTGRES_CONNECTION_STRING")
/// .unwrap_or("host=localhost user=postgres".to_owned());
/// let mut db = postgres::Client::connect(&connection_string, postgres::NoTls)?;
///
/// let delete_count = DeletePerson {
/// id: 123
/// }.execute_statement(&mut db)?;
///
/// println!("Deleted {} people", delete_count);
/// Ok(())
/// }
/// ```
/// For a more thorough example (including bulk queries), see the example
/// project folder in the [GitHub repository](https://github.com/jmoore34/postgres-named-parameters).
///
/// # Notes
/// * In order to use `#[derive(Statement)`, you must also provide the helper
/// attribute `#[statement(sql = "...")`
/// * The `sql` parameter is required and must be a string literal
/// * Unlike [Query], there is no `row` parameter because
/// `Statement` does not return rows (but rather a count of the number of
/// rows affected)
/// * At compile time, the derive macro will check that the parameter names you
/// used in your query match the field names defined in the struct. A mismatch
/// (e.g. using "@idd" instead of "@id" when the field name is `id`) will
/// cause a compiler error.
/// * If you want to include a single literal `@` in your SQL, you must escape
/// it by doubling it (`@@`)
/// A `Query` is a SQL query that returns rows from the database.
///
/// # Example
/// `Query` can be derived like so:
/// ```no_run
/// # #[derive(FromRow, Debug)]
/// # struct Person {
/// # first_name: String,
/// # last_name: String,
/// # hobby: Option<String>,
/// # alive: bool,
/// # }
/// #[derive(Query)]
/// #[query(
/// // Write the query using named parameters
/// sql = "
/// SELECT *
/// FROM Person
/// WHERE (first_name = @name OR last_name = @name)
/// AND alive = @alive",
/// // Specify what type the rows should be decoded to
/// row = Person
/// )]
/// struct GetPeople<'a> {
/// // Define the query's parameters
/// alive: bool,
/// name: &'a str,
/// }
/// ```
/// Note that the struct you specify for `row` must implement
/// [FromRow](postgres_from_row::FromRow). You can do this by using
/// `#[derive(FromRow)]`, which you can get by adding
/// [postgres-from-row](https://crates.io/crates/postgres-from-row) to your
/// `Cargo.toml`. (Note: as of time of writing, [postgres-from-row](https://crates.io/crates/postgres-from-row) does not yet
/// support [borrowed fields](https://github.com/remkop22/postgres-from-row/issues/12).)
/// ```no_run
/// #[derive(FromRow, Debug)]
/// struct Person {
/// first_name: String,
/// last_name: String,
/// hobby: Option<String>,
/// alive: bool,
/// }
/// ````
///
/// Your can then use your query like this:
/// ```no_run
/// # #[derive(FromRow, Debug)]
/// # struct Person {
/// # first_name: String,
/// # last_name: String,
/// # hobby: Option<String>,
/// # alive: bool,
/// # }
/// # #[query(
/// # // Write the query using named parameters
/// # sql = "
/// # SELECT *
/// # FROM Person
/// # WHERE (first_name = @name OR last_name = @name)
/// # AND alive = @alive",
/// # // Specify what type the rows should be decoded to
/// # row = Person
/// # )]
/// # struct GetPeople<'a> {
/// # alive: bool,
/// # name: &'a str,
/// # }
/// fn main() -> Result<(), postgres::Error> {
/// let connection_string = std::env::var("POSTGRES_CONNECTION_STRING")
/// .unwrap_or("host=localhost user=postgres".to_owned());
/// let mut db = postgres::Client::connect(&connection_string, postgres::NoTls)?;
///
/// let people = GetPeople {
/// alive: true,
/// name: "John",
/// }
/// .query_all(&mut db)?;
///
/// println!("Found: {:?}", people);
/// Ok(())
/// }
/// ```
/// At compile time, the SQL is transformed to use numbered parameters. For
/// example, the above query will roughly desugar to:
/// ```no_run
/// # fn main() -> Result<(), postgres::Error> {
/// # let connection_string = std::env::var("POSTGRES_CONNECTION_STRING")
/// # .unwrap_or("host=localhost user=postgres".to_owned());
/// # let mut db = postgres::Client::connect(&connection_string, postgres::NoTls)?;
/// let people: Vec<Person> = db.query(
/// "SELECT *
/// FROM Person
/// WHERE (first_name = $2 OR last_name = $2)
/// AND alive = $1",
/// &[&true, &"John"],
/// )?
/// .iter()
/// .map(Person::try_from_row)
/// .collect::<Result<Vec<Person>,postgres::Error>>()?;
/// # Ok(())
/// # }
/// ````
///
/// For a more thorough example (including bulk queries), see the example
/// project folder in the [GitHub repository](https://github.com/jmoore34/postgres-named-parameters).
///
/// # Notes
/// * In order to use `#[derive(Query)`, you must also provide the helper
/// attribute `#[statement(sql = "...", row = ...)`
/// * Both the `sql` and `row` parameters are required
/// * The `sql` parameter must be a string literal
/// * The `row` parameter must implement
/// [FromRow](postgres_from_row::FromRow) (see above).
/// * At compile time, the derive macro will check that the parameter names you
/// used in your query match the field names defined in the struct. A mismatch
/// (e.g. using "@naame" instead of "@name" when the field name is `name`) will
/// cause a compiler error.
/// * If you want to include a single literal `@` in your SQL, you must escape
/// it by doubling it (`@@`)