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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
pub mod iter;
use std::collections::BTreeMap;
use std::fmt;
use super::common::{Context, Value, ValueType};
#[derive(Debug, PartialEq, Eq)]
pub struct OwnedContext;
impl Context for OwnedContext {
type String = String;
type I64 = i64;
type XML = String;
}
pub type Field = Value<OwnedContext>;
impl fmt::Display for Field {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Field::Nothing => write!(f, "NULL"),
Field::Integer(i) => write!(f, "{}", i),
Field::Float(v) => write!(f, "{}", v),
Field::Text(t) => write!(f, "{:?}", t),
Field::Boolean(b) => write!(f, "{}", b),
Field::BigInt(i) => write!(f, "{}", i),
Field::VarChar(v) => write!(f, "{:?}", v),
}
}
}
#[derive(Debug, Default)]
pub struct Row(Vec<Field>);
impl From<Vec<Field>> for Row {
fn from(fields: Vec<Field>) -> Self {
Row(fields)
}
}
impl Row {
pub fn new() -> Row {
Row(Vec::new())
}
pub fn into_fields(self) -> Vec<Field> {
self.0
}
pub fn fields(&self) -> &Vec<Field> {
&self.0
}
pub fn fields_mut(&mut self) -> &mut Vec<Field> {
&mut self.0
}
}
#[derive(Debug, Default)]
pub struct Bucket(pub Vec<Row>);
impl Bucket {
pub fn new() -> Bucket {
Bucket(Vec::new())
}
pub fn rows(self) -> Vec<Row> {
self.0
}
pub fn rows_ref(&self) -> &Vec<Row> {
&self.0
}
pub fn rows_mut(&mut self) -> &mut Vec<Row> {
&mut self.0
}
}
#[derive(Debug)]
pub struct Column {
pub name: String,
pub field_type: ValueType,
}
impl From<(&str, ValueType)> for Column {
fn from(data: (&str, ValueType)) -> Self {
Column {
name: String::from(data.0),
field_type: data.1,
}
}
}
#[derive(Debug)]
pub struct TableDef {
pub columns: Vec<Column>,
pub name: String,
}
#[derive(Debug, Default)]
pub struct TableData {
pub buckets: Vec<Bucket>,
}
impl TableData {
pub fn new() -> Self {
TableData {
buckets: Vec::new(),
}
}
}
#[derive(Debug)]
pub struct Table {
definition: TableDef,
data: TableData,
}
impl Table {
pub fn from(definition: TableDef, data: TableData) -> Self {
Table { definition, data }
}
pub fn new(definition: TableDef) -> Self {
let data = TableData::new();
Table { definition, data }
}
pub fn into_buckets(self) -> Vec<Bucket> {
self.data.buckets
}
pub fn buckets(&self) -> &[Bucket] {
&self.data.buckets
}
pub fn buckets_mut(&mut self) -> &mut Vec<Bucket> {
&mut self.data.buckets
}
pub fn into_columns(self) -> Vec<Column> {
self.definition.columns
}
pub fn columns(&self) -> &[Column] {
&self.definition.columns
}
pub fn columns_mut(&mut self) -> &mut Vec<Column> {
&mut self.definition.columns
}
pub fn name(&self) -> &str {
self.definition.name.as_ref()
}
}
#[derive(Debug, Default)]
pub struct Schema {
pub tables: BTreeMap<String, Table>,
}
impl Schema {
pub fn new() -> Schema {
Schema {
tables: BTreeMap::new(),
}
}
pub fn table(&self, name: &str) -> Option<&Table> {
self.tables.get(name)
}
pub fn table_mut(&mut self, name: &str) -> Option<&mut Table> {
self.tables.get_mut(name)
}
pub fn table_count(&self) -> usize {
self.tables.len()
}
}
impl From<Vec<Table>> for Schema {
fn from(tables: Vec<Table>) -> Self {
let mut tree = BTreeMap::new();
for table in tables {
tree.insert(table.name().to_string(), table);
}
Schema { tables: tree }
}
}