• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "data_convertor.h"
17 
18 #include "rdb_errno.h"
19 
20 #include "dfs_error.h"
21 #include "gallery_file_const.h"
22 #include "utils_log.h"
23 #include "sdk_helper.h"
24 
25 namespace OHOS {
26 namespace FileManagement {
27 namespace CloudSync {
28 using namespace std;
29 using namespace NativeRdb;
30 
ResultSetToRecords(const shared_ptr<NativeRdb::ResultSet> resultSet,vector<DriveKit::DKRecord> & records)31 int32_t DataConvertor::ResultSetToRecords(const shared_ptr<NativeRdb::ResultSet> resultSet,
32     vector<DriveKit::DKRecord> &records)
33 {
34     int32_t rowCount = 0;
35     int ret = resultSet->GetRowCount(rowCount);
36     if (ret != NativeRdb::E_OK || rowCount < 0) {
37         LOGE("result set get row count err %{public}d", ret);
38         return E_RDB;
39     }
40     /* no match rows */
41     if (rowCount == 0) {
42         return E_STOP;
43     }
44     /* reserve to avoid repeatedly alloc and copy */
45     records.reserve(rowCount);
46 
47     /* iterate all rows */
48     while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
49         DriveKit::DKRecord record;
50         ret = Convert(record, *resultSet);
51         if (ret != E_OK) {
52             LOGE("covert result to record err %{public}d", ret);
53             HandleErr(ret, *resultSet);
54             continue;
55         }
56         records.emplace_back(move(record));
57     }
58 
59     return E_OK;
60 }
61 
HandleErr(int32_t err,NativeRdb::ResultSet & resultSet)62 void DataConvertor::HandleErr(int32_t err, NativeRdb::ResultSet &resultSet)
63 {
64 }
65 
GetInt(const string & key,int32_t & val,NativeRdb::ResultSet & resultSet)66 int32_t DataConvertor::GetInt(const string &key, int32_t &val, NativeRdb::ResultSet &resultSet)
67 {
68     int32_t index;
69     int32_t err = resultSet.GetColumnIndex(key, index);
70     if (err != NativeRdb::E_OK) {
71         LOGE("result set get  %{public}s column index err %{public}d", key.c_str(), err);
72         return E_RDB;
73     }
74 
75     err = resultSet.GetInt(index, val);
76     if (err != 0) {
77         LOGE("result set get int err %{public}d", err);
78         return E_RDB;
79     }
80 
81     return E_OK;
82 }
83 
GetLong(const string & key,int64_t & val,NativeRdb::ResultSet & resultSet)84 int32_t DataConvertor::GetLong(const string &key, int64_t &val, NativeRdb::ResultSet &resultSet)
85 {
86     int32_t index;
87     int32_t err = resultSet.GetColumnIndex(key, index);
88     if (err != NativeRdb::E_OK) {
89         LOGE("result set get  %{public}s column index err %{public}d", key.c_str(), err);
90         return E_RDB;
91     }
92 
93     err = resultSet.GetLong(index, val);
94     if (err != 0) {
95         LOGE("result set get int err %{public}d", err);
96         return E_RDB;
97     }
98 
99     return E_OK;
100 }
101 
GetDouble(const string & key,double & val,NativeRdb::ResultSet & resultSet)102 int32_t DataConvertor::GetDouble(const string &key, double &val, NativeRdb::ResultSet &resultSet)
103 {
104     int32_t index;
105     int32_t err = resultSet.GetColumnIndex(key, index);
106     if (err != NativeRdb::E_OK) {
107         LOGE("result set get  %{public}s column index err %{public}d", key.c_str(), err);
108         return E_RDB;
109     }
110 
111     err = resultSet.GetDouble(index, val);
112     if (err != 0) {
113         LOGE("result set get double err %{public}d", err);
114         return E_RDB;
115     }
116 
117     return E_OK;
118 }
119 
GetString(const string & key,string & val,NativeRdb::ResultSet & resultSet)120 int32_t DataConvertor::GetString(const string &key, string &val, NativeRdb::ResultSet &resultSet)
121 {
122     int32_t index;
123     int32_t err = resultSet.GetColumnIndex(key, index);
124     if (err != NativeRdb::E_OK) {
125         LOGE("result set get  %{public}s column index err %{public}d", key.c_str(), err);
126         return E_RDB;
127     }
128 
129     err = resultSet.GetString(index, val);
130     if (err != 0) {
131         LOGE("result set get string err %{public}d", err);
132         return E_RDB;
133     }
134 
135     return E_OK;
136 }
137 
GetBool(const string & key,bool & val,NativeRdb::ResultSet & resultSet)138 int32_t DataConvertor::GetBool(const string &key, bool &val, NativeRdb::ResultSet &resultSet)
139 {
140     int32_t index;
141     int32_t err = resultSet.GetColumnIndex(key, index);
142     if (err != NativeRdb::E_OK) {
143         LOGE("result set get  %{public}s column index err %{public}d", key.c_str(), err);
144         return E_RDB;
145     }
146 
147     int32_t tmpValue;
148     err = resultSet.GetInt(index, tmpValue);
149     if (err != 0) {
150         LOGE("result set get bool err %{public}d", err);
151         return E_RDB;
152     }
153     val = !!tmpValue;
154 
155     return E_OK;
156 }
157 
GetIntComp(const DriveKit::DKRecordField & field,int & val)158 static int32_t GetIntComp(const DriveKit::DKRecordField &field, int &val)
159 {
160     if (field.GetInt(val) != DriveKit::DKLocalErrorCode::NO_ERROR) {
161         string str;
162         if (field.GetString(str) != DriveKit::DKLocalErrorCode::NO_ERROR) {
163             LOGE("record filed bad type %{public}d", static_cast<int>(field.GetType()));
164             return E_INVAL_ARG;
165         }
166         try {
167             val = std::stoi(str);
168         } catch (const std::out_of_range &e) {
169             LOGE("record filed convert to int failed");
170             return E_INVAL_ARG;
171         }
172         return E_OK;
173     }
174     return E_OK;
175 }
176 
GetLongComp(const DriveKit::DKRecordField & field,int64_t & val)177 int32_t DataConvertor::GetLongComp(const DriveKit::DKRecordField &field, int64_t &val)
178 {
179     if (field.GetLong(val) != DriveKit::DKLocalErrorCode::NO_ERROR) {
180         string str;
181         if (field.GetString(str) != DriveKit::DKLocalErrorCode::NO_ERROR) {
182             LOGE("record filed bad type %{public}d", static_cast<int>(field.GetType()));
183             return E_INVAL_ARG;
184         }
185         try {
186             val = std::stoll(str);
187         } catch (const std::out_of_range &e) {
188             LOGE("record filed convert to int64 failed");
189             return E_INVAL_ARG;
190         }
191         return E_OK;
192     }
193     return E_OK;
194 }
195 
GetDoubleComp(const DriveKit::DKRecordField & field,double & val)196 static int32_t GetDoubleComp(const DriveKit::DKRecordField &field, double &val)
197 {
198     if (field.GetDouble(val) != DriveKit::DKLocalErrorCode::NO_ERROR) {
199         string str;
200         if (field.GetString(str) != DriveKit::DKLocalErrorCode::NO_ERROR) {
201             LOGE("record filed bad type %{public}d", static_cast<int>(field.GetType()));
202             return E_INVAL_ARG;
203         }
204         try {
205             val = std::stod(str);
206         } catch (const std::out_of_range &e) {
207             LOGE("record filed convert to double failed");
208             return E_INVAL_ARG;
209         }
210         return E_OK;
211     }
212     return E_OK;
213 }
214 
GetBoolComp(const DriveKit::DKRecordField & field,bool & val)215 static int32_t GetBoolComp(const DriveKit::DKRecordField &field, bool &val)
216 {
217     if (field.GetBool(val) != DriveKit::DKLocalErrorCode::NO_ERROR) {
218         string str;
219         if (field.GetString(str) != DriveKit::DKLocalErrorCode::NO_ERROR) {
220             LOGE("record filed bad type %{public}d", static_cast<int>(field.GetType()));
221             return E_INVAL_ARG;
222         }
223         if (str == "true") {
224             val = true;
225         } else if (str == "false") {
226             val = false;
227         } else {
228             return E_INVAL_ARG;
229         }
230         return E_OK;
231     }
232     return E_OK;
233 }
234 
235 const std::unordered_map<DataType,
236     std::function<int(const DriveKit::DKRecordField &, NativeRdb::ValuesBucket &, const std::string &)>>
237     TYPE_HANDLERS = {
238         {DataType::INT,
__anon58ea24db0102() 239          [](const DriveKit::DKRecordField &value, NativeRdb::ValuesBucket &bucket, const std::string &field) {
240              int intValue;
241              if (GetIntComp(value, intValue) != E_OK) {
242                  return E_INVAL_ARG;
243              }
244              bucket.PutInt(field, intValue);
245              return E_OK;
246          }},
247         {DataType::LONG,
__anon58ea24db0202() 248          [](const DriveKit::DKRecordField &value, NativeRdb::ValuesBucket &bucket, const std::string &field) {
249              int64_t longValue;
250              if (DataConvertor::GetLongComp(value, longValue) != E_OK) {
251                  return E_INVAL_ARG;
252              }
253              bucket.PutLong(field, longValue);
254              return E_OK;
255          }},
256         {DataType::STRING,
__anon58ea24db0302() 257          [](const DriveKit::DKRecordField &value, NativeRdb::ValuesBucket &bucket, const std::string &field) {
258              std::string stringValue;
259              if (value.GetString(stringValue) != DriveKit::DKLocalErrorCode::NO_ERROR) {
260                  return E_INVAL_ARG;
261              }
262              if (stringValue == "") {
263                 return E_OK;
264              }
265              bucket.PutString(field, stringValue);
266              return E_OK;
267          }},
268         {DataType::DOUBLE,
__anon58ea24db0402() 269          [](const DriveKit::DKRecordField &value, NativeRdb::ValuesBucket &bucket, const std::string &field) {
270              double doubleValue;
271              if (GetDoubleComp(value, doubleValue) != E_OK) {
272                  return E_INVAL_ARG;
273              }
274              bucket.PutDouble(field, doubleValue);
275              return E_OK;
276          }},
277         {DataType::BOOL,
__anon58ea24db0502() 278          [](const DriveKit::DKRecordField &value, NativeRdb::ValuesBucket &bucket, const std::string &field) {
279              bool boolValue;
280              if (GetBoolComp(value, boolValue) != E_OK) {
281                  return E_INVAL_ARG;
282              }
283              bucket.PutInt(field, static_cast<int>(boolValue));
284              return E_OK;
285          }}};
286 
HandleField(const DriveKit::DKRecordField & value,NativeRdb::ValuesBucket & bucket,const std::string & field,DataType type)287 int DataConvertor::HandleField(const DriveKit::DKRecordField &value, NativeRdb::ValuesBucket &bucket,
288     const std::string &field, DataType type)
289 {
290     auto it = TYPE_HANDLERS.find(type);
291     if (it == TYPE_HANDLERS.end()) {
292         LOGE("invalid data type %d", static_cast<int>(type));
293         return E_INVAL_ARG;
294     }
295     return it->second(value, bucket, field);
296 }
297 
RecordToValueBucket(DriveKit::DKRecord & record,NativeRdb::ValuesBucket & valueBucket)298 int32_t DataConvertor::RecordToValueBucket(DriveKit::DKRecord &record, NativeRdb::ValuesBucket &valueBucket)
299 {
300     return Convert(record, valueBucket);
301 }
302 
303 } // namespace CloudSync
304 } // namespace FileManagement
305 } // namespace OHOS
306