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 
16 #include "cache_cursor.h"
17 
18 #include "value_proxy.h"
19 
20 namespace OHOS::DistributedRdb {
21 using namespace OHOS::DistributedData;
22 
CacheCursor(std::vector<DistributedData::VBucket> &&records)23 CacheCursor::CacheCursor(std::vector<DistributedData::VBucket> &&records)
24     : row_(0), maxCol_(0), records_(std::move(records))
25 {
26     maxRow_ = records_.size();
27     if (maxRow_ > 0) {
28         for (auto it = records_[0].begin(); it != records_[0].end(); it++) {
29             colNames_.push_back(it->first);
30             colTypes_.push_back(it->second.index());
31         }
32         maxCol_ = colNames_.size();
33     }
34 }
35 
GetColumnNames(std::vector<std::string> &names) const36 int32_t CacheCursor::GetColumnNames(std::vector<std::string> &names) const
37 {
38     names = colNames_;
39     return GeneralError::E_OK;
40 }
41 
42 
GetColumnName(int32_t col, std::string &name) const43 int32_t CacheCursor::GetColumnName(int32_t col, std::string &name) const
44 {
45     if (col < 0 || col >= maxCol_) {
46         return GeneralError::E_INVALID_ARGS;
47     }
48     name = colNames_[col];
49     return GeneralError::E_OK;
50 }
51 
GetColumnType(int32_t col) const52 int32_t CacheCursor::GetColumnType(int32_t col) const
53 {
54     if (col < 0 || col >= maxCol_) {
55         return DistributedDB::ResultSet::ColumnType::INVALID_TYPE;
56     }
57     return colTypes_[col];
58 }
59 
GetCount() const60 int32_t CacheCursor::GetCount() const
61 {
62     return maxRow_;
63 }
64 
MoveToFirst()65 int32_t CacheCursor::MoveToFirst()
66 {
67     row_ = 0;
68     return GeneralError::E_OK;
69 }
70 
MoveToNext()71 int32_t CacheCursor::MoveToNext()
72 {
73     auto row = row_;
74     if (row >= maxRow_ - 1) {
75         return GeneralError::E_ERROR;
76     }
77     row++;
78     row_ = row;
79     return GeneralError::E_OK;
80 }
81 
MoveToPrev()82 int32_t CacheCursor::MoveToPrev()
83 {
84     return GeneralError::E_NOT_SUPPORT;
85 }
86 
GetEntry(DistributedData::VBucket &entry)87 int32_t CacheCursor::GetEntry(DistributedData::VBucket &entry)
88 {
89     return GetRow(entry);
90 }
91 
GetRow(DistributedData::VBucket &data)92 int32_t CacheCursor::GetRow(DistributedData::VBucket &data)
93 {
94     auto row = row_;
95     if (row >= maxRow_) {
96         return GeneralError::E_RECODE_LIMIT_EXCEEDED;
97     }
98     data = records_[row];
99     return GeneralError::E_OK;
100 }
101 
Get(int32_t col, DistributedData::Value &value)102 int32_t CacheCursor::Get(int32_t col, DistributedData::Value &value)
103 {
104     if (col < 0 || col >= maxCol_) {
105         return GeneralError::E_INVALID_ARGS;
106     }
107     return Get(colNames_[col], value);
108 }
109 
Get(const std::string &col, DistributedData::Value &value)110 int32_t CacheCursor::Get(const std::string &col, DistributedData::Value &value)
111 {
112     auto row = row_;
113     if (row >= maxRow_) {
114         return GeneralError::E_RECODE_LIMIT_EXCEEDED;
115     }
116     auto it = records_[row].find(col);
117     if (it == records_[row].end()) {
118         return GeneralError::E_INVALID_ARGS;
119     }
120     value = it->second;
121     return GeneralError::E_OK;
122 }
123 
Close()124 int32_t CacheCursor::Close()
125 {
126     return GeneralError::E_OK;
127 }
128 
IsEnd()129 bool CacheCursor::IsEnd()
130 {
131     return false;
132 }
133 }