Skip to content

expose SimpleQueryRow's column names #780

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

Merged
merged 1 commit into from
May 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions tokio-postgres/src/row.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Rows.

use crate::row::sealed::{AsName, Sealed};
use crate::simple_query::SimpleColumn;
use crate::statement::Column;
use crate::types::{FromSql, Type, WrongType};
use crate::{Error, Statement};
Expand Down Expand Up @@ -188,16 +189,25 @@ impl Row {
}
}

impl AsName for SimpleColumn {
fn as_name(&self) -> &str {
self.name()
}
}

/// A row of data returned from the database by a simple query.
pub struct SimpleQueryRow {
columns: Arc<[String]>,
columns: Arc<[SimpleColumn]>,
body: DataRowBody,
ranges: Vec<Option<Range<usize>>>,
}

impl SimpleQueryRow {
#[allow(clippy::new_ret_no_self)]
pub(crate) fn new(columns: Arc<[String]>, body: DataRowBody) -> Result<SimpleQueryRow, Error> {
pub(crate) fn new(
columns: Arc<[SimpleColumn]>,
body: DataRowBody,
) -> Result<SimpleQueryRow, Error> {
let ranges = body.ranges().collect().map_err(Error::parse)?;
Ok(SimpleQueryRow {
columns,
Expand All @@ -206,6 +216,11 @@ impl SimpleQueryRow {
})
}

/// Returns information about the columns of data in the row.
pub fn columns(&self) -> &[SimpleColumn] {
&self.columns
}

/// Determines if the row contains no values.
pub fn is_empty(&self) -> bool {
self.len() == 0
Expand Down
21 changes: 19 additions & 2 deletions tokio-postgres/src/simple_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

/// Information about a column of a single query row.
pub struct SimpleColumn {
name: String,
}

impl SimpleColumn {
pub(crate) fn new(name: String) -> SimpleColumn {
SimpleColumn { name }
}

/// Returns the name of the column.
pub fn name(&self) -> &str {
&self.name
}
}

pub async fn simple_query(client: &InnerClient, query: &str) -> Result<SimpleQueryStream, Error> {
debug!("executing simple query: {}", query);

Expand Down Expand Up @@ -56,7 +72,7 @@ pin_project! {
/// A stream of simple query results.
pub struct SimpleQueryStream {
responses: Responses,
columns: Option<Arc<[String]>>,
columns: Option<Arc<[SimpleColumn]>>,
#[pin]
_p: PhantomPinned,
}
Expand Down Expand Up @@ -86,10 +102,11 @@ impl Stream for SimpleQueryStream {
Message::RowDescription(body) => {
let columns = body
.fields()
.map(|f| Ok(f.name().to_string()))
.map(|f| Ok(SimpleColumn::new(f.name().to_string())))
.collect::<Vec<_>>()
.map_err(Error::parse)?
.into();

*this.columns = Some(columns);
}
Message::DataRow(body) => {
Expand Down
4 changes: 4 additions & 0 deletions tokio-postgres/tests/test/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,17 @@ async fn simple_query() {
}
match &messages[2] {
SimpleQueryMessage::Row(row) => {
assert_eq!(row.columns().get(0).map(|c| c.name()), Some("id"));
assert_eq!(row.columns().get(1).map(|c| c.name()), Some("name"));
assert_eq!(row.get(0), Some("1"));
assert_eq!(row.get(1), Some("steven"));
}
_ => panic!("unexpected message"),
}
match &messages[3] {
SimpleQueryMessage::Row(row) => {
assert_eq!(row.columns().get(0).map(|c| c.name()), Some("id"));
assert_eq!(row.columns().get(1).map(|c| c.name()), Some("name"));
assert_eq!(row.get(0), Some("2"));
assert_eq!(row.get(1), Some("joe"));
}
Expand Down