databend_client/
session.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
15use serde::{Deserialize, Serialize};
16use std::collections::{BTreeMap, HashMap};
17
18#[derive(Deserialize, Serialize, Debug, Default, Clone)]
19pub struct SessionState {
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub catalog: Option<String>,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub database: Option<String>,
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub settings: Option<BTreeMap<String, String>>,
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub role: Option<String>,
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub secondary_roles: Option<Vec<String>>,
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub txn_state: Option<String>,
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub need_sticky: Option<bool>,
34    #[serde(skip_serializing_if = "Option::is_none")]
35    pub need_keep_alive: Option<bool>,
36
37    // hide fields of no interest (but need to send back to server in next query)
38    #[serde(flatten)]
39    additional_fields: HashMap<String, serde_json::Value>,
40}
41
42impl SessionState {
43    pub fn with_settings(mut self, settings: Option<BTreeMap<String, String>>) -> Self {
44        self.settings = settings;
45        self
46    }
47
48    pub fn with_database(mut self, database: Option<String>) -> Self {
49        self.database = database;
50        self
51    }
52
53    pub fn with_role(mut self, role: Option<String>) -> Self {
54        self.role = role;
55        self
56    }
57}