client/
lib.rs

1// Copyright 2023 Greptime Team
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#![feature(assert_matches)]
16
17mod client;
18pub mod client_manager;
19pub mod database;
20pub mod error;
21pub mod flow;
22pub mod load_balance;
23mod metrics;
24pub mod region;
25
26pub use api;
27use api::v1::greptime_response::Response;
28use api::v1::{AffectedRows, GreptimeResponse};
29pub use common_catalog::consts::{DEFAULT_CATALOG_NAME, DEFAULT_SCHEMA_NAME};
30use common_error::status_code::StatusCode;
31pub use common_query::{Output, OutputData, OutputMeta};
32pub use common_recordbatch::{RecordBatches, SendableRecordBatchStream};
33use snafu::OptionExt;
34
35pub use self::client::Client;
36pub use self::database::Database;
37pub use self::error::{Error, Result};
38use crate::error::{IllegalDatabaseResponseSnafu, ServerSnafu};
39
40pub fn from_grpc_response(response: GreptimeResponse) -> Result<u32> {
41    let header = response.header.context(IllegalDatabaseResponseSnafu {
42        err_msg: "missing header",
43    })?;
44    let status = header.status.context(IllegalDatabaseResponseSnafu {
45        err_msg: "missing status",
46    })?;
47
48    if StatusCode::is_success(status.status_code) {
49        let res = response.response.context(IllegalDatabaseResponseSnafu {
50            err_msg: "missing response",
51        })?;
52        match res {
53            Response::AffectedRows(AffectedRows { value }) => Ok(value),
54        }
55    } else {
56        let status_code =
57            StatusCode::from_u32(status.status_code).context(IllegalDatabaseResponseSnafu {
58                err_msg: format!("invalid status: {:?}", status),
59            })?;
60        ServerSnafu {
61            code: status_code,
62            msg: status.err_msg,
63        }
64        .fail()
65    }
66}