clickhouse_client/
lib.rs

1//! Clickhouse client
2//!
3//! This crate provides a Clickhouse client.
4//!
5//! - HTTP interface
6//! - Query builder
7//! - ORM to map to Rust types
8//!
9//! # Features
10//!
11//! - **time**: support for the `time` crate types
12//! - **uuid**: support for the `uuid` crate types
13
14#![deny(missing_docs)]
15
16#[cfg(test)]
17mod tests;
18
19use intf::{http::Http, Interface};
20
21pub mod error;
22pub mod intf;
23pub mod orm;
24pub mod query;
25pub mod schema;
26pub mod value;
27
28/// Clickhouse client
29pub struct Client<T>
30where
31    T: Interface,
32{
33    /// Database
34    pub db: Option<String>,
35    /// Credentials
36    pub credentials: Option<(String, String)>,
37    /// Interface
38    pub interface: T,
39}
40
41impl Default for Client<Http> {
42    fn default() -> Client<Http> {
43        let interface = Http::new("http://localhost:8123");
44        Self {
45            db: None,
46            credentials: Default::default(),
47            interface,
48        }
49    }
50}
51
52impl<T> std::fmt::Debug for Client<T>
53where
54    T: Interface + std::fmt::Debug,
55{
56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57        f.debug_struct("Client")
58            .field("db", &self.db)
59            .field("credentials", &self.credentials)
60            .field("interface", &self.interface)
61            .finish()
62    }
63}
64
65impl<T> Clone for Client<T>
66where
67    T: Interface + Clone,
68{
69    fn clone(&self) -> Self {
70        Self {
71            db: self.db.clone(),
72            credentials: self.credentials.clone(),
73            interface: self.interface.clone(),
74        }
75    }
76}
77
78impl<T> Client<T>
79where
80    T: Interface,
81{
82    /// Sets the target database
83    pub fn database(mut self, db: &str) -> Self {
84        self.db = Some(db.to_string());
85        self
86    }
87
88    /// Adds the credentials
89    pub fn credentials(mut self, username: &str, password: &str) -> Self {
90        self.credentials = Some((username.to_string(), password.to_string()));
91        self
92    }
93}
94
95/// Client with the HTTP interface
96pub type HttpClient = Client<Http>;