Skip to content

Commit

Permalink
Merge pull request #833 from sfackler/deferred-constraint
Browse files Browse the repository at this point in the history
Fix commit-time error reporting
  • Loading branch information
sfackler authored Oct 19, 2021
2 parents d454616 + b2df115 commit 7f38b39
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
25 changes: 14 additions & 11 deletions tokio-postgres/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,22 @@ where
};
let mut responses = start(client, buf).await?;

let mut rows = 0;
loop {
match responses.next().await? {
Message::DataRow(_) => {}
Message::CommandComplete(body) => {
let rows = body
rows = body
.tag()
.map_err(Error::parse)?
.rsplit(' ')
.next()
.unwrap()
.parse()
.unwrap_or(0);
return Ok(rows);
}
Message::EmptyQueryResponse => return Ok(0),
Message::EmptyQueryResponse => rows = 0,
Message::ReadyForQuery(_) => return Ok(rows),
_ => return Err(Error::unexpected_message()),
}
}
Expand Down Expand Up @@ -203,15 +204,17 @@ impl Stream for RowStream {

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.project();
match ready!(this.responses.poll_next(cx)?) {
Message::DataRow(body) => {
Poll::Ready(Some(Ok(Row::new(this.statement.clone(), body)?)))
loop {
match ready!(this.responses.poll_next(cx)?) {
Message::DataRow(body) => {
return Poll::Ready(Some(Ok(Row::new(this.statement.clone(), body)?)))
}
Message::EmptyQueryResponse
| Message::CommandComplete(_)
| Message::PortalSuspended => {}
Message::ReadyForQuery(_) => return Poll::Ready(None),
_ => return Poll::Ready(Some(Err(Error::unexpected_message()))),
}
Message::EmptyQueryResponse
| Message::CommandComplete(_)
| Message::PortalSuspended => Poll::Ready(None),
Message::ErrorResponse(body) => Poll::Ready(Some(Err(Error::db(body)))),
_ => Poll::Ready(Some(Err(Error::unexpected_message()))),
}
}
}
26 changes: 26 additions & 0 deletions tokio-postgres/tests/test/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,3 +805,29 @@ async fn query_opt() {
.err()
.unwrap();
}

#[tokio::test]
async fn deferred_constraint() {
let client = connect("user=postgres").await;

client
.batch_execute(
"
CREATE TEMPORARY TABLE t (
i INT,
UNIQUE (i) DEFERRABLE INITIALLY DEFERRED
);
",
)
.await
.unwrap();

client
.execute("INSERT INTO t (i) VALUES (1)", &[])
.await
.unwrap();
client
.execute("INSERT INTO t (i) VALUES (1)", &[])
.await
.unwrap_err();
}

0 comments on commit 7f38b39

Please sign in to comment.