/* * Copyright (C) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef RESULT_SET_UTILS_H #define RESULT_SET_UTILS_H #include "ringtone_log.h" #include "ringtone_type.h" namespace OHOS { namespace Media { #define EXPORT __attribute__ ((visibility ("default"))) class ResultSetUtils { public: template static std::variant GetValFromColumn(const std::string &columnName, T &resultSet, RingtoneResultSetDataType type) { if (resultSet == nullptr) { RINGTONE_ERR_LOG("resultSet is nullptr"); return DefaultVariantVal(type); } int32_t err; int32_t index = 0; err = resultSet->GetColumnIndex(columnName, index); if (err) { RINGTONE_ERR_LOG("get column index err %{public}d", err); return DefaultVariantVal(type); } std::variant data; switch (type) { case RingtoneResultSetDataType::DATA_TYPE_STRING: { data = GetStringValFromColumn(index, resultSet); break; } case RingtoneResultSetDataType::DATA_TYPE_INT32: { data = GetIntValFromColumn(index, resultSet); break; } case RingtoneResultSetDataType::DATA_TYPE_INT64: { data = GetLongValFromColumn(index, resultSet); break; } case RingtoneResultSetDataType::DATA_TYPE_DOUBLE: { data = GetDoubleValFromColumn(index, resultSet); break; } default: { break; } } return data; } template static inline std::string GetStringValFromColumn(int index, T &resultSet) { std::string stringVal; if (resultSet->GetString(index, stringVal)) { return ""; } return stringVal; } template static inline int32_t GetIntValFromColumn(int index, T &resultSet) { int32_t integerVal; if (resultSet->GetInt(index, integerVal)) { return 0; } return integerVal; } template static inline int64_t GetLongValFromColumn(int index, T &resultSet) { int64_t integer64Val; if (resultSet->GetLong(index, integer64Val)) { return 0; } return integer64Val; } template static inline double GetDoubleValFromColumn(int index, T &resultSet) { double doubleVal; if (resultSet->GetDouble(index, doubleVal)) { return 0; } return doubleVal; } private: static std::variant DefaultVariantVal(RingtoneResultSetDataType type) { switch (type) { case RingtoneResultSetDataType::DATA_TYPE_STRING: return std::string(); case RingtoneResultSetDataType::DATA_TYPE_INT32: return 0; case RingtoneResultSetDataType::DATA_TYPE_INT64: return static_cast(0); case RingtoneResultSetDataType::DATA_TYPE_DOUBLE: return static_cast(0); default: RINGTONE_ERR_LOG("invalid data type %{public}d", type); } return 0; } }; template static inline std::string GetStringVal(const std::string &field, const ResultSet &result) { return std::get(ResultSetUtils::GetValFromColumn(field, result, DATA_TYPE_STRING)); } template static inline int32_t GetInt32Val(const std::string &field, const ResultSet &result) { return std::get(ResultSetUtils::GetValFromColumn(field, result, DATA_TYPE_INT32)); } template static inline int64_t GetInt64Val(const std::string &field, const ResultSet &result) { return std::get(ResultSetUtils::GetValFromColumn(field, result, DATA_TYPE_INT64)); } } // namespace Media } // namespace OHOS #endif // RESULT_SET_UTILS_H