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
//! # QuestDB
//!
//! The questdb crate allows for a simple way of connecting to a questdb instance.
//!
//! # Usage
//! ```no-test
//! [dependencies]
//! questdb = "0.1"
//! ```
//!
//! # Example
//! ```no-test
//! use questdb::QuestDB;
//! use serde::{Serialize, Deserialize};
//!
//!
//! #[derive(Serialize, Deserialize, Debug)]
//! struct TestData {
//!     id: i32,
//!     ts: String,
//!     temp: f64,
//!     sensor_id: i32,
//! }
//!
//! #[tokio::main]
//! async fn main() {
//!     let connection = QuestDB::new("http://192.168.1.37:9000");
//!
//!     let res = connection.exec::<TestData>(
//!         "select * from readings",
//!         Some(2),
//!         None,
//!         None
//!     ).await.unwrap();
//!
//!     println!("{:#?}", res);
//!
//!     /* Output:
//!     [
//!         TestData {
//!             id: 1,
//!             ts: "2019-10-17T00:00:00.000000Z",
//!             temp: 16.470730545675295,
//!             sensor_id: 295,
//!         },
//!         TestData {
//!             id: 2,
//!             ts: "2019-10-17T00:00:00.100000Z",
//!             temp: 19.75780877621018,
//!             sensor_id: 9835,
//!         },
//!     ]
//!     */
//!
//! }
//!
//! ```

mod api;
mod error;
mod types;

/// Object to connect to a questdb
pub use api::QuestDB;

/// Custom error
pub use error::Error;

/// Schema types
pub use types::Schema;

use serde::Deserialize;

#[derive(Deserialize, Debug)]
struct TestData {
    id: i32,
    ts: String,
    temp: f64,
    sensor_id: i32,
}

#[cfg(test)]
mod tests {
    use crate::{
        api::QuestDB,
        TestData,
        new_schema,
        Schema,
    };
    use std::fs::File;

    #[tokio::test]
    async fn test_exec() {
        let connection = QuestDB::new("http://192.168.1.37:9000");
        let _res = match connection.exec::<TestData>("select * from nu_table", Some(5), None, None).await {
            Ok(res) => res,
            Err(e) => {
                println!("{}", e);
                return;
            }
        };
    }

    #[tokio::test]
    async fn test_imp() {
        let connection = QuestDB::new("http://192.168.1.37:9000");

        let schema = new_schema!(("movieId", Schema::String), ("imdbId", Schema::Int));

        let _res = match connection.imp(
            "./test.csv",
            Some(schema),
            "nu_table123",
            None,
            None,
            None,
        ).await {
            Ok(res) => res,
            Err(e) => {
                println!("{}", e);
                return;
            }
        };
    }

    #[tokio::test]
    async fn test_exp() {
        let connection = QuestDB::new("http://192.168.1.37:9000");

        let mut output_file = File::create("output.csv").unwrap();
        let _res = match connection.exp("select * from nu_table", Some(5), &mut output_file).await {
            Ok(res) => res,
            Err(e) => {
                println!("{}", e);
                return;
            }
        };
    }
}