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 std::string stringVal;
46 err = resultSet->GetString(index, stringVal);
47 CHECK_AND_ERR_LOG(!err, "get string err %{public}d", err);
48 data = stringVal;
49 break;
50 }
51 case ResultSetDataType::TYPE_INT32: {
52 int32_t integerVal;
53 err = resultSet->GetInt(index, integerVal);
54 CHECK_AND_ERR_LOG(!err, "get int err %{public}d", err);
55 data = integerVal;
56 break;
57 }
58 case ResultSetDataType::TYPE_INT64: {
59 int64_t integer64Val;
60 err = resultSet->GetLong(index, integer64Val);
61 CHECK_AND_ERR_LOG(!err, "get int64 err %{public}d", err);
62 data = integer64Val;
63 break;
64 }
65 case ResultSetDataType::TYPE_DOUBLE: {
66 double doubleVal;
67 err = resultSet->GetDouble(index, doubleVal);
68 CHECK_AND_ERR_LOG(!err, "get double err %{public}d", err);
69 data = doubleVal;
70 break;
71 }
72 default: {
73 COMMON_ERR_LOG("invalid data type %{public}d", type);
74 break;
75 }
76 }
77
78 return data;
79 }
80
81 private:
DefaultVariantVal(ResultSetDataType type)82 static std::variant<int32_t, std::string, int64_t, double> DefaultVariantVal(ResultSetDataType type)
83 {
84 switch (type) {
85 case ResultSetDataType::TYPE_STRING:
86 return std::string();
87 case ResultSetDataType::TYPE_INT32:
88 return 0;
89 case ResultSetDataType::TYPE_INT64:
90 return static_cast<int64_t>(0);
91 case ResultSetDataType::TYPE_DOUBLE:
92 return static_cast<double>(0);
93 default:
94 COMMON_ERR_LOG("invalid data type %{public}d", type);
95 }
96
97 return 0;
98 }
99 };
100
101 template<typename ResultSet>
GetStringVal(const std::string & field,const ResultSet & result)102 static inline std::string GetStringVal(const std::string &field, const ResultSet &result)
103 {
104 return get<std::string>(ResultSetUtils::GetValFromColumn(field, result, TYPE_STRING));
105 }
106 template<typename ResultSet>
GetInt32Val(const std::string & field,const ResultSet & result)107 static inline int32_t GetInt32Val(const std::string &field, const ResultSet &result)
108 {
109 return get<int32_t>(ResultSetUtils::GetValFromColumn(field, result, TYPE_INT32));
110 }
111 template<typename ResultSet>
GetInt64Val(const std::string & field,const ResultSet & result)112 static inline int64_t GetInt64Val(const std::string &field, const ResultSet &result)
113 {
114 return get<int64_t>(ResultSetUtils::GetValFromColumn(field, result, TYPE_INT64));
115 }
116 } // namespace Media
117 } // namespace OHOS
118 #endif // INTERFACES_INNERAPI_MEDIA_LIBRARY_HELPER_INCLUDE_RESULT_SET_UTILS_H_
119