diff --git a/edgedb-tokio/src/lib.rs b/edgedb-tokio/src/lib.rs index 6cb60db8..92751240 100644 --- a/edgedb-tokio/src/lib.rs +++ b/edgedb-tokio/src/lib.rs @@ -137,6 +137,7 @@ mod sealed; pub mod state; mod transaction; pub mod tutorial; +mod query_executor; pub use edgedb_derive::{ConfigDelta, GlobalsDelta, Queryable}; @@ -147,6 +148,7 @@ pub use errors::Error; pub use options::{RetryCondition, RetryOptions, TransactionOptions}; pub use state::{ConfigDelta, GlobalsDelta}; pub use transaction::Transaction; +pub use query_executor::QueryExecutor; #[cfg(feature = "unstable")] pub use builder::get_project_dir; diff --git a/edgedb-tokio/src/query_executor.rs b/edgedb-tokio/src/query_executor.rs new file mode 100644 index 00000000..52737459 --- /dev/null +++ b/edgedb-tokio/src/query_executor.rs @@ -0,0 +1,192 @@ +use edgedb_protocol::model::Json; +use edgedb_protocol::query_arg::QueryArgs; +use edgedb_protocol::QueryResult; +use std::future::Future; + +use crate::{Client, Error, Transaction}; + +/// Abstracts over different query executors +/// In particular &Client and &mut Transaction +pub trait QueryExecutor { + /// see [Client::query] + fn query(self, query: &str, arguments: &A) -> impl Future, Error>> + where + A: QueryArgs, + R: QueryResult; + + /// see [Client::query_single] + fn query_single( + self, + query: &str, + arguments: &A, + ) -> impl Future, Error>> + where + A: QueryArgs, + R: QueryResult; + + /// see [Client::query_required_single] + fn query_required_single( + self, + query: &str, + arguments: &A, + ) -> impl Future> + where + A: QueryArgs, + R: QueryResult; + + /// see [Client::query_json] + fn query_json( + self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future>; + + /// see [Client::query_single_json] + fn query_single_json( + &mut self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future, Error>>; + + /// see [Client::query_required_single_json] + fn query_required_single_json( + &mut self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future>; + + /// see [Client::execute] + fn execute(&mut self, query: &str, arguments: &A) -> impl Future> + where + A: QueryArgs; +} + +impl QueryExecutor for &Client { + fn query(self, query: &str, arguments: &A) -> impl Future, Error>> + where + A: QueryArgs, + R: QueryResult, + { + Client::query(self, query, arguments) + } + + fn query_single( + self, + query: &str, + arguments: &A, + ) -> impl Future, Error>> + where + A: QueryArgs, + R: QueryResult, + { + Client::query_single(self, query, arguments) + } + + fn query_required_single( + self, + query: &str, + arguments: &A, + ) -> impl Future> + where + A: QueryArgs, + R: QueryResult, + { + Client::query_required_single(self, query, arguments) + } + + fn query_json( + self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future> { + Client::query_json(self, query, arguments) + } + + fn query_single_json( + &mut self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future, Error>> { + Client::query_single_json(self, query, arguments) + } + + fn query_required_single_json( + &mut self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future> { + Client::query_required_single_json(self, query, arguments) + } + + fn execute(&mut self, query: &str, arguments: &A) -> impl Future> + where + A: QueryArgs, + { + Client::execute(self, query, arguments) + } +} + +impl QueryExecutor for &mut Transaction { + fn query(self, query: &str, arguments: &A) -> impl Future, Error>> + where + A: QueryArgs, + R: QueryResult, + { + Transaction::query(self, query, arguments) + } + + fn query_single( + self, + query: &str, + arguments: &A, + ) -> impl Future, Error>> + where + A: QueryArgs, + R: QueryResult, + { + Transaction::query_single(self, query, arguments) + } + + fn query_required_single( + self, + query: &str, + arguments: &A, + ) -> impl Future> + where + A: QueryArgs, + R: QueryResult, + { + Transaction::query_required_single(self, query, arguments) + } + + fn query_json( + self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future> { + Transaction::query_json(self, query, arguments) + } + + fn query_single_json( + &mut self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future, Error>> { + Transaction::query_single_json(self, query, arguments) + } + + fn query_required_single_json( + &mut self, + query: &str, + arguments: &impl QueryArgs, + ) -> impl Future> { + Transaction::query_required_single_json(self, query, arguments) + } + + fn execute(&mut self, query: &str, arguments: &A) -> impl Future> + where + A: QueryArgs, + { + Transaction::execute(self, query, arguments) + } +}