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
use super::{
frontend, pipeline, BackendMessage, BytesMut, Connection, Duration, Error, Oid, PipelineBatch,
Result, Row, ToSql,
};
use crate::row::{self, SimpleQueryMessage, SimpleQueryRow};
impl Connection {
/// Execute a query that returns rows.
///
/// Parameters are encoded in binary format.
///
/// ```rust,no_run
/// # async fn example(conn: &mut sentinel_driver::Connection) -> sentinel_driver::Result<()> {
/// let rows = conn.query("SELECT * FROM users WHERE id = $1", &[&42i32]).await?;
/// # Ok(())
/// # }
/// ```
pub async fn query(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>> {
if let Some(timeout) = self.query_timeout {
return self.query_with_timeout(sql, params, timeout).await;
}
let result = self.query_internal(sql, params).await?;
match result {
pipeline::QueryResult::Rows(rows) => Ok(rows),
pipeline::QueryResult::Command(_) => Ok(Vec::new()),
}
}
/// Execute a query that returns a single row.
///
/// Returns an error if no rows are returned.
pub async fn query_one(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Row> {
let rows = self.query(sql, params).await?;
rows.into_iter()
.next()
.ok_or_else(|| Error::Protocol("query returned no rows".into()))
}
/// Execute a query that returns an optional single row.
pub async fn query_opt(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<Option<Row>> {
let rows = self.query(sql, params).await?;
Ok(rows.into_iter().next())
}
/// Execute a non-SELECT query (INSERT, UPDATE, DELETE, etc.).
///
/// Returns the number of rows affected.
pub async fn execute(&mut self, sql: &str, params: &[&(dyn ToSql + Sync)]) -> Result<u64> {
if let Some(timeout) = self.query_timeout {
return self.execute_with_timeout(sql, params, timeout).await;
}
let result = self.query_internal(sql, params).await?;
match result {
pipeline::QueryResult::Command(r) => Ok(r.rows_affected),
pipeline::QueryResult::Rows(_) => Ok(0),
}
}
/// Execute a query with a timeout.
///
/// If the query does not complete within `timeout`, a cancel request
/// is sent to the server and the connection is marked as broken.
pub async fn query_with_timeout(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
timeout: Duration,
) -> Result<Vec<Row>> {
let cancel_token = self.cancel_token();
match tokio::time::timeout(timeout, self.query_internal(sql, params)).await {
Ok(result) => {
let result = result?;
match result {
pipeline::QueryResult::Rows(rows) => Ok(rows),
pipeline::QueryResult::Command(_) => Ok(Vec::new()),
}
}
Err(_elapsed) => {
self.is_broken = true;
// Fire-and-forget cancel
tokio::spawn(async move {
cancel_token.cancel().await.ok();
});
Err(Error::Timeout(format!(
"query timeout after {}ms",
timeout.as_millis()
)))
}
}
}
/// Execute a non-SELECT query with a timeout.
///
/// If the query does not complete within `timeout`, a cancel request
/// is sent to the server and the connection is marked as broken.
pub async fn execute_with_timeout(
&mut self,
sql: &str,
params: &[&(dyn ToSql + Sync)],
timeout: Duration,
) -> Result<u64> {
let cancel_token = self.cancel_token();
match tokio::time::timeout(timeout, self.query_internal(sql, params)).await {
Ok(result) => {
let result = result?;
match result {
pipeline::QueryResult::Command(r) => Ok(r.rows_affected),
pipeline::QueryResult::Rows(_) => Ok(0),
}
}
Err(_elapsed) => {
self.is_broken = true;
tokio::spawn(async move {
cancel_token.cancel().await.ok();
});
Err(Error::Timeout(format!(
"query timeout after {}ms",
timeout.as_millis()
)))
}
}
}
/// Execute a simple query (no parameters, text protocol).
///
/// Returns row data (in text format) and command completions. Useful
/// for DDL statements, multi-statement queries, and queries where you
/// don't need binary-decoded typed values.
///
/// ```rust,no_run
/// # async fn example(conn: &mut sentinel_driver::Connection) -> sentinel_driver::Result<()> {
/// use sentinel_driver::SimpleQueryMessage;
///
/// let messages = conn.simple_query("SELECT 1 AS n; SELECT 'hello' AS greeting").await?;
/// for msg in &messages {
/// match msg {
/// SimpleQueryMessage::Row(row) => {
/// println!("value: {:?}", row.get(0));
/// }
/// SimpleQueryMessage::CommandComplete(n) => {
/// println!("rows: {n}");
/// }
/// }
/// }
/// # Ok(())
/// # }
/// ```
pub async fn simple_query(&mut self, sql: &str) -> Result<Vec<SimpleQueryMessage>> {
frontend::query(self.conn.write_buf(), sql);
self.conn.send().await?;
let mut results = Vec::new();
loop {
match self.conn.recv().await? {
BackendMessage::DataRow { columns } => {
// Extract text-format column values from DataRow
let mut text_columns = Vec::with_capacity(columns.len());
for i in 0..columns.len() {
let value = columns
.get(i)
.map(|bytes| String::from_utf8_lossy(&bytes).into_owned());
text_columns.push(value);
}
results.push(SimpleQueryMessage::Row(SimpleQueryRow::new(text_columns)));
}
BackendMessage::CommandComplete { tag } => {
let parsed = row::parse_command_tag(&tag);
results.push(SimpleQueryMessage::CommandComplete(parsed.rows_affected));
}
BackendMessage::ReadyForQuery { transaction_status } => {
self.transaction_status = transaction_status;
break;
}
BackendMessage::ErrorResponse { fields } => {
self.drain_until_ready().await.ok();
return Err(Error::server(
fields.severity,
fields.code,
fields.message,
fields.detail,
fields.hint,
fields.position,
));
}
_ => {}
}
}
Ok(results)
}
// ── query_typed ────────────────────────────────────
/// Execute a query with inline parameter types, skipping the prepare step.
///
/// Instead of a separate Prepare round-trip, the parameter types are
/// specified directly in the Parse message. This saves one round-trip
/// compared to [`query()`](Self::query) at the cost of requiring the
/// caller to specify types explicitly.
///
/// ```rust,no_run
/// # async fn example(conn: &mut sentinel_driver::Connection) -> sentinel_driver::Result<()> {
/// use sentinel_driver::Oid;
///
/// let rows = conn.query_typed(
/// "SELECT $1::int4 + $2::int4 AS sum",
/// &[(&1i32, Oid::INT4), (&2i32, Oid::INT4)],
/// ).await?;
/// # Ok(())
/// # }
/// ```
pub async fn query_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<Vec<Row>> {
let result = self.query_typed_internal(sql, params).await?;
match result {
pipeline::QueryResult::Rows(rows) => Ok(rows),
pipeline::QueryResult::Command(_) => Ok(Vec::new()),
}
}
/// Execute a typed query that returns a single row.
pub async fn query_typed_one(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<Row> {
let rows = self.query_typed(sql, params).await?;
rows.into_iter()
.next()
.ok_or_else(|| Error::Protocol("query returned no rows".into()))
}
/// Execute a typed query that returns an optional single row.
pub async fn query_typed_opt(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<Option<Row>> {
let rows = self.query_typed(sql, params).await?;
Ok(rows.into_iter().next())
}
/// Execute a typed non-SELECT query, returning rows affected.
pub async fn execute_typed(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<u64> {
let result = self.query_typed_internal(sql, params).await?;
match result {
pipeline::QueryResult::Command(r) => Ok(r.rows_affected),
pipeline::QueryResult::Rows(_) => Ok(0),
}
}
async fn query_typed_internal(
&mut self,
sql: &str,
params: &[(&(dyn ToSql + Sync), Oid)],
) -> Result<pipeline::QueryResult> {
let param_types: Vec<u32> = params.iter().map(|(_, oid)| oid.0).collect();
let mut encoded_params: Vec<Option<Vec<u8>>> = Vec::with_capacity(params.len());
for (value, _) in params {
if value.is_null() {
encoded_params.push(None);
} else {
let mut buf = BytesMut::new();
value.to_sql(&mut buf)?;
encoded_params.push(Some(buf.to_vec()));
}
}
let mut batch = PipelineBatch::new();
batch.add(sql.to_string(), param_types, encoded_params);
let mut results = batch.execute(&mut self.conn).await?;
results
.pop()
.ok_or_else(|| Error::protocol("pipeline returned no results"))
}
}