typed_clickhouse/
lib.rs

1use std::collections::HashMap;
2
3use hyper::{self, client::HttpConnector};
4
5use self::error::Result;
6pub use self::{
7    insert::Insert, inserter::Inserter, introspection::Reflection, query::Query, watch::Watch,
8};
9
10mod buflist;
11pub mod error;
12mod insert;
13mod inserter;
14mod introspection;
15mod query;
16mod response;
17mod rowbinary;
18mod sql_builder;
19mod watch;
20
21mod sealed {
22    pub trait Sealed {}
23}
24
25#[derive(Clone)]
26pub struct Client {
27    client: hyper::Client<HttpConnector>,
28
29    url: String,
30    database: Option<String>,
31    user: Option<String>,
32    password: Option<String>,
33    options: HashMap<String, String>,
34}
35
36impl Default for Client {
37    fn default() -> Self {
38        Self {
39            client: hyper::Client::new(),
40            url: String::new(),
41            database: None,
42            user: None,
43            password: None,
44            options: HashMap::new(),
45        }
46    }
47}
48
49impl Client {
50    // TODO: use `url` crate?
51    pub fn with_url(mut self, url: impl Into<String>) -> Self {
52        self.url = url.into();
53        self
54    }
55
56    pub fn with_database(mut self, database: impl Into<String>) -> Self {
57        self.database = Some(database.into());
58        self
59    }
60
61    pub fn with_user(mut self, user: impl Into<String>) -> Self {
62        self.user = Some(user.into());
63        self
64    }
65
66    pub fn with_password(mut self, password: impl Into<String>) -> Self {
67        self.password = Some(password.into());
68        self
69    }
70
71    pub fn with_option(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
72        self.options.insert(name.into(), value.into());
73        self
74    }
75
76    pub fn insert<T: Reflection>(&self, table: &str) -> Result<Insert<T>> {
77        Insert::new(self, table)
78    }
79
80    pub fn inserter<T: Reflection>(&self, table: &str) -> Result<Inserter<T>> {
81        Inserter::new(self, table)
82    }
83
84    pub fn query(&self, query: &str) -> Query {
85        Query::new(self, query)
86    }
87
88    pub fn watch(&self, query: &str) -> Watch {
89        Watch::new(self, query)
90    }
91}