1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
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 #define LOG_TAG "GdbDataSet"
16 #include "full_result.h"
17
18 #include "gdb_errors.h"
19 #include "statement.h"
20 #include "logger.h"
21
22 namespace OHOS::DistributedDataAip {
InitData(std::shared_ptr<Statement> stmt)23 int32_t FullResult::InitData(std::shared_ptr<Statement> stmt)
24 {
25 if (stmt == nullptr) {
26 return E_STATEMENT_EMPTY;
27 }
28 data_ = std::vector<std::unordered_map<std::string, GraphValue>>();
29 int32_t errCode = stmt->Step();
30 while (errCode == E_OK) {
31 auto [ret, oneResult] = GetRow(stmt);
32 if (ret != E_OK) {
33 return ret;
34 }
35 data_.emplace_back(oneResult);
36 errCode = stmt->Step();
37 }
38 return (errCode == E_GRD_NO_DATA) ? E_OK : errCode;
39 }
40
GetAllData() const41 std::vector<std::unordered_map<std::string, GraphValue>> FullResult::GetAllData() const
42 {
43 return data_;
44 }
GetRow(std::shared_ptr<Statement> stmt)45 std::pair<int32_t, std::unordered_map<std::string, GraphValue>> FullResult::GetRow(std::shared_ptr<Statement> stmt)
46 {
47 std::unordered_map<std::string, GraphValue> res;
48 if (stmt == nullptr) {
49 return { E_STATEMENT_EMPTY, res };
50 }
51 auto columnCount = stmt->GetColumnCount();
52 if (columnCount == 0) {
53 LOG_ERROR("GetKeys failed ret=%{public}d.", E_NO_DATA);
54 return { E_NO_DATA, res };
55 }
56
57 for (uint32_t i = 0; i < columnCount; i++) {
58 auto [ret, key] = stmt->GetColumnName(i);
59 if (ret != E_OK) {
60 LOG_ERROR("GetKeys failed ret=%{public}d.", ret);
61 return { ret, res };
62 }
63 auto [err, value] = stmt->GetColumnValue(i);
64 if (err != E_OK) {
65 LOG_ERROR("GetValue failed, key=%{public}s, ret=%{public}d.", key.c_str(), err);
66 return { err, res };
67 }
68 res.emplace(key, value);
69 }
70 return { E_OK, res };
71 }
72
73 } // namespace OHOS::DistributedDataAip