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 "rdb_cursor.h"
17 
18 #include "result_set.h"
19 #include "value_proxy.h"
20 #include "store_types.h"
21 namespace OHOS::DistributedRdb {
22 using namespace OHOS::DistributedData;
23 using namespace DistributedDB;
RdbCursor(std::shared_ptr<DistributedDB::ResultSet> resultSet)24 RdbCursor::RdbCursor(std::shared_ptr<DistributedDB::ResultSet> resultSet)
25     : resultSet_(std::move(resultSet))
26 {
27 }
28 
~RdbCursor()29 RdbCursor::~RdbCursor()
30 {
31     if (resultSet_) {
32         resultSet_->Close();
33     }
34     resultSet_ = nullptr;
35 }
36 
GetColumnNames(std::vector<std::string> & names) const37 int32_t RdbCursor::GetColumnNames(std::vector<std::string> &names) const
38 {
39     resultSet_->GetColumnNames(names);
40     return GeneralError::E_OK;
41 }
42 
GetColumnName(int32_t col,std::string & name) const43 int32_t RdbCursor::GetColumnName(int32_t col, std::string &name) const
44 {
45     return resultSet_->GetColumnName(col, name) == DBStatus::OK ? GeneralError::E_OK : GeneralError::E_ERROR;
46 }
47 
GetColumnType(int32_t col) const48 int32_t RdbCursor::GetColumnType(int32_t col) const
49 {
50     ResultSet::ColumnType dbColumnType = ResultSet::ColumnType::INVALID_TYPE;
51     auto status = resultSet_->GetColumnType(col, dbColumnType);
52     if (status != DBStatus::OK) {
53         dbColumnType = ResultSet::ColumnType::INVALID_TYPE;
54     }
55     return Convert(dbColumnType);
56 }
57 
GetCount() const58 int32_t RdbCursor::GetCount() const
59 {
60     return resultSet_->GetCount();
61 }
62 
MoveToFirst()63 int32_t RdbCursor::MoveToFirst()
64 {
65     return resultSet_->MoveToFirst() ? GeneralError::E_OK : GeneralError::E_ERROR;
66 }
67 
MoveToNext()68 int32_t RdbCursor::MoveToNext()
69 {
70     return resultSet_->MoveToNext() ? GeneralError::E_OK : GeneralError::E_ERROR;
71 }
72 
MoveToPrev()73 int32_t RdbCursor::MoveToPrev()
74 {
75     return resultSet_->MoveToPrevious() ? GeneralError::E_OK : GeneralError::E_ERROR;
76 }
77 
GetEntry(VBucket & entry)78 int32_t RdbCursor::GetEntry(VBucket &entry)
79 {
80     return GetRow(entry);
81 }
82 
GetRow(VBucket & data)83 int32_t RdbCursor::GetRow(VBucket &data)
84 {
85     std::map<std::string, VariantData> bucket;
86     auto ret = resultSet_->GetRow(bucket);
87     data = ValueProxy::Convert(std::move(bucket));
88     return ret == DBStatus::OK ? GeneralError::E_OK : GeneralError::E_ERROR;
89 }
90 
Get(int32_t col,DistributedData::Value & value)91 int32_t RdbCursor::Get(int32_t col, DistributedData::Value &value)
92 {
93     std::string name;
94     auto status = resultSet_->GetColumnName(col, name);
95     if (status != DBStatus::OK) {
96         return GeneralError::E_ERROR;
97     }
98     VBucket bucket;
99     if (GetRow(bucket) != GeneralError::E_OK) {
100         return GeneralError::E_ERROR;
101     }
102     value = bucket[name];
103     return GeneralError::E_OK;
104 }
105 
Get(const std::string & col,DistributedData::Value & value)106 int32_t RdbCursor::Get(const std::string &col, DistributedData::Value &value)
107 {
108     int32_t index = -1;
109     auto ret = resultSet_->GetColumnIndex(col, index);
110     if (ret != DBStatus::OK) {
111         return GeneralError::E_ERROR;
112     }
113     return Get(index, value);
114 }
115 
Close()116 int32_t RdbCursor::Close()
117 {
118     resultSet_->Close();
119     return GeneralError::E_OK;
120 }
121 
IsEnd()122 bool RdbCursor::IsEnd()
123 {
124     return resultSet_->IsAfterLast();
125 }
126 
Convert(ResultSet::ColumnType columnType)127 int32_t RdbCursor::Convert(ResultSet::ColumnType columnType)
128 {
129     switch (columnType) {
130         case ResultSet::ColumnType::INT64:
131             return TYPE_INDEX<int64_t>;
132         case ResultSet::ColumnType::STRING:
133             return TYPE_INDEX<std::string>;
134         case ResultSet::ColumnType::BLOB:
135             return TYPE_INDEX<std::vector<uint8_t>>;
136         case ResultSet::ColumnType::DOUBLE:
137             return TYPE_INDEX<double>;
138         case ResultSet::ColumnType::NULL_VALUE:
139             return TYPE_INDEX<std::monostate>;
140         case ResultSet::ColumnType::INVALID_TYPE:
141             return TYPE_INDEX<std::monostate>;
142         default:
143             return TYPE_INDEX<std::monostate>;
144     }
145 }
146 } // namespace OHOS::DistributedRdb
147