streamhouse/
lib.rs

1extern crate self as streamhouse;
2
3mod error;
4pub use error::Error;
5
6mod query;
7mod stream;
8pub mod types;
9
10pub(crate) mod row;
11pub use row::Row;
12pub(crate) use row::{Column, WriteRowBinary};
13pub(crate) use types::ColumnType;
14
15/// Types that are used internally in `#[derive(Row)]`
16pub mod internal {
17    pub use crate::row::{Bytes, Column, WriteRowBinary};
18    pub use crate::types::ColumnType;
19}
20
21/// Derive macro for the [`Row`] trait
22pub use streamhouse_derive::Row;
23
24/// A client for accessing clickhouse.
25///
26/// Note that cloning the `Client` is reasonably inexpensive, and internally it
27/// stores a connection pool.
28pub struct Client {
29    client: hyper::Client<hyper::client::HttpConnector>,
30    url: String,
31    user: Option<String>,
32    password: Option<String>,
33    database: Option<String>,
34}
35
36impl Client {
37    pub fn builder() -> ClientBuilder {
38        ClientBuilder::default()
39    }
40}
41
42/// A builder which is the only way to construct a [`Client`].
43#[derive(Default, Clone)]
44pub struct ClientBuilder {
45    client: hyper::client::Builder,
46    url: Option<String>,
47    user: Option<String>,
48    password: Option<String>,
49    database: Option<String>,
50}
51
52impl ClientBuilder {
53    pub fn with_url(self, url: impl Into<String>) -> Self {
54        ClientBuilder {
55            url: Some(url.into()),
56            ..self
57        }
58    }
59    pub fn with_user(self, user: impl Into<String>) -> Self {
60        ClientBuilder {
61            user: Some(user.into()),
62            ..self
63        }
64    }
65    pub fn with_password(self, password: impl Into<String>) -> Self {
66        ClientBuilder {
67            password: Some(password.into()),
68            ..self
69        }
70    }
71    pub fn with_database(self, database: impl Into<String>) -> Self {
72        ClientBuilder {
73            database: Some(database.into()),
74            ..self
75        }
76    }
77    pub fn build(self) -> Client {
78        Client {
79            client: self.client.build_http(),
80            url: self.url.expect("Need to specify url for Client"),
81            user: self.user,
82            password: self.password,
83            database: self.database,
84        }
85    }
86}