typedb_driver/transaction/query.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
20use std::pin::Pin;
21
22#[cfg(not(feature = "sync"))]
23use futures::TryStreamExt;
24#[cfg(feature = "sync")]
25use itertools::Itertools;
26
27use crate::{
28 answer::{ConceptMap, ConceptMapGroup, Explainable, ValueGroup, JSON},
29 common::{stream::Stream, Promise, Result},
30 concept::Value,
31 connection::TransactionStream,
32 logic::Explanation,
33 Options,
34};
35
36/// Provides methods for executing TypeQL queries in the transaction.
37#[derive(Debug)]
38pub struct QueryManager<'tx> {
39 transaction_stream: Pin<&'tx TransactionStream>,
40}
41
42impl<'tx> QueryManager<'tx> {
43 pub(super) fn new(transaction_stream: Pin<&'tx TransactionStream>) -> Self {
44 Self { transaction_stream }
45 }
46
47 /// Performs a TypeQL Define query with default options.
48 /// See [`QueryManager::define_with_options`]
49 pub fn define(&self, query: &str) -> impl Promise<'tx, Result> {
50 self.define_with_options(query, Options::new())
51 }
52
53 /// Performs a TypeQL Define query in the transaction.
54 ///
55 /// # Arguments
56 ///
57 /// * `query` -- The TypeQL Define query to be executed
58 /// * `options` -- Specify query options
59 ///
60 /// # Examples
61 ///
62 /// ```rust
63 #[cfg_attr(feature = "sync", doc = "transaction.query().define_with_options(query, options).resolve()")]
64 #[cfg_attr(not(feature = "sync"), doc = "transaction.query().define_with_options(query, options).await")]
65 /// ```
66 pub fn define_with_options(&self, query: &str, options: Options) -> impl Promise<'tx, Result> {
67 self.transaction_stream.get_ref().define(query.to_string(), options)
68 }
69
70 /// Performs a TypeQL Undefine query with default options
71 /// See [`QueryManager::undefine_with_options`]
72 pub fn undefine(&self, query: &str) -> impl Promise<'tx, Result> {
73 self.undefine_with_options(query, Options::new())
74 }
75
76 /// Performs a TypeQL Undefine query in the transaction.
77 ///
78 /// # Arguments
79 ///
80 /// * `query` -- The TypeQL Undefine query to be executed
81 /// * `options` -- Specify query options
82 ///
83 /// # Examples
84 ///
85 /// ```rust
86 #[cfg_attr(feature = "sync", doc = "transaction.query().undefine_with_options(query, options).resolve()")]
87 #[cfg_attr(not(feature = "sync"), doc = "transaction.query().undefine_with_options(query, options).await")]
88 /// ```
89 pub fn undefine_with_options(&self, query: &str, options: Options) -> impl Promise<'tx, Result> {
90 self.transaction_stream.get_ref().undefine(query.to_string(), options)
91 }
92
93 /// Performs a TypeQL Delete query with default options.
94 /// See [`QueryManager::delete_with_options`]
95 pub fn delete(&self, query: &str) -> impl Promise<'tx, Result> {
96 self.delete_with_options(query, Options::new())
97 }
98
99 /// Performs a TypeQL Delete query in the transaction.
100 ///
101 /// # Arguments
102 ///
103 /// * `query` -- The TypeQL Delete query to be executed
104 /// * `options` -- Specify query options
105 ///
106 /// # Examples
107 ///
108 /// ```rust
109 #[cfg_attr(feature = "sync", doc = "transaction.query().delete_with_options(query, options).resolve()")]
110 #[cfg_attr(not(feature = "sync"), doc = "transaction.query().delete_with_options(query, options).await")]
111 /// ```
112 pub fn delete_with_options(&self, query: &str, options: Options) -> impl Promise<'tx, Result> {
113 self.transaction_stream.get_ref().delete(query.to_string(), options)
114 }
115
116 /// Performs a TypeQL Match (Get) query with default options.
117 /// See [`QueryManager::get_with_options`]
118 pub fn get(&self, query: &str) -> Result<impl Stream<Item = Result<ConceptMap>> + 'tx> {
119 self.get_with_options(query, Options::new())
120 }
121
122 /// Performs a TypeQL Match (Get) query in the transaction.
123 ///
124 /// # Arguments
125 ///
126 /// * `query` -- The TypeQL Match (Get) query to be executed
127 /// * `options` -- Specify query options
128 ///
129 /// # Examples
130 ///
131 /// ```rust
132 /// transaction.query().get_with_options(query, options)
133 /// ```
134 pub fn get_with_options(
135 &self,
136 query: &str,
137 options: Options,
138 ) -> Result<impl Stream<Item = Result<ConceptMap>> + 'tx> {
139 self.transaction_stream.get_ref().get(query.to_string(), options)
140 }
141
142 /// Performs a TypeQL Insert query with default options.
143 /// See [`QueryManager::insert_with_options`]
144 pub fn insert(&self, query: &str) -> Result<impl Stream<Item = Result<ConceptMap>> + 'tx> {
145 self.insert_with_options(query, Options::new())
146 }
147
148 /// Performs a TypeQL Insert query in the transaction.
149 ///
150 /// # Arguments
151 ///
152 /// * `query` -- The TypeQL Insert query to be executed
153 /// * `options` -- Specify query options
154 ///
155 /// # Examples
156 ///
157 /// ```rust
158 /// transaction.query().insert_with_options(query, options)
159 /// ```
160 pub fn insert_with_options(
161 &self,
162 query: &str,
163 options: Options,
164 ) -> Result<impl Stream<Item = Result<ConceptMap>> + 'tx> {
165 self.transaction_stream.get_ref().insert(query.to_string(), options)
166 }
167
168 /// Performs a TypeQL Update query with default options.
169 /// See [`QueryManager::update_with_options`]
170 pub fn update(&self, query: &str) -> Result<impl Stream<Item = Result<ConceptMap>> + 'tx> {
171 self.update_with_options(query, Options::new())
172 }
173
174 /// Performs a TypeQL Update query in the transaction.
175 ///
176 /// # Arguments
177 ///
178 /// * `query` -- The TypeQL Update query to be executed
179 /// * `options` -- Specify query options
180 ///
181 /// # Examples
182 ///
183 /// ```rust
184 /// transaction.query().update_with_options(query, options)
185 /// ```
186 pub fn update_with_options(
187 &self,
188 query: &str,
189 options: Options,
190 ) -> Result<impl Stream<Item = Result<ConceptMap>> + 'tx> {
191 self.transaction_stream.get_ref().update(query.to_string(), options)
192 }
193
194 /// Performs a TypeQL Match Aggregate query with default options.
195 /// See [`QueryManager::get_aggregate`]
196 pub fn get_aggregate(&self, query: &str) -> impl Promise<'tx, Result<Option<Value>>> {
197 self.get_aggregate_with_options(query, Options::new())
198 }
199
200 /// Performs a TypeQL Match Aggregate query in the transaction.
201 ///
202 /// # Arguments
203 ///
204 /// * `query` -- The TypeQL Match Aggregate query to be executed
205 /// * `options` -- Specify query options
206 ///
207 /// # Examples
208 ///
209 /// ```rust
210 #[cfg_attr(feature = "sync", doc = "transaction.query().get_aggregate_with_options(query, options).resolve()")]
211 #[cfg_attr(not(feature = "sync"), doc = "transaction.query().get_aggregate_with_options(query, options).await")]
212 /// ```
213 pub fn get_aggregate_with_options(
214 &self,
215 query: &str,
216 options: Options,
217 ) -> impl Promise<'tx, Result<Option<Value>>> {
218 self.transaction_stream.get_ref().get_aggregate(query.to_string(), options)
219 }
220
221 /// Performs a TypeQL Match Group query with default options.
222 /// See [`QueryManager::get_group`]
223 pub fn get_group(&self, query: &str) -> Result<impl Stream<Item = Result<ConceptMapGroup>> + 'tx> {
224 self.get_group_with_options(query, Options::new())
225 }
226
227 /// Performs a TypeQL Match Group query in the transaction.
228 ///
229 /// # Arguments
230 ///
231 /// * `query` -- The TypeQL Match Group query to be executed
232 /// * `options` -- Specify query options
233 ///
234 /// # Examples
235 ///
236 /// ```rust
237 /// transaction.query().get_group_with_options(query, options)
238 /// ```
239 pub fn get_group_with_options(
240 &self,
241 query: &str,
242 options: Options,
243 ) -> Result<impl Stream<Item = Result<ConceptMapGroup>> + 'tx> {
244 self.transaction_stream.get_ref().get_group(query.to_string(), options)
245 }
246
247 /// Performs a TypeQL Match Group Aggregate query with default options.
248 /// See [`QueryManager::get_group_aggregate_with_options`]
249 pub fn get_group_aggregate(&self, query: &str) -> Result<impl Stream<Item = Result<ValueGroup>> + 'tx> {
250 self.get_group_aggregate_with_options(query, Options::new())
251 }
252
253 /// Performs a TypeQL Match Group Aggregate query in the transaction.
254 ///
255 /// # Arguments
256 ///
257 /// * `query` -- The TypeQL Match Group Aggregate query to be executed
258 /// * `options` -- Specify query options
259 ///
260 /// # Examples
261 ///
262 /// ```rust
263 /// transaction.query().get_group_aggregate_with_options(query, options)
264 /// ```
265 pub fn get_group_aggregate_with_options(
266 &self,
267 query: &str,
268 options: Options,
269 ) -> Result<impl Stream<Item = Result<ValueGroup>> + 'tx> {
270 self.transaction_stream.get_ref().get_group_aggregate(query.to_string(), options)
271 }
272
273 /// Performs a TypeQL Fetch query with default options.
274 /// See [`QueryManager::fetch_with_options`]
275 pub fn fetch(&self, query: &str) -> Result<impl Stream<Item = Result<JSON>> + 'tx> {
276 self.fetch_with_options(query, Options::new())
277 }
278
279 /// Performs a TypeQL Match Group Aggregate query in the transaction.
280 ///
281 /// # Arguments
282 ///
283 /// * `query` -- The TypeQL Match Group Aggregate query to be executed
284 /// * `options` -- Specify query options
285 ///
286 /// # Examples
287 ///
288 /// ```rust
289 /// transaction.query().fetch_with_options(query, options)
290 /// ```
291 pub fn fetch_with_options(&self, query: &str, options: Options) -> Result<impl Stream<Item = Result<JSON>> + 'tx> {
292 Ok(self.transaction_stream.get_ref().fetch(query.to_string(), options)?.map_ok(|tree| tree.into_json()))
293 }
294
295 /// Performs a TypeQL Explain query in the transaction.
296 /// See [``QueryManager::explain_with_options]
297 pub fn explain(&self, explainable: &Explainable) -> Result<impl Stream<Item = Result<Explanation>> + 'tx> {
298 self.explain_with_options(explainable, Options::new())
299 }
300
301 /// Performs a TypeQL Explain query in the transaction.
302 ///
303 /// # Arguments
304 ///
305 /// * `explainable` -- The Explainable to be explained
306 /// * `options` -- Specify query options
307 ///
308 /// # Examples
309 ///
310 /// ```rust
311 /// transaction.query().explain_with_options(explainable, options)
312 /// ```
313 pub fn explain_with_options(
314 &self,
315 explainable: &Explainable,
316 options: Options,
317 ) -> Result<impl Stream<Item = Result<Explanation>> + 'tx> {
318 self.transaction_stream.get_ref().explain(explainable.id, options)
319 }
320}