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

Commit0c064a9

Browse files
authored
Merge pull request#780 from mpajkowski/650/simple-query-row-getter
expose SimpleQueryRow's column names
2 parents3390cf3 +b03ffcd commit0c064a9

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

‎tokio-postgres/src/row.rs‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Rows.
22
33
usecrate::row::sealed::{AsName,Sealed};
4+
usecrate::simple_query::SimpleColumn;
45
usecrate::statement::Column;
56
usecrate::types::{FromSql,Type,WrongType};
67
usecrate::{Error,Statement};
@@ -188,16 +189,25 @@ impl Row {
188189
}
189190
}
190191

192+
implAsNameforSimpleColumn{
193+
fnas_name(&self) ->&str{
194+
self.name()
195+
}
196+
}
197+
191198
/// A row of data returned from the database by a simple query.
192199
pubstructSimpleQueryRow{
193-
columns:Arc<[String]>,
200+
columns:Arc<[SimpleColumn]>,
194201
body:DataRowBody,
195202
ranges:Vec<Option<Range<usize>>>,
196203
}
197204

198205
implSimpleQueryRow{
199206
#[allow(clippy::new_ret_no_self)]
200-
pub(crate)fnnew(columns:Arc<[String]>,body:DataRowBody) ->Result<SimpleQueryRow,Error>{
207+
pub(crate)fnnew(
208+
columns:Arc<[SimpleColumn]>,
209+
body:DataRowBody,
210+
) ->Result<SimpleQueryRow,Error>{
201211
let ranges = body.ranges().collect().map_err(Error::parse)?;
202212
Ok(SimpleQueryRow{
203213
columns,
@@ -206,6 +216,11 @@ impl SimpleQueryRow {
206216
})
207217
}
208218

219+
/// Returns information about the columns of data in the row.
220+
pubfncolumns(&self) ->&[SimpleColumn]{
221+
&self.columns
222+
}
223+
209224
/// Determines if the row contains no values.
210225
pubfnis_empty(&self) ->bool{
211226
self.len() ==0

‎tokio-postgres/src/simple_query.rs‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ use std::pin::Pin;
1414
use std::sync::Arc;
1515
use std::task::{Context,Poll};
1616

17+
/// Information about a column of a single query row.
18+
pubstructSimpleColumn{
19+
name:String,
20+
}
21+
22+
implSimpleColumn{
23+
pub(crate)fnnew(name:String) ->SimpleColumn{
24+
SimpleColumn{ name}
25+
}
26+
27+
/// Returns the name of the column.
28+
pubfnname(&self) ->&str{
29+
&self.name
30+
}
31+
}
32+
1733
pubasyncfnsimple_query(client:&InnerClient,query:&str) ->Result<SimpleQueryStream,Error>{
1834
debug!("executing simple query: {}", query);
1935

@@ -56,7 +72,7 @@ pin_project! {
5672
/// A stream of simple query results.
5773
pubstructSimpleQueryStream{
5874
responses:Responses,
59-
columns:Option<Arc<[String]>>,
75+
columns:Option<Arc<[SimpleColumn]>>,
6076
#[pin]
6177
_p:PhantomPinned,
6278
}
@@ -86,10 +102,11 @@ impl Stream for SimpleQueryStream {
86102
Message::RowDescription(body) =>{
87103
let columns = body
88104
.fields()
89-
.map(|f|Ok(f.name().to_string()))
105+
.map(|f|Ok(SimpleColumn::new(f.name().to_string())))
90106
.collect::<Vec<_>>()
91107
.map_err(Error::parse)?
92108
.into();
109+
93110
*this.columns =Some(columns);
94111
}
95112
Message::DataRow(body) =>{

‎tokio-postgres/tests/test/main.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,17 @@ async fn simple_query() {
282282
}
283283
match&messages[2]{
284284
SimpleQueryMessage::Row(row) =>{
285+
assert_eq!(row.columns().get(0).map(|c| c.name()),Some("id"));
286+
assert_eq!(row.columns().get(1).map(|c| c.name()),Some("name"));
285287
assert_eq!(row.get(0),Some("1"));
286288
assert_eq!(row.get(1),Some("steven"));
287289
}
288290
_ =>panic!("unexpected message"),
289291
}
290292
match&messages[3]{
291293
SimpleQueryMessage::Row(row) =>{
294+
assert_eq!(row.columns().get(0).map(|c| c.name()),Some("id"));
295+
assert_eq!(row.columns().get(1).map(|c| c.name()),Some("name"));
292296
assert_eq!(row.get(0),Some("2"));
293297
assert_eq!(row.get(1),Some("joe"));
294298
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp