• 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 #ifndef OHOS_CLOUD_SYNC_SERVICE_DATA_CONVERTOR_H
17 #define OHOS_CLOUD_SYNC_SERVICE_DATA_CONVERTOR_H
18 
19 #include <memory>
20 
21 #include "values_bucket.h"
22 #include "result_set.h"
23 
24 #include "sdk_helper.h"
25 
26 namespace OHOS {
27 namespace FileManagement {
28 namespace CloudSync {
29 enum class DataType : int32_t {
30     INT,
31     LONG,
32     DOUBLE,
33     STRING,
34     BOOL
35 };
36 
37 class DataConvertor {
38 public:
39     DataConvertor() = default;
40     virtual ~DataConvertor() = default;
41 
42     /* resultSet -> record */
43     virtual int32_t Convert(DriveKit::DKRecord &record, NativeRdb::ResultSet &resultSet) = 0;
44     /* record -> resultSet */
45     virtual int32_t Convert(DriveKit::DKRecord &record, NativeRdb::ValuesBucket &valueBucket) = 0;
46 
47     int32_t ResultSetToRecords(const std::shared_ptr<NativeRdb::ResultSet> resultSet,
48         std::vector<DriveKit::DKRecord> &records);
49     int32_t RecordToValueBucket(DriveKit::DKRecord &record, NativeRdb::ValuesBucket &valueBucket);
50     static int32_t GetLongComp(const DriveKit::DKRecordField &field, int64_t &val);
51     static int32_t GetInt(const std::string &key, int32_t &val, NativeRdb::ResultSet &resultSet);
52     static int32_t GetLong(const std::string &key, int64_t &val, NativeRdb::ResultSet &resultSet);
53     static int32_t GetDouble(const std::string &key, double &val, NativeRdb::ResultSet &resultSet);
54     static int32_t GetString(const std::string &key, std::string &val, NativeRdb::ResultSet &resultSet);
55     static int32_t GetBool(const std::string &key, bool &val, NativeRdb::ResultSet &resultSet);
56     static int32_t HandleField(const DriveKit::DKRecordField &value, NativeRdb::ValuesBucket &bucket,
57         const std::string &field, DataType type);
58 };
59 
60 #define SET_RECORD_INT(key, resultSet, record)    \
61     do {    \
62         int32_t val;    \
63         int32_t ret = GetInt(key, val, resultSet);    \
64         if (ret != E_OK) {    \
65             return ret;    \
66         }    \
67         (record)[key] = DriveKit::DKRecordField(val);    \
68     } while (0)
69 
70 #define SET_RECORD_LONG(key, resultSet, record)    \
71     do {    \
72         int64_t val;    \
73         int32_t ret = GetLong(key, val, resultSet);    \
74         if (ret != E_OK) {    \
75             return ret;    \
76         }    \
77         (record)[key] = DriveKit::DKRecordField(val);    \
78     } while (0)
79 
80 #define SET_RECORD_DOUBLE(key, resultSet, record)    \
81     do {    \
82         double val;    \
83         int32_t ret = GetDouble(key, val, resultSet);    \
84         if (ret != E_OK) {    \
85             return ret;    \
86         }    \
87         (record)[key] = DriveKit::DKRecordField(val);    \
88     } while (0)
89 
90 #define SET_RECORD_STRING(key, resultSet, record)    \
91     do {    \
92         string val;    \
93         int32_t ret = GetString(key, val, resultSet);    \
94         if (ret != E_OK) {    \
95             return ret;    \
96         }    \
97         (record)[key] = DriveKit::DKRecordField(val);    \
98     } while (0)
99 
100 #define SET_RECORD_BOOL(key, resultSet, record)    \
101     do {    \
102         bool val;    \
103         int32_t ret = GetBool(key, val, resultSet);    \
104         if (ret != E_OK) {    \
105             return ret;    \
106         }    \
107         (record)[key] = DriveKit::DKRecordField(val);    \
108     } while (0)
109 
110 #define RETURN_ON_ERR(ret)    \
111     do {    \
112         if ((ret) != E_OK) {    \
113             return ret;    \
114         }    \
115     } while (0)
116 } // namespace CloudSync
117 } // namespace FileManagement
118 } // namespace OHOS
119 #endif // OHOS_CLOUD_SYNC_SERVICE_DATA_CONVERTOR_H
120