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

add Aurora serverless (data api) driver #866

Closed
wants to merge 15 commits into from
553 changes: 535 additions & 18 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ postgres = [ "sqlx-core/postgres", "sqlx-macros/postgres" ]
mysql = [ "sqlx-core/mysql", "sqlx-macros/mysql" ]
sqlite = [ "sqlx-core/sqlite", "sqlx-macros/sqlite" ]
mssql = [ "sqlx-core/mssql", "sqlx-macros/mssql" ]
aurora = [ "sqlx-core/aurora" ]

# types
bigdecimal = [ "sqlx-core/bigdecimal", "sqlx-macros/bigdecimal" ]
Expand Down
1 change: 1 addition & 0 deletions sqlx-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ default = [ "postgres", "sqlite", "mysql" ]
mysql = [ "sqlx/mysql" ]
postgres = [ "sqlx/postgres" ]
sqlite = [ "sqlx/sqlite" ]
aurora = [ "sqlx/aurora" ]

# workaround for musl + openssl issues
openssl-vendored = [ "openssl/vendored" ]
3 changes: 3 additions & 0 deletions sqlx-cli/src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,8 @@ fn get_db_kind(url: &str) -> anyhow::Result<&'static str> {

#[cfg(feature = "mssql")]
AnyKind::Mssql => Ok("MSSQL"),

#[cfg(feature = "aurora")]
AnyKind::Aurora => Ok("Aurora"),
}
}
9 changes: 7 additions & 2 deletions sqlx-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ postgres = [ "md-5", "sha2", "base64", "sha-1", "rand", "hmac", "futures-channel
mysql = [ "sha-1", "sha2", "generic-array", "num-bigint", "base64", "digest", "rand", "rsa" ]
sqlite = [ "libsqlite3-sys" ]
mssql = [ "uuid", "encoding_rs", "regex" ]
aurora = [ "rusoto_core", "rusoto_rds_data", "regex" ]
any = []

# types
Expand All @@ -46,8 +47,8 @@ runtime-tokio-rustls = [ "sqlx-rt/runtime-tokio-rustls", "_tls-rustls", "_rt-tok
_rt-actix = []
_rt-async-std = []
_rt-tokio = []
_tls-native-tls = []
_tls-rustls = [ "rustls", "webpki", "webpki-roots" ]
_tls-native-tls = [ "rusoto_core/native-tls", "rusoto_rds_data/native-tls" ]
_tls-rustls = [ "rustls", "webpki", "webpki-roots", "rusoto_core/rustls", "rusoto_rds_data/rustls" ]
Comment on lines +50 to +51
Copy link
Author

@tarkah tarkah Dec 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this causes the deps to be included, even if "aurora" isn't. It seems there is a rustlang tracking issue for weak dependencies that could solve this, but isn't ready yet. Is there another workaround so we don't have 2 feature gates for aurora... "aurora-native-tls" / "aurora-rustls"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cross-reference: rust-lang/cargo#8818


# support offline/decoupled building (enables serialization of `Describe`)
offline = [ "serde", "either/serde" ]
Expand Down Expand Up @@ -107,3 +108,7 @@ webpki-roots = { version = "0.20.0", optional = true }
whoami = "0.9.0"
stringprep = "0.1.2"
lru-cache = "0.1.2"

# Auroroa dependencies
rusoto_core = { version = "0.45.0", default-features = false, optional = true }
rusoto_rds_data = { version = "0.45.0", default-features = false, features = ["serialize_structs", "deserialize_structs"], optional = true }
27 changes: 27 additions & 0 deletions sqlx-core/src/any/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ pub(crate) enum AnyArgumentBufferKind<'q> {
crate::mssql::MssqlArguments,
std::marker::PhantomData<&'q ()>,
),

#[cfg(feature = "aurora")]
Aurora(
crate::aurora::AuroraArguments,
std::marker::PhantomData<&'q ()>,
),
}

// control flow inferred type bounds would be fun
Expand Down Expand Up @@ -131,3 +137,24 @@ impl<'q> From<AnyArguments<'q>> for crate::postgres::PgArguments {
}
}
}

#[cfg(feature = "aurora")]
#[allow(irrefutable_let_patterns)]
impl<'q> From<AnyArguments<'q>> for crate::aurora::AuroraArguments {
fn from(args: AnyArguments<'q>) -> Self {
let mut buf = AnyArgumentBuffer(AnyArgumentBufferKind::Aurora(
Default::default(),
std::marker::PhantomData,
));

for value in args.values {
let _ = value.encode_by_ref(&mut buf);
}

if let AnyArgumentBufferKind::Aurora(args, _) = buf.0 {
args
} else {
unreachable!()
}
}
}
30 changes: 30 additions & 0 deletions sqlx-core/src/any/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use crate::sqlite::{SqliteColumn, SqliteRow, SqliteStatement};
#[cfg(feature = "mssql")]
use crate::mssql::{MssqlColumn, MssqlRow, MssqlStatement};

#[cfg(feature = "aurora")]
use crate::aurora::{AuroraColumn, AuroraRow, AuroraStatement};

#[derive(Debug, Clone)]
pub struct AnyColumn {
pub(crate) kind: AnyColumnKind,
Expand All @@ -34,6 +37,9 @@ pub(crate) enum AnyColumnKind {

#[cfg(feature = "mssql")]
Mssql(MssqlColumn),

#[cfg(feature = "aurora")]
Aurora(AuroraColumn),
}

impl Column for AnyColumn {
Expand All @@ -52,6 +58,9 @@ impl Column for AnyColumn {

#[cfg(feature = "mssql")]
AnyColumnKind::Mssql(row) => row.ordinal(),

#[cfg(feature = "aurora")]
AnyColumnKind::Aurora(row) => row.ordinal(),
}
}

Expand All @@ -68,6 +77,9 @@ impl Column for AnyColumn {

#[cfg(feature = "mssql")]
AnyColumnKind::Mssql(row) => row.name(),

#[cfg(feature = "aurora")]
AnyColumnKind::Aurora(row) => row.name(),
}
}

Expand Down Expand Up @@ -441,3 +453,21 @@ impl<I: ?Sized> AnyColumnIndex for I where
I: ColumnIndex<SqliteRow> + for<'q> ColumnIndex<SqliteStatement<'q>>
{
}

#[cfg(all(
not(any(feature = "mysql", feature = "mssql", feature = "postgres", feature = "sqlite")),
feature = "aurora"
))]
pub trait AnyColumnIndex:
ColumnIndex<AuroraRow> + for<'q> ColumnIndex<AuroraStatement<'q>>
{
}

#[cfg(all(
not(any(feature = "mysql", feature = "mssql", feature = "postgres", feature = "sqlite")),
feature = "aurora"
))]
impl<I: ?Sized> AnyColumnIndex for I where
I: ColumnIndex<AuroraRow> + for<'q> ColumnIndex<AuroraStatement<'q>>
{
}
7 changes: 7 additions & 0 deletions sqlx-core/src/any/connection/establish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ impl AnyConnection {
.await
.map(AnyConnectionKind::Mssql)
}

#[cfg(feature = "aurora")]
AnyConnectOptionsKind::Aurora(options) => {
crate::aurora::AuroraConnection::connect_with(options)
.await
.map(AnyConnectionKind::Aurora)
}
}
.map(AnyConnection)
}
Expand Down
18 changes: 18 additions & 0 deletions sqlx-core/src/any/connection/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ impl<'c> Executor<'c> for &'c mut AnyConnection {
.fetch_many((query, arguments.map(Into::into)))
.map_ok(|v| v.map_right(Into::into).map_left(Into::into))
.boxed(),

#[cfg(feature = "aurora")]
AnyConnectionKind::Aurora(conn) => conn
.fetch_many((query, arguments.map(Into::into)))
.map_ok(|v| v.map_right(Into::into).map_left(Into::into))
.boxed(),
}
}

Expand Down Expand Up @@ -86,6 +92,12 @@ impl<'c> Executor<'c> for &'c mut AnyConnection {
.fetch_optional((query, arguments.map(Into::into)))
.await?
.map(Into::into),

#[cfg(feature = "aurora")]
AnyConnectionKind::Aurora(conn) => conn
.fetch_optional((query, arguments.map(Into::into)))
.await?
.map(Into::into),
})
})
}
Expand All @@ -112,6 +124,9 @@ impl<'c> Executor<'c> for &'c mut AnyConnection {

#[cfg(feature = "mssql")]
AnyConnectionKind::Mssql(conn) => conn.prepare(sql).await.map(Into::into)?,

#[cfg(feature = "aurora")]
AnyConnectionKind::Aurora(conn) => conn.prepare(sql).await.map(Into::into)?,
})
})
}
Expand All @@ -136,6 +151,9 @@ impl<'c> Executor<'c> for &'c mut AnyConnection {

#[cfg(feature = "mssql")]
AnyConnectionKind::Mssql(conn) => conn.describe(sql).await.map(map_describe)?,

#[cfg(feature = "aurora")]
AnyConnectionKind::Aurora(conn) => conn.describe(sql).await.map(map_describe)?,
})
})
}
Expand Down
21 changes: 21 additions & 0 deletions sqlx-core/src/any/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ use crate::mssql;
use crate::mysql;
use crate::transaction::Transaction;

#[cfg(feature = "aurora")]
use crate::aurora;

mod establish;
mod executor;

Expand Down Expand Up @@ -45,6 +48,9 @@ pub(crate) enum AnyConnectionKind {

#[cfg(feature = "sqlite")]
Sqlite(sqlite::SqliteConnection),

#[cfg(feature = "aurora")]
Aurora(aurora::AuroraConnection),
}

macro_rules! delegate_to {
Expand All @@ -61,6 +67,9 @@ macro_rules! delegate_to {

#[cfg(feature = "mssql")]
AnyConnectionKind::Mssql(conn) => conn.$method($($arg),*),

#[cfg(feature = "aurora")]
AnyConnectionKind::Aurora(conn) => conn.$method($($arg),*),
}
};
}
Expand All @@ -79,6 +88,9 @@ macro_rules! delegate_to_mut {

#[cfg(feature = "mssql")]
AnyConnectionKind::Mssql(conn) => conn.$method($($arg),*),

#[cfg(feature = "aurora")]
AnyConnectionKind::Aurora(conn) => conn.$method($($arg),*),
}
};
}
Expand All @@ -101,6 +113,9 @@ impl Connection for AnyConnection {

#[cfg(feature = "mssql")]
AnyConnectionKind::Mssql(conn) => conn.close(),

#[cfg(feature = "aurora")]
AnyConnectionKind::Aurora(conn) => conn.close(),
}
}

Expand Down Expand Up @@ -129,6 +144,9 @@ impl Connection for AnyConnection {
// no cache
#[cfg(feature = "mssql")]
AnyConnectionKind::Mssql(_) => 0,

#[cfg(feature = "aurora")]
AnyConnectionKind::Aurora(conn) => conn.cached_statements_size(),
}
}

Expand All @@ -146,6 +164,9 @@ impl Connection for AnyConnection {
// no cache
#[cfg(feature = "mssql")]
AnyConnectionKind::Mssql(_) => Box::pin(futures_util::future::ok(())),

#[cfg(feature = "aurora")]
AnyConnectionKind::Aurora(conn) => conn.clear_cached_statements(),
}
}

Expand Down
20 changes: 20 additions & 0 deletions sqlx-core/src/any/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use crate::mssql::Mssql;
#[cfg(feature = "sqlite")]
use crate::sqlite::Sqlite;

#[cfg(feature = "aurora")]
use crate::aurora::Aurora;

// Implements Decode for any T where T supports Decode for any database that has support currently
// compiled into SQLx
macro_rules! impl_any_decode {
Expand Down Expand Up @@ -44,6 +47,11 @@ macro_rules! impl_any_decode {
crate::any::value::AnyValueRefKind::Postgres(value) => {
<$ty as crate::decode::Decode<'r, crate::postgres::Postgres>>::decode(value)
}

#[cfg(feature = "aurora")]
crate::any::value::AnyValueRefKind::Aurora(value) => {
<$ty as crate::decode::Decode<'r, crate::aurora::Aurora>>::decode(value)
}
}
}
}
Expand Down Expand Up @@ -361,3 +369,15 @@ pub trait AnyDecode<'r>: Decode<'r, Sqlite> + Type<Sqlite> {}
feature = "sqlite"
))]
impl<'r, T> AnyDecode<'r> for T where T: Decode<'r, Sqlite> + Type<Sqlite> {}

#[cfg(all(
not(any(feature = "mysql", feature = "mssql", feature = "postgres", feature = "sqlite")),
feature = "aurora"
))]
pub trait AnyDecode<'r>: Decode<'r, Aurora> + Type<Aurora> {}

#[cfg(all(
not(any(feature = "mysql", feature = "mssql", feature = "postgres", feature = "sqlite")),
feature = "aurora"
))]
impl<'r, T> AnyDecode<'r> for T where T: Decode<'r, Aurora> + Type<Aurora> {}
18 changes: 18 additions & 0 deletions sqlx-core/src/any/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use crate::mssql::Mssql;
#[cfg(feature = "sqlite")]
use crate::sqlite::Sqlite;

#[cfg(feature = "aurora")]
use crate::aurora::Aurora;

// Implements Encode for any T where T supports Encode for any database that has support currently
// compiled into SQLx
macro_rules! impl_any_encode {
Expand All @@ -39,6 +42,9 @@ macro_rules! impl_any_encode {

#[cfg(feature = "sqlite")]
crate::any::arguments::AnyArgumentBufferKind::Sqlite(args) => args.add(self),

#[cfg(feature = "aurora")]
crate::any::arguments::AnyArgumentBufferKind::Aurora(args, _) => args.add(self),
}

// unused
Expand Down Expand Up @@ -359,3 +365,15 @@ pub trait AnyEncode<'q>: Encode<'q, Sqlite> + Type<Sqlite> {}
feature = "sqlite"
))]
impl<'q, T> AnyEncode<'q> for T where T: Encode<'q, Sqlite> + Type<Sqlite> {}

#[cfg(all(
not(any(feature = "mysql", feature = "mssql", feature = "postgres", feature = "sqlite")),
feature = "aurora"
))]
pub trait AnyEncode<'q>: Encode<'q, Aurora> + Type<Aurora> {}

#[cfg(all(
not(any(feature = "mysql", feature = "mssql", feature = "postgres", feature = "sqlite")),
feature = "aurora"
))]
impl<'q, T> AnyEncode<'q> for T where T: Encode<'q, Aurora> + Type<Aurora> {}
13 changes: 13 additions & 0 deletions sqlx-core/src/any/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub enum AnyKind {

#[cfg(feature = "mssql")]
Mssql,

#[cfg(feature = "aurora")]
Aurora,
}

impl FromStr for AnyKind {
Expand Down Expand Up @@ -61,6 +64,16 @@ impl FromStr for AnyKind {
Err(Error::Configuration("database URL has the scheme of a MSSQL database but the `mssql` feature is not enabled".into()))
}

#[cfg(feature = "aurora")]
_ if uri.starts_with("aurora+data:") => {
Ok(AnyKind::Aurora)
}

#[cfg(not(feature = "aurora"))]
_ if uri.starts_with("aurora+data:") => {
Err(Error::Configuration("database URL has the scheme of an Aurora database but the `aurora` feature is not enabled".into()))
}

_ => Err(Error::Configuration(format!("unrecognized database url: {:?}", uri).into()))
}
}
Expand Down
Loading