forked from transact-rs/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.rs
More file actions
413 lines (340 loc) 路 13.2 KB
/
Copy pathexecutor.rs
File metadata and controls
413 lines (340 loc) 路 13.2 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use std::collections::{HashMap, HashSet};
use std::fmt::Write;
use futures_core::future::BoxFuture;
use futures_util::{stream, StreamExt, TryStreamExt};
use crate::arguments::Arguments;
use crate::cursor::Cursor;
use crate::describe::{Column, Describe};
use crate::executor::{Execute, Executor, RefExecutor};
use crate::postgres::protocol::{
self, CommandComplete, Field, Message, ParameterDescription, ReadyForQuery, RowDescription,
StatementId, TypeFormat, TypeId,
};
use crate::postgres::types::SharedStr;
use crate::postgres::{PgArguments, PgConnection, PgCursor, PgRow, PgTypeInfo, Postgres};
use crate::row::Row;
impl PgConnection {
pub(crate) fn write_simple_query(&mut self, query: &str) {
self.stream.write(protocol::Query(query));
}
pub(crate) fn write_prepare(&mut self, query: &str, args: &PgArguments) -> StatementId {
if let Some(&id) = self.cache_statement.get(query) {
id
} else {
let id = StatementId(self.next_statement_id);
self.next_statement_id += 1;
self.stream.write(protocol::Parse {
statement: id,
query,
param_types: &*args.types,
});
self.cache_statement.insert(query.into(), id);
id
}
}
pub(crate) fn write_describe(&mut self, d: protocol::Describe) {
self.stream.write(d);
}
pub(crate) fn write_bind(&mut self, portal: &str, statement: StatementId, args: &PgArguments) {
self.stream.write(protocol::Bind {
portal,
statement,
formats: &[TypeFormat::Binary],
values_len: args.types.len() as i16,
values: &*args.values,
result_formats: &[TypeFormat::Binary],
});
}
pub(crate) fn write_execute(&mut self, portal: &str, limit: i32) {
self.stream.write(protocol::Execute { portal, limit });
}
pub(crate) fn write_sync(&mut self) {
self.stream.write(protocol::Sync);
}
async fn wait_until_ready(&mut self) -> crate::Result<()> {
// depending on how the previous query finished we may need to continue
// pulling messages from the stream until we receive a [ReadyForQuery] message
// postgres sends the [ReadyForQuery] message when it's fully complete with processing
// the previous query
if !self.is_ready {
loop {
if let Message::ReadyForQuery = self.stream.read().await? {
// we are now ready to go
self.is_ready = true;
break;
}
}
}
Ok(())
}
// Write out the query to the connection stream, ensure that we are synchronized at the
// most recent [ReadyForQuery] and flush our buffer to postgres.
//
// It is safe to call this method repeatedly (but all data from postgres would be lost) but
// it is assumed that a call to [PgConnection::affected_rows] or [PgCursor::next] would
// immediately follow.
pub(crate) async fn run(
&mut self,
query: &str,
arguments: Option<PgArguments>,
) -> crate::Result<Option<StatementId>> {
let statement = if let Some(arguments) = arguments {
// Check the statement cache for a statement ID that matches the given query
// If it doesn't exist, we generate a new statement ID and write out [Parse] to the
// connection command buffer
let statement = self.write_prepare(query, &arguments);
// Next, [Bind] attaches the arguments to the statement and creates a named portal
self.write_bind("", statement, &arguments);
// Next, [Describe] will return the expected result columns and types
// Conditionally run [Describe] only if the results have not been cached
if !self.cache_statement_columns.contains_key(&statement) {
self.write_describe(protocol::Describe::Portal(""));
}
// Next, [Execute] then executes the named portal
self.write_execute("", 0);
// Finally, [Sync] asks postgres to process the messages that we sent and respond with
// a [ReadyForQuery] message when it's completely done. Theoretically, we could send
// dozens of queries before a [Sync] and postgres can handle that. Execution on the server
// is still serial but it would reduce round-trips. Some kind of builder pattern that is
// termed batching might suit this.
self.write_sync();
Some(statement)
} else {
// https://www.postgresql.org/docs/12/protocol-flow.html#id-1.10.5.7.4
self.write_simple_query(query);
None
};
self.wait_until_ready().await?;
self.stream.flush().await?;
self.is_ready = false;
Ok(statement)
}
async fn describe<'e, 'q: 'e>(
&'e mut self,
query: &'q str,
) -> crate::Result<Describe<Postgres>> {
self.is_ready = false;
let statement = self.write_prepare(query, &Default::default());
self.write_describe(protocol::Describe::Statement(statement));
self.write_sync();
self.stream.flush().await?;
let params = loop {
match self.stream.read().await? {
Message::ParseComplete => {}
Message::ParameterDescription => {
break ParameterDescription::read(self.stream.buffer())?;
}
message => {
return Err(protocol_err!(
"expected ParameterDescription; received {:?}",
message
)
.into());
}
};
};
let result = match self.stream.read().await? {
Message::NoData => None,
Message::RowDescription => Some(RowDescription::read(self.stream.buffer())?),
message => {
return Err(protocol_err!(
"expected RowDescription or NoData; received {:?}",
message
)
.into());
}
};
self.wait_until_ready().await?;
let result_fields = result.map_or_else(Default::default, |r| r.fields);
// TODO: cache this result
let type_names = self
.get_type_names(
params
.ids
.iter()
.cloned()
.chain(result_fields.iter().map(|field| field.type_id)),
)
.await?;
Ok(Describe {
param_types: params
.ids
.iter()
.map(|id| PgTypeInfo::new(*id, &type_names[&id.0]))
.collect::<Vec<_>>()
.into_boxed_slice(),
result_columns: self
.map_result_columns(result_fields, type_names)
.await?
.into_boxed_slice(),
})
}
async fn get_type_names(
&mut self,
ids: impl IntoIterator<Item = TypeId>,
) -> crate::Result<HashMap<u32, SharedStr>> {
let type_ids: HashSet<u32> = ids.into_iter().map(|id| id.0).collect::<HashSet<u32>>();
if type_ids.is_empty() {
return Ok(HashMap::new());
}
// uppercase type names are easier to visually identify
let mut query = "select types.type_id, UPPER(pg_type.typname) from (VALUES ".to_string();
let mut args = PgArguments::default();
let mut pushed = false;
// TODO: dedup this with the one below, ideally as an API we can export
for (i, (&type_id, bind)) in type_ids.iter().zip((1..).step_by(2)).enumerate() {
if pushed {
query += ", ";
}
pushed = true;
let _ = write!(query, "(${}, ${})", bind, bind + 1);
// not used in the output but ensures are values are sorted correctly
args.add(i as i32);
args.add(type_id as i32);
}
query += ") as types(idx, type_id) \
inner join pg_catalog.pg_type on pg_type.oid = type_id \
order by types.idx";
crate::query::query(&query)
.bind_all(args)
.try_map(|row: PgRow| -> crate::Result<(u32, SharedStr)> {
Ok((
row.try_get::<i32, _>(0)? as u32,
row.try_get::<String, _>(1)?.into(),
))
})
.fetch(self)
.try_collect()
.await
}
async fn map_result_columns(
&mut self,
fields: Box<[Field]>,
type_names: HashMap<u32, SharedStr>,
) -> crate::Result<Vec<Column<Postgres>>> {
if fields.is_empty() {
return Ok(vec![]);
}
let mut query = "select col.idx, pg_attribute.attnotnull from (VALUES ".to_string();
let mut pushed = false;
let mut args = PgArguments::default();
for (i, (field, bind)) in fields.iter().zip((1..).step_by(3)).enumerate() {
if pushed {
query += ", ";
}
pushed = true;
let _ = write!(
query,
"(${}::int4, ${}::int4, ${}::int2)",
bind,
bind + 1,
bind + 2
);
args.add(i as i32);
args.add(field.table_id.map(|id| id as i32));
args.add(field.column_id);
}
query += ") as col(idx, table_id, col_idx) \
left join pg_catalog.pg_attribute on table_id is not null and attrelid = table_id and attnum = col_idx \
order by col.idx;";
log::trace!("describe pg_attribute query: {:#?}", query);
crate::query::query(&query)
.bind_all(args)
.try_map(|row: PgRow| {
let idx = row.try_get::<i32, _>(0)?;
let non_null = row.try_get::<Option<bool>, _>(1)?;
Ok((idx, non_null))
})
.fetch(self)
.zip(stream::iter(fields.into_vec().into_iter().enumerate()))
.map(|(row, (fidx, field))| -> crate::Result<Column<_>> {
let (idx, non_null) = row?;
if idx != fidx as i32 {
return Err(
protocol_err!("missing field from query, field: {:?}", field).into(),
);
}
Ok(Column {
name: field.name,
table_id: field.table_id,
type_info: PgTypeInfo::new(field.type_id, &type_names[&field.type_id.0]),
non_null,
})
})
.try_collect()
.await
}
// Poll messages from Postgres, counting the rows affected, until we finish the query
// This must be called directly after a call to [PgConnection::execute]
async fn affected_rows(&mut self) -> crate::Result<u64> {
let mut rows = 0;
loop {
match self.stream.read().await? {
Message::ParseComplete
| Message::BindComplete
| Message::NoData
| Message::EmptyQueryResponse
| Message::RowDescription => {}
Message::DataRow => {
// TODO: should we log a warning? this is almost
// definitely a programmer error
}
Message::CommandComplete => {
rows += CommandComplete::read(self.stream.buffer())?.affected_rows;
}
Message::ReadyForQuery => {
// TODO: How should we handle an ERROR status form ReadyForQuery
let _ready = ReadyForQuery::read(self.stream.buffer())?;
self.is_ready = true;
break;
}
message => {
return Err(
protocol_err!("affected_rows: unexpected message: {:?}", message).into(),
);
}
}
}
Ok(rows)
}
}
impl Executor for super::PgConnection {
type Database = Postgres;
fn execute<'e, 'q: 'e, 'c: 'e, E: 'e>(
&'c mut self,
query: E,
) -> BoxFuture<'e, crate::Result<u64>>
where
E: Execute<'q, Self::Database>,
{
Box::pin(async move {
let (query, arguments) = query.into_parts();
self.run(query, arguments).await?;
self.affected_rows().await
})
}
fn fetch<'q, E>(&mut self, query: E) -> PgCursor<'_, 'q>
where
E: Execute<'q, Self::Database>,
{
PgCursor::from_connection(self, query)
}
fn describe<'e, 'q, E: 'e>(
&'e mut self,
query: E,
) -> BoxFuture<'e, crate::Result<Describe<Self::Database>>>
where
E: Execute<'q, Self::Database>,
{
Box::pin(async move { self.describe(query.into_parts().0).await })
}
}
impl<'c> RefExecutor<'c> for &'c mut super::PgConnection {
type Database = Postgres;
fn fetch_by_ref<'q, E>(self, query: E) -> PgCursor<'c, 'q>
where
E: Execute<'q, Self::Database>,
{
PgCursor::from_connection(self, query)
}
}