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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use std::fmt::Debug;
use std::str::Utf8Error;
use std::sync::PoisonError;
use bigdecimal::BigDecimal;
use bytebuffer::ByteBuffer;
use chrono::{DateTime, Utc};
use quick_error::quick_error;
use crate::response::VoltResponseInfo;
#[allow(dead_code)]
pub const ARRAY_COLUMN: i8 = -99;
pub const NULL_COLUMN: i8 = 1;
pub const TINYINT_COLUMN: i8 = 3;
pub const SHORT_COLUMN: i8 = 4;
pub const INT_COLUMN: i8 = 5;
pub const LONG_COLUMN: i8 = 6;
pub const FLOAT_COLUMN: i8 = 8;
pub const STRING_COLUMN: i8 = 9;
pub const TIMESTAMP_COLUMN: i8 = 11;
pub const TABLE: i8 = 21;
pub const DECIMAL_COLUMN: i8 = 22;
pub const VAR_BIN_COLUMN: i8 = 25;
pub const NULL_DECIMAL: [u8; 16] = [128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
pub const NULL_BIT_VALUE: [u8; 1] = [128];
pub const NULL_BYTE_VALUE: [u8; 2] = [128, 0];
pub const NULL_SHORT_VALUE: [u8; 4] = [128, 0, 0, 0];
pub const NULL_INT_VALUE: [u8; 8] = [128, 0, 0, 0, 0, 0, 0, 0];
pub const NULL_LONG_VALUE: [u8; 16] = [128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
pub const NULL_TIMESTAMP: [u8; 8] = [128, 0, 0, 0, 0, 0, 0, 0];
pub const NULL_FLOAT_VALUE: [u8; 8] = [255, 239, 255, 255, 255, 255, 255, 255];
pub const NULL_VARCHAR: [u8; 4] = [255, 255, 255, 255];
#[macro_export]
macro_rules! volt_param {
() => (
std::vec::Vec::new()
);
( $( $x:expr ),* ) => {
{
let mut temp_vec = Vec::new();
$(
temp_vec.push(&$x as & dyn Value);
)*
temp_vec
}
};
}
quick_error! {
#[derive(Debug)]
pub enum VoltError {
Io(err: std::io::Error) {
from()
display("I/O error: {}", err)
source(err)
}
RecvError(err: std::sync::mpsc::RecvError){
from()
display("Recv error: {}", err)
source(err)
}
ExecuteFail ( info: VoltResponseInfo ){
display("volt execute failed: {:?}", info)
}
InvalidColumnType(tp: i8) {
display("InvalidColumnType {}", tp)
}
NoValue (descr : String) {
display("Error {}", descr)
}
Other(descr: String) {
display("Error {}", descr)
}
NegativeNumTables (num: i16) {
display("Error {}", num)
}
Utf8Error(err : Utf8Error){
from()
display("Utf8 error: {}", err)
source(err)
}
PoisonError (descr: String){
display("Error {}", descr)
}
BadReturnStatusOnTable (status: i8) {
display("Error {}", status)
}
AuthFailed
}}
impl<T> From<PoisonError<T>> for VoltError {
fn from(p: PoisonError<T>) -> VoltError {
VoltError::PoisonError(p.to_string().to_owned())
}
}
pub trait Value: Debug {
fn get_write_length(&self) -> i32;
fn marshal(&self, bytebuffer: &mut ByteBuffer);
fn marshal_in_table(&self, bytebuffer: &mut ByteBuffer, column_type: i8);
fn to_value_string(&self) -> String;
}
trait WriteBool {
fn write_bool(&mut self, val: bool);
}
impl WriteBool for ByteBuffer {
fn write_bool(&mut self, val: bool) {
if val {
self.write_i8(1)
} else {
self.write_i8(0);
}
}
}
impl Value for bool {
fn get_write_length(&self) -> i32 {
return 2;
}
fn marshal(&self, bytebuffer: &mut ByteBuffer) {
bytebuffer.write_i8(TINYINT_COLUMN);
bytebuffer.write_bool(*self);
}
fn marshal_in_table(&self, bytebuffer: &mut ByteBuffer, column_type: i8) {
bytebuffer.write_bool(*self);
}
fn to_value_string(&self) -> String {
return self.to_string();
}
}
impl Value for BigDecimal {
fn get_write_length(&self) -> i32 {
return 17;
}
fn marshal(&self, bytebuffer: &mut ByteBuffer) {
bytebuffer.write_i8(DECIMAL_COLUMN);
self.marshal_in_table(bytebuffer, DECIMAL_COLUMN);
}
fn marshal_in_table(&self, bytebuffer: &mut ByteBuffer, column_type: i8) {
let (b, _) = self.clone().with_scale(12).into_bigint_and_exponent();
let bs = b.to_signed_bytes_be();
let pad = 16 - bs.len();
if pad > 0 {
let arr = vec![0; pad];
bytebuffer.write_bytes(&arr)
}
bytebuffer.write_bytes(&bs);
}
fn to_value_string(&self) -> String {
return self.to_string();
}
}
impl Value for i8 {
fn get_write_length(&self) -> i32 {
return 2;
}
fn marshal(&self, bytebuffer: &mut ByteBuffer) {
bytebuffer.write_i8(TINYINT_COLUMN);
bytebuffer.write_i8(*self);
}
fn marshal_in_table(&self, bytebuffer: &mut ByteBuffer, column_type: i8) {
bytebuffer.write_i8(0);
bytebuffer.write_i8(*self);
}
fn to_value_string(&self) -> String {
return self.to_string();
}
}
impl Value for i16 {
fn get_write_length(&self) -> i32 {
return 3;
}
fn marshal(&self, bytebuffer: &mut ByteBuffer) {
bytebuffer.write_i8(SHORT_COLUMN);
bytebuffer.write_i16(*self);
}
fn marshal_in_table(&self, bytebuffer: &mut ByteBuffer, column_type: i8) {
bytebuffer.write_i16(0);
bytebuffer.write_i16(*self);
}
fn to_value_string(&self) -> String {
return self.to_string();
}
}
impl Value for i32 {
fn get_write_length(&self) -> i32 {
return 5;
}
fn marshal(&self, bytebuffer: &mut ByteBuffer) {
bytebuffer.write_i8(INT_COLUMN);
bytebuffer.write_i32(*self);
}
fn marshal_in_table(&self, bytebuffer: &mut ByteBuffer, column_type: i8) {
bytebuffer.write_i32(0);
bytebuffer.write_i32(*self);
}
fn to_value_string(&self) -> String {
return self.to_string();
}
}
impl Value for i64 {
fn get_write_length(&self) -> i32 {
return 9;
}
fn marshal(&self, bytebuffer: &mut ByteBuffer) {
bytebuffer.write_i8(LONG_COLUMN);
bytebuffer.write_i64(*self);
}
fn marshal_in_table(&self, bytebuffer: &mut ByteBuffer, column_type: i8) {
bytebuffer.write_i64(0);
bytebuffer.write_i64(*self);
}
fn to_value_string(&self) -> String {
return self.to_string();
}
}
impl Value for f64 {
fn get_write_length(&self) -> i32 {
return 9;
}
fn marshal(&self, bytebuffer: &mut ByteBuffer) {
bytebuffer.write_i8(FLOAT_COLUMN);
bytebuffer.write_f64(*self);
}
fn marshal_in_table(&self, bytebuffer: &mut ByteBuffer, column_type: i8) {
bytebuffer.write_f64(*self);
}
fn to_value_string(&self) -> String {
return self.to_string();
}
}
impl Value for String {
fn get_write_length(&self) -> i32 {
return (5 + self.len()) as i32;
}
fn marshal(&self, bytebuffer: &mut ByteBuffer) {
bytebuffer.write_i8(STRING_COLUMN);
bytebuffer.write_string(self);
}
fn marshal_in_table(&self, bytebuffer: &mut ByteBuffer, column_type: i8) {
bytebuffer.write_string(self);
}
fn to_value_string(&self) -> String {
return self.to_string();
}
}
impl Value for &str {
fn get_write_length(&self) -> i32 {
return (5 + self.len()) as i32;
}
fn marshal(&self, bytebuffer: &mut ByteBuffer) {
bytebuffer.write_i8(STRING_COLUMN);
bytebuffer.write_string(self);
}
fn marshal_in_table(&self, bytebuffer: &mut ByteBuffer, column_type: i8) {
bytebuffer.write_string(self);
}
fn to_value_string(&self) -> String {
return self.to_string();
}
}
impl Value for Vec<u8> {
fn get_write_length(&self) -> i32 {
return (5 + self.len()) as i32;
}
fn marshal(&self, bytebuffer: &mut ByteBuffer) {
bytebuffer.write_i8(VAR_BIN_COLUMN);
bytebuffer.write_u32(self.len() as u32);
bytebuffer.write_bytes(&self);
}
fn marshal_in_table(&self, bytebuffer: &mut ByteBuffer, column_type: i8) {
bytebuffer.write_u32(self.len() as u32);
bytebuffer.write_bytes(&self);
}
fn to_value_string(&self) -> String {
return format!("{:?}", self);
}
}
impl Value for [u8] {
fn get_write_length(&self) -> i32 {
return (5 + self.len()) as i32;
}
fn marshal(&self, bytebuffer: &mut ByteBuffer) {
bytebuffer.write_i8(VAR_BIN_COLUMN);
bytebuffer.write_u32(self.len() as u32);
bytebuffer.write_bytes(&self);
}
fn marshal_in_table(&self, bytebuffer: &mut ByteBuffer, column_type: i8) {
bytebuffer.write_u32(self.len() as u32);
bytebuffer.write_bytes(&self);
}
fn to_value_string(&self) -> String {
return format!("{:?}", self);
}
}
impl Value for DateTime<Utc> {
fn get_write_length(&self) -> i32 {
return 9;
}
fn marshal(&self, bytebuffer: &mut ByteBuffer) {
bytebuffer.write_i8(TIMESTAMP_COLUMN);
bytebuffer.write_i64(self.timestamp_millis() * 1000);
}
fn marshal_in_table(&self, bytebuffer: &mut ByteBuffer, column_type: i8) {
bytebuffer.write_i64(self.timestamp_millis() * 1000);
}
fn to_value_string(&self) -> String {
return self.to_string();
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use bigdecimal::num_bigint::BigInt;
use chrono::TimeZone;
use crate::procedure_invocation::new_procedure_invocation;
use super::*;
#[test]
fn test_encoding_proc() {
let mut zero_vec: Vec<&dyn Value> = Vec::new();
zero_vec.push(&"select * from account limit 1;");
let mut proc = new_procedure_invocation(1, false, &zero_vec, "@AdHoc");
let bs = proc.bytes();
assert_eq!(bs, vec!(0, 0, 0, 56, 0, 0, 0, 0, 6, 64, 65, 100, 72, 111, 99, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 9, 0, 0, 0, 30, 115, 101, 108, 101, 99, 116, 32, 42, 32, 102, 114, 111, 109, 32, 97, 99, 99, 111, 117, 110, 116, 32, 108, 105, 109, 105, 116, 32, 49, 59));
}
#[test]
fn test_time_stamp() {
let time = Utc.timestamp_millis(1637323002445000 / 1000);
println!("{}", time.timestamp_millis() * 1000);
}
#[test]
fn test_big_decimal() {
let bs: Vec<u8> = vec!(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 243, 16, 122, 64, 0);
let int = BigInt::from_signed_bytes_be(&*bs);
let decimal = BigDecimal::new(int, 12);
let (b, _) = decimal.into_bigint_and_exponent();
let b = b.to_signed_bytes_be();
println!("{:?}", b);
let decimal = BigDecimal::from_str("1.11").unwrap().with_scale(12);
println!("{:?}", decimal.into_bigint_and_exponent());
}
#[test]
fn test_big_test_bytes() {
let i = ByteBuffer::from_bytes(&NULL_BYTE_VALUE).read_i8().unwrap();
assert_eq!(i, -128);
let i = ByteBuffer::from_bytes(&NULL_SHORT_VALUE).read_i16().unwrap();
assert_eq!(i, -32768);
let i = ByteBuffer::from_bytes(&NULL_INT_VALUE).read_i32().unwrap();
assert_eq!(i, -2147483648);
let i = ByteBuffer::from_bytes(&NULL_LONG_VALUE).read_i64().unwrap();
assert_eq!(i, -9223372036854775808);
let mut byte = ByteBuffer::new();
byte.write_i32(-1);
let bs = byte.to_bytes();
println!("{:?}", bs)
}
#[test]
fn test_error() {
let err = VoltError::NoValue("key is af".to_owned());
println!("{:?} {} ", err, err);
}
#[test]
fn test_vec() {
let xs: [u8; 5] = [1, 2, 3, 4, 5];
println!("{}", xs.get_write_length());
let mut ve = Vec::new();
ve.push(1 as u8);
ve.push(10 as u8);
println!("{}", &ve.get_write_length());
}
}