Implement get_result

This commit is contained in:
Zachery Gyurkovitz 2019-07-19 14:12:07 -07:00
parent 089637a3b8
commit a4b5cfc408
2 changed files with 52 additions and 4 deletions

View File

@ -1,5 +1,5 @@
use super::Connection;
use sqlx_postgres_protocol::{self as proto, Execute, Message, Parse, Sync};
use sqlx_postgres_protocol::{self as proto, DataRow, Execute, Message, Parse, Sync};
use std::io;
pub struct Prepare<'a> {
@ -81,4 +81,49 @@ impl<'a> Prepare<'a> {
// FIXME: This is an end-of-file error. How we should bubble this up here?
unreachable!()
}
}
#[inline]
pub async fn get_result(self) -> io::Result<Option<DataRow>> {
proto::bind::trailer(
&mut self.connection.wbuf,
self.bind_state,
self.bind_values,
&[],
);
self.connection.send(Execute::new("", 0));
self.connection.send(Sync);
self.connection.flush().await?;
let mut row: Option<DataRow> = None;
while let Some(message) = self.connection.receive().await? {
match message {
Message::BindComplete | Message::ParseComplete => {
// Indicates successful completion of a phase
}
Message::DataRow(data_row) => {
// we only care about the first result.
if row.is_none() {
row = Some(data_row);
}
}
Message::CommandComplete(_) => {}
Message::ReadyForQuery(_) => {
// Successful completion of the whole cycle
return Ok(row);
}
message => {
unimplemented!("received {:?} unimplemented message", message);
}
}
}
// FIXME: This is an end-of-file error. How we should bubble this up here?
unreachable!()
}
}

View File

@ -31,11 +31,14 @@ CREATE TABLE IF NOT EXISTS users (
.execute()
.await?;
conn.prepare("INSERT INTO users (name) VALUES ($1)")
let row_id = conn
.prepare("INSERT INTO users (name) VALUES ($1) RETURNING id")
.bind(b"Joe")
.execute()
.get_result()
.await?;
println!("row_id: {:?}", row_id);
let count = conn.prepare("SELECT name FROM users").execute().await?;
println!("users: {}", count);