forked from rust-postgres/rust-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrow.rs
More file actions
136 lines (116 loc) · 3.27 KB
/
Copy pathrow.rs
File metadata and controls
136 lines (116 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use fallible_iterator::FallibleIterator;
use postgres_protocol::message::backend::DataRowBody;
use std::fmt;
use std::ops::Range;
use std::str;
use crate::proto;
use crate::row::sealed::Sealed;
use crate::stmt::Column;
use crate::types::{FromSql, WrongType};
use crate::Error;
mod sealed {
pub trait Sealed {}
}
/// A trait implemented by types that can index into columns of a row.
///
/// This cannot be implemented outside of this crate.
pub trait RowIndex: Sealed {
#[doc(hidden)]
fn __idx(&self, columns: &[Column]) -> Option<usize>;
}
impl Sealed for usize {}
impl RowIndex for usize {
#[inline]
fn __idx(&self, columns: &[Column]) -> Option<usize> {
if *self >= columns.len() {
None
} else {
Some(*self)
}
}
}
impl Sealed for str {}
impl RowIndex for str {
#[inline]
fn __idx(&self, columns: &[Column]) -> Option<usize> {
if let Some(idx) = columns.iter().position(|d| d.name() == self) {
return Some(idx);
};
// FIXME ASCII-only case insensitivity isn't really the right thing to
// do. Postgres itself uses a dubious wrapper around tolower and JDBC
// uses the US locale.
columns
.iter()
.position(|d| d.name().eq_ignore_ascii_case(self))
}
}
impl<'a, T> Sealed for &'a T where T: ?Sized + Sealed {}
impl<'a, T> RowIndex for &'a T
where
T: ?Sized + RowIndex,
{
#[inline]
fn __idx(&self, columns: &[Column]) -> Option<usize> {
T::__idx(*self, columns)
}
}
pub struct Row {
statement: proto::Statement,
body: DataRowBody,
ranges: Vec<Option<Range<usize>>>,
}
impl Row {
#[allow(clippy::new_ret_no_self)]
pub(crate) fn new(statement: proto::Statement, body: DataRowBody) -> Result<Row, Error> {
let ranges = body.ranges().collect().map_err(Error::parse)?;
Ok(Row {
statement,
body,
ranges,
})
}
pub fn columns(&self) -> &[Column] {
self.statement.columns()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn len(&self) -> usize {
self.columns().len()
}
pub fn get<'a, I, T>(&'a self, idx: I) -> T
where
I: RowIndex + fmt::Display,
T: FromSql<'a>,
{
match self.get_inner(&idx) {
Ok(Some(ok)) => ok,
Err(err) => panic!("error retrieving column {}: {}", idx, err),
Ok(None) => panic!("no such column {}", idx),
}
}
pub fn try_get<'a, I, T>(&'a self, idx: I) -> Result<Option<T>, Error>
where
I: RowIndex,
T: FromSql<'a>,
{
self.get_inner(&idx)
}
fn get_inner<'a, I, T>(&'a self, idx: &I) -> Result<Option<T>, Error>
where
I: RowIndex,
T: FromSql<'a>,
{
let idx = match idx.__idx(self.columns()) {
Some(idx) => idx,
None => return Ok(None),
};
let ty = self.columns()[idx].type_();
if !T::accepts(ty) {
return Err(Error::from_sql(Box::new(WrongType::new(ty.clone()))));
}
let buf = self.ranges[idx].clone().map(|r| &self.body.buffer()[r]);
let value = FromSql::from_sql_nullable(ty, buf);
value.map(Some).map_err(Error::from_sql)
}
}