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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use packets::Column;
use smallvec::SmallVec;
use std::fmt;
use std::ops::Index;
use std::sync::Arc;
use value::convert::{from_value, from_value_opt, FromValue, FromValueError};
use value::Value;
pub mod convert;
#[derive(Clone, PartialEq)]
pub struct Row {
values: SmallVec<[Option<Value>; 12]>,
columns: Arc<Vec<Column>>,
}
impl fmt::Debug for Row {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut debug = f.debug_struct("Row");
for (val, column) in self.values.iter().zip(self.columns.iter()) {
match *val {
Some(ref val) => {
debug.field(column.name_str().as_ref(), val);
}
None => {
debug.field(column.name_str().as_ref(), &"<taken>");
}
}
}
debug.finish()
}
}
pub fn new_row(values: SmallVec<[Value; 12]>, columns: Arc<Vec<Column>>) -> Row {
assert!(values.len() == columns.len());
Row {
values: values.into_iter().map(|value| Some(value)).collect(),
columns,
}
}
impl Row {
pub fn len(&self) -> usize {
self.values.len()
}
pub fn as_ref(&self, index: usize) -> Option<&Value> {
self.values.get(index).and_then(|x| x.as_ref())
}
pub fn get<T, I>(&self, index: I) -> Option<T>
where
T: FromValue,
I: ColumnIndex,
{
index.idx(&*self.columns).and_then(|idx| {
self.values
.get(idx)
.and_then(|x| x.as_ref())
.map(|x| from_value::<T>(x.clone()))
})
}
pub fn get_opt<T, I>(&self, index: I) -> Option<Result<T, FromValueError>>
where
T: FromValue,
I: ColumnIndex,
{
index
.idx(&*self.columns)
.and_then(|idx| self.values.get(idx))
.and_then(|x| x.as_ref())
.and_then(|x| Some(from_value_opt::<T>(x.clone())))
}
pub fn take<T, I>(&mut self, index: I) -> Option<T>
where
T: FromValue,
I: ColumnIndex,
{
index.idx(&*self.columns).and_then(|idx| {
self.values
.get_mut(idx)
.and_then(|x| x.take())
.map(from_value::<T>)
})
}
pub fn take_opt<T, I>(&mut self, index: I) -> Option<Result<T, FromValueError>>
where
T: FromValue,
I: ColumnIndex,
{
index
.idx(&*self.columns)
.and_then(|idx| self.values.get_mut(idx))
.and_then(|x| x.take())
.and_then(|x| Some(from_value_opt::<T>(x)))
}
pub fn unwrap(self) -> Vec<Value> {
self.values
.into_iter()
.map(|x| x.expect("Can't unwrap row if some of columns was taken"))
.collect()
}
#[doc(hidden)]
pub fn place(&mut self, index: usize, value: Value) {
self.values[index] = Some(value);
}
}
impl Index<usize> for Row {
type Output = Value;
fn index<'a>(&'a self, index: usize) -> &'a Value {
self.values[index].as_ref().unwrap()
}
}
impl<'a> Index<&'a str> for Row {
type Output = Value;
fn index<'r>(&'r self, index: &'a str) -> &'r Value {
for (i, column) in self.columns.iter().enumerate() {
if column.name_ref() == index.as_bytes() {
return self.values[i].as_ref().unwrap();
}
}
panic!("No such column: `{}` in row {:?}", index, self);
}
}
pub trait ColumnIndex {
fn idx(&self, columns: &Vec<Column>) -> Option<usize>;
}
impl ColumnIndex for usize {
fn idx(&self, columns: &Vec<Column>) -> Option<usize> {
if *self >= columns.len() {
None
} else {
Some(*self)
}
}
}
impl<'a> ColumnIndex for &'a str {
fn idx(&self, columns: &Vec<Column>) -> Option<usize> {
for (i, c) in columns.iter().enumerate() {
if c.name_ref() == self.as_bytes() {
return Some(i);
}
}
None
}
}