1 /*
2  * Copyright (c) 2023 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 #include "cursor_mock.h"
16 
17 #include "store/general_value.h"
18 namespace OHOS {
19 namespace DistributedData {
CursorMock(std::shared_ptr<ResultSet> resultSet)20 CursorMock::CursorMock(std::shared_ptr<ResultSet> resultSet) : resultSet_(std::move(resultSet)) {}
~CursorMock()21 CursorMock::~CursorMock() {}
GetColumnNames(std::vector<std::string> &names) const22 int32_t CursorMock::GetColumnNames(std::vector<std::string> &names) const
23 {
24     if (!resultSet_->empty()) {
25         for (auto &[key, value] : *resultSet_->begin()) {
26             names.push_back(key);
27         }
28     }
29     return GeneralError::E_OK;
30 }
31 
GetColumnName(int32_t col, std::string &name) const32 int32_t CursorMock::GetColumnName(int32_t col, std::string &name) const
33 {
34     if (!resultSet_->empty()) {
35         int index = 0;
36         for (auto &[key, value] : *resultSet_->begin()) {
37             if (index++ == col) {
38                 name = key;
39                 break;
40             }
41         }
42     }
43     return GeneralError::E_OK;
44 }
45 
GetColumnType(int32_t col) const46 int32_t CursorMock::GetColumnType(int32_t col) const
47 {
48     if (!resultSet_->empty()) {
49         int index = 0;
50         for (auto &[key, value] : *resultSet_->begin()) {
51             if (index++ == col) {
52                 return value.index();
53             }
54         }
55     }
56     return GeneralError::E_OK;
57 }
58 
GetCount() const59 int32_t CursorMock::GetCount() const
60 {
61     return resultSet_->size();
62 }
63 
MoveToFirst()64 int32_t CursorMock::MoveToFirst()
65 {
66     index_ = 0;
67     return GeneralError::E_OK;
68 }
69 
MoveToNext()70 int32_t CursorMock::MoveToNext()
71 {
72     index_++;
73     return GeneralError::E_OK;
74 }
75 
MoveToPrev()76 int32_t CursorMock::MoveToPrev()
77 {
78     index_--;
79     return GeneralError::E_OK;
80 }
81 
GetEntry(DistributedData::VBucket &entry)82 int32_t CursorMock::GetEntry(DistributedData::VBucket &entry)
83 {
84     GetRow(entry);
85     return GeneralError::E_OK;
86 }
87 
GetRow(DistributedData::VBucket &data)88 int32_t CursorMock::GetRow(DistributedData::VBucket &data)
89 {
90     if (index_ >= 0 && index_ < resultSet_->size()) {
91         data = (*resultSet_)[index_];
92     }
93     return GeneralError::E_OK;
94 }
95 
Get(int32_t col, DistributedData::Value &value)96 int32_t CursorMock::Get(int32_t col, DistributedData::Value &value)
97 {
98     if (index_ >= 0 && index_ < resultSet_->size()) {
99         int i = 0;
100         for (auto &[k, v] : (*resultSet_)[index_]) {
101             if (i++ == col) {
102                 value = v;
103                 break;
104             }
105         }
106     }
107     return GeneralError::E_OK;
108 }
109 
Get(const std::string &col, DistributedData::Value &value)110 int32_t CursorMock::Get(const std::string &col, DistributedData::Value &value)
111 {
112     if (index_ >= 0 && index_ < resultSet_->size()) {
113         for (auto &[k, v] : (*resultSet_)[index_]) {
114             if (k == col) {
115                 value = v;
116                 break;
117             }
118         }
119     }
120     return GeneralError::E_OK;
121 }
122 
Close()123 int32_t CursorMock::Close()
124 {
125     resultSet_->clear();
126     return GeneralError::E_OK;
127 }
128 
IsEnd()129 bool CursorMock::IsEnd()
130 {
131     return index_ == resultSet_->size() - 1;
132 }
133 } // namespace DistributedData
134 } // namespace OHOS
135