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 ITABLE_H
17 #define ITABLE_H
18 
19 #include <vector>
20 
21 #include "rdb_errno.h"
22 #include "result_set.h"
23 #include "values_bucket.h"
24 
25 #include "update_define.h"
26 #include "update_log.h"
27 
28 using ResultSet = OHOS::NativeRdb::ResultSet;
29 
30 namespace OHOS {
31 namespace UpdateEngine {
32 const std::string COLUMN_ID = "id";
33 
34 template <typename T>
35 class ITable {
36 public:
37     virtual ~ITable() = default;
38     virtual std::string GetTableName() = 0;
39     virtual std::string GetTableCreateSql() = 0;
40 
ParseDbValues(std::shared_ptr<ResultSet> resultSet,std::vector<T> & values)41     void ParseDbValues(std::shared_ptr<ResultSet> resultSet, std::vector<T> &values)
42     {
43         ENGINE_CHECK(resultSet != nullptr, return, "ParseDbValues resultSet is null");
44         resultSet->GoToFirstRow();
45         bool isEnded = true;
46         resultSet->IsEnded(isEnded);
47         while (!isEnded) {
48             T value;
49             ParseDbValue(resultSet.get(), value);
50             values.push_back(value);
51             resultSet->GoToNextRow();
52             resultSet->IsEnded(isEnded);
53         }
54     }
55 
BuildDbValues(const std::vector<T> & values,std::vector<NativeRdb::ValuesBucket> & dbValues)56     void BuildDbValues(const std::vector<T> &values, std::vector<NativeRdb::ValuesBucket> &dbValues)
57     {
58         for (T value : values) {
59             NativeRdb::ValuesBucket genericValue;
60             BuildDbValue(value, genericValue);
61             dbValues.push_back(genericValue);
62         }
63     }
64 
65 protected:
66     template <typename ValueType>
GetColumnValue(ResultSet * resultSet,const std::string & columnName,ValueType & value)67     void GetColumnValue(ResultSet *resultSet, const std::string &columnName, ValueType &value)
68     {
69         ENGINE_CHECK(resultSet != nullptr, return, "GetColumnValue resultSet is null");
70         int index;
71         int ret = resultSet->GetColumnIndex(columnName, index);
72         if (ret != NativeRdb::E_OK) {
73             ENGINE_LOGE("GetColumnValue %{public}s column index fail, ret = %{public}d", columnName.c_str(), ret);
74             return;
75         }
76         GetIndexValue(resultSet, index, value);
77     }
78 
PutColumnValue(NativeRdb::ValuesBucket & dbValue,const std::string & columnName,const std::string & value)79     void PutColumnValue(NativeRdb::ValuesBucket &dbValue, const std::string &columnName, const std::string &value)
80     {
81         dbValue.PutString(columnName, value);
82     }
83 
PutColumnValue(NativeRdb::ValuesBucket & dbValue,const std::string & columnName,const int32_t & value)84     void PutColumnValue(NativeRdb::ValuesBucket &dbValue, const std::string &columnName, const int32_t &value)
85     {
86         dbValue.PutInt(columnName, value);
87     }
88 
PutColumnValue(NativeRdb::ValuesBucket & dbValue,const std::string & columnName,const int64_t & value)89     void PutColumnValue(NativeRdb::ValuesBucket &dbValue, const std::string &columnName, const int64_t &value)
90     {
91         dbValue.PutLong(columnName, value);
92     }
93 
PutColumnValue(NativeRdb::ValuesBucket & dbValue,const std::string & columnName,const double & value)94     void PutColumnValue(NativeRdb::ValuesBucket &dbValue, const std::string &columnName, const double &value)
95     {
96         dbValue.PutDouble(columnName, value);
97     }
98 
PutColumnValue(NativeRdb::ValuesBucket & dbValue,const std::string & columnName,const bool & value)99     void PutColumnValue(NativeRdb::ValuesBucket &dbValue, const std::string &columnName, const bool &value)
100     {
101         int32_t intValue = value ? 1 : 0;
102         dbValue.PutInt(columnName, intValue);
103     }
104 
105 private:
106     virtual void ParseDbValue(ResultSet *resultSet, T &value) = 0;
107     virtual void BuildDbValue(const T &value, NativeRdb::ValuesBucket &dbValue) = 0;
108 
GetIndexValue(ResultSet * resultSet,int index,std::string & value)109     int GetIndexValue(ResultSet *resultSet, int index, std::string &value)
110     {
111         return resultSet->GetString(index, value);
112     }
113 
GetIndexValue(ResultSet * resultSet,int index,int & value)114     int GetIndexValue(ResultSet *resultSet, int index, int &value)
115     {
116         return resultSet->GetInt(index, value);
117     }
118 
GetIndexValue(ResultSet * resultSet,int index,int64_t & value)119     int GetIndexValue(ResultSet *resultSet, int index, int64_t &value)
120     {
121         return resultSet->GetLong(index, value);
122     }
123 
GetIndexValue(ResultSet * resultSet,int index,double & value)124     int GetIndexValue(ResultSet *resultSet, int index, double &value)
125     {
126         return resultSet->GetDouble(index, value);
127     }
128 
GetIndexValue(ResultSet * resultSet,int index,bool & value)129     int GetIndexValue(ResultSet *resultSet, int index, bool &value)
130     {
131         int32_t intValue = 0;
132         int ret = resultSet->GetInt(index, intValue);
133         value = (intValue != 0);
134         return ret;
135     }
136 };
137 } // namespace UpdateEngine
138 } // namespace OHOS
139 #endif // ITABLE_H