1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//! Query execution methods for GrafeoDB.
use grafeo_common::utils::error::Result;
use super::{FromValue, QueryResult};
impl super::GrafeoDB {
/// Executes a closure with a one-shot session, syncing graph context back
/// to the database afterward. This ensures `USE GRAPH`, `SESSION SET GRAPH`,
/// and `SESSION RESET` persist across one-shot `execute()` calls.
fn with_session<F>(&self, func: F) -> Result<QueryResult>
where
F: FnOnce(&crate::session::Session) -> Result<QueryResult>,
{
let session = self.session();
let result = func(&session);
// Sync graph and schema state back, even on error (the session command may
// have succeeded before a subsequent query failed in the same session).
*self.current_graph.write() = session.current_graph();
*self.current_schema.write() = session.current_schema();
result
}
/// Runs a query directly on the database.
///
/// A convenience method that creates a temporary session behind the
/// scenes. If you're running multiple queries, grab a
/// [`session()`](Self::session) instead to avoid the overhead.
///
/// Graph context commands (`USE GRAPH`, `SESSION SET GRAPH`, `SESSION RESET`)
/// persist across calls: running `execute("USE GRAPH analytics")` followed
/// by `execute("MATCH (n) RETURN n")` routes the second query to the
/// analytics graph.
///
/// # Errors
///
/// Returns an error if parsing or execution fails.
pub fn execute(&self, query: &str) -> Result<QueryResult> {
self.with_session(|s| s.execute(query))
}
/// Runs a read-only GQL query and returns a lazy result stream.
///
/// The stream pulls chunks from the operator pipeline on demand, so
/// memory usage is bounded regardless of result-set size. Rejects
/// mutations, EXPLAIN/PROFILE, schema/session commands, and queries
/// whose planner emits a push-based pipeline (ORDER BY, aggregate,
/// DISTINCT, etc.). Use [`execute`](Self::execute) for those.
///
/// # Stability: Experimental
///
/// New in 0.5.40. Signature may change before being promoted to Beta.
///
/// # Errors
///
/// Returns an error if parsing or planning fails, or if the query is a
/// kind that cannot be streamed.
#[cfg(all(feature = "gql", feature = "lpg"))]
pub fn execute_streaming(
&self,
query: &str,
) -> Result<crate::query::executor::stream::OwnedResultStream> {
use crate::query::executor::stream::OwnedResultStream;
let session = self.session();
let (source, columns, deadline) = session.build_streaming_plan(query)?;
// Sync graph/schema state back (parser may have changed the session's
// view, although streaming rejects session commands so this is a
// defensive no-op today).
*self.current_graph.write() = session.current_graph();
*self.current_schema.write() = session.current_schema();
Ok(OwnedResultStream::new(source, columns, deadline))
}
/// Executes a GQL query with visibility at the specified epoch.
///
/// This enables time-travel queries: the query sees the database
/// as it existed at the given epoch.
///
/// # Errors
///
/// Returns an error if parsing or execution fails.
#[cfg(feature = "gql")]
pub fn execute_at_epoch(
&self,
query: &str,
epoch: grafeo_common::types::EpochId,
) -> Result<QueryResult> {
self.with_session(|s| s.execute_at_epoch(query, epoch))
}
/// Executes a query with parameters and returns the result.
///
/// # Errors
///
/// Returns an error if the query fails.
pub fn execute_with_params(
&self,
query: &str,
params: std::collections::HashMap<String, grafeo_common::types::Value>,
) -> Result<QueryResult> {
self.with_session(|s| s.execute_with_params(query, params))
}
/// Executes a Cypher query and returns the result.
///
/// # Errors
///
/// Returns an error if the query fails.
#[cfg(feature = "cypher")]
pub fn execute_cypher(&self, query: &str) -> Result<QueryResult> {
self.with_session(|s| s.execute_cypher(query))
}
/// Executes a Cypher query with parameters and returns the result.
///
/// # Errors
///
/// Returns an error if the query fails.
#[cfg(feature = "cypher")]
pub fn execute_cypher_with_params(
&self,
query: &str,
params: std::collections::HashMap<String, grafeo_common::types::Value>,
) -> Result<QueryResult> {
self.with_session(|s| s.execute_language(query, "cypher", Some(params)))
}
/// Executes a Gremlin query and returns the result.
///
/// # Errors
///
/// Returns an error if the query fails.
#[cfg(feature = "gremlin")]
pub fn execute_gremlin(&self, query: &str) -> Result<QueryResult> {
self.with_session(|s| s.execute_gremlin(query))
}
/// Executes a Gremlin query with parameters and returns the result.
///
/// # Errors
///
/// Returns an error if the query fails.
#[cfg(feature = "gremlin")]
pub fn execute_gremlin_with_params(
&self,
query: &str,
params: std::collections::HashMap<String, grafeo_common::types::Value>,
) -> Result<QueryResult> {
self.with_session(|s| s.execute_gremlin_with_params(query, params))
}
/// Executes a GraphQL query and returns the result.
///
/// # Errors
///
/// Returns an error if the query fails.
#[cfg(feature = "graphql")]
pub fn execute_graphql(&self, query: &str) -> Result<QueryResult> {
self.with_session(|s| s.execute_graphql(query))
}
/// Executes a GraphQL query with parameters and returns the result.
///
/// # Errors
///
/// Returns an error if the query fails.
#[cfg(feature = "graphql")]
pub fn execute_graphql_with_params(
&self,
query: &str,
params: std::collections::HashMap<String, grafeo_common::types::Value>,
) -> Result<QueryResult> {
self.with_session(|s| s.execute_graphql_with_params(query, params))
}
/// Executes a SQL/PGQ query (SQL:2023 GRAPH_TABLE) and returns the result.
///
/// # Errors
///
/// Returns an error if the query fails.
#[cfg(feature = "sql-pgq")]
pub fn execute_sql(&self, query: &str) -> Result<QueryResult> {
self.with_session(|s| s.execute_sql(query))
}
/// Executes a SQL/PGQ query with parameters and returns the result.
///
/// # Errors
///
/// Returns an error if the query fails.
#[cfg(feature = "sql-pgq")]
pub fn execute_sql_with_params(
&self,
query: &str,
params: std::collections::HashMap<String, grafeo_common::types::Value>,
) -> Result<QueryResult> {
self.with_session(|s| s.execute_sql_with_params(query, params))
}
/// Executes a query in the specified language by name.
///
/// Supported language names: `"gql"`, `"cypher"`, `"gremlin"`, `"graphql"`,
/// `"sparql"`, `"sql"`. Each requires the corresponding feature flag.
///
/// # Errors
///
/// Returns an error if the language is unknown/disabled, or if the query
/// fails.
pub fn execute_language(
&self,
query: &str,
language: &str,
params: Option<std::collections::HashMap<String, grafeo_common::types::Value>>,
) -> Result<QueryResult> {
self.with_session(|s| s.execute_language(query, language, params))
}
/// Executes a query and returns a single scalar value.
///
/// # Errors
///
/// Returns an error if the query fails or doesn't return exactly one row.
pub fn query_scalar<T: FromValue>(&self, query: &str) -> Result<T> {
let result = self.execute(query)?;
result.scalar()
}
}