forked from transact-rs/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrow.rs
More file actions
59 lines (49 loc) 路 1.6 KB
/
Copy pathrow.rs
File metadata and controls
59 lines (49 loc) 路 1.6 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
use core::str::{from_utf8, Utf8Error};
use std::collections::HashMap;
use std::convert::TryFrom;
use std::sync::Arc;
use crate::error::UnexpectedNullError;
use crate::postgres::protocol::{DataRow, TypeFormat};
use crate::postgres::Postgres;
use crate::row::{ColumnIndex, Row};
/// A value from Postgres. This may be in a BINARY or TEXT format depending
/// on the data type and if the query was prepared or not.
pub enum PgValue<'c> {
Binary(&'c [u8]),
Text(&'c str),
}
impl<'c> TryFrom<Option<PgValue<'c>>> for PgValue<'c> {
type Error = crate::Error;
#[inline]
fn try_from(value: Option<PgValue<'c>>) -> Result<Self, Self::Error> {
match value {
Some(value) => Ok(value),
None => Err(crate::Error::decode(UnexpectedNullError)),
}
}
}
pub struct PgRow<'c> {
pub(super) data: DataRow<'c>,
pub(super) columns: Arc<HashMap<Box<str>, usize>>,
pub(super) formats: Arc<[TypeFormat]>,
}
impl<'c> Row<'c> for PgRow<'c> {
type Database = Postgres;
fn len(&self) -> usize {
self.data.len()
}
fn try_get_raw<'r, I>(&'r self, index: I) -> crate::Result<Option<PgValue<'c>>>
where
I: ColumnIndex<Self::Database>,
{
let index = index.resolve(self)?;
let buffer = self.data.get(index);
buffer
.map(|buf| match self.formats[index] {
TypeFormat::Binary => Ok(PgValue::Binary(buf)),
TypeFormat::Text => Ok(PgValue::Text(from_utf8(buf)?)),
})
.transpose()
.map_err(|err: Utf8Error| crate::Error::Decode(Box::new(err)))
}
}