1 /*
2 * Copyright (C) 2022 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 #ifndef INTERFACES_INNERAPI_MEDIA_LIBRARY_HELPER_INCLUDE_RESULT_SET_UTILS_H_
16 #define INTERFACES_INNERAPI_MEDIA_LIBRARY_HELPER_INCLUDE_RESULT_SET_UTILS_H_
17
18 #include "medialibrary_common_log.h"
19 #include "fetch_result.h"
20
21 namespace OHOS {
22 namespace Media {
23 class ResultSetUtils {
24 public:
25 template<typename T>
GetValFromColumn(const std::string & columnName,T & resultSet,ResultSetDataType type)26 static std::variant<int32_t, std::string, int64_t, double> GetValFromColumn(const std::string &columnName,
27 T &resultSet, ResultSetDataType type)
28 {
29 if (resultSet == nullptr) {
30 COMMON_ERR_LOG("resultSet is nullptr");
31 return DefaultVariantVal(type);
32 }
33
34 int32_t err;
35 int32_t index = 0;
36 err = resultSet->GetColumnIndex(columnName, index);
37 if (err) {
38 COMMON_ERR_LOG("get column index err %{public}d", err);
39 return DefaultVariantVal(type);
40 }
41
42 std::variant<int32_t, std::string, int64_t, double> data;
43 switch (type) {
44 case ResultSetDataType::TYPE_STRING: {
45 data = GetStringValFromColumn(index, resultSet);
46 break;
47 }
48 case ResultSetDataType::TYPE_INT32: {
49 data = GetIntValFromColumn(index, resultSet);
50 break;
51 }
52 case ResultSetDataType::TYPE_INT64: {
53 data = GetLongValFromColumn(index, resultSet);
54 break;
55 }
56 case ResultSetDataType::TYPE_DOUBLE: {
57 data = GetDoubleValFromColumn(index, resultSet);
58 break;
59 }
60 default: {
61 break;
62 }
63 }
64
65 return data;
66 }
67
68 template<typename T>
GetStringValFromColumn(int index,T & resultSet)69 static inline std::string GetStringValFromColumn(int index, T &resultSet)
70 {
71 std::string stringVal;
72 if (resultSet->GetString(index, stringVal)) {
73 return "";
74 }
75 return stringVal;
76 }
77
78 template<typename T>
GetIntValFromColumn(int index,T & resultSet)79 static inline int32_t GetIntValFromColumn(int index, T &resultSet)
80 {
81 int32_t integerVal;
82 if (resultSet->GetInt(index, integerVal)) {
83 return 0;
84 }
85 return integerVal;
86 }
87
88 template<typename T>
GetLongValFromColumn(int index,T & resultSet)89 static inline int64_t GetLongValFromColumn(int index, T &resultSet)
90 {
91 int64_t integer64Val;
92 if (resultSet->GetLong(index, integer64Val)) {
93 return 0;
94 }
95 return integer64Val;
96 }
97
98 template<typename T>
GetDoubleValFromColumn(int index,T & resultSet)99 static inline double GetDoubleValFromColumn(int index, T &resultSet)
100 {
101 double doubleVal;
102 if (resultSet->GetDouble(index, doubleVal)) {
103 return 0;
104 }
105 return doubleVal;
106 }
107
108 private:
DefaultVariantVal(ResultSetDataType type)109 static std::variant<int32_t, std::string, int64_t, double> DefaultVariantVal(ResultSetDataType type)
110 {
111 switch (type) {
112 case ResultSetDataType::TYPE_STRING:
113 return std::string();
114 case ResultSetDataType::TYPE_INT32:
115 return 0;
116 case ResultSetDataType::TYPE_INT64:
117 return static_cast<int64_t>(0);
118 case ResultSetDataType::TYPE_DOUBLE:
119 return static_cast<double>(0);
120 default:
121 COMMON_ERR_LOG("invalid data type %{public}d", type);
122 }
123
124 return 0;
125 }
126 };
127
128 template<typename ResultSet>
GetStringVal(const std::string & field,const ResultSet & result)129 static inline std::string GetStringVal(const std::string &field, const ResultSet &result)
130 {
131 return get<std::string>(ResultSetUtils::GetValFromColumn(field, result, TYPE_STRING));
132 }
133 template<typename ResultSet>
GetInt32Val(const std::string & field,const ResultSet & result)134 static inline int32_t GetInt32Val(const std::string &field, const ResultSet &result)
135 {
136 return get<int32_t>(ResultSetUtils::GetValFromColumn(field, result, TYPE_INT32));
137 }
138 template<typename ResultSet>
GetInt64Val(const std::string & field,const ResultSet & result)139 static inline int64_t GetInt64Val(const std::string &field, const ResultSet &result)
140 {
141 return get<int64_t>(ResultSetUtils::GetValFromColumn(field, result, TYPE_INT64));
142 }
143 template<typename ResultSet>
GetDoubleVal(const std::string & field,const ResultSet & result)144 static inline double GetDoubleVal(const std::string &field, const ResultSet &result)
145 {
146 return get<double>(ResultSetUtils::GetValFromColumn(field, result, TYPE_DOUBLE));
147 }
148
TryToGoToFirstRow(const std::shared_ptr<NativeRdb::ResultSet> & resultSet)149 static inline bool TryToGoToFirstRow(const std::shared_ptr<NativeRdb::ResultSet>& resultSet)
150 {
151 if (resultSet == nullptr) {
152 COMMON_ERR_LOG("resultSet is nullptr");
153 return false;
154 }
155
156 if (resultSet->GoToFirstRow() != NativeRdb::E_OK) {
157 COMMON_ERR_LOG("failed to go to first row");
158 return false;
159 }
160
161 return true;
162 }
163 } // namespace Media
164 } // namespace OHOS
165 #endif // INTERFACES_INNERAPI_MEDIA_LIBRARY_HELPER_INCLUDE_RESULT_SET_UTILS_H_
166