grafeo_engine/database/
query.rs1#[cfg(any(feature = "cypher", feature = "sql-pgq", feature = "rdf"))]
4use std::sync::Arc;
5
6use grafeo_common::utils::error::Result;
7#[cfg(feature = "rdf")]
8use grafeo_core::graph::rdf::RdfStore;
9
10use super::{FromValue, QueryResult};
11
12impl super::GrafeoDB {
13 pub fn execute(&self, query: &str) -> Result<QueryResult> {
23 let session = self.session();
24 session.execute(query)
25 }
26
27 pub fn execute_at_epoch(
36 &self,
37 query: &str,
38 epoch: grafeo_common::types::EpochId,
39 ) -> Result<QueryResult> {
40 let session = self.session();
41 session.execute_at_epoch(query, epoch)
42 }
43
44 pub fn execute_with_params(
50 &self,
51 query: &str,
52 params: std::collections::HashMap<String, grafeo_common::types::Value>,
53 ) -> Result<QueryResult> {
54 let session = self.session();
55 session.execute_with_params(query, params)
56 }
57
58 #[cfg(feature = "cypher")]
64 pub fn execute_cypher(&self, query: &str) -> Result<QueryResult> {
65 let session = self.session();
66 session.execute_cypher(query)
67 }
68
69 #[cfg(feature = "cypher")]
75 pub fn execute_cypher_with_params(
76 &self,
77 query: &str,
78 params: std::collections::HashMap<String, grafeo_common::types::Value>,
79 ) -> Result<QueryResult> {
80 use crate::query::processor::{QueryLanguage, QueryProcessor};
81
82 let processor = QueryProcessor::for_lpg(Arc::clone(&self.store));
84 processor.process(query, QueryLanguage::Cypher, Some(¶ms))
85 }
86
87 #[cfg(feature = "gremlin")]
93 pub fn execute_gremlin(&self, query: &str) -> Result<QueryResult> {
94 let session = self.session();
95 session.execute_gremlin(query)
96 }
97
98 #[cfg(feature = "gremlin")]
104 pub fn execute_gremlin_with_params(
105 &self,
106 query: &str,
107 params: std::collections::HashMap<String, grafeo_common::types::Value>,
108 ) -> Result<QueryResult> {
109 let session = self.session();
110 session.execute_gremlin_with_params(query, params)
111 }
112
113 #[cfg(feature = "graphql")]
119 pub fn execute_graphql(&self, query: &str) -> Result<QueryResult> {
120 let session = self.session();
121 session.execute_graphql(query)
122 }
123
124 #[cfg(feature = "graphql")]
130 pub fn execute_graphql_with_params(
131 &self,
132 query: &str,
133 params: std::collections::HashMap<String, grafeo_common::types::Value>,
134 ) -> Result<QueryResult> {
135 let session = self.session();
136 session.execute_graphql_with_params(query, params)
137 }
138
139 #[cfg(feature = "sql-pgq")]
145 pub fn execute_sql(&self, query: &str) -> Result<QueryResult> {
146 let session = self.session();
147 session.execute_sql(query)
148 }
149
150 #[cfg(feature = "sql-pgq")]
156 pub fn execute_sql_with_params(
157 &self,
158 query: &str,
159 params: std::collections::HashMap<String, grafeo_common::types::Value>,
160 ) -> Result<QueryResult> {
161 use crate::query::processor::{QueryLanguage, QueryProcessor};
162
163 let processor = QueryProcessor::for_lpg(Arc::clone(&self.store));
165 processor.process(query, QueryLanguage::SqlPgq, Some(¶ms))
166 }
167
168 #[cfg(all(feature = "sparql", feature = "rdf"))]
188 pub fn execute_sparql(&self, query: &str) -> Result<QueryResult> {
189 use crate::query::{
190 Executor, optimizer::Optimizer, planner::rdf::RdfPlanner, translators::sparql,
191 };
192
193 let logical_plan = sparql::translate(query)?;
195
196 let optimizer = Optimizer::from_store(&self.store);
198 let optimized_plan = optimizer.optimize(logical_plan)?;
199
200 let planner = RdfPlanner::new(Arc::clone(&self.rdf_store));
202 let mut physical_plan = planner.plan(&optimized_plan)?;
203
204 let executor = Executor::with_columns(physical_plan.columns.clone());
206 executor.execute(physical_plan.operator.as_mut())
207 }
208
209 pub fn execute_language(
219 &self,
220 query: &str,
221 language: &str,
222 params: Option<std::collections::HashMap<String, grafeo_common::types::Value>>,
223 ) -> Result<QueryResult> {
224 let session = self.session();
225 session.execute_language(query, language, params)
226 }
227
228 #[cfg(feature = "rdf")]
232 #[must_use]
233 pub fn rdf_store(&self) -> &Arc<RdfStore> {
234 &self.rdf_store
235 }
236
237 pub fn query_scalar<T: FromValue>(&self, query: &str) -> Result<T> {
243 let result = self.execute(query)?;
244 result.scalar()
245 }
246}