1 /*
2 * Copyright (c) 2021 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 "kv_store_result_set_impl.h"
17
18 #include "db_errno.h"
19
20 namespace DistributedDB {
21 const int KvStoreResultSetImpl::INIT_POSTION = -1;
22
KvStoreResultSetImpl(IKvDBResultSet * resultSet)23 KvStoreResultSetImpl::KvStoreResultSetImpl(IKvDBResultSet *resultSet)
24 : resultSet_(resultSet)
25 {
26 }
27
GetCount() const28 int KvStoreResultSetImpl::GetCount() const
29 {
30 if (resultSet_ == nullptr) {
31 return 0;
32 }
33 return resultSet_->GetCount();
34 }
35
GetPosition() const36 int KvStoreResultSetImpl::GetPosition() const
37 {
38 if (resultSet_ == nullptr) {
39 return INIT_POSTION;
40 }
41 return resultSet_->GetPosition();
42 }
43
Move(int offset)44 bool KvStoreResultSetImpl::Move(int offset)
45 {
46 int64_t position = GetPosition();
47 int64_t aimPos = position + offset;
48 if (aimPos > INT_MAX) {
49 return MoveToPosition(INT_MAX);
50 }
51 if (aimPos < INIT_POSTION) {
52 return MoveToPosition(INIT_POSTION);
53 }
54 return MoveToPosition(aimPos);
55 }
56
MoveToPosition(int position)57 bool KvStoreResultSetImpl::MoveToPosition(int position)
58 {
59 if (resultSet_ == nullptr) {
60 return false;
61 }
62 if (resultSet_->MoveTo(position) == E_OK) {
63 return true;
64 }
65 return false;
66 }
67
MoveToFirst()68 bool KvStoreResultSetImpl::MoveToFirst()
69 {
70 return MoveToPosition(0);
71 }
72
MoveToLast()73 bool KvStoreResultSetImpl::MoveToLast()
74 {
75 return MoveToPosition(GetCount() - 1);
76 }
77
MoveToNext()78 bool KvStoreResultSetImpl::MoveToNext()
79 {
80 // move 1 step forward in this result set
81 return Move(1);
82 }
83
MoveToPrevious()84 bool KvStoreResultSetImpl::MoveToPrevious()
85 {
86 // move 1 step backward in this result set
87 return Move(-1);
88 }
89
IsFirst() const90 bool KvStoreResultSetImpl::IsFirst() const
91 {
92 if (resultSet_ == nullptr) {
93 return false;
94 }
95 int position = resultSet_->GetPosition();
96 if (GetCount() == 0) {
97 return false;
98 }
99 if (position == 0) {
100 return true;
101 }
102 return false;
103 }
104
IsLast() const105 bool KvStoreResultSetImpl::IsLast() const
106 {
107 if (resultSet_ == nullptr) {
108 return false;
109 }
110 int position = resultSet_->GetPosition();
111 int count = GetCount();
112 if (count == 0) {
113 return false;
114 }
115 if (position == (count - 1)) {
116 return true;
117 }
118 return false;
119 }
120
IsBeforeFirst() const121 bool KvStoreResultSetImpl::IsBeforeFirst() const
122 {
123 if (resultSet_ == nullptr) {
124 return false;
125 }
126 int position = resultSet_->GetPosition();
127
128 if (GetCount() == 0) {
129 return true;
130 }
131 if (position <= INIT_POSTION) {
132 return true;
133 }
134 return false;
135 }
136
IsAfterLast() const137 bool KvStoreResultSetImpl::IsAfterLast() const
138 {
139 if (resultSet_ == nullptr) {
140 return false;
141 }
142 int position = resultSet_->GetPosition();
143 int count = GetCount();
144 if (count == 0) {
145 return true;
146 }
147 if (position >= count) {
148 return true;
149 }
150 return false;
151 }
152
GetEntry(Entry & entry) const153 DBStatus KvStoreResultSetImpl::GetEntry(Entry &entry) const
154 {
155 if (resultSet_ == nullptr) {
156 return DB_ERROR;
157 }
158 if (GetCount() == 0) {
159 return NOT_FOUND;
160 }
161
162 if (resultSet_->GetEntry(entry) == E_OK) {
163 return OK;
164 }
165 return NOT_FOUND;
166 }
167
GetResultSet(IKvDBResultSet * & resultSet) const168 void KvStoreResultSetImpl::GetResultSet(IKvDBResultSet *&resultSet) const
169 {
170 resultSet = resultSet_;
171 }
172
IsClosed() const173 bool KvStoreResultSetImpl::IsClosed() const
174 {
175 return false;
176 }
177
Close()178 void KvStoreResultSetImpl::Close()
179 {
180 return;
181 }
182
GetColumnNames(std::vector<std::string> & columnNames) const183 void KvStoreResultSetImpl::GetColumnNames(std::vector<std::string> &columnNames) const
184 {
185 (void)columnNames;
186 return;
187 }
188
GetColumnType(int columnIndex,ColumnType & columnType) const189 DBStatus KvStoreResultSetImpl::GetColumnType(int columnIndex, ColumnType &columnType) const
190 {
191 (void)columnIndex;
192 (void)columnType;
193 return NOT_SUPPORT;
194 }
195
GetColumnIndex(const std::string & columnName,int & columnIndex) const196 DBStatus KvStoreResultSetImpl::GetColumnIndex(const std::string &columnName, int &columnIndex) const
197 {
198 (void)columnIndex;
199 (void)columnName;
200 return NOT_SUPPORT;
201 }
202
GetColumnName(int columnIndex,std::string & columnName) const203 DBStatus KvStoreResultSetImpl::GetColumnName(int columnIndex, std::string &columnName) const
204 {
205 (void)columnIndex;
206 (void)columnName;
207 return NOT_SUPPORT;
208 }
209
Get(int columnIndex,std::vector<uint8_t> & value) const210 DBStatus KvStoreResultSetImpl::Get(int columnIndex, std::vector<uint8_t> &value) const
211 {
212 (void)columnIndex;
213 (void)value;
214 return NOT_SUPPORT;
215 }
216
Get(int columnIndex,std::string & value) const217 DBStatus KvStoreResultSetImpl::Get(int columnIndex, std::string &value) const
218 {
219 (void)columnIndex;
220 (void)value;
221 return NOT_SUPPORT;
222 }
223
Get(int columnIndex,int64_t & value) const224 DBStatus KvStoreResultSetImpl::Get(int columnIndex, int64_t &value) const
225 {
226 (void)columnIndex;
227 (void)value;
228 return NOT_SUPPORT;
229 }
230
Get(int columnIndex,double & value) const231 DBStatus KvStoreResultSetImpl::Get(int columnIndex, double &value) const
232 {
233 (void)columnIndex;
234 (void)value;
235 return NOT_SUPPORT;
236 }
237
IsColumnNull(int columnIndex,bool & isNull) const238 DBStatus KvStoreResultSetImpl::IsColumnNull(int columnIndex, bool &isNull) const
239 {
240 (void)columnIndex;
241 (void)isNull;
242 return NOT_SUPPORT;
243 }
244
GetRow(std::map<std::string,VariantData> & data) const245 DBStatus KvStoreResultSetImpl::GetRow(std::map<std::string, VariantData> &data) const
246 {
247 (void)data;
248 return NOT_SUPPORT;
249 }
250 } // namespace DistributedDB
251