Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add AnyQuery trait to support custom sql builders #1226

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion sqlx-core/src/any/connection/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::any::connection::AnyConnectionKind;
use crate::any::{
Any, AnyColumn, AnyConnection, AnyQueryResult, AnyRow, AnyStatement, AnyTypeInfo,
};
use crate::any_query::AnyKind;
use crate::database::Database;
use crate::describe::Describe;
use crate::error::Error;
Expand All @@ -11,6 +12,19 @@ use futures_core::future::BoxFuture;
use futures_core::stream::BoxStream;
use futures_util::{StreamExt, TryStreamExt};

fn get_kind(connection_kind: &AnyConnectionKind) -> AnyKind {
match connection_kind {
#[cfg(feature = "postgres")]
AnyConnectionKind::Postgres(_) => AnyKind::Postgres,
#[cfg(feature = "mysql")]
AnyConnectionKind::MySql(_) => AnyKind::MySql,
#[cfg(feature = "sqlite")]
AnyConnectionKind::Sqlite(_) => AnyKind::Sqlite,
#[cfg(feature = "mssql")]
AnyConnectionKind::Mssql(_) => AnyKind::Mssql,
}
}

impl<'c> Executor<'c> for &'c mut AnyConnection {
type Database = Any;

Expand All @@ -23,7 +37,7 @@ impl<'c> Executor<'c> for &'c mut AnyConnection {
E: Execute<'q, Self::Database>,
{
let arguments = query.take_arguments();
let query = query.sql();
let query = query.sql_for_kind(get_kind(&self.0));

match &mut self.0 {
#[cfg(feature = "postgres")]
Expand Down
5 changes: 4 additions & 1 deletion sqlx-core/src/any/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ mod arguments;
pub(crate) mod column;
mod connection;
mod database;
mod kind;
mod options;
mod query_result;
pub(crate) mod row;
Expand Down Expand Up @@ -45,6 +44,10 @@ pub type AnyPool = crate::pool::Pool<Any>;

pub type AnyPoolOptions = crate::pool::PoolOptions<Any>;

mod kind {
pub type AnyKind = crate::any_query::AnyKind;
}

// NOTE: required due to the lack of lazy normalization
impl_into_arguments_for_arguments!(AnyArguments<'q>);
impl_executor_for_pool_connection!(Any, AnyConnection, AnyRow);
Expand Down
10 changes: 10 additions & 0 deletions sqlx-core/src/any/kind.rs → sqlx-core/src/any_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,13 @@ impl FromStr for AnyKind {
}
}
}

pub trait AnyQuery<'q> : Send + Sized + std::marker::Sync {
fn build(&self, kind: Option<AnyKind>) -> &'q str;
}

impl<'q> AnyQuery<'q> for &'q str {
fn build(&self, _kind: Option<AnyKind>) -> &'q str {
self
}
}
28 changes: 24 additions & 4 deletions sqlx-core/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::database::{Database, HasArguments, HasStatement};
use crate::describe::Describe;
use crate::error::Error;
use crate::any_query::{AnyKind, AnyQuery};
use either::Either;
use futures_core::future::BoxFuture;
use futures_core::stream::BoxStream;
Expand Down Expand Up @@ -186,6 +187,11 @@ pub trait Execute<'q, DB: Database>: Send + Sized {
/// Gets the SQL that will be executed.
fn sql(&self) -> &'q str;

/// Gets the SQL that will be executed for a given driver.
fn sql_for_kind(&self, _kind: AnyKind) -> &'q str {
self.sql()
}

/// Gets the previously cached statement, if available.
fn statement(&self) -> Option<&<DB as HasStatement<'q>>::Statement>;

Expand All @@ -202,10 +208,17 @@ pub trait Execute<'q, DB: Database>: Send + Sized {

// NOTE: `Execute` is explicitly not implemented for String and &String to make it slightly more
// involved to write `conn.execute(format!("SELECT {}", val))`
impl<'q, DB: Database> Execute<'q, DB> for &'q str {
impl<'q, DB: Database, Q> Execute<'q, DB> for Q
where Q: AnyQuery<'q>
{
#[inline]
fn sql(&self) -> &'q str {
self
self.build(None)
}

#[inline]
fn sql_for_kind(&self, kind: AnyKind) -> &'q str {
self.build(Some(kind))
}

#[inline]
Expand All @@ -224,10 +237,17 @@ impl<'q, DB: Database> Execute<'q, DB> for &'q str {
}
}

impl<'q, DB: Database> Execute<'q, DB> for (&'q str, Option<<DB as HasArguments<'q>>::Arguments>) {
impl<'q, DB: Database, Q> Execute<'q, DB> for (Q, Option<<DB as HasArguments<'q>>::Arguments>)
where Q: AnyQuery<'q>
{
#[inline]
fn sql(&self) -> &'q str {
self.0
self.0.build(None)
}

#[inline]
fn sql_for_kind(&self, kind: AnyKind) -> &'q str {
self.0.build(Some(kind))
}

#[inline]
Expand Down
1 change: 1 addition & 0 deletions sqlx-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub mod column;
#[macro_use]
pub mod statement;

pub mod any_query;
mod common;
pub mod database;
pub mod describe;
Expand Down