mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-04-11 22:35:05 +00:00
feat: add sqlx::Done and return from Executor::execute()
+ Done::rows_affected() + Done::last_insert_id()
This commit is contained in:
@@ -12,7 +12,7 @@ use crate::postgres::message::{
|
||||
Query, RowDescription,
|
||||
};
|
||||
use crate::postgres::type_info::PgType;
|
||||
use crate::postgres::{PgArguments, PgConnection, PgRow, PgValueFormat, Postgres};
|
||||
use crate::postgres::{PgArguments, PgConnection, PgDone, PgRow, PgValueFormat, Postgres};
|
||||
use crate::statement::StatementInfo;
|
||||
|
||||
async fn prepare(
|
||||
@@ -142,7 +142,7 @@ impl PgConnection {
|
||||
query: &str,
|
||||
arguments: Option<PgArguments>,
|
||||
limit: u8,
|
||||
) -> Result<impl Stream<Item = Result<Either<u64, PgRow>, Error>> + '_, Error> {
|
||||
) -> Result<impl Stream<Item = Result<Either<PgDone, PgRow>, Error>> + '_, Error> {
|
||||
// before we continue, wait until we are "ready" to accept more queries
|
||||
self.wait_until_ready().await?;
|
||||
|
||||
@@ -219,7 +219,9 @@ impl PgConnection {
|
||||
// a SQL command completed normally
|
||||
let cc: CommandComplete = message.decode()?;
|
||||
|
||||
r#yield!(Either::Left(cc.rows_affected()));
|
||||
r#yield!(Either::Left(PgDone {
|
||||
rows_affected: cc.rows_affected(),
|
||||
}));
|
||||
}
|
||||
|
||||
MessageFormat::EmptyQueryResponse => {
|
||||
@@ -272,7 +274,7 @@ impl<'c> Executor<'c> for &'c mut PgConnection {
|
||||
fn fetch_many<'e, 'q: 'e, E: 'q>(
|
||||
self,
|
||||
mut query: E,
|
||||
) -> BoxStream<'e, Result<Either<u64, PgRow>, Error>>
|
||||
) -> BoxStream<'e, Result<Either<PgDone, PgRow>, Error>>
|
||||
where
|
||||
'c: 'e,
|
||||
E: Execute<'q, Self::Database>,
|
||||
|
||||
@@ -2,7 +2,7 @@ use crate::database::{Database, HasArguments, HasStatementCache, HasValueRef};
|
||||
use crate::postgres::arguments::PgArgumentBuffer;
|
||||
use crate::postgres::value::{PgValue, PgValueRef};
|
||||
use crate::postgres::{
|
||||
PgArguments, PgColumn, PgConnection, PgRow, PgTransactionManager, PgTypeInfo,
|
||||
PgArguments, PgColumn, PgConnection, PgDone, PgRow, PgTransactionManager, PgTypeInfo,
|
||||
};
|
||||
|
||||
/// PostgreSQL database driver.
|
||||
@@ -16,6 +16,8 @@ impl Database for Postgres {
|
||||
|
||||
type Row = PgRow;
|
||||
|
||||
type Done = PgDone;
|
||||
|
||||
type Column = PgColumn;
|
||||
|
||||
type TypeInfo = PgTypeInfo;
|
||||
|
||||
24
sqlx-core/src/postgres/done.rs
Normal file
24
sqlx-core/src/postgres/done.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use crate::done::Done;
|
||||
use crate::postgres::Postgres;
|
||||
use std::iter::{Extend, IntoIterator};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct PgDone {
|
||||
pub(super) rows_affected: u64,
|
||||
}
|
||||
|
||||
impl Done for PgDone {
|
||||
type Database = Postgres;
|
||||
|
||||
fn rows_affected(&self) -> u64 {
|
||||
self.rows_affected
|
||||
}
|
||||
}
|
||||
|
||||
impl Extend<PgDone> for PgDone {
|
||||
fn extend<T: IntoIterator<Item = PgDone>>(&mut self, iter: T) {
|
||||
for elem in iter {
|
||||
self.rows_affected += elem.rows_affected;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ use crate::executor::{Execute, Executor};
|
||||
use crate::pool::PoolOptions;
|
||||
use crate::pool::{Pool, PoolConnection};
|
||||
use crate::postgres::message::{MessageFormat, Notification};
|
||||
use crate::postgres::{PgConnection, PgRow, Postgres};
|
||||
use crate::postgres::{PgConnection, PgDone, PgRow, Postgres};
|
||||
use crate::statement::StatementInfo;
|
||||
use either::Either;
|
||||
use futures_channel::mpsc;
|
||||
@@ -197,7 +197,7 @@ impl<'c> Executor<'c> for &'c mut PgListener {
|
||||
fn fetch_many<'e, 'q: 'e, E: 'q>(
|
||||
self,
|
||||
query: E,
|
||||
) -> BoxStream<'e, Result<Either<u64, PgRow>, Error>>
|
||||
) -> BoxStream<'e, Result<Either<PgDone, PgRow>, Error>>
|
||||
where
|
||||
'c: 'e,
|
||||
E: Execute<'q, Self::Database>,
|
||||
|
||||
@@ -4,6 +4,7 @@ mod arguments;
|
||||
mod column;
|
||||
mod connection;
|
||||
mod database;
|
||||
mod done;
|
||||
mod error;
|
||||
mod io;
|
||||
mod listener;
|
||||
@@ -22,6 +23,7 @@ pub use arguments::{PgArgumentBuffer, PgArguments};
|
||||
pub use column::PgColumn;
|
||||
pub use connection::PgConnection;
|
||||
pub use database::Postgres;
|
||||
pub use done::PgDone;
|
||||
pub use error::{PgDatabaseError, PgErrorPosition};
|
||||
pub use listener::{PgListener, PgNotification};
|
||||
pub use message::PgSeverity;
|
||||
|
||||
Reference in New Issue
Block a user