Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
sfackler merged 1 commit intorust-postgres:masterfrommpajkowski:650/simple-query-row-getter
May 30, 2021
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionstokio-postgres/src/row.rs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionstokio-postgres/src/simple_query.rs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 deletionstokio-postgres/tests/test/main.rs
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp