|
1 |
| -use std::ops::{ControlFlow, Deref, DerefMut}; |
2 | 1 | use std::str::FromStr;
|
3 | 2 |
|
4 |
| -use log::Level; |
5 |
| -use sqlx_core::bytes::Buf; |
6 |
| - |
7 | 3 | use crate::connection::tls::MaybeUpgradeTls;
|
8 | 4 | use crate::error::Error;
|
9 |
| -use crate::message::{ |
10 |
| - BackendMessage, BackendMessageFormat, EncodeMessage, FrontendMessage, Notice, ReceivedMessage, |
11 |
| -}; |
12 | 5 | use crate::net::{self, BufferedSocket, Socket};
|
13 |
| -use crate::{PgConnectOptions, PgDatabaseError, PgSeverity}; |
| 6 | +use crate::PgConnectOptions; |
14 | 7 |
|
15 | 8 | // the stream is a separate type from the connection to uphold the invariant where an instantiated
|
16 | 9 | // [PgConnection] is a **valid** connection to postgres
|
@@ -44,154 +37,6 @@ impl PgStream {
|
44 | 37 | inner: BufferedSocket::new(socket),
|
45 | 38 | })
|
46 | 39 | }
|
47 |
| - |
48 |
| - #[inline(always)] |
49 |
| - pub(crate) fn write_msg(&mut self, message: impl FrontendMessage) -> Result<(), Error> { |
50 |
| - self.write(EncodeMessage(message)) |
51 |
| - } |
52 |
| - |
53 |
| - pub(crate) async fn send<T>(&mut self, message: T) -> Result<(), Error> |
54 |
| - where |
55 |
| - T: FrontendMessage, |
56 |
| - { |
57 |
| - self.write_msg(message)?; |
58 |
| - self.flush().await?; |
59 |
| - Ok(()) |
60 |
| - } |
61 |
| - |
62 |
| - // Expect a specific type and format |
63 |
| - pub(crate) async fn recv_expect<B: BackendMessage>(&mut self) -> Result<B, Error> { |
64 |
| - self.recv().await?.decode() |
65 |
| - } |
66 |
| - |
67 |
| - pub(crate) async fn recv_unchecked(&mut self) -> Result<ReceivedMessage, Error> { |
68 |
| - // NOTE: to not break everything, this should be cancel-safe; |
69 |
| - // DO NOT modify `buf` unless a full message has been read |
70 |
| - self.inner |
71 |
| - .try_read(|buf| { |
72 |
| - // all packets in postgres start with a 5-byte header |
73 |
| - // this header contains the message type and the total length of the message |
74 |
| - let Some(mut header) = buf.get(..5) else { |
75 |
| - return Ok(ControlFlow::Continue(5)); |
76 |
| - }; |
77 |
| - |
78 |
| - let format = BackendMessageFormat::try_from_u8(header.get_u8())?; |
79 |
| - |
80 |
| - let message_len = header.get_u32() as usize; |
81 |
| - |
82 |
| - let expected_len = message_len |
83 |
| - .checked_add(1) |
84 |
| - // this shouldn't really happen but is mostly a sanity check |
85 |
| - .ok_or_else(|| { |
86 |
| - err_protocol!("message_len + 1 overflows usize: {message_len}") |
87 |
| - })?; |
88 |
| - |
89 |
| - if buf.len() < expected_len { |
90 |
| - return Ok(ControlFlow::Continue(expected_len)); |
91 |
| - } |
92 |
| - |
93 |
| - // `buf` SHOULD NOT be modified ABOVE this line |
94 |
| - |
95 |
| - // pop off the format code since it's not counted in `message_len` |
96 |
| - buf.advance(1); |
97 |
| - |
98 |
| - // consume the message, including the length prefix |
99 |
| - let mut contents = buf.split_to(message_len).freeze(); |
100 |
| - |
101 |
| - // cut off the length prefix |
102 |
| - contents.advance(4); |
103 |
| - |
104 |
| - Ok(ControlFlow::Break(ReceivedMessage { format, contents })) |
105 |
| - }) |
106 |
| - .await |
107 |
| - } |
108 |
| - |
109 |
| - // Get the next message from the server |
110 |
| - // May wait for more data from the server |
111 |
| - pub(crate) async fn recv(&mut self) -> Result<ReceivedMessage, Error> { |
112 |
| - loop { |
113 |
| - let message = self.recv_unchecked().await?; |
114 |
| - |
115 |
| - match message.format { |
116 |
| - BackendMessageFormat::ErrorResponse => { |
117 |
| - // An error returned from the database server. |
118 |
| - return Err(message.decode::<PgDatabaseError>()?.into()); |
119 |
| - } |
120 |
| - |
121 |
| - // BackendMessageFormat::ParameterStatus => { |
122 |
| - // // informs the frontend about the current (initial) |
123 |
| - // // setting of backend parameters |
124 |
| - |
125 |
| - // let ParameterStatus { name, value } = message.decode()?; |
126 |
| - // // TODO: handle `client_encoding`, `DateStyle` change |
127 |
| - |
128 |
| - // match name.as_str() { |
129 |
| - // "server_version" => { |
130 |
| - // self.server_version_num = parse_server_version(&value); |
131 |
| - // } |
132 |
| - // _ => { |
133 |
| - // self.parameter_statuses.insert(name, value); |
134 |
| - // } |
135 |
| - // } |
136 |
| - |
137 |
| - // continue; |
138 |
| - // } |
139 |
| - BackendMessageFormat::NoticeResponse => { |
140 |
| - // do we need this to be more configurable? |
141 |
| - // if you are reading this comment and think so, open an issue |
142 |
| - |
143 |
| - let notice: Notice = message.decode()?; |
144 |
| - |
145 |
| - let (log_level, tracing_level) = match notice.severity() { |
146 |
| - PgSeverity::Fatal | PgSeverity::Panic | PgSeverity::Error => { |
147 |
| - (Level::Error, tracing::Level::ERROR) |
148 |
| - } |
149 |
| - PgSeverity::Warning => (Level::Warn, tracing::Level::WARN), |
150 |
| - PgSeverity::Notice => (Level::Info, tracing::Level::INFO), |
151 |
| - PgSeverity::Debug => (Level::Debug, tracing::Level::DEBUG), |
152 |
| - PgSeverity::Info | PgSeverity::Log => (Level::Trace, tracing::Level::TRACE), |
153 |
| - }; |
154 |
| - |
155 |
| - let log_is_enabled = log::log_enabled!( |
156 |
| - target: "sqlx::postgres::notice", |
157 |
| - log_level |
158 |
| - ) || sqlx_core::private_tracing_dynamic_enabled!( |
159 |
| - target: "sqlx::postgres::notice", |
160 |
| - tracing_level |
161 |
| - ); |
162 |
| - if log_is_enabled { |
163 |
| - sqlx_core::private_tracing_dynamic_event!( |
164 |
| - target: "sqlx::postgres::notice", |
165 |
| - tracing_level, |
166 |
| - message = notice.message() |
167 |
| - ); |
168 |
| - } |
169 |
| - |
170 |
| - continue; |
171 |
| - } |
172 |
| - |
173 |
| - _ => {} |
174 |
| - } |
175 |
| - |
176 |
| - return Ok(message); |
177 |
| - } |
178 |
| - } |
179 |
| -} |
180 |
| - |
181 |
| -impl Deref for PgStream { |
182 |
| - type Target = BufferedSocket<Box<dyn Socket>>; |
183 |
| - |
184 |
| - #[inline] |
185 |
| - fn deref(&self) -> &Self::Target { |
186 |
| - &self.inner |
187 |
| - } |
188 |
| -} |
189 |
| - |
190 |
| -impl DerefMut for PgStream { |
191 |
| - #[inline] |
192 |
| - fn deref_mut(&mut self) -> &mut Self::Target { |
193 |
| - &mut self.inner |
194 |
| - } |
195 | 40 | }
|
196 | 41 |
|
197 | 42 | // reference:
|
|
0 commit comments