Skip to main content

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