feat: support Done for Any

This commit is contained in:
Ryan Leckey 2020-07-14 04:41:27 -07:00
parent 00137d4a04
commit 47d566f5b6
8 changed files with 83 additions and 6 deletions

View file

@ -4,7 +4,7 @@ use futures_core::stream::BoxStream;
use futures_util::{StreamExt, TryStreamExt};
use crate::any::connection::AnyConnectionKind;
use crate::any::{Any, AnyColumn, AnyConnection, AnyRow, AnyTypeInfo};
use crate::any::{Any, AnyColumn, AnyDone, AnyConnection, AnyRow, AnyTypeInfo};
use crate::database::Database;
use crate::error::Error;
use crate::executor::{Execute, Executor};
@ -16,7 +16,7 @@ impl<'c> Executor<'c> for &'c mut AnyConnection {
fn fetch_many<'e, 'q: 'e, E: 'q>(
self,
mut query: E,
) -> BoxStream<'e, Result<Either<u64, AnyRow>, Error>>
) -> BoxStream<'e, Result<Either<AnyDone, AnyRow>, Error>>
where
'c: 'e,
E: Execute<'q, Self::Database>,
@ -28,25 +28,25 @@ impl<'c> Executor<'c> for &'c mut AnyConnection {
#[cfg(feature = "postgres")]
AnyConnectionKind::Postgres(conn) => conn
.fetch_many((query, arguments.map(Into::into)))
.map_ok(|v| v.map_right(Into::into))
.map_ok(|v| v.map_right(Into::into).map_left(Into::into))
.boxed(),
#[cfg(feature = "mysql")]
AnyConnectionKind::MySql(conn) => conn
.fetch_many((query, arguments.map(Into::into)))
.map_ok(|v| v.map_right(Into::into))
.map_ok(|v| v.map_right(Into::into).map_left(Into::into))
.boxed(),
#[cfg(feature = "sqlite")]
AnyConnectionKind::Sqlite(conn) => conn
.fetch_many((query, arguments.map(Into::into)))
.map_ok(|v| v.map_right(Into::into))
.map_ok(|v| v.map_right(Into::into).map_left(Into::into))
.boxed(),
#[cfg(feature = "mssql")]
AnyConnectionKind::Mssql(conn) => conn
.fetch_many((query, arguments.map(Into::into)))
.map_ok(|v| v.map_right(Into::into))
.map_ok(|v| v.map_right(Into::into).map_left(Into::into))
.boxed(),
}
}

View file

@ -1,6 +1,7 @@
use crate::any::{
AnyArgumentBuffer, AnyArguments, AnyColumn, AnyConnection, AnyRow, AnyTransactionManager,
AnyTypeInfo, AnyValue, AnyValueRef,
AnyDone,
};
use crate::database::{Database, HasArguments, HasStatementCache, HasValueRef};
@ -16,6 +17,8 @@ impl Database for Any {
type Row = AnyRow;
type Done = AnyDone;
type Column = AnyColumn;
type TypeInfo = AnyTypeInfo;

32
sqlx-core/src/any/done.rs Normal file
View file

@ -0,0 +1,32 @@
use crate::done::Done;
use crate::any::Any;
use std::iter::{Extend, IntoIterator};
#[derive(Debug, Default)]
pub struct AnyDone {
pub(crate) rows_affected: u64,
pub(crate) last_insert_id: Option<i64>,
}
impl AnyDone {
pub fn last_insert_id(&self) -> Option<i64> {
self.last_insert_id
}
}
impl Done for AnyDone {
type Database = Any;
fn rows_affected(&self) -> u64 {
self.rows_affected
}
}
impl Extend<AnyDone> for AnyDone {
fn extend<T: IntoIterator<Item = AnyDone>>(&mut self, iter: T) {
for elem in iter {
self.rows_affected += elem.rows_affected;
self.last_insert_id = elem.last_insert_id;
}
}
}

View file

@ -15,6 +15,7 @@ mod kind;
mod options;
pub(crate) mod row;
mod transaction;
mod done;
pub(crate) mod type_info;
pub mod types;
pub(crate) mod value;
@ -29,6 +30,7 @@ pub use database::Any;
pub use decode::AnyDecode;
pub use encode::AnyEncode;
pub use kind::AnyKind;
pub use done::AnyDone;
pub use options::AnyConnectOptions;
pub use r#type::AnyType;
pub use row::AnyRow;

View file

@ -22,3 +22,13 @@ impl Extend<MssqlDone> for MssqlDone {
}
}
}
#[cfg(feature = "any")]
impl From<MssqlDone> for crate::any::AnyDone {
fn from(done: MssqlDone) -> Self {
crate::any::AnyDone {
rows_affected: done.rows_affected,
last_insert_id: None,
}
}
}

View file

@ -30,3 +30,13 @@ impl Extend<MySqlDone> for MySqlDone {
}
}
}
#[cfg(feature = "any")]
impl From<MySqlDone> for crate::any::AnyDone {
fn from(done: MySqlDone) -> Self {
crate::any::AnyDone {
rows_affected: done.rows_affected,
last_insert_id: Some(done.last_insert_id as i64),
}
}
}

View file

@ -22,3 +22,13 @@ impl Extend<PgDone> for PgDone {
}
}
}
#[cfg(feature = "any")]
impl From<PgDone> for crate::any::AnyDone {
fn from(done: PgDone) -> Self {
crate::any::AnyDone {
rows_affected: done.rows_affected,
last_insert_id: None,
}
}
}

View file

@ -30,3 +30,13 @@ impl Extend<SqliteDone> for SqliteDone {
}
}
}
#[cfg(feature = "any")]
impl From<SqliteDone> for crate::any::AnyDone {
fn from(done: SqliteDone) -> Self {
crate::any::AnyDone {
rows_affected: done.changes,
last_insert_id: Some(done.last_insert_rowid),
}
}
}