Merge pull request #780 from mpajkowski/650/simple-query-row-getter
expose SimpleQueryRow's column names
This commit is contained in:
commit
0c064a9f31
@ -1,6 +1,7 @@
|
|||||||
//! Rows.
|
//! Rows.
|
||||||
|
|
||||||
use crate::row::sealed::{AsName, Sealed};
|
use crate::row::sealed::{AsName, Sealed};
|
||||||
|
use crate::simple_query::SimpleColumn;
|
||||||
use crate::statement::Column;
|
use crate::statement::Column;
|
||||||
use crate::types::{FromSql, Type, WrongType};
|
use crate::types::{FromSql, Type, WrongType};
|
||||||
use crate::{Error, Statement};
|
use crate::{Error, Statement};
|
||||||
@ -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.
|
/// A row of data returned from the database by a simple query.
|
||||||
pub struct SimpleQueryRow {
|
pub struct SimpleQueryRow {
|
||||||
columns: Arc<[String]>,
|
columns: Arc<[SimpleColumn]>,
|
||||||
body: DataRowBody,
|
body: DataRowBody,
|
||||||
ranges: Vec<Option<Range<usize>>>,
|
ranges: Vec<Option<Range<usize>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SimpleQueryRow {
|
impl SimpleQueryRow {
|
||||||
#[allow(clippy::new_ret_no_self)]
|
#[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)?;
|
let ranges = body.ranges().collect().map_err(Error::parse)?;
|
||||||
Ok(SimpleQueryRow {
|
Ok(SimpleQueryRow {
|
||||||
columns,
|
columns,
|
||||||
@ -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.
|
/// Determines if the row contains no values.
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.len() == 0
|
self.len() == 0
|
||||||
|
@ -14,6 +14,22 @@ use std::pin::Pin;
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::task::{Context, Poll};
|
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> {
|
pub async fn simple_query(client: &InnerClient, query: &str) -> Result<SimpleQueryStream, Error> {
|
||||||
debug!("executing simple query: {}", query);
|
debug!("executing simple query: {}", query);
|
||||||
|
|
||||||
@ -56,7 +72,7 @@ pin_project! {
|
|||||||
/// A stream of simple query results.
|
/// A stream of simple query results.
|
||||||
pub struct SimpleQueryStream {
|
pub struct SimpleQueryStream {
|
||||||
responses: Responses,
|
responses: Responses,
|
||||||
columns: Option<Arc<[String]>>,
|
columns: Option<Arc<[SimpleColumn]>>,
|
||||||
#[pin]
|
#[pin]
|
||||||
_p: PhantomPinned,
|
_p: PhantomPinned,
|
||||||
}
|
}
|
||||||
@ -86,10 +102,11 @@ impl Stream for SimpleQueryStream {
|
|||||||
Message::RowDescription(body) => {
|
Message::RowDescription(body) => {
|
||||||
let columns = body
|
let columns = body
|
||||||
.fields()
|
.fields()
|
||||||
.map(|f| Ok(f.name().to_string()))
|
.map(|f| Ok(SimpleColumn::new(f.name().to_string())))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.map_err(Error::parse)?
|
.map_err(Error::parse)?
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
*this.columns = Some(columns);
|
*this.columns = Some(columns);
|
||||||
}
|
}
|
||||||
Message::DataRow(body) => {
|
Message::DataRow(body) => {
|
||||||
|
@ -282,6 +282,8 @@ async fn simple_query() {
|
|||||||
}
|
}
|
||||||
match &messages[2] {
|
match &messages[2] {
|
||||||
SimpleQueryMessage::Row(row) => {
|
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(0), Some("1"));
|
||||||
assert_eq!(row.get(1), Some("steven"));
|
assert_eq!(row.get(1), Some("steven"));
|
||||||
}
|
}
|
||||||
@ -289,6 +291,8 @@ async fn simple_query() {
|
|||||||
}
|
}
|
||||||
match &messages[3] {
|
match &messages[3] {
|
||||||
SimpleQueryMessage::Row(row) => {
|
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(0), Some("2"));
|
||||||
assert_eq!(row.get(1), Some("joe"));
|
assert_eq!(row.get(1), Some("joe"));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user