-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Closed
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f3e453a
add Aurora driver
tarkah 6f74ce3
add null type
tarkah ae73c0a
add query logger
tarkah 19548d1
finish is called on drop
tarkah ae3b024
add more types
tarkah fa7f2af
add db type
tarkah 57087cb
add migrate for aurora
tarkah 8ed8681
add aurora to Any
tarkah 5d8b40b
add database to uri
tarkah 53aa60e
fix decode error message
tarkah 88e1e43
fix cfg
tarkah 5db4e59
add aurora feature to cli
tarkah 332cdd4
fix cfg
tarkah 258c519
prepare sql & parameters
tarkah e4a468a
remove dbg
tarkah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use crate::arguments::Arguments; | ||
use crate::aurora::Aurora; | ||
use crate::encode::Encode; | ||
use crate::types::Type; | ||
|
||
use rusoto_rds_data::SqlParameter; | ||
|
||
/// Implementation of [`Arguments`] for Aurora. | ||
#[derive(Default)] | ||
pub struct AuroraArguments { | ||
pub(crate) parameters: Vec<SqlParameter>, | ||
} | ||
|
||
impl<'q> Arguments<'q> for AuroraArguments { | ||
type Database = Aurora; | ||
|
||
fn reserve(&mut self, additional: usize, _size: usize) { | ||
self.parameters.reserve(additional); | ||
} | ||
|
||
fn add<T>(&mut self, value: T) | ||
where | ||
T: Encode<'q, Self::Database> + Type<Self::Database>, | ||
{ | ||
let _ = value.encode(&mut self.parameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use crate::aurora::type_info::AuroraTypeInfo; | ||
use crate::aurora::Aurora; | ||
use crate::column::Column; | ||
use crate::ext::ustr::UStr; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct AuroraColumn { | ||
pub(crate) ordinal: usize, | ||
pub(crate) name: UStr, | ||
pub(crate) type_info: AuroraTypeInfo, | ||
} | ||
|
||
impl crate::column::private_column::Sealed for AuroraColumn {} | ||
|
||
impl Column for AuroraColumn { | ||
type Database = Aurora; | ||
|
||
fn ordinal(&self) -> usize { | ||
self.ordinal | ||
} | ||
|
||
fn name(&self) -> &str { | ||
&*self.name | ||
} | ||
|
||
fn type_info(&self) -> &AuroraTypeInfo { | ||
&self.type_info | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
use super::AuroraConnection; | ||
use crate::aurora::error::AuroraDatabaseError; | ||
use crate::aurora::statement::AuroraStatementMetadata; | ||
use crate::aurora::{ | ||
Aurora, AuroraArguments, AuroraColumn, AuroraDone, AuroraRow, AuroraStatement, AuroraTypeInfo, | ||
}; | ||
use crate::describe::Describe; | ||
use crate::error::Error; | ||
use crate::executor::{Execute, Executor}; | ||
use crate::ext::ustr::UStr; | ||
use crate::logger::QueryLogger; | ||
|
||
use either::Either; | ||
use futures_core::future::BoxFuture; | ||
use futures_core::stream::BoxStream; | ||
use futures_core::Stream; | ||
use futures_util::stream; | ||
use futures_util::{pin_mut, TryStreamExt}; | ||
use rusoto_rds_data::{ExecuteStatementRequest, ExecuteStatementResponse, RdsData}; | ||
use std::borrow::Cow; | ||
use std::sync::Arc; | ||
|
||
impl AuroraConnection { | ||
async fn run<'e, 'c: 'e, 'q: 'e>( | ||
&'c mut self, | ||
query: &'q str, | ||
arguments: Option<AuroraArguments>, | ||
) -> Result<impl Stream<Item = Result<Either<AuroraDone, AuroraRow>, Error>> + 'e, Error> { | ||
let mut logger = QueryLogger::new(query, self.log_settings.clone()); | ||
|
||
// TODO: is this correct? | ||
let transaction_id = self.transaction_ids.last().cloned(); | ||
|
||
let request = ExecuteStatementRequest { | ||
sql: query.to_owned(), | ||
parameters: arguments.map(|m| m.parameters), | ||
resource_arn: self.resource_arn.clone(), | ||
secret_arn: self.secret_arn.clone(), | ||
database: self.database.clone(), | ||
schema: self.schema.clone(), | ||
transaction_id, | ||
include_result_metadata: Some(true), | ||
..Default::default() | ||
}; | ||
|
||
let ExecuteStatementResponse { | ||
column_metadata, | ||
number_of_records_updated, | ||
records, | ||
.. | ||
} = self | ||
.client | ||
.execute_statement(request) | ||
.await | ||
.map_err(AuroraDatabaseError)?; | ||
|
||
let rows_affected = number_of_records_updated.unwrap_or_default() as u64; | ||
let column_metadata = column_metadata.unwrap_or_default(); | ||
|
||
let mut rows = records | ||
.unwrap_or_default() | ||
.into_iter() | ||
.map(|fields| { | ||
let columns: Vec<_> = fields | ||
.iter() | ||
.zip(&column_metadata) | ||
.enumerate() | ||
.map(|(ordinal, (field, metadata))| AuroraColumn { | ||
ordinal, | ||
name: UStr::new(metadata.name.as_deref().unwrap_or_default()), | ||
type_info: AuroraTypeInfo::from(field), | ||
}) | ||
.collect(); | ||
|
||
let column_names = columns | ||
.iter() | ||
.map(|column| (column.name.clone(), column.ordinal)) | ||
.collect(); | ||
let parameters = columns.iter().map(|column| column.type_info).collect(); | ||
|
||
let metadata = Arc::new(AuroraStatementMetadata { | ||
columns, | ||
column_names, | ||
parameters, | ||
}); | ||
|
||
let row = AuroraRow { fields, metadata }; | ||
|
||
logger.increment_rows(); | ||
|
||
Ok(Either::Right(row)) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
rows.push(Ok(Either::Left(AuroraDone { rows_affected }))); | ||
|
||
logger.finish(); | ||
|
||
Ok(stream::iter(rows)) | ||
} | ||
} | ||
|
||
impl<'c> Executor<'c> for &'c mut AuroraConnection { | ||
type Database = Aurora; | ||
|
||
fn fetch_many<'e, 'q: 'e, E: 'q>( | ||
self, | ||
mut query: E, | ||
) -> BoxStream<'e, Result<Either<AuroraDone, AuroraRow>, Error>> | ||
where | ||
'c: 'e, | ||
E: Execute<'q, Self::Database>, | ||
{ | ||
let sql = query.sql(); | ||
let arguments = query.take_arguments(); | ||
|
||
// TODO: implement statement caching? | ||
//let metadata = query.statement(); | ||
//let persistent = query.persistent(); | ||
|
||
Box::pin(try_stream! { | ||
let s = self.run(sql, arguments).await?; | ||
pin_mut!(s); | ||
|
||
while let Some(v) = s.try_next().await? { | ||
r#yield!(v); | ||
} | ||
|
||
Ok(()) | ||
}) | ||
} | ||
|
||
fn fetch_optional<'e, 'q: 'e, E: 'q>( | ||
self, | ||
query: E, | ||
) -> BoxFuture<'e, Result<Option<AuroraRow>, Error>> | ||
where | ||
'c: 'e, | ||
E: Execute<'q, Self::Database>, | ||
{ | ||
let mut s = self.fetch_many(query); | ||
|
||
Box::pin(async move { | ||
while let Some(v) = s.try_next().await? { | ||
if let Either::Right(r) = v { | ||
return Ok(Some(r)); | ||
} | ||
} | ||
|
||
Ok(None) | ||
}) | ||
} | ||
|
||
fn prepare_with<'e, 'q: 'e>( | ||
self, | ||
sql: &'q str, | ||
_parameters: &[AuroraTypeInfo], | ||
) -> BoxFuture<'e, Result<AuroraStatement<'q>, Error>> | ||
where | ||
'c: 'e, | ||
{ | ||
Box::pin(async move { | ||
Ok(AuroraStatement { | ||
sql: Cow::Borrowed(sql), | ||
metadata: Default::default(), | ||
}) | ||
}) | ||
} | ||
|
||
fn describe<'e, 'q: 'e>( | ||
self, | ||
sql: &'q str, | ||
) -> BoxFuture<'e, Result<Describe<Self::Database>, Error>> | ||
where | ||
'c: 'e, | ||
{ | ||
Box::pin(async move { | ||
let metadata: AuroraStatementMetadata = Default::default(); | ||
|
||
let nullable = Vec::with_capacity(metadata.columns.len()); | ||
|
||
Ok(Describe { | ||
nullable, | ||
columns: metadata.columns, | ||
parameters: None, | ||
}) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
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