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
use types::{FromSqlError, FromSqlResult};
use super::{Value, Type};
#[derive(Copy,Clone,Debug,PartialEq)]
pub enum ValueRef<'a> {
Null,
Integer(i64),
Real(f64),
Text(&'a str),
Blob(&'a [u8]),
}
impl<'a> ValueRef<'a> {
pub fn data_type(&self) -> Type {
match *self {
ValueRef::Null => Type::Null,
ValueRef::Integer(_) => Type::Integer,
ValueRef::Real(_) => Type::Real,
ValueRef::Text(_) => Type::Text,
ValueRef::Blob(_) => Type::Blob,
}
}
}
impl<'a> ValueRef<'a> {
pub fn as_i64(&self) -> FromSqlResult<i64> {
match *self {
ValueRef::Integer(i) => Ok(i),
_ => Err(FromSqlError::InvalidType),
}
}
pub fn as_f64(&self) -> FromSqlResult<f64> {
match *self {
ValueRef::Real(f) => Ok(f),
_ => Err(FromSqlError::InvalidType),
}
}
pub fn as_str(&self) -> FromSqlResult<&str> {
match *self {
ValueRef::Text(t) => Ok(t),
_ => Err(FromSqlError::InvalidType),
}
}
pub fn as_blob(&self) -> FromSqlResult<&[u8]> {
match *self {
ValueRef::Blob(b) => Ok(b),
_ => Err(FromSqlError::InvalidType),
}
}
}
impl<'a> From<ValueRef<'a>> for Value {
fn from(borrowed: ValueRef) -> Value {
match borrowed {
ValueRef::Null => Value::Null,
ValueRef::Integer(i) => Value::Integer(i),
ValueRef::Real(r) => Value::Real(r),
ValueRef::Text(s) => Value::Text(s.to_string()),
ValueRef::Blob(b) => Value::Blob(b.to_vec()),
}
}
}
impl<'a> From<&'a str> for ValueRef<'a> {
fn from(s: &str) -> ValueRef {
ValueRef::Text(s)
}
}
impl<'a> From<&'a [u8]> for ValueRef<'a> {
fn from(s: &[u8]) -> ValueRef {
ValueRef::Blob(s)
}
}
impl<'a> From<&'a Value> for ValueRef<'a> {
fn from(value: &'a Value) -> ValueRef<'a> {
match *value {
Value::Null => ValueRef::Null,
Value::Integer(i) => ValueRef::Integer(i),
Value::Real(r) => ValueRef::Real(r),
Value::Text(ref s) => ValueRef::Text(s),
Value::Blob(ref b) => ValueRef::Blob(b),
}
}
}