Repository navigation
Expand file tree
/
Copy pathvalue.rs
More file actions
121 lines (107 loc) · 3.81 KB
/
Copy pathvalue.rs
File metadata and controls
121 lines (107 loc) · 3.81 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
use std::borrow::Cow;
use crate::database::{Database, HasValueRef};
use crate::decode::Decode;
use crate::error::{mismatched_types, Error};
/// An owned value from the database.
pub trait Value {
type Database: Database;
/// Get this value as a reference.
fn as_ref(&self) -> <Self::Database as HasValueRef<'_>>::ValueRef;
/// Get the ty'pe information, if available, for this value.
///
/// Some database implementations do not implement type deduction for
/// expressions (`SELECT 2 + 5`); and, this will return `None` in those cases.
fn type_info(&self) -> Option<Cow<'_, <Self::Database as Database>::TypeInfo>>;
/// Returns `true` if the SQL value is `NULL`.
fn is_null(&self) -> bool;
/// Decode this single value into the requested type.
///
/// # Panics
///
/// Panics if the value cannot be decoded into the requested type.
/// See [`try_decode`](#method.try_decode) for a non-panicking version.
///
#[inline]
fn decode<'r, T>(&'r self) -> T
where
T: Decode<'r, Self::Database>,
{
self.try_decode::<T>().unwrap()
}
/// Decode this single value into the requested type.
///
/// Unlike [`decode`](#method.decode), this method does not check that the type of this
/// value is compatible with the Rust type and blindly tries to decode the value.
///
/// # Panics
///
/// Panics if the value cannot be decoded into the requested type.
/// See [`try_decode_unchecked`](#method.try_decode_unchecked) for a non-panicking version.
///
#[inline]
fn decode_unchecked<'r, T>(&'r self) -> T
where
T: Decode<'r, Self::Database>,
{
self.try_decode_unchecked::<T>().unwrap()
}
/// Decode this single value into the requested type.
///
/// # Errors
///
/// * [`Decode`] if the value could not be decoded into the requested type.
///
/// [`Decode`]: crate::Error::Decode
///
#[inline]
fn try_decode<'r, T>(&'r self) -> Result<T, Error>
where
T: Decode<'r, Self::Database>,
{
if !self.is_null() {
if let Some(actual_ty) = self.type_info() {
if !T::accepts(&actual_ty) {
return Err(Error::Decode(mismatched_types::<Self::Database, T>(
&T::type_info(),
&actual_ty,
)));
}
}
}
self.try_decode_unchecked()
}
/// Decode this single value into the requested type.
///
/// Unlike [`try_decode`](#method.try_decode), this method does not check that the type of this
/// value is compatible with the Rust type and blindly tries to decode the value.
///
/// # Errors
///
/// * [`Decode`] if the value could not be decoded into the requested type.
///
/// [`Decode`]: crate::Error::Decode
///
#[inline]
fn try_decode_unchecked<'r, T>(&'r self) -> Result<T, Error>
where
T: Decode<'r, Self::Database>,
{
T::decode(self.as_ref()).map_err(Error::Decode)
}
}
/// A reference to a single value from the database.
pub trait ValueRef<'r>: Sized {
type Database: Database;
/// Creates an owned value from this value reference.
///
/// This is just a reference increment in PostgreSQL and MySQL and thus is `O(1)`. In SQLite,
/// this is a copy.
fn to_owned(&self) -> <Self::Database as Database>::Value;
/// Get the type information, if available, for this value.
///
/// Some database implementations do not implement type deduction for
/// expressions (`SELECT 2 + 5`); and, this will return `None` in those cases.
fn type_info(&self) -> Option<Cow<'_, <Self::Database as Database>::TypeInfo>>;
/// Returns `true` if the SQL value is `NULL`.
fn is_null(&self) -> bool;
}