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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use std::error::Error;
use storage::ResultSet;
use storage::{Column, SqlType};
use storage::types::FromSql;
use std::cmp::{max};
pub struct DataSet {
data: Vec<Vec<Vec<u8>>>,
columns: Vec<Column>,
current_pos : usize,
line_cnt: usize
}
impl DataSet {
pub fn get_col_cnt (&self) -> usize {
self.columns.len()
}
pub fn data_empty (&self) -> bool {
if self.data.len() == 0 {
return true
}
false
}
pub fn metadata_empty(&self) -> bool {
if self.columns.len() == 0 {
return true
}
false
}
pub fn get_col_idx (&self, name: String) -> Option<usize> {
for i in 0..self.columns.len() {
if self.columns[i].name == name {
return Some(i)
}
}
None
}
pub fn get_col_name (&mut self, idx: usize) -> Option<&str> {
if idx >= self.columns.len() {
None
} else {
Some(&self.columns[idx].name)
}
}
pub fn get_type_by_name (&mut self, name: String) -> Option<SqlType> {
match self.get_col_idx (name) {
Some(idx) => Some(self.columns[idx].sql_type),
None => None
}
}
pub fn get_is_primary_by_name (&mut self, name: String) -> Option<bool> {
match self.get_col_idx (name) {
Some(idx) => Some(self.columns[idx].is_primary_key),
None => None
}
}
pub fn get_allow_null_by_name (&mut self, name: String) -> Option<bool> {
match self.get_col_idx (name) {
Some(idx) => Some(self.columns[idx].allow_null),
None => None
}
}
pub fn get_description_by_name (&mut self, name: String) -> Option<&str> {
match self.get_col_idx (name) {
Some(idx) => Some(&self.columns[idx].description),
None => None
}
}
pub fn get_type_by_idx (&mut self, idx: usize) -> Option<SqlType> {
if idx >= self.columns.len() {
None
} else {
Some(self.columns[idx].sql_type)
}
}
pub fn get_is_primary_key_by_idx (&mut self, idx: usize) -> Option<bool> {
if idx >= self.columns.len() {
None
} else {
Some(self.columns[idx].is_primary_key)
}
}
pub fn get_allow_null_by_idx (&mut self, idx: usize) -> Option<bool> {
if idx >= self.columns.len() {
None
} else {
Some(self.columns[idx].allow_null)
}
}
pub fn get_description_by_idx (&mut self, idx: usize) -> Option<&str> {
if idx >= self.columns.len() {
None
} else {
Some(&self.columns[idx].description)
}
}
pub fn next_int_by_idx (&mut self, idx: usize) -> Option<i32> {
if idx >= self.columns.len() {
None
} else {
match i32::from_sql(&self.data[self.current_pos - 1][idx][..]) {
Ok(val) => Some(val),
Err(e) => {println!("int by idx: {:?}", e); None}
}
}
}
pub fn next_bool_by_idx (&mut self, idx: usize) -> Option<bool> {
if idx >= self.columns.len() {
None
} else {
match bool::from_sql(&self.data[self.current_pos - 1][idx][..]) {
Ok(val) => Some(val),
Err(e) => {println!("bool by idx: {:?}", e); None}
}
}
}
pub fn next_char_by_idx (&mut self, idx: usize) -> Option<String> {
if idx >= self.columns.len() {
None
} else {
let mut pos = 0;
let data = &self.data[self.current_pos - 1][idx][..];
while pos < self.columns[idx].sql_type.size() as usize {
if data[pos] == 0 {
break;
}
pos += 1;
}
match String::from_sql(&self.data[self.current_pos - 1][idx][0..pos]) {
Ok(val) => { Some(val) },
Err(e) => { None }
}
}
}
pub fn next_int_by_name (&mut self, name: String) -> Option<i32> {
match self.get_col_idx (name) {
Some(idx) => self.next_int_by_idx (idx),
None => None
}
}
pub fn next_bool_by_name (&mut self, name: String) -> Option<bool> {
match self.get_col_idx (name) {
Some(idx) => self.next_bool_by_idx (idx),
None => None
}
}
pub fn next_char_by_name (&mut self, name: String) -> Option<String> {
match self.get_col_idx (name) {
Some(idx) => self.next_char_by_idx (idx),
None => None
}
}
pub fn first (&mut self) {
self.current_pos = 0
}
pub fn last (&mut self) {
self.current_pos = self.line_cnt
}
pub fn next(&mut self) -> bool {
if self.current_pos >= self.line_cnt {
false
} else {
self.current_pos += 1;
true
}
}
pub fn previous (&mut self) -> bool {
if self.current_pos == 0 {
false
} else {
self.current_pos -= 1;
true
}
}
}
pub fn preprocess (data: &ResultSet) -> DataSet {
let col_count = data.columns.len();
let data_len = data.data.len();
let mut line_len = 0;
let mut arr = Vec::<u32>::new();
for i in 0..(col_count) {
line_len += data.columns[i].get_size();
arr.push(data.columns[i].get_size());
}
if line_len == 0 {
return DataSet {data: Vec::new(), columns: data.columns.clone(),
current_pos: 0, line_cnt: 0}
}
let line_count = data_len / line_len as usize;
let mut process_data = Vec::new();
let mut pos = 0;
for i in 0..(line_count) {
let mut colvec = Vec::new();
for j in 0..(col_count) {
let mut linevec = Vec::<u8>::new();
for _ in 0..(arr[j]) {
linevec.push(data.data[pos]);
pos += 1;
}
colvec.push(linevec);
}
process_data.push(colvec);
}
DataSet {data:process_data, columns: data.columns.clone(),
current_pos: 0, line_cnt: line_count}
}
#[derive(PartialEq, RustcEncodable, RustcDecodable)]
#[repr(u8)]
pub enum PkgType {
Greet = 0,
Login,
Command,
Error,
Ok,
Response,
AccDenied,
AccGranted,
}
#[derive(RustcEncodable, RustcDecodable, Debug)]
pub struct ClientErrMsg {
code: u16,
pub msg: String
}
impl From<super::Error> for ClientErrMsg {
fn from(error: super::Error) -> ClientErrMsg {
match error {
super::Error::Io(_) => ClientErrMsg {
code: 0,
msg: error.description().into()
},
super::Error::UnexpectedPkg => ClientErrMsg {
code: 2,
msg: error.description().into()
},
super::Error::UnknownCmd => ClientErrMsg {
code: 3,
msg: error.description().into()
},
super::Error::Encode(_) => ClientErrMsg {
code: 4,
msg: error.description().into()
},
super::Error::Decode(_) => ClientErrMsg {
code: 5,
msg: error.description().into()
},
super::Error::UnEoq(_) => ClientErrMsg {
code: 6,
msg: error.description().into()
}
}
}
}
#[derive(RustcEncodable, RustcDecodable)]
pub struct Greeting {
pub protocol_version: u8,
pub message: String,
}
impl Greeting {
pub fn make_greeting(version: u8, msg: String) -> Greeting {
Greeting { protocol_version: version, message: msg }
}
}
#[derive(Default, RustcEncodable, RustcDecodable)]
pub struct Login {
pub username: String,
pub password: String
}
#[derive(RustcEncodable, RustcDecodable, Debug, PartialEq)]
#[repr(u8)]
pub enum Command {
Quit,
Ping,
Query(String),
}