Line data Source code
1 : use crate::query::RowStream;
2 : use crate::{CancelToken, Client, Error, ReadyForQueryStatus};
3 :
4 : /// A representation of a PostgreSQL database transaction.
5 : ///
6 : /// Transactions will implicitly roll back when dropped. Use the `commit` method to commit the changes made in the
7 : /// transaction. Transactions can be nested, with inner transactions implemented via safepoints.
8 : pub struct Transaction<'a> {
9 : client: &'a mut Client,
10 : done: bool,
11 : }
12 :
13 : impl Drop for Transaction<'_> {
14 0 : fn drop(&mut self) {
15 0 : if self.done {
16 0 : return;
17 0 : }
18 :
19 0 : let _ = self.client.inner_mut().send_simple_query("ROLLBACK");
20 0 : }
21 : }
22 :
23 : impl<'a> Transaction<'a> {
24 0 : pub(crate) fn new(client: &'a mut Client) -> Transaction<'a> {
25 0 : Transaction {
26 0 : client,
27 0 : done: false,
28 0 : }
29 0 : }
30 :
31 : /// Consumes the transaction, committing all changes made within it.
32 0 : pub async fn commit(mut self) -> Result<ReadyForQueryStatus, Error> {
33 0 : self.done = true;
34 0 : self.client.batch_execute("COMMIT").await
35 0 : }
36 :
37 : /// Rolls the transaction back, discarding all changes made within it.
38 : ///
39 : /// This is equivalent to `Transaction`'s `Drop` implementation, but provides any error encountered to the caller.
40 0 : pub async fn rollback(mut self) -> Result<ReadyForQueryStatus, Error> {
41 0 : self.done = true;
42 0 : self.client.batch_execute("ROLLBACK").await
43 0 : }
44 :
45 : /// Like `Client::query_raw_txt`.
46 0 : pub async fn query_raw_txt<S, I>(
47 0 : &mut self,
48 0 : statement: &str,
49 0 : params: I,
50 0 : ) -> Result<RowStream<'_>, Error>
51 0 : where
52 0 : S: AsRef<str>,
53 0 : I: IntoIterator<Item = Option<S>>,
54 0 : I::IntoIter: ExactSizeIterator,
55 0 : {
56 0 : self.client.query_raw_txt(statement, params).await
57 0 : }
58 :
59 : /// Like `Client::cancel_token`.
60 0 : pub fn cancel_token(&self) -> CancelToken {
61 0 : self.client.cancel_token()
62 0 : }
63 :
64 : /// Returns a reference to the underlying `Client`.
65 0 : pub fn client(&self) -> &Client {
66 0 : self.client
67 0 : }
68 :
69 : /// Returns a reference to the underlying `Client`.
70 0 : pub fn client_mut(&mut self) -> &mut Client {
71 0 : self.client
72 0 : }
73 : }
|