voltdb_client_rust/
lib.rs

1//! This crate offers:
2//!
3//! *   Voltdb database driver in pure rust;
4//!
5//! ## Installation
6//!
7//! Put the desired version of the crate into the `dependencies` section of your `Cargo.toml`:
8//!
9//! ```toml
10//! [dependencies]
11//! voltdb-client-rust = "0.1"
12//! ```
13//!
14//! ## Example
15//!
16//!
17//!
18//! ```no_run
19//!use std::fs;
20//!use std::time::SystemTime;
21//!
22//!use voltdb_client_rust::*;
23//!
24//!fn main() -> Result<(), VoltError> {
25//!    #[derive(Debug)]
26//!    struct Test {
27//!        t1: Option<i8>,
28//!        t2: Option<i16>,
29//!        t3: Option<i32>,
30//!        t4: Option<i64>,
31//!        t5: Option<f64>,
32//!        t6: Option<BigDecimal>,
33//!        t7: Option<String>,
34//!        t8: Option<Vec<u8>>,
35//!        t9: Option<DateTime<Utc>>,
36//!    }
37//!    /// Convert table to struct.
38//!    impl From<&mut VoltTable> for Test {
39//!        fn from(table: &mut VoltTable) -> Self {
40//!            Test {
41//!                t1: table.fetch("T1").unwrap(),
42//!                t2: table.fetch("t2").unwrap(),
43//!                t3: table.fetch("t3").unwrap(),
44//!                t4: table.fetch("t4").unwrap(),
45//!                t5: table.fetch("t5").unwrap(),
46//!                t6: table.fetch("t6").unwrap(),
47//!                t7: table.fetch("t7").unwrap(),
48//!                t8: table.fetch("t8").unwrap(),
49//!                t9: table.fetch("t9").unwrap(),
50//!            }
51//!        }
52//!    }
53//!
54//!    let hosts = vec![IpPort::new("localhost".to_string(), 21211)];
55//!    let mut pool = Pool::new(Opts::new(hosts)).unwrap();
56//!
57//!    let mut node = pool.get_conn()?;
58//!    // Create table if not exists.
59//!    let has_table_check = node.query("select t1 from test_types limit 1");
60//!    match has_table_check {
61//!        Ok(_) => {}
62//!        Err(err) => {
63//!            println!("{:?}", err);
64//!            println!("will create table");
65//!            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);";
66//!            node.query(create_table)?;
67//!        }
68//!    }
69//!
70//!    // Insert empty data into table , make sure None is working.
71//!    let insert = "insert into test_types (T1) values (NULL);";
72//!    node.query(insert)?;
73//!    // Insert min/max value to table to validate the encoding.
74//!    node.query("insert into test_types (T1,T2,T3,T4) values (1, -32767, -2147483647, -9223372036854775807 );")?;
75//!    node.query("insert into test_types (T1,T2,T3,T4) values (1, 32767, 2147483647, 9223372036854775807 );")?;
76//!    let mut table = node.query("select * from test_types")?;
77//!    while table.advance_row() {
78//!        let test: Test = table.map_row();
79//!        println!("{:?}", test);
80//!    }
81//!    let bs = vec![1 as u8, 2, 3, 4];
82//!    let time = DateTime::from(SystemTime::now());
83//!    let none_i16: Option<i16> = Option::None;
84//!
85//!
86//!    // call sp with marco `volt_parma!` , test_types.insert is crated with table.
87//!    let mut table = node.call_sp("test_types.insert", volt_param![1,none_i16,3,4,5,6,"7",bs,time])?;
88//!    while table.advance_row() {
89//!        println!("{}", table.debug_row());
90//!    }
91//!
92//!    // upload and create sp if not exists. the proc is looks like this
93//!    // package com.johnny;
94//!    //
95//!    // import org.voltdb.SQLStmt;
96//!    // import org.voltdb.VoltProcedure;
97//!    // import org.voltdb.VoltTable;
98//!    // import org.voltdb.VoltType;
99//!    //
100//!    //
101//!    // public class ApplicationCreate extends VoltProcedure {
102//!    //
103//!    //     final SQLStmt appCreate = new SQLStmt("insert into test_types (T1,T2,T3,T4,T5,T6,T7,T8,T9) values (?,?,?,?,?,?,?,?,?);");
104//!    //
105//!    //
106//!    //     public VoltTable run(VoltTable application) {
107//!    //         if (application.advanceRow()) {
108//!    //             Object[] row = new Object[application.getColumnCount()];
109//!    //             row[0] = application.get(0, VoltType.TINYINT);
110//!    //             row[1] = application.get(1, VoltType.SMALLINT);
111//!    //             row[2] = application.get(2, VoltType.INTEGER);
112//!    //             row[3] = application.get(3, VoltType.BIGINT);
113//!    //             row[4] = application.get(4, VoltType.FLOAT);
114//!    //             row[5] = application.get(5, VoltType.DECIMAL);
115//!    //             row[6] = application.get(6, VoltType.STRING);
116//!    //             row[7] = application.get(7, VoltType.VARBINARY);
117//!    //             row[8] = application.get(8, VoltType.TIMESTAMP);
118//!    //             voltQueueSQL(appCreate, row);
119//!    //         }
120//!    //         voltExecuteSQL(true);
121//!    //         return application;
122//!    //     }
123//!    // }
124//!    let mut table = node.list_procedures()?;
125//!    let mut sp_created = false;
126//!    while table.advance_row() {
127//!        if table.get_string_by_column("PROCEDURE_NAME")?.unwrap() == "ApplicationCreate" {
128//!            sp_created = true;
129//!            println!("already created {}", table.debug_row());
130//!            break;
131//!        }
132//!    }
133//!
134//!    if !sp_created {
135//!        // upload proc into server
136//!        let jars = fs::read("tests/procedures.jar").unwrap();
137//!        let mut table = node.upload_jar(jars).unwrap();
138//!        assert!(table.has_error().is_none());
139//!        let script = "CREATE PROCEDURE  FROM CLASS com.johnny.ApplicationCreate;";
140//!        node.query(script)?;
141//!    }
142//!
143//!
144//!    let null_i16: Option<i16> = Option::None;
145//!    let header = vec!["T1", "T2", "T3", "T4", "T5", "T6", "T7", "T8", "T9"];
146//!    let tp = vec![TINYINT_COLUMN, SHORT_COLUMN, INT_COLUMN, LONG_COLUMN, FLOAT_COLUMN, DECIMAL_COLUMN, STRING_COLUMN, VAR_BIN_COLUMN, TIMESTAMP_COLUMN];
147//!    let header: Vec<String> = header.iter().map(|f| f.to_string()).collect::<Vec<String>>();
148//!    let mut table = VoltTable::new_table(tp, header);
149//!    let decimal = BigDecimal::from(16);
150//!    let data = volt_param! {true, null_i16 , 2147483647 as i32,  9223372036854775807 as i64 ,  15.0, decimal  , "17",bs, time   };
151//!    table.add_row(data)?;
152//!    // call proc with volt table
153//!    let mut res = node.call_sp("ApplicationCreate", volt_param![table])?;
154//!    while res.advance_row() {
155//!        println!("{:?}", res.debug_row());
156//!    }
157//!    Ok({})
158//!}
159
160#![crate_name = "voltdb_client_rust"]
161#![crate_type = "rlib"]
162#![crate_type = "dylib"]
163
164pub mod table;
165pub mod node;
166pub mod encode;
167mod procedure_invocation;
168mod response;
169mod generate;
170mod pool;
171mod encode_option;
172
173pub use chrono;
174pub use bigdecimal;
175
176pub use crate::node::{*};
177pub use crate::table::{*};
178pub use crate::encode::{*};
179pub use crate::pool::{*};
180pub use crate::bigdecimal::BigDecimal;
181pub use crate::chrono::{DateTime, Utc};
182
183#[macro_export]
184macro_rules! volt_param {
185    () => (
186        std::vec::Vec::new()
187    );
188      ( $( $x:expr ),* ) => {
189        {
190            let mut temp_vec = Vec::new();
191            $(
192                temp_vec.push(&$x as & dyn Value);
193            )*
194            temp_vec
195        }
196    };
197}