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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* [Asynchronous Command Processing](https://www.postgresql.org/docs/current/libpq-async.html)
*/
impl Connection {
/**
* Submits a command to the server without waiting for the result(s).
*
* See
* [PQsendQuery](https://www.postgresql.org/docs/current/libpq-async.html#LIBPQ-PQSENDQUERY).
*/
pub fn send_query(&self, command: &str) -> crate::errors::Result {
log::trace!("Sending query '{command}'");
let c_command = crate::ffi::to_cstr(command);
let success = unsafe { pq_sys::PQsendQuery(self.into(), c_command.as_ptr()) };
if success == 1 {
Ok(())
} else {
self.error()
}
}
/**
* Submits a command and separate parameters to the server without waiting for the result(s).
*
* See
* [PQsendQueryParams](https://www.postgresql.org/docs/current/libpq-async.html#LIBPQ-PQSENDQUERYPARAMS).
*/
pub fn send_query_params(
&self,
command: &str,
param_types: &[crate::Oid],
param_values: &[Option<&[u8]>],
param_formats: &[crate::Format],
result_format: crate::Format,
) -> crate::errors::Result {
let (values, formats, lengths) = Self::transform_params(param_values, param_formats);
Self::trace_query("Sending", command, param_types, param_values, param_formats);
let c_command = crate::ffi::to_cstr(command);
let success = unsafe {
pq_sys::PQsendQueryParams(
self.into(),
c_command.as_ptr(),
values.len() as i32,
if param_types.is_empty() {
std::ptr::null()
} else {
param_types.as_ptr()
},
values.as_ptr(),
if lengths.is_empty() {
std::ptr::null()
} else {
lengths.as_ptr()
},
if formats.is_empty() {
std::ptr::null()
} else {
formats.as_ptr()
},
result_format as i32,
)
};
if success == 1 {
Ok(())
} else {
self.error()
}
}
/**
* Sends a request to create a prepared statement with the given parameters, without waiting
* for completion.
*
* See
* [PQsendPrepare](https://www.postgresql.org/docs/current/libpq-async.html#LIBPQ-PQSENDPREPARE).
*/
pub fn send_prepare(
&self,
name: Option<&str>,
query: &str,
param_types: &[crate::Oid],
) -> crate::errors::Result {
let prefix = format!("Sending prepare {}", name.unwrap_or("anonymous"));
Self::trace_query(&prefix, query, param_types, &[], &[]);
let c_name = crate::ffi::to_cstr(name.unwrap_or_default());
let c_query = crate::ffi::to_cstr(query);
let success = unsafe {
pq_sys::PQsendPrepare(
self.into(),
c_name.as_ptr(),
c_query.as_ptr(),
param_types.len() as i32,
param_types.as_ptr(),
)
};
if success == 1 {
Ok(())
} else {
self.error()
}
}
/**
* Sends a request to execute a prepared statement with given parameters, without waiting for the result(s).
*
* See [PQsendQueryPrepared](https://www.postgresql.org/docs/current/libpq-async.html#LIBPQ-PQSENDQUERYPREPARED).
*/
pub fn send_query_prepared(
&self,
name: Option<&str>,
param_values: &[Option<&[u8]>],
param_formats: &[crate::Format],
result_format: crate::Format,
) -> crate::errors::Result {
let prefix = format!("Send {} prepared query", name.unwrap_or("anonymous"));
Self::trace_query(&prefix, "", &[], param_values, param_formats);
let (values, formats, lengths) = Self::transform_params(param_values, param_formats);
let c_name = crate::ffi::to_cstr(name.unwrap_or_default());
let success = unsafe {
pq_sys::PQsendQueryPrepared(
self.into(),
c_name.as_ptr(),
values.len() as i32,
values.as_ptr(),
if lengths.is_empty() {
std::ptr::null()
} else {
lengths.as_ptr()
},
if formats.is_empty() {
std::ptr::null()
} else {
formats.as_ptr()
},
result_format as i32,
)
};
if success == 1 {
Ok(())
} else {
self.error()
}
}
/**
* Submits a request to obtain information about the specified prepared statement, without waiting for completion.
*
* See [PQsendDescribePortal](https://www.postgresql.org/docs/current/libpq-async.html#LIBPQ-PQSENDDESCRIBEPORTAL).
*/
pub fn send_describe_prepared(&self, name: Option<&str>) -> crate::errors::Result {
log::trace!(
"Sending describe prepared query {}",
name.unwrap_or("anonymous")
);
let c_name = crate::ffi::to_cstr(name.unwrap_or_default());
let success = unsafe { pq_sys::PQsendDescribePrepared(self.into(), c_name.as_ptr()) };
if success == 1 {
Ok(())
} else {
self.error()
}
}
/**
* Submits a request to obtain information about the specified portal, without waiting for completion.
*
* See
* [PQsendDescribePortal](https://www.postgresql.org/docs/current/libpq-async.html#LIBPQ-PQSENDDESCRIBEPORTAL).
*/
pub fn send_describe_portal(&self, name: Option<&str>) -> crate::errors::Result {
log::trace!("Sending describe portal {}", name.unwrap_or("anonymous"));
let c_name = crate::ffi::to_cstr(name.unwrap_or_default());
let success = unsafe { pq_sys::PQsendDescribePortal(self.into(), c_name.as_ptr()) };
if success == 1 {
Ok(())
} else {
self.error()
}
}
/**
* Waits for the next result a prior `send_*` call, and returns it.
*
* See [PQgetResult](https://www.postgresql.org/docs/current/libpq-async.html#LIBPQ-PQGETRESULT).
*/
pub fn result(&self) -> Option<crate::PQResult> {
let raw = unsafe { pq_sys::PQgetResult(self.into()) };
if raw.is_null() {
None
} else {
Some(raw.into())
}
}
/**
* If input is available from the server, consume it.
*
* See
* [PQconsumeInput](https://www.postgresql.org/docs/current/libpq-async.html#LIBPQ-PQCONSUMEINPUT).
*/
pub fn consume_input(&self) -> crate::errors::Result {
log::trace!("Consume input");
let success = unsafe { pq_sys::PQconsumeInput(self.into()) };
if success == 1 {
Ok(())
} else {
self.error()
}
}
/**
* Returns `true` if a command is busy, that is, `Result` would block waiting for input.
*
* See [PQisBusy](https://www.postgresql.org/docs/current/libpq-async.html#LIBPQ-PQISBUSY).
*/
pub fn is_busy(&self) -> bool {
unsafe { pq_sys::PQisBusy(self.into()) == 1 }
}
/**
* Sets the nonblocking status of the connection.
*
* See
* [PQsetnonblocking](https://www.postgresql.org/docs/current/libpq-async.html#LIBPQ-PQSETNONBLOCKING).
*/
pub fn set_non_blocking(&self, non_blocking: bool) -> crate::errors::Result {
if non_blocking {
log::trace!("Set non blocking");
} else {
log::trace!("Set blocking");
}
let status = unsafe { pq_sys::PQsetnonblocking(self.into(), non_blocking as i32) };
if status < 0 {
self.error()
} else {
Ok(())
}
}
/**
* Returns the blocking status of the database connection.
*
* See
* [PQisnonblocking](https://www.postgresql.org/docs/current/libpq-async.html#LIBPQ-PQISNONBLOCKING).
*/
pub fn is_non_blocking(&self) -> bool {
unsafe { pq_sys::PQisnonblocking(self.into()) == 1 }
}
/**
* Attempts to flush any queued output data to the server.
*
* See [PQflush](https://www.postgresql.org/docs/current/libpq-async.html#LIBPQ-PQFLUSH).
*/
pub fn flush(&self) -> crate::errors::Result {
log::trace!("Flush");
let status = unsafe { pq_sys::PQflush(self.into()) };
if status == 0 {
Ok(())
} else {
self.error()
}
}
/**
* Submits a request to close the specified prepared statement, without waiting for completion.
*
* See
* [PQsendClosePrepared](https://www.postgresql.org/docs/current/libpq-async.html#LIBPQ-PQSENDCLOSEPREPARED).
*/
#[cfg(feature = "v17")]
pub fn send_close_prepared(&self, name: Option<&str>) -> crate::errors::Result {
log::trace!("Send close prepared {:?}", name.unwrap_or_default());
let c_name = crate::ffi::to_cstr(name.unwrap_or_default());
let status = unsafe { pq_sys::PQsendClosePrepared(self.into(), c_name.as_ptr()) };
if status == 0 {
Ok(())
} else {
self.error()
}
}
/**
* Submits a request to close specified portal, without waiting for completion.
*
* See
* [PQsendClosePortal](https://www.postgresql.org/docs/currencurrentt/libpq-async.html#LIBPQ-PQSENDCLOSEPORTAL).
*/
#[cfg(feature = "v17")]
pub fn send_close_portal(&self, name: Option<&str>) -> crate::errors::Result {
log::trace!("Send close portal {:?}", name.unwrap_or_default());
let c_name = crate::ffi::to_cstr(name.unwrap_or_default());
let status = unsafe { pq_sys::PQsendClosePortal(self.into(), c_name.as_ptr()) };
if status == 1 {
Ok(())
} else {
self.error()
}
}
}