forked from transact-rs/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.rs
More file actions
83 lines (68 loc) · 2.38 KB
/
Copy pathexecute.rs
File metadata and controls
83 lines (68 loc) · 2.38 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
use sqlx_core::{Execute, Result, Runtime};
use crate::protocol::backend::{BackendMessage, BackendMessageType};
use crate::{PgClientError, PgConnection, PgQueryResult, Postgres};
impl<Rt: Runtime> PgConnection<Rt> {
fn handle_message_in_execute(
&mut self,
message: BackendMessage,
result: &mut PgQueryResult,
) -> Result<bool> {
match message.ty {
BackendMessageType::BindComplete => {}
// ignore rows received or metadata about them
// TODO: should we log a warning? its wasteful to use `execute` on a query
// that does return rows
BackendMessageType::DataRow | BackendMessageType::RowDescription => {}
BackendMessageType::CommandComplete => {
// one statement has finished
result.extend(Some(PgQueryResult::parse(message.contents)?));
}
BackendMessageType::ReadyForQuery => {
self.handle_ready_for_query(message.deserialize()?);
// all statements are finished
return Ok(true);
}
ty => {
return Err(PgClientError::UnexpectedMessageType {
ty: ty as u8,
context: "executing a query [execute]",
}
.into());
}
}
Ok(false)
}
}
macro_rules! impl_execute {
($(@$blocking:ident)? $self:ident, $query:ident) => {{
raw_query!($(@$blocking)? $self, $query);
let mut result = PgQueryResult::default();
loop {
let message = read_message!($(@$blocking)? $self.stream)?;
if $self.handle_message_in_execute(message, &mut result)? {
break;
}
}
Ok(result)
}};
}
impl<Rt: Runtime> PgConnection<Rt> {
#[cfg(feature = "async")]
pub(super) async fn execute_async<'q, 'a, E>(&mut self, query: E) -> Result<PgQueryResult>
where
Rt: sqlx_core::Async,
E: Execute<'q, 'a, Postgres>,
{
flush!(self);
impl_execute!(self, query)
}
#[cfg(feature = "blocking")]
pub(super) fn execute_blocking<'q, 'a, E>(&mut self, query: E) -> Result<PgQueryResult>
where
Rt: sqlx_core::blocking::Runtime,
E: Execute<'q, 'a, Postgres>,
{
flush!(@blocking self);
impl_execute!(@blocking self, query)
}
}