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_with_params(
33 &self,
34 query: &str,
35 params: std::collections::HashMap<String, grafeo_common::types::Value>,
36 ) -> Result<QueryResult> {
37 let session = self.session();
38 session.execute_with_params(query, params)
39 }
40
41 #[cfg(feature = "cypher")]
47 pub fn execute_cypher(&self, query: &str) -> Result<QueryResult> {
48 let session = self.session();
49 session.execute_cypher(query)
50 }
51
52 #[cfg(feature = "cypher")]
58 pub fn execute_cypher_with_params(
59 &self,
60 query: &str,
61 params: std::collections::HashMap<String, grafeo_common::types::Value>,
62 ) -> Result<QueryResult> {
63 use crate::query::processor::{QueryLanguage, QueryProcessor};
64
65 let processor = QueryProcessor::for_lpg(Arc::clone(&self.store));
67 processor.process(query, QueryLanguage::Cypher, Some(¶ms))
68 }
69
70 #[cfg(feature = "gremlin")]
76 pub fn execute_gremlin(&self, query: &str) -> Result<QueryResult> {
77 let session = self.session();
78 session.execute_gremlin(query)
79 }
80
81 #[cfg(feature = "gremlin")]
87 pub fn execute_gremlin_with_params(
88 &self,
89 query: &str,
90 params: std::collections::HashMap<String, grafeo_common::types::Value>,
91 ) -> Result<QueryResult> {
92 let session = self.session();
93 session.execute_gremlin_with_params(query, params)
94 }
95
96 #[cfg(feature = "graphql")]
102 pub fn execute_graphql(&self, query: &str) -> Result<QueryResult> {
103 let session = self.session();
104 session.execute_graphql(query)
105 }
106
107 #[cfg(feature = "graphql")]
113 pub fn execute_graphql_with_params(
114 &self,
115 query: &str,
116 params: std::collections::HashMap<String, grafeo_common::types::Value>,
117 ) -> Result<QueryResult> {
118 let session = self.session();
119 session.execute_graphql_with_params(query, params)
120 }
121
122 #[cfg(feature = "sql-pgq")]
128 pub fn execute_sql(&self, query: &str) -> Result<QueryResult> {
129 let session = self.session();
130 session.execute_sql(query)
131 }
132
133 #[cfg(feature = "sql-pgq")]
139 pub fn execute_sql_with_params(
140 &self,
141 query: &str,
142 params: std::collections::HashMap<String, grafeo_common::types::Value>,
143 ) -> Result<QueryResult> {
144 use crate::query::processor::{QueryLanguage, QueryProcessor};
145
146 let processor = QueryProcessor::for_lpg(Arc::clone(&self.store));
148 processor.process(query, QueryLanguage::SqlPgq, Some(¶ms))
149 }
150
151 #[cfg(all(feature = "sparql", feature = "rdf"))]
171 pub fn execute_sparql(&self, query: &str) -> Result<QueryResult> {
172 use crate::query::{
173 Executor, optimizer::Optimizer, planner_rdf::RdfPlanner, sparql_translator,
174 };
175
176 let logical_plan = sparql_translator::translate(query)?;
178
179 let optimizer = Optimizer::from_store(&self.store);
181 let optimized_plan = optimizer.optimize(logical_plan)?;
182
183 let planner = RdfPlanner::new(Arc::clone(&self.rdf_store));
185 let mut physical_plan = planner.plan(&optimized_plan)?;
186
187 let executor = Executor::with_columns(physical_plan.columns.clone());
189 executor.execute(physical_plan.operator.as_mut())
190 }
191
192 #[cfg(feature = "rdf")]
196 #[must_use]
197 pub fn rdf_store(&self) -> &Arc<RdfStore> {
198 &self.rdf_store
199 }
200
201 pub fn query_scalar<T: FromValue>(&self, query: &str) -> Result<T> {
207 let result = self.execute(query)?;
208 result.scalar()
209 }
210}