• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     return GeneralError::E_NOT_SUPPORT;
84 }
85 
GetRow(DistributedData::VBucket & data)86 int32_t CursorMock::GetRow(DistributedData::VBucket &data)
87 {
88     if (index_ >= 0 && index_ < resultSet_->size()) {
89         data = (*resultSet_)[index_];
90     }
91     return GeneralError::E_OK;
92 }
93 
Get(int32_t col,DistributedData::Value & value)94 int32_t CursorMock::Get(int32_t col, DistributedData::Value &value)
95 {
96     if (index_ >= 0 && index_ < resultSet_->size()) {
97         int i = 0;
98         for (auto &[k, v] : (*resultSet_)[index_]) {
99             if (i++ == col) {
100                 value = v;
101                 break;
102             }
103         }
104     }
105     return GeneralError::E_OK;
106 }
107 
Get(const std::string & col,DistributedData::Value & value)108 int32_t CursorMock::Get(const std::string &col, DistributedData::Value &value)
109 {
110     if (index_ >= 0 && index_ < resultSet_->size()) {
111         for (auto &[k, v] : (*resultSet_)[index_]) {
112             if (k == col) {
113                 value = v;
114                 break;
115             }
116         }
117     }
118     return GeneralError::E_OK;
119 }
120 
Close()121 int32_t CursorMock::Close()
122 {
123     resultSet_->clear();
124     return GeneralError::E_OK;
125 }
126 
IsEnd()127 bool CursorMock::IsEnd()
128 {
129     return index_ == resultSet_->size() - 1;
130 }
131 } // namespace DistributedData
132 } // namespace OHOS
133