mirror of
https://github.com/neondatabase/neon.git
synced 2026-01-17 02:12:56 +00:00
Our rust-postgres fork is getting messy. Mostly because proxy wants more
control over the raw protocol than tokio-postgres provides. As such,
it's diverging more and more. Storage and compute also make use of
rust-postgres, but in more normal usage, thus they don't need our crazy
changes.
Idea:
* proxy maintains their subset
* other teams use a minimal patch set against upstream rust-postgres
Reviewing this code will be difficult. To implement it, I
1. Copied tokio-postgres, postgres-protocol and postgres-types from
00940fcdb5
2. Updated their package names with the `2` suffix to make them compile
in the workspace.
3. Updated proxy to use those packages
4. Copied in the code from tokio-postgres-rustls 0.13 (with some patches
applied https://github.com/jbg/tokio-postgres-rustls/pull/32
https://github.com/jbg/tokio-postgres-rustls/pull/33)
5. Removed as much dead code as I could find in the vendored libraries
6. Updated the tokio-postgres-rustls code to use our existing channel
binding implementation
75 lines
2.3 KiB
Rust
75 lines
2.3 KiB
Rust
use crate::codec::FrontendMessage;
|
|
use crate::connection::RequestMessages;
|
|
use crate::query::RowStream;
|
|
use crate::{CancelToken, Client, Error, ReadyForQueryStatus};
|
|
use postgres_protocol2::message::frontend;
|
|
|
|
/// A representation of a PostgreSQL database transaction.
|
|
///
|
|
/// Transactions will implicitly roll back when dropped. Use the `commit` method to commit the changes made in the
|
|
/// transaction. Transactions can be nested, with inner transactions implemented via safepoints.
|
|
pub struct Transaction<'a> {
|
|
client: &'a mut Client,
|
|
done: bool,
|
|
}
|
|
|
|
impl Drop for Transaction<'_> {
|
|
fn drop(&mut self) {
|
|
if self.done {
|
|
return;
|
|
}
|
|
|
|
let buf = self.client.inner().with_buf(|buf| {
|
|
frontend::query("ROLLBACK", buf).unwrap();
|
|
buf.split().freeze()
|
|
});
|
|
let _ = self
|
|
.client
|
|
.inner()
|
|
.send(RequestMessages::Single(FrontendMessage::Raw(buf)));
|
|
}
|
|
}
|
|
|
|
impl<'a> Transaction<'a> {
|
|
pub(crate) fn new(client: &'a mut Client) -> Transaction<'a> {
|
|
Transaction {
|
|
client,
|
|
done: false,
|
|
}
|
|
}
|
|
|
|
/// Consumes the transaction, committing all changes made within it.
|
|
pub async fn commit(mut self) -> Result<ReadyForQueryStatus, Error> {
|
|
self.done = true;
|
|
self.client.batch_execute("COMMIT").await
|
|
}
|
|
|
|
/// Rolls the transaction back, discarding all changes made within it.
|
|
///
|
|
/// This is equivalent to `Transaction`'s `Drop` implementation, but provides any error encountered to the caller.
|
|
pub async fn rollback(mut self) -> Result<ReadyForQueryStatus, Error> {
|
|
self.done = true;
|
|
self.client.batch_execute("ROLLBACK").await
|
|
}
|
|
|
|
/// Like `Client::query_raw_txt`.
|
|
pub async fn query_raw_txt<S, I>(&self, statement: &str, params: I) -> Result<RowStream, Error>
|
|
where
|
|
S: AsRef<str>,
|
|
I: IntoIterator<Item = Option<S>>,
|
|
I::IntoIter: ExactSizeIterator,
|
|
{
|
|
self.client.query_raw_txt(statement, params).await
|
|
}
|
|
|
|
/// Like `Client::cancel_token`.
|
|
pub fn cancel_token(&self) -> CancelToken {
|
|
self.client.cancel_token()
|
|
}
|
|
|
|
/// Returns a reference to the underlying `Client`.
|
|
pub fn client(&self) -> &Client {
|
|
self.client
|
|
}
|
|
}
|