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