typedb_driver/transaction/mod.rs
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20pub mod concept;
21pub mod logic;
22pub mod query;
23
24use std::{fmt, marker::PhantomData, pin::Pin};
25
26use self::{concept::ConceptManager, logic::LogicManager, query::QueryManager};
27use crate::{
28 common::{Promise, Result, TransactionType},
29 connection::TransactionStream,
30 error::ConnectionError,
31 Options,
32};
33
34/// A transaction with a TypeDB database.
35pub struct Transaction<'a> {
36 /// The transaction’s type (READ or WRITE)
37 type_: TransactionType,
38 /// The options for the transaction
39 options: Options,
40 transaction_stream: Pin<Box<TransactionStream>>,
41
42 _lifetime_guard: PhantomData<&'a ()>,
43}
44
45impl Transaction<'_> {
46 pub(super) fn new(transaction_stream: TransactionStream) -> Self {
47 let transaction_stream = Box::pin(transaction_stream);
48 Transaction {
49 type_: transaction_stream.type_(),
50 options: transaction_stream.options(),
51 transaction_stream,
52 _lifetime_guard: PhantomData,
53 }
54 }
55
56 /// Closes the transaction.
57 ///
58 /// # Examples
59 ///
60 /// ```rust
61 /// transaction.close()
62 /// ```
63 pub fn is_open(&self) -> bool {
64 self.transaction_stream.is_open()
65 }
66
67 /// Retrieves the transaction’s type (READ or WRITE).
68 pub fn type_(&self) -> TransactionType {
69 self.type_
70 }
71
72 /// Retrieves the``QueryManager`` for this Transaction, from which any TypeQL query can be executed.
73 pub fn query(&self) -> QueryManager<'_> {
74 QueryManager::new(self.transaction_stream.as_ref())
75 }
76
77 /// The `ConceptManager` for this transaction, providing access to all Concept API methods.
78 pub fn concept(&self) -> ConceptManager<'_> {
79 ConceptManager::new(self.transaction_stream.as_ref())
80 }
81
82 /// Retrieves the `LogicManager` for this Transaction, providing access to all Concept API - Logic methods.
83 pub fn logic(&self) -> LogicManager<'_> {
84 LogicManager::new(self.transaction_stream.as_ref())
85 }
86
87 /// Registers a callback function which will be executed when this transaction is closed.
88 ///
89 /// # Arguments
90 ///
91 /// * `function` -- The callback function.
92 ///
93 /// # Examples
94 ///
95 /// ```rust
96 /// transaction.on_close(function)
97 /// ```
98 pub fn on_close(&self, callback: impl FnOnce(ConnectionError) + Send + Sync + 'static) {
99 self.transaction_stream.on_close(callback)
100 }
101
102 /// Closes the transaction.
103 ///
104 /// # Examples
105 ///
106 /// ```rust
107 /// transaction.force_close()
108 /// ```
109 pub fn force_close(&self) {
110 self.transaction_stream.force_close();
111 }
112
113 /// Commits the changes made via this transaction to the TypeDB database. Whether or not the transaction is commited successfully, it gets closed after the commit call.
114 ///
115 /// # Examples
116 ///
117 /// ```rust
118 #[cfg_attr(feature = "sync", doc = "transaction.commit()")]
119 #[cfg_attr(not(feature = "sync"), doc = "transaction.commit().await")]
120 /// ```
121 pub fn commit(self) -> impl Promise<'static, Result> {
122 let stream = self.transaction_stream;
123 stream.commit()
124 }
125
126 /// Rolls back the uncommitted changes made via this transaction.
127 ///
128 /// # Examples
129 ///
130 /// ```rust
131 #[cfg_attr(feature = "sync", doc = "transaction.rollback()")]
132 #[cfg_attr(not(feature = "sync"), doc = "transaction.rollback().await")]
133 /// ```
134 pub fn rollback(&self) -> impl Promise<'_, Result> {
135 self.transaction_stream.rollback()
136 }
137}
138
139impl fmt::Debug for Transaction<'_> {
140 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141 f.debug_struct("Transaction").field("type_", &self.type_).field("options", &self.options).finish()
142 }
143}