• 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 #include "abs_result_set.h"
17 
18 #include <algorithm>
19 #include <string>
20 
21 #include "logger.h"
22 #include "rdb_errno.h"
23 #include "rdb_trace.h"
24 #include "result_set.h"
25 
26 namespace OHOS {
27 namespace NativeRdb {
28 using namespace OHOS::Rdb;
29 
Put(const std::string & name,const ValueObject & value)30 void RowEntity::Put(const std::string &name, const ValueObject &value)
31 {
32     auto it = values_.emplace(name, std::move(value));
33     indexs_.push_back(it.first);
34 }
35 
Get(const std::string & name) const36 ValueObject RowEntity::Get(const std::string &name) const
37 {
38     auto it = values_.find(name);
39     if (it == values_.end()) {
40         return ValueObject();
41     }
42     return it->second;
43 }
44 
Get(int index) const45 ValueObject RowEntity::Get(int index) const
46 {
47     if (index < 0 || index >= static_cast<int>(indexs_.size())) {
48         return ValueObject();
49     }
50     return indexs_[index]->second;
51 }
52 
Get() const53 const std::map<std::string, ValueObject> &RowEntity::Get() const
54 {
55     return values_;
56 }
57 
Steal()58 std::map<std::string, ValueObject> RowEntity::Steal()
59 {
60     indexs_.clear();
61     return std::move(values_);
62 }
63 
Clear()64 void RowEntity::Clear()
65 {
66     values_.clear();
67     indexs_.clear();
68 }
69 
AbsResultSet()70 AbsResultSet::AbsResultSet() : rowPos_(INIT_POS), isClosed(false)
71 {
72 }
73 
~AbsResultSet()74 AbsResultSet::~AbsResultSet()
75 {
76     rowPos_ = INIT_POS;
77     isClosed = false;
78 }
79 
GetRowCount(int & count)80 int AbsResultSet::GetRowCount(int &count)
81 {
82     return E_OK;
83 }
84 
GetAllColumnNames(std::vector<std::string> & columnNames)85 int AbsResultSet::GetAllColumnNames(std::vector<std::string> &columnNames)
86 {
87     return E_OK;
88 }
89 
GetBlob(int columnIndex,std::vector<uint8_t> & blob)90 int AbsResultSet::GetBlob(int columnIndex, std::vector<uint8_t> &blob)
91 {
92     return E_OK;
93 }
94 
GetString(int columnIndex,std::string & value)95 int AbsResultSet::GetString(int columnIndex, std::string &value)
96 {
97     return E_OK;
98 }
99 
GetInt(int columnIndex,int & value)100 int AbsResultSet::GetInt(int columnIndex, int &value)
101 {
102     return E_OK;
103 }
104 
GetLong(int columnIndex,int64_t & value)105 int AbsResultSet::GetLong(int columnIndex, int64_t &value)
106 {
107     return E_OK;
108 }
109 
GetDouble(int columnIndex,double & value)110 int AbsResultSet::GetDouble(int columnIndex, double &value)
111 {
112     return E_OK;
113 }
114 
IsColumnNull(int columnIndex,bool & isNull)115 int AbsResultSet::IsColumnNull(int columnIndex, bool &isNull)
116 {
117     return E_OK;
118 }
119 
GetRow(RowEntity & rowEntity)120 int AbsResultSet::GetRow(RowEntity &rowEntity)
121 {
122     rowEntity.Clear();
123     std::vector<std::string> columnNames;
124     int ret = GetAllColumnNames(columnNames);
125     if (ret != E_OK) {
126         LOG_ERROR("GetAllColumnNames::ret is %{public}d", ret);
127         return ret;
128     }
129     int columnCount = static_cast<int>(columnNames.size());
130 
131     ColumnType columnType;
132     for (int columnIndex = 0; columnIndex < columnCount; ++columnIndex) {
133         ret = GetColumnType(columnIndex, columnType);
134         if (ret != E_OK) {
135             LOG_ERROR("GetColumnType::ret is %{public}d", ret);
136             return ret;
137         }
138         switch (columnType) {
139             case ColumnType::TYPE_NULL: {
140                 rowEntity.Put(columnNames[columnIndex], ValueObject());
141                 break;
142             }
143             case ColumnType::TYPE_INTEGER: {
144                 int64_t value;
145                 GetLong(columnIndex, value);
146                 rowEntity.Put(columnNames[columnIndex], ValueObject(value));
147                 break;
148             }
149             case ColumnType::TYPE_FLOAT: {
150                 double value;
151                 GetDouble(columnIndex, value);
152                 rowEntity.Put(columnNames[columnIndex], ValueObject(value));
153                 break;
154             }
155             case ColumnType::TYPE_STRING: {
156                 std::string value;
157                 GetString(columnIndex, value);
158                 rowEntity.Put(columnNames[columnIndex], ValueObject(value));
159                 break;
160             }
161             case ColumnType::TYPE_BLOB: {
162                 std::vector<uint8_t> value;
163                 GetBlob(columnIndex, value);
164                 rowEntity.Put(columnNames[columnIndex], ValueObject(value));
165                 break;
166             }
167             case ColumnType::TYPE_ASSET: {
168                 ValueObject::Asset value;
169                 GetAsset(columnIndex, value);
170                 rowEntity.Put(columnNames[columnIndex], ValueObject(value));
171                 break;
172             }
173             case ColumnType::TYPE_ASSETS: {
174                 ValueObject::Assets value;
175                 GetAssets(columnIndex, value);
176                 rowEntity.Put(columnNames[columnIndex], ValueObject(value));
177                 break;
178             }
179             default: {
180                 return E_ERROR;
181             }
182         }
183     }
184     return E_OK;
185 }
186 
GoToRow(int position)187 int AbsResultSet::GoToRow(int position)
188 {
189     return E_OK;
190 }
191 
GetColumnType(int columnIndex,ColumnType & columnType)192 int AbsResultSet::GetColumnType(int columnIndex, ColumnType &columnType)
193 {
194     return E_OK;
195 }
196 
GetRowIndex(int & position) const197 int AbsResultSet::GetRowIndex(int &position) const
198 {
199     position = rowPos_;
200     return E_OK;
201 }
202 
GoTo(int offset)203 int AbsResultSet::GoTo(int offset)
204 {
205     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
206     int ret = GoToRow(rowPos_ + offset);
207     if (ret != E_OK) {
208         LOG_WARN("GoToRow ret is %{public}d", ret);
209         return ret;
210     }
211     return E_OK;
212 }
213 
GoToFirstRow()214 int AbsResultSet::GoToFirstRow()
215 {
216     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
217     int ret = GoToRow(0);
218     if (ret != E_OK) {
219         LOG_DEBUG("GoToRow ret is %{public}d", ret);
220         return ret;
221     }
222     return E_OK;
223 }
224 
GoToLastRow()225 int AbsResultSet::GoToLastRow()
226 {
227     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
228     int rowCnt = 0;
229     int ret = GetRowCount(rowCnt);
230     if (ret != E_OK) {
231         LOG_ERROR("Failed to GetRowCount, ret is %{public}d", ret);
232         return ret;
233     }
234 
235     ret = GoToRow(rowCnt - 1);
236     if (ret != E_OK) {
237         LOG_WARN("GoToRow ret is %{public}d", ret);
238         return ret;
239     }
240     return E_OK;
241 }
242 
GoToNextRow()243 int AbsResultSet::GoToNextRow()
244 {
245     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
246     int ret = GoToRow(rowPos_ + 1);
247     if (ret != E_OK) {
248         LOG_DEBUG("GoToRow ret is %{public}d", ret);
249         return ret;
250     }
251     return E_OK;
252 }
253 
GoToPreviousRow()254 int AbsResultSet::GoToPreviousRow()
255 {
256     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
257     int ret = GoToRow(rowPos_ - 1);
258     if (ret != E_OK) {
259         LOG_WARN("GoToRow ret is %{public}d", ret);
260         return ret;
261     }
262     return E_OK;
263 }
264 
IsAtFirstRow(bool & result) const265 int AbsResultSet::IsAtFirstRow(bool &result) const
266 {
267     result = (rowPos_ == 0);
268     return E_OK;
269 }
270 
IsAtLastRow(bool & result)271 int AbsResultSet::IsAtLastRow(bool &result)
272 {
273     int rowCnt = 0;
274     int ret = GetRowCount(rowCnt);
275     if (ret != E_OK) {
276         LOG_ERROR("Failed to GetRowCount, ret is %{public}d", ret);
277         return ret;
278     }
279     result = (rowPos_ == (rowCnt - 1));
280     return E_OK;
281 }
282 
IsStarted(bool & result) const283 int AbsResultSet::IsStarted(bool &result) const
284 {
285     result = (rowPos_ != INIT_POS);
286     return E_OK;
287 }
288 
IsEnded(bool & result)289 int AbsResultSet::IsEnded(bool &result)
290 {
291     int rowCnt = 0;
292     int ret = GetRowCount(rowCnt);
293     if (ret != E_OK) {
294         LOG_ERROR("Failed to GetRowCount, ret is %{public}d", ret);
295         return ret;
296     }
297     result = (rowCnt == 0) ? true : (rowPos_ == rowCnt);
298     return E_OK;
299 }
300 
GetColumnCount(int & count)301 int AbsResultSet::GetColumnCount(int &count)
302 {
303     if (columnCount_ != -1) {
304         count = columnCount_;
305         return E_OK;
306     }
307     std::vector<std::string> columnNames;
308     int ret = GetAllColumnNames(columnNames);
309     if (ret != E_OK) {
310         LOG_ERROR("Failed to GetAllColumnNames, ret is %{public}d", ret);
311         return ret;
312     }
313     columnCount_ = static_cast<int>(columnNames.size());
314     count = columnCount_;
315     return E_OK;
316 }
317 
GetColumnIndex(const std::string & columnName,int & columnIndex)318 int AbsResultSet::GetColumnIndex(const std::string &columnName, int &columnIndex)
319 {
320     std::lock_guard<std::mutex> lock(columnMapLock_);
321     auto it = columnMap_.find(columnName);
322     if (it != columnMap_.end()) {
323         columnIndex = it->second;
324         return E_OK;
325     }
326 
327     auto periodIndex = columnName.rfind('.');
328     std::string columnNameLower = columnName;
329     if (periodIndex != std::string::npos) {
330         columnNameLower = columnNameLower.substr(periodIndex + 1);
331     }
332     transform(columnNameLower.begin(), columnNameLower.end(), columnNameLower.begin(), ::tolower);
333     std::vector<std::string> columnNames;
334     int ret = GetAllColumnNames(columnNames);
335     if (ret != E_OK) {
336         LOG_ERROR("Failed to GetAllColumnNames, ret is %{public}d", ret);
337         return ret;
338     }
339 
340     columnIndex = 0;
341     for (const auto& name : columnNames) {
342         std::string lowerName = name;
343         transform(name.begin(), name.end(), lowerName.begin(), ::tolower);
344         if (lowerName == columnNameLower) {
345             columnMap_.insert(std::make_pair(columnName, columnIndex));
346             return E_OK;
347         }
348         columnIndex++;
349     }
350     columnIndex = -1;
351     LOG_WARN("columnName %{public}s is not in resultSet", columnName.c_str());
352     return E_ERROR;
353 }
354 
GetColumnName(int columnIndex,std::string & columnName)355 int AbsResultSet::GetColumnName(int columnIndex, std::string &columnName)
356 {
357     int rowCnt = 0;
358     int ret = GetColumnCount(rowCnt);
359     if (ret != E_OK) {
360         LOG_ERROR("Failed to GetColumnCount, ret is %{public}d", ret);
361         return ret;
362     }
363     if (columnIndex >= rowCnt || columnIndex < 0) {
364         LOG_ERROR("invalid column columnIndex as %{public}d", columnIndex);
365         return E_INVALID_COLUMN_INDEX;
366     }
367     std::vector<std::string> columnNames;
368     GetAllColumnNames(columnNames);
369     columnName = columnNames[columnIndex];
370     return E_OK;
371 }
372 
IsClosed() const373 bool AbsResultSet::IsClosed() const
374 {
375     return isClosed;
376 }
377 
Close()378 int AbsResultSet::Close()
379 {
380     isClosed = true;
381     return E_OK;
382 }
383 
GetModifyTime(std::string & modifyTime)384 int AbsResultSet::GetModifyTime(std::string &modifyTime)
385 {
386     return E_NOT_SUPPORT;
387 }
388 
GetAsset(int32_t col,ValueObject::Asset & value)389 int AbsResultSet::GetAsset(int32_t col, ValueObject::Asset &value)
390 {
391     return E_NOT_SUPPORT;
392 }
393 
GetAssets(int32_t col,ValueObject::Assets & value)394 int AbsResultSet::GetAssets(int32_t col, ValueObject::Assets &value)
395 {
396     return E_NOT_SUPPORT;
397 }
398 
Get(int32_t col,ValueObject & value)399 int AbsResultSet::Get(int32_t col, ValueObject &value)
400 {
401     return E_NOT_SUPPORT;
402 }
403 } // namespace NativeRdb
404 } // namespace OHOS