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