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