Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a03e770

Browse files
committed
Move row out
1 parent ac2b849 commit a03e770

File tree

3 files changed

+74
-48
lines changed

3 files changed

+74
-48
lines changed

postgres-tokio/src/lib.rs

+8-47
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use postgres_protocol::message::{backend, frontend};
2121
use postgres_protocol::message::backend::{ErrorResponseBody, ErrorFields};
2222
use postgres_shared::RowData;
2323
use std::collections::HashMap;
24-
use std::error::Error as StdError;
2524
use std::fmt;
2625
use std::io;
2726
use std::mem;
@@ -37,9 +36,11 @@ use params::{ConnectParams, IntoConnectParams};
3736
use stream::PostgresStream;
3837
use tls::Handshake;
3938
use transaction::Transaction;
40-
use types::{Oid, Type, ToSql, SessionInfo, IsNull, FromSql, WrongType, Other, Kind, Field};
39+
use types::{Oid, Type, ToSql, SessionInfo, IsNull, FromSql, Other, Kind, Field};
40+
use rows::Row;
4141

4242
pub mod error;
43+
pub mod rows;
4344
mod stream;
4445
pub mod tls;
4546
pub mod transaction;
@@ -900,7 +901,7 @@ impl Connection {
900901
-> BoxStateStream<Row, Connection, Error> {
901902
let columns = statement.columns.clone();
902903
self.raw_execute(&statement.name, "", &statement.params, params)
903-
.map(|c| c.read_rows().map(move |r| Row { columns: columns.clone(), data: r }))
904+
.map(|c| c.read_rows().map(move |r| Row::new(columns.clone(), r)))
904905
.flatten_state_stream()
905906
.boxed()
906907
}
@@ -949,50 +950,6 @@ impl Statement {
949950
}
950951
}
951952

952-
pub struct Row {
953-
columns: Arc<Vec<Column>>,
954-
data: RowData,
955-
}
956-
957-
impl Row {
958-
pub fn columns(&self) -> &[Column] {
959-
&self.columns
960-
}
961-
962-
pub fn len(&self) -> usize {
963-
self.columns.len()
964-
}
965-
966-
pub fn get<T, I>(&self, idx: I) -> T
967-
where T: FromSql,
968-
I: RowIndex + fmt::Debug
969-
{
970-
match self.try_get(&idx) {
971-
Ok(Some(v)) => v,
972-
Ok(None) => panic!("no such column {:?}", idx),
973-
Err(e) => panic!("error retrieving row {:?}: {}", idx, e),
974-
}
975-
}
976-
977-
pub fn try_get<T, I>(&self, idx: I) -> Result<Option<T>, Box<StdError + Sync + Send>>
978-
where T: FromSql,
979-
I: RowIndex
980-
{
981-
let idx = match idx.idx(&self.columns) {
982-
Some(idx) => idx,
983-
None => return Ok(None),
984-
};
985-
986-
let ty = self.columns[idx].type_();
987-
if !T::accepts(ty) {
988-
return Err(Box::new(WrongType::new(ty.clone())));
989-
}
990-
991-
// FIXME
992-
T::from_sql_nullable(ty, self.data.get(idx), &SessionInfo::new(&HashMap::new())).map(Some)
993-
}
994-
}
995-
996953
fn connect_err(fields: &mut ErrorFields) -> ConnectError {
997954
match DbError::new(fields) {
998955
Ok(err) => ConnectError::Db(Box::new(err)),
@@ -1006,6 +963,10 @@ fn bad_message<T>() -> T
1006963
io::Error::new(io::ErrorKind::InvalidInput, "unexpected message").into()
1007964
}
1008965

966+
trait RowNew {
967+
fn new(columns: Arc<Vec<Column>>, data: RowData) -> Row;
968+
}
969+
1009970
trait TransactionNew {
1010971
fn new(c: Connection) -> Transaction;
1011972
}

postgres-tokio/src/rows.rs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use postgres_shared::{RowData, Column};
2+
use std::collections::HashMap;
3+
use std::error::Error;
4+
use std::fmt;
5+
use std::sync::Arc;
6+
7+
#[doc(inline)]
8+
pub use postgres_shared::RowIndex;
9+
10+
use RowNew;
11+
use types::{WrongType, FromSql, SessionInfo};
12+
13+
pub struct Row {
14+
columns: Arc<Vec<Column>>,
15+
data: RowData,
16+
}
17+
18+
impl RowNew for Row {
19+
fn new(columns: Arc<Vec<Column>>, data: RowData) -> Row {
20+
Row {
21+
columns: columns,
22+
data: data,
23+
}
24+
}
25+
}
26+
27+
impl Row {
28+
pub fn columns(&self) -> &[Column] {
29+
&self.columns
30+
}
31+
32+
pub fn len(&self) -> usize {
33+
self.columns.len()
34+
}
35+
36+
pub fn get<T, I>(&self, idx: I) -> T
37+
where T: FromSql,
38+
I: RowIndex + fmt::Debug
39+
{
40+
match self.try_get(&idx) {
41+
Ok(Some(v)) => v,
42+
Ok(None) => panic!("no such column {:?}", idx),
43+
Err(e) => panic!("error retrieving row {:?}: {}", idx, e),
44+
}
45+
}
46+
47+
pub fn try_get<T, I>(&self, idx: I) -> Result<Option<T>, Box<Error + Sync + Send>>
48+
where T: FromSql,
49+
I: RowIndex
50+
{
51+
let idx = match idx.idx(&self.columns) {
52+
Some(idx) => idx,
53+
None => return Ok(None),
54+
};
55+
56+
let ty = self.columns[idx].type_();
57+
if !T::accepts(ty) {
58+
return Err(Box::new(WrongType::new(ty.clone())));
59+
}
60+
61+
// FIXME
62+
T::from_sql_nullable(ty, self.data.get(idx), &SessionInfo::new(&HashMap::new())).map(Some)
63+
}
64+
}

postgres-tokio/src/transaction.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use futures::{Future, BoxFuture};
22
use futures_state_stream::{StateStream, BoxStateStream};
33

4-
use {Connection, Statement, Row, TransactionNew};
4+
use {Connection, Statement, TransactionNew};
55
use error::Error;
66
use types::ToSql;
7+
use rows::Row;
78

89
#[derive(Debug)]
910
pub struct Transaction(Connection);

0 commit comments

Comments
 (0)