• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "CacheResultSet"
17 #include "cache_result_set.h"
18 
19 #include <algorithm>
20 #include <string>
21 
22 #include "abs_result_set.h"
23 #include "logger.h"
24 #include "rdb_errno.h"
25 #include "rdb_trace.h"
26 namespace OHOS {
27 namespace NativeRdb {
CacheResultSet()28 CacheResultSet::CacheResultSet() : row_(0), maxRow_(0), maxCol_(0)
29 {
30 }
CacheResultSet(std::vector<NativeRdb::ValuesBucket> && valueBuckets)31 CacheResultSet::CacheResultSet(std::vector<NativeRdb::ValuesBucket> &&valueBuckets)
32     : row_(0), maxCol_(0), valueBuckets_(std::move(valueBuckets))
33 {
34     maxRow_ = static_cast<int>(valueBuckets_.size());
35     if (maxRow_ > 0) {
36         for (auto it = valueBuckets_[0].values_.begin(); it != valueBuckets_[0].values_.end(); it++) {
37             colNames_.push_back(it->first);
38             colTypes_.push_back(it->second.GetType());
39         }
40         maxCol_ = static_cast<int>(colNames_.size());
41     }
42 }
43 
~CacheResultSet()44 CacheResultSet::~CacheResultSet()
45 {
46 }
47 
GetRowCount(int & count)48 int CacheResultSet::GetRowCount(int &count)
49 {
50     count = static_cast<int>(maxRow_);
51     return E_OK;
52 }
53 
GetAllColumnNames(std::vector<std::string> & columnNames)54 int CacheResultSet::GetAllColumnNames(std::vector<std::string> &columnNames)
55 {
56     columnNames = colNames_;
57     return E_OK;
58 }
59 
GetBlob(int columnIndex,std::vector<uint8_t> & blob)60 int CacheResultSet::GetBlob(int columnIndex, std::vector<uint8_t> &blob)
61 {
62     if (columnIndex < 0 || columnIndex >= maxCol_) {
63         return E_INVALID_ARGS;
64     }
65     auto name = colNames_[columnIndex];
66     std::shared_lock<decltype(rwMutex_)> lock(rwMutex_);
67     if (row_ < 0 || row_ >= maxRow_) {
68         return E_ERROR;
69     }
70     return valueBuckets_[row_].values_[name].GetBlob(blob);
71 }
72 
GetString(int columnIndex,std::string & value)73 int CacheResultSet::GetString(int columnIndex, std::string &value)
74 {
75     if (columnIndex < 0 || columnIndex >= maxCol_) {
76         return E_INVALID_ARGS;
77     }
78     auto name = colNames_[columnIndex];
79     std::shared_lock<decltype(rwMutex_)> lock(rwMutex_);
80     if (row_ < 0 || row_ >= maxRow_) {
81         return E_ERROR;
82     }
83     return valueBuckets_[row_].values_[name].GetString(value);
84 }
85 
GetInt(int columnIndex,int & value)86 int CacheResultSet::GetInt(int columnIndex, int &value)
87 {
88     if (columnIndex < 0 || columnIndex >= maxCol_) {
89         return E_INVALID_ARGS;
90     }
91     auto name = colNames_[columnIndex];
92     std::shared_lock<decltype(rwMutex_)> lock(rwMutex_);
93     if (row_ < 0 || row_ >= maxRow_) {
94         return E_ERROR;
95     }
96     return valueBuckets_[row_].values_[name].GetInt(value);
97 }
98 
GetLong(int columnIndex,int64_t & value)99 int CacheResultSet::GetLong(int columnIndex, int64_t &value)
100 {
101     if (columnIndex < 0 || columnIndex >= maxCol_) {
102         return E_INVALID_ARGS;
103     }
104     auto name = colNames_[columnIndex];
105     std::shared_lock<decltype(rwMutex_)> lock(rwMutex_);
106     if (row_ < 0 || row_ >= maxRow_) {
107         return E_ERROR;
108     }
109     return valueBuckets_[row_].values_[name].GetLong(value);
110 }
111 
GetDouble(int columnIndex,double & value)112 int CacheResultSet::GetDouble(int columnIndex, double &value)
113 {
114     if (columnIndex < 0 || columnIndex >= maxCol_) {
115         return E_INVALID_ARGS;
116     }
117     auto name = colNames_[columnIndex];
118     std::shared_lock<decltype(rwMutex_)> lock(rwMutex_);
119     if (row_ < 0 || row_ >= maxRow_) {
120         return E_ERROR;
121     }
122     return valueBuckets_[row_].values_[name].GetDouble(value);
123 }
124 
IsColumnNull(int columnIndex,bool & isNull)125 int CacheResultSet::IsColumnNull(int columnIndex, bool &isNull)
126 {
127     if (columnIndex < 0 || columnIndex >= maxCol_) {
128         return E_INVALID_ARGS;
129     }
130     auto name = colNames_[columnIndex];
131     std::shared_lock<decltype(rwMutex_)> lock(rwMutex_);
132     if (row_ < 0 || row_ >= maxRow_) {
133         return E_ERROR;
134     }
135     isNull = valueBuckets_[row_].values_[name].GetType() == ValueObject::TYPE_NULL;
136     return E_OK;
137 }
138 
GetRow(RowEntity & rowEntity)139 int CacheResultSet::GetRow(RowEntity &rowEntity)
140 {
141     std::shared_lock<decltype(rwMutex_)> lock(rwMutex_);
142     if (row_ < 0 || row_ >= maxRow_) {
143         return E_ERROR;
144     }
145     rowEntity.Clear(colNames_.size());
146     int32_t index = 0;
147     for (auto &columnName : colNames_) {
148         ValueObject object;
149         if (!valueBuckets_[row_].GetObject(columnName, object)) {
150             return E_ERROR;
151         }
152         rowEntity.Put(columnName, index, std::move(object));
153         index++;
154     }
155     return E_OK;
156 }
157 
GoToRow(int position)158 int CacheResultSet::GoToRow(int position)
159 {
160     std::unique_lock<decltype(rwMutex_)> lock(rwMutex_);
161     if (position >= maxRow_) {
162         row_ = maxRow_;
163         return E_ERROR;
164     }
165     if (position < 0) {
166         row_ = -1;
167         return E_ERROR;
168     }
169     row_ = position;
170     return E_OK;
171 }
172 
GetColumnType(int columnIndex,ColumnType & columnType)173 int CacheResultSet::GetColumnType(int columnIndex, ColumnType &columnType)
174 {
175     if (columnIndex < 0 || columnIndex >= maxCol_) {
176         return E_INVALID_ARGS;
177     }
178     auto index = colTypes_[columnIndex];
179     if (index < ValueObject::TYPE_NULL || index >= ValueObject::TYPE_MAX) {
180         return E_INVALID_ARGS;
181     }
182     columnType = COLUMNTYPES[index];
183     return E_OK;
184 }
185 
GetRowIndex(int & position) const186 int CacheResultSet::GetRowIndex(int &position) const
187 {
188     std::shared_lock<decltype(rwMutex_)> lock(rwMutex_);
189     position = row_;
190     return E_OK;
191 }
192 
GoTo(int offset)193 int CacheResultSet::GoTo(int offset)
194 {
195     int target = offset;
196     {
197         std::shared_lock<decltype(rwMutex_)> lock(rwMutex_);
198         target += row_;
199     }
200     return GoToRow(target);
201 }
202 
GoToFirstRow()203 int CacheResultSet::GoToFirstRow()
204 {
205     return GoToRow(0);
206 }
207 
GoToLastRow()208 int CacheResultSet::GoToLastRow()
209 {
210     return GoToRow(maxRow_ - 1);
211 }
212 
GoToNextRow()213 int CacheResultSet::GoToNextRow()
214 {
215     return GoTo(1);
216 }
217 
GoToPreviousRow()218 int CacheResultSet::GoToPreviousRow()
219 {
220     return GoTo(-1);
221 }
222 
IsAtFirstRow(bool & result) const223 int CacheResultSet::IsAtFirstRow(bool &result) const
224 {
225     std::shared_lock<decltype(rwMutex_)> lock(rwMutex_);
226     result = row_ == 0;
227     return E_OK;
228 }
229 
IsAtLastRow(bool & result)230 int CacheResultSet::IsAtLastRow(bool &result)
231 {
232     std::shared_lock<decltype(rwMutex_)> lock(rwMutex_);
233     result = row_ == maxRow_ - 1;
234     return E_OK;
235 }
236 
IsStarted(bool & result) const237 int CacheResultSet::IsStarted(bool &result) const
238 {
239     std::shared_lock<decltype(rwMutex_)> lock(rwMutex_);
240     result = row_ == -1;
241     return E_OK;
242 }
243 
IsEnded(bool & result)244 int CacheResultSet::IsEnded(bool &result)
245 {
246     std::shared_lock<decltype(rwMutex_)> lock(rwMutex_);
247     result = maxRow_ == 0 || row_ == maxRow_;
248     return E_OK;
249 }
250 
GetColumnCount(int & count)251 int CacheResultSet::GetColumnCount(int &count)
252 {
253     count = maxCol_;
254     return E_OK;
255 }
256 
GetColumnIndex(const std::string & columnName,int & columnIndex)257 int CacheResultSet::GetColumnIndex(const std::string &columnName, int &columnIndex)
258 {
259     for (int i = 0; i < maxCol_; ++i) {
260         if (colNames_[i] == columnName) {
261             columnIndex = i;
262             return E_OK;
263         }
264     }
265     return E_ERROR;
266 }
267 
GetColumnName(int columnIndex,std::string & columnName)268 int CacheResultSet::GetColumnName(int columnIndex, std::string &columnName)
269 {
270     if (columnIndex < 0 || columnIndex >= maxCol_) {
271         return E_INVALID_ARGS;
272     }
273     columnName = colNames_[columnIndex];
274     return E_OK;
275 }
276 
IsClosed() const277 bool CacheResultSet::IsClosed() const
278 {
279     return false;
280 }
281 
Close()282 int CacheResultSet::Close()
283 {
284     return E_NOT_SUPPORT;
285 }
286 
GetAsset(int32_t col,ValueObject::Asset & value)287 int CacheResultSet::GetAsset(int32_t col, ValueObject::Asset &value)
288 {
289     if (col < 0 || col >= maxCol_) {
290         return E_INVALID_ARGS;
291     }
292     auto name = colNames_[col];
293     std::shared_lock<decltype(rwMutex_)> lock(rwMutex_);
294     if (row_ < 0 || row_ >= maxRow_) {
295         return E_ERROR;
296     }
297     return valueBuckets_[row_].values_[name].GetAsset(value);
298 }
299 
GetAssets(int32_t col,ValueObject::Assets & value)300 int CacheResultSet::GetAssets(int32_t col, ValueObject::Assets &value)
301 {
302     if (col < 0 || col >= maxCol_) {
303         return E_INVALID_ARGS;
304     }
305     auto name = colNames_[col];
306     std::shared_lock<decltype(rwMutex_)> lock(rwMutex_);
307     if (row_ < 0 || row_ >= maxRow_) {
308         return E_ERROR;
309     }
310     return valueBuckets_[row_].values_[name].GetAssets(value);
311 }
312 
GetFloat32Array(int32_t index,ValueObject::FloatVector & vecs)313 int CacheResultSet::GetFloat32Array(int32_t index, ValueObject::FloatVector &vecs)
314 {
315     return E_NOT_SUPPORT;
316 }
317 
Get(int32_t col,ValueObject & value)318 int CacheResultSet::Get(int32_t col, ValueObject &value)
319 {
320     if (col < 0 || col >= maxCol_) {
321         return E_INVALID_ARGS;
322     }
323     auto name = colNames_[col];
324     std::shared_lock<decltype(rwMutex_)> lock(rwMutex_);
325     if (row_ < 0 || row_ >= maxRow_) {
326         return E_ERROR;
327     }
328     value = valueBuckets_[row_].values_[name];
329     return E_OK;
330 }
331 
GetSize(int columnIndex,size_t & size)332 int CacheResultSet::GetSize(int columnIndex, size_t &size)
333 {
334     return E_NOT_SUPPORT;
335 }
336 } // namespace NativeRdb
337 } // namespace OHOS