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
/// Because of cyclic references to modules we need to use super::Error to use
/// the enum. Nightly Build supports using enums - so we can fix super::Error in
/// about 3 months ;)

use std::error::Error;
use storage::ResultSet;
use storage::{Column, SqlType};
use storage::types::FromSql;
use std::cmp::{max};

/// Representation of a ResultSet with its useful functions to get data.
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() { // idx out of bounds
            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() { //idx out of bounds
            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() { //idx out of bounds
            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() { //idx out of bounds
            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() { //idx out of bounds
            None
        } else {
            Some(&self.columns[idx].description)
        }
    }

    /// Return next data entry. next() has to be called first it initialize
    /// the pointer
    pub fn next_int_by_idx (&mut self, idx: usize) -> Option<i32> {
        if idx >= self.columns.len() { //idx out of bounds
            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}
            }
        }
    }

    /// Return next data entry. next() has to be called first it initialize
    /// the pointer
    pub fn next_bool_by_idx (&mut self, idx: usize) -> Option<bool> {
        if idx >= self.columns.len() { //idx out of bounds
            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}
            }
        }
    }

    /// Return next data entry. next() has to be called first it initialize
    /// the pointer
    pub fn next_char_by_idx (&mut self, idx: usize) -> Option<String> {
        if idx >= self.columns.len() { //idx out of bounds
            None
        } else {
            // find the first pos that does not contain '0' value
            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 }
            }
        }
    }

    /// Return next data entry. next() has to be called first it initialize
    /// the pointer
    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
        }
    }

    /// Return next data entry. next() has to be called first it initialize
    /// the pointer
    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
        }
    }

    /// Return next data entry. next() has to be called first it initialize
    /// the pointer
    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
        }
    }

    /// Set the data pointer before the first entry (pos = -1). next() has to be
    /// called first to start a new next... - loop
    pub fn first (&mut self) {
        self.current_pos = 0
    }

    /// Set the data pointer after the last entry . previous() has to be called
    /// first to start a new backward loop
    pub fn last (&mut self) {
        self.current_pos = self.line_cnt
    }

    /// Move the pointer to the next line. Return false if end of data, else true.
    pub fn next(&mut self) -> bool {
        if self.current_pos >= self.line_cnt  {
            false
        } else {
            self.current_pos += 1;
            true
        }
    }

    /// Move the pointer to the previous line. Return false if end of data, else true.
    pub fn previous (&mut self) -> bool {
        if self.current_pos == 0 {
            false
        } else {
            self.current_pos -= 1;
            true
        }
    }
}

/// Sort the Vec<u8> data into DataSet for further use.
pub fn preprocess (data: &ResultSet) -> DataSet {
    let col_count = data.columns.len();
    let data_len = data.data.len();
    // get line length
    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());
    }
    // number of lines
    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();

    // split data
    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);   // push the single data vec to column
        }
        process_data.push(colvec);
    }
    // println!("data = {:?}", data);
    // println!("process data = {:?}", process_data);
    DataSet {data:process_data, columns: data.columns.clone(),
                    current_pos: 0, line_cnt: line_count}
}

/// Code numeric value sent as first byte
#[derive(PartialEq, RustcEncodable, RustcDecodable)]
#[repr(u8)]
pub enum PkgType {
    Greet = 0,
    Login,
    Command,
    Error,
    Ok,
    Response,
    AccDenied,
    AccGranted,
}

/// Struct to send the kind of error and error message to the client
#[derive(RustcEncodable, RustcDecodable, Debug)]
pub struct ClientErrMsg {
    code: u16,
    pub msg: String
}

/// Convert the possible Error to a serializable ClientErrMsg struct
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()
            }
        }
    }
}

/// This is the first packet being sent by the server after the TCP connection
/// is established.
#[derive(RustcEncodable, RustcDecodable)]
pub struct Greeting {
    pub protocol_version: u8,   // 1 byte
    pub message: String,        // n bytes
}

impl Greeting {
    pub fn make_greeting(version: u8, msg: String) -> Greeting {
        Greeting { protocol_version: version, message: msg }
    }
}

/// The client responds with this packet to a `Greeting` packet, finishing the
/// authentication handshake.
#[derive(Default, RustcEncodable, RustcDecodable)]
pub struct Login {
    pub username: String,
    pub password: String
}

/// Sent by the client to the server.
///
/// Many commands are executed via query, but there are some "special"
/// commands that are not sent as query.
#[derive(RustcEncodable, RustcDecodable, Debug, PartialEq)]
#[repr(u8)]
pub enum Command {
    Quit,
    Ping,
    Query(String),
    // Shutdown,
    // Statistics,
}