opensrv_clickhouse/types/query.rs
1// Copyright 2021 Datafuse Labs.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#[derive(Clone, Debug)]
16pub struct Query {
17 sql: String,
18 id: String,
19}
20
21#[allow(dead_code)]
22impl Query {
23 pub fn new(sql: impl AsRef<str>) -> Self {
24 Self {
25 sql: sql.as_ref().to_string(),
26 id: "".to_string(),
27 }
28 }
29
30 #[must_use]
31 pub fn id(self, id: impl AsRef<str>) -> Self {
32 Self {
33 id: id.as_ref().to_string(),
34 ..self
35 }
36 }
37
38 pub(crate) fn get_sql(&self) -> &str {
39 &self.sql
40 }
41
42 pub(crate) fn get_id(&self) -> &str {
43 &self.id
44 }
45
46 pub(crate) fn map_sql<F>(self, f: F) -> Self
47 where
48 F: Fn(&str) -> String,
49 {
50 Self {
51 sql: f(&self.sql),
52 ..self
53 }
54 }
55}
56
57impl<T> From<T> for Query
58where
59 T: AsRef<str>,
60{
61 fn from(source: T) -> Self {
62 Self::new(source)
63 }
64}