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