Voltdb-client-rust

Overview

Voltdb-client-rust is a socket client library for [voltdb] written in rust

Here list the data type and rust data type mapping

| SQL Datatype | Compatible Rust Datatypes | Option Supported | |--- |--- |--- | | TINYINT | bool | ✓ | | SMALLINT | i8/u8 | ✓ | | INTEGER | i16/u16 | ✓ | | BIGINT | i32/u32 | ✓ | | FLOAT | f64 | ✓ | | DECIMAL | bigdecimal::BigDecimal | ✓ | | GEOGRAPHY | - | | | GEOGRAPHY_POINT | - | | | VARCHAR | String | ✓ | | VARBINARY | Vec< u8> | ✓ | | TIMESTAMP | chrono::DateTime | ✓ |

Example

toml [dependencies] voltdb-client-rust = { version = "0.1"} Then, on your main.rs:

```rust,no_run use std::fs; use std::time::SystemTime;

use bigdecimal::BigDecimal; use chrono::{DateTime, Utc};

use voltdbclientrust::encode::; use voltdb_client_rust::node::; use voltdbclientrust::table::VoltTable; use voltdbclientrust::volt_param;

fn main() -> Result<(), VoltError> { #[derive(Debug)] struct Test { t1: Option, t2: Option, t3: Option, t4: Option, t5: Option, t6: Option, t7: Option, t8: Option>, t9: Option>, } impl From<&mut VoltTable> for Test { fn from(table: &mut VoltTable) -> Self { let t1 = table.getboolbycolumn("T1").unwrap(); let t2 = table.geti16bycolumn("t2").unwrap(); let t3 = table.geti32bycolumn("t3").unwrap(); let t4 = table.geti64bycolumn("t4").unwrap(); let t5 = table.getf64bycolumn("t5").unwrap(); let t6 = table.getdecimalbycolumn("t6").unwrap(); let t7 = table.getstringbycolumn("t7").unwrap(); let t8 = table.getbytesopbycolumn("t8").unwrap(); let t9 = table.gettimebycolumn("t9").unwrap(); Test { t1, t2, t3, t4, t5, t6, t7, t8, t9, } } }

let mut node = get_node("localhost:21211")?;

let has_table_check = block_for_result(&node.query("select t1 from test_types limit 1")?);
match has_table_check {
    Ok(_) => {}
    Err(err) => {
        println!("{:?}", err);
        println!("will create table");
        let create_table = "CREATE TABLE  test_types
                (
                t1 TINYINT,
                t2 SMALLINT,
                t3 INTEGER,
                t4 BIGINT,
                t5 FLOAT,
                t6 DECIMAL,
                t7 VARCHAR,
                t8 VARBINARY,
                t9 TIMESTAMP,
                );";
        block_for_result(&node.query(create_table)?)?;
    }
}

let insert = "insert into test_types (T1) values (NULL);";
block_for_result(&node.query(insert)?)?;
block_for_result(&node.query("insert into test_types (T1,T2,T3,T4) values (1, 32767, 2147483647, 9223372036854775807 );")?)?;
let mut table = block_for_result(&node.query("select * from test_types")?)?;
while table.advance_row() {
    let test: Test = table.map_row();
    println!("{:?}", test);
}
let bs = vec![1 as u8, 2, 3, 4];
let time = DateTime::from(SystemTime::now());


// call proc with parameters
let mut table = block_for_result(&node.call_sp("test_types.insert", volt_param![1,2,3,4,5,6,"7",bs,time])?)?;
while table.advance_row() {
    println!("{}", table.debug_row());
}
// upload proc into server
let jars = fs::read("tests/procedures.jar").unwrap();
let x = node.upload_jar(jars).unwrap();
let mut table = x.recv().unwrap();
assert!(table.has_error().is_none());
// create sp
let script = "CREATE PROCEDURE  FROM CLASS com.johnny.ApplicationCreate;";
let res = block_for_result(&node.query(script)?);
match res {
    Ok(_) => {}
    Err(err) => {
        println!("{}", err)
    }
};
let header = vec!["T1", "T2", "T3", "T4", "T5", "T6", "T7", "T8", "T9"];
let tp = vec![TINYINT_COLUMN, SHORT_COLUMN, INT_COLUMN, LONG_COLUMN, FLOAT_COLUMN, DECIMAL_COLUMN, STRING_COLUMN, VAR_BIN_COLUMN, TIMESTAMP_COLUMN];
let header: Vec<String> = header.iter().map(|f| f.to_string()).collect::<Vec<String>>();
let mut table = VoltTable::new_table(tp, header);
let decimal = BigDecimal::from(16);
let data = volt_param! {true, 32767 as i16 , 2147483647 as i32,  9223372036854775807 as i64 ,  15.0, decimal  , "17",bs, time   };
table.add_row(data)?;
// call proc with volt table
let mut res = block_for_result(&node.call_sp("ApplicationCreate", volt_param![table])?)?;
while res.advance_row() {
    println!("{:?}", res.debug_row());
}
Ok({})

}

```

More examples can be found here.

License

This project is licensed under the [MIT license].