clickhouse_client/query/
mod.rs

1//! Queries
2
3mod comp;
4mod crud;
5mod data;
6mod exec;
7mod fmt;
8mod result;
9mod sql;
10mod stmt;
11
12#[cfg(test)]
13mod tests;
14
15pub use comp::*;
16pub use crud::*;
17pub use data::*;
18pub use exec::*;
19pub use fmt::*;
20pub use result::*;
21pub use sql::*;
22pub use stmt::*;
23
24use crate::value::{ChValue, Value};
25
26/// Query
27///
28/// A Query object is a complete representation of a query
29#[derive(Default, Debug)]
30pub struct Query {
31    /// Statement (eg SELECT * FROM ...)
32    pub statement: String,
33    /// Data
34    pub data: Option<QueryData>,
35    /// Target DB
36    pub db: Option<String>,
37    /// Credentials (username, password)
38    pub credentials: Option<(String, String)>,
39    /// Format
40    pub format: Option<Format>,
41    /// Compress the request
42    pub compress_request: Option<Compression>,
43    /// Compress the HTTP response
44    pub compress_response: Option<Compression>,
45}
46
47impl Query {
48    /// Creates a new builder
49    pub fn new(stmt: &str) -> Self {
50        Query {
51            statement: stmt.to_string(),
52            data: None,
53            db: None,
54            credentials: None,
55            format: None,
56            compress_request: None,
57            compress_response: None,
58        }
59    }
60
61    /// Asssigns a statement
62    pub fn statement(mut self, stmt: &str) -> Self {
63        self.statement = stmt.to_string();
64        self
65    }
66
67    /// Asssigns the query data
68    pub fn data(mut self, table: QueryData) -> Self {
69        self.data = Some(table);
70        self
71    }
72
73    /// Binds the statement with a [ChValue]
74    ///
75    /// Query parameters are defined by `??`
76    pub fn bind_val(mut self, value: impl ChValue) -> Self {
77        self.statement = self.statement.bind_val(value);
78        self
79    }
80
81    /// Binds the statement with a raw query value
82    ///
83    /// For instance, strings are not enclosed by `'`.
84    ///
85    /// Query parameters are defined by `??`
86    pub fn bind_str(mut self, value: &str) -> Self {
87        self.statement = self.statement.bind_str(value);
88        self
89    }
90
91    /// Binds the statement with a list of values
92    pub fn bind_val_list(mut self, values: Vec<Value>) -> Self {
93        self.statement = self.statement.bind_val_list(values);
94        self
95    }
96
97    /// Binds the statement with a list of valeus as strings
98    pub fn bind_str_list(mut self, values: Vec<&str>) -> Self {
99        self.statement = self.statement.bind_str_list(values);
100        self
101    }
102
103    /// Assigns a target DB
104    pub fn db(mut self, db: &str) -> Self {
105        self.db = Some(db.to_string());
106        self
107    }
108
109    /// Assigns the credentials
110    pub fn credentials(mut self, username: &str, password: &str) -> Self {
111        self.credentials = Some((username.to_string(), password.to_string()));
112        self
113    }
114
115    /// Assigns a format
116    ///
117    /// Eg. RowBinary
118    pub fn format(mut self, format: Format) -> Self {
119        self.format = Some(format);
120        self
121    }
122
123    /// Compress the HTTP request
124    ///
125    /// Eg. RowBinary
126    pub fn compress_request(mut self, compression: Compression) -> Self {
127        self.compress_request = Some(compression);
128        self
129    }
130
131    /// Compress the HTTP response
132    pub fn compress_response(mut self, compression: Compression) -> Self {
133        self.compress_response = Some(compression);
134        self
135    }
136}