Skip to content

Commit

Permalink
Add QueryExecutor trait (#338)
Browse files Browse the repository at this point in the history
Co-authored-by: Aljaž Mur Eržen <aljaz@edgedb.com>
  • Loading branch information
CodesInChaos and aljazerzen authored Aug 28, 2024
1 parent 99de7ec commit 3c4cc04
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ debug = true
lto = true

[workspace.package]
rust-version = "1.72" # keep in sync with flake.nix
rust-version = "1.75" # keep in sync with flake.nix

[workspace.lints.clippy]
useless_format = 'allow'
Expand Down
2 changes: 2 additions & 0 deletions edgedb-tokio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ mod builder;
mod client;
mod errors;
mod options;
mod query_executor;
mod sealed;
pub mod state;
mod transaction;
Expand All @@ -145,6 +146,7 @@ pub use client::Client;
pub use credentials::TlsSecurity;
pub use errors::Error;
pub use options::{RetryCondition, RetryOptions, TransactionOptions};
pub use query_executor::QueryExecutor;
pub use state::{ConfigDelta, GlobalsDelta};
pub use transaction::Transaction;

Expand Down
200 changes: 200 additions & 0 deletions edgedb-tokio/src/query_executor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
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<R, A>(
self,
query: &str,
arguments: &A,
) -> impl Future<Output = Result<Vec<R>, Error>> + Send
where
A: QueryArgs,
R: QueryResult + Send;

/// see [Client::query_single]
fn query_single<R, A>(
self,
query: &str,
arguments: &A,
) -> impl Future<Output = Result<Option<R>, Error>> + Send
where
A: QueryArgs,
R: QueryResult + Send;

/// see [Client::query_required_single]
fn query_required_single<R, A>(
self,
query: &str,
arguments: &A,
) -> impl Future<Output = Result<R, Error>> + Send
where
A: QueryArgs,
R: QueryResult + Send;

/// see [Client::query_json]
fn query_json(
self,
query: &str,
arguments: &impl QueryArgs,
) -> impl Future<Output = Result<Json, Error>> + Send;

/// see [Client::query_single_json]
fn query_single_json(
&mut self,
query: &str,
arguments: &impl QueryArgs,
) -> impl Future<Output = Result<Option<Json>, Error>> + Send;

/// see [Client::query_required_single_json]
fn query_required_single_json(
&mut self,
query: &str,
arguments: &impl QueryArgs,
) -> impl Future<Output = Result<Json, Error>>;

/// see [Client::execute]
fn execute<A>(
&mut self,
query: &str,
arguments: &A,
) -> impl Future<Output = Result<(), Error>> + Send
where
A: QueryArgs;
}

impl QueryExecutor for &Client {
fn query<R, A>(self, query: &str, arguments: &A) -> impl Future<Output = Result<Vec<R>, Error>>
where
A: QueryArgs,
R: QueryResult,
{
Client::query(self, query, arguments)
}

fn query_single<R, A>(
self,
query: &str,
arguments: &A,
) -> impl Future<Output = Result<Option<R>, Error>>
where
A: QueryArgs,
R: QueryResult + Send,
{
Client::query_single(self, query, arguments)
}

fn query_required_single<R, A>(
self,
query: &str,
arguments: &A,
) -> impl Future<Output = Result<R, Error>> + Send
where
A: QueryArgs,
R: QueryResult + Send,
{
Client::query_required_single(self, query, arguments)
}

fn query_json(
self,
query: &str,
arguments: &impl QueryArgs,
) -> impl Future<Output = Result<Json, Error>> {
Client::query_json(self, query, arguments)
}

fn query_single_json(
&mut self,
query: &str,
arguments: &impl QueryArgs,
) -> impl Future<Output = Result<Option<Json>, Error>> {
Client::query_single_json(self, query, arguments)
}

fn query_required_single_json(
&mut self,
query: &str,
arguments: &impl QueryArgs,
) -> impl Future<Output = Result<Json, Error>> {
Client::query_required_single_json(self, query, arguments)
}

fn execute<A>(&mut self, query: &str, arguments: &A) -> impl Future<Output = Result<(), Error>>
where
A: QueryArgs,
{
Client::execute(self, query, arguments)
}
}

impl QueryExecutor for &mut Transaction {
fn query<R, A>(self, query: &str, arguments: &A) -> impl Future<Output = Result<Vec<R>, Error>>
where
A: QueryArgs,
R: QueryResult,
{
Transaction::query(self, query, arguments)
}

fn query_single<R, A>(
self,
query: &str,
arguments: &A,
) -> impl Future<Output = Result<Option<R>, Error>>
where
A: QueryArgs,
R: QueryResult,
{
Transaction::query_single(self, query, arguments)
}

fn query_required_single<R, A>(
self,
query: &str,
arguments: &A,
) -> impl Future<Output = Result<R, Error>>
where
A: QueryArgs,
R: QueryResult,
{
Transaction::query_required_single(self, query, arguments)
}

fn query_json(
self,
query: &str,
arguments: &impl QueryArgs,
) -> impl Future<Output = Result<Json, Error>> {
Transaction::query_json(self, query, arguments)
}

fn query_single_json(
&mut self,
query: &str,
arguments: &impl QueryArgs,
) -> impl Future<Output = Result<Option<Json>, Error>> {
Transaction::query_single_json(self, query, arguments)
}

fn query_required_single_json(
&mut self,
query: &str,
arguments: &impl QueryArgs,
) -> impl Future<Output = Result<Json, Error>> {
Transaction::query_required_single_json(self, query, arguments)
}

fn execute<A>(&mut self, query: &str, arguments: &A) -> impl Future<Output = Result<(), Error>>
where
A: QueryArgs,
{
Transaction::execute(self, query, arguments)
}
}
4 changes: 2 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
devShells.minimum = pkgs.mkShell {
buildInputs = [
(fenix_pkgs.toolchainOf {
channel = "1.72"; # keep in sync with ./Cargo.toml rust-version
sha256 = "sha256-dxE7lmCFWlq0nl/wKcmYvpP9zqQbBitAQgZ1zx9Ooik=";
channel = "1.75"; # keep in sync with ./Cargo.toml rust-version
sha256 = "sha256-SXRtAuO4IqNOQq+nLbrsDFbVk+3aVA8NNpSZsKlVH/8=";
}).defaultToolchain
] ++ common;
};
Expand Down

0 comments on commit 3c4cc04

Please sign in to comment.