13e5483f6Sopenharmony_ci/*
23e5483f6Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
33e5483f6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43e5483f6Sopenharmony_ci * you may not use this file except in compliance with the License.
53e5483f6Sopenharmony_ci * You may obtain a copy of the License at
63e5483f6Sopenharmony_ci *
73e5483f6Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83e5483f6Sopenharmony_ci *
93e5483f6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103e5483f6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113e5483f6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123e5483f6Sopenharmony_ci * See the License for the specific language governing permissions and
133e5483f6Sopenharmony_ci * limitations under the License.
143e5483f6Sopenharmony_ci */
153e5483f6Sopenharmony_ci#ifndef RESULT_SET_UTILS_H
163e5483f6Sopenharmony_ci#define RESULT_SET_UTILS_H
173e5483f6Sopenharmony_ci
183e5483f6Sopenharmony_ci#include "ringtone_log.h"
193e5483f6Sopenharmony_ci#include "ringtone_type.h"
203e5483f6Sopenharmony_ci
213e5483f6Sopenharmony_cinamespace OHOS {
223e5483f6Sopenharmony_cinamespace Media {
233e5483f6Sopenharmony_ci#define EXPORT __attribute__ ((visibility ("default")))
243e5483f6Sopenharmony_ciclass ResultSetUtils {
253e5483f6Sopenharmony_cipublic:
263e5483f6Sopenharmony_ci    template<typename T>
273e5483f6Sopenharmony_ci    static std::variant<int32_t, std::string, int64_t, double> GetValFromColumn(const std::string &columnName,
283e5483f6Sopenharmony_ci        T &resultSet, RingtoneResultSetDataType type)
293e5483f6Sopenharmony_ci    {
303e5483f6Sopenharmony_ci        if (resultSet == nullptr) {
313e5483f6Sopenharmony_ci            RINGTONE_ERR_LOG("resultSet is nullptr");
323e5483f6Sopenharmony_ci            return DefaultVariantVal(type);
333e5483f6Sopenharmony_ci        }
343e5483f6Sopenharmony_ci
353e5483f6Sopenharmony_ci        int32_t err;
363e5483f6Sopenharmony_ci        int32_t index = 0;
373e5483f6Sopenharmony_ci        err = resultSet->GetColumnIndex(columnName, index);
383e5483f6Sopenharmony_ci        if (err) {
393e5483f6Sopenharmony_ci            RINGTONE_ERR_LOG("get column index err %{public}d", err);
403e5483f6Sopenharmony_ci            return DefaultVariantVal(type);
413e5483f6Sopenharmony_ci        }
423e5483f6Sopenharmony_ci
433e5483f6Sopenharmony_ci        std::variant<int32_t, std::string, int64_t, double> data;
443e5483f6Sopenharmony_ci        switch (type) {
453e5483f6Sopenharmony_ci            case RingtoneResultSetDataType::DATA_TYPE_STRING: {
463e5483f6Sopenharmony_ci                data = GetStringValFromColumn(index, resultSet);
473e5483f6Sopenharmony_ci                break;
483e5483f6Sopenharmony_ci            }
493e5483f6Sopenharmony_ci            case RingtoneResultSetDataType::DATA_TYPE_INT32: {
503e5483f6Sopenharmony_ci                data = GetIntValFromColumn(index, resultSet);
513e5483f6Sopenharmony_ci                break;
523e5483f6Sopenharmony_ci            }
533e5483f6Sopenharmony_ci            case RingtoneResultSetDataType::DATA_TYPE_INT64: {
543e5483f6Sopenharmony_ci                data = GetLongValFromColumn(index, resultSet);
553e5483f6Sopenharmony_ci                break;
563e5483f6Sopenharmony_ci            }
573e5483f6Sopenharmony_ci            case RingtoneResultSetDataType::DATA_TYPE_DOUBLE: {
583e5483f6Sopenharmony_ci                data = GetDoubleValFromColumn(index, resultSet);
593e5483f6Sopenharmony_ci                break;
603e5483f6Sopenharmony_ci            }
613e5483f6Sopenharmony_ci            default: {
623e5483f6Sopenharmony_ci                break;
633e5483f6Sopenharmony_ci            }
643e5483f6Sopenharmony_ci        }
653e5483f6Sopenharmony_ci
663e5483f6Sopenharmony_ci        return data;
673e5483f6Sopenharmony_ci    }
683e5483f6Sopenharmony_ci
693e5483f6Sopenharmony_ci    template<typename T>
703e5483f6Sopenharmony_ci    static inline std::string GetStringValFromColumn(int index, T &resultSet)
713e5483f6Sopenharmony_ci    {
723e5483f6Sopenharmony_ci        std::string stringVal;
733e5483f6Sopenharmony_ci        if (resultSet->GetString(index, stringVal)) {
743e5483f6Sopenharmony_ci            return "";
753e5483f6Sopenharmony_ci        }
763e5483f6Sopenharmony_ci        return stringVal;
773e5483f6Sopenharmony_ci    }
783e5483f6Sopenharmony_ci
793e5483f6Sopenharmony_ci    template<typename T>
803e5483f6Sopenharmony_ci    static inline int32_t GetIntValFromColumn(int index, T &resultSet)
813e5483f6Sopenharmony_ci    {
823e5483f6Sopenharmony_ci        int32_t integerVal;
833e5483f6Sopenharmony_ci        if (resultSet->GetInt(index, integerVal)) {
843e5483f6Sopenharmony_ci            return 0;
853e5483f6Sopenharmony_ci        }
863e5483f6Sopenharmony_ci        return integerVal;
873e5483f6Sopenharmony_ci    }
883e5483f6Sopenharmony_ci
893e5483f6Sopenharmony_ci    template<typename T>
903e5483f6Sopenharmony_ci    static inline int64_t GetLongValFromColumn(int index, T &resultSet)
913e5483f6Sopenharmony_ci    {
923e5483f6Sopenharmony_ci        int64_t integer64Val;
933e5483f6Sopenharmony_ci        if (resultSet->GetLong(index, integer64Val)) {
943e5483f6Sopenharmony_ci            return 0;
953e5483f6Sopenharmony_ci        }
963e5483f6Sopenharmony_ci        return integer64Val;
973e5483f6Sopenharmony_ci    }
983e5483f6Sopenharmony_ci
993e5483f6Sopenharmony_ci    template<typename T>
1003e5483f6Sopenharmony_ci    static inline double GetDoubleValFromColumn(int index, T &resultSet)
1013e5483f6Sopenharmony_ci    {
1023e5483f6Sopenharmony_ci        double doubleVal;
1033e5483f6Sopenharmony_ci        if (resultSet->GetDouble(index, doubleVal)) {
1043e5483f6Sopenharmony_ci            return 0;
1053e5483f6Sopenharmony_ci        }
1063e5483f6Sopenharmony_ci        return doubleVal;
1073e5483f6Sopenharmony_ci    }
1083e5483f6Sopenharmony_ci
1093e5483f6Sopenharmony_ciprivate:
1103e5483f6Sopenharmony_ci    static std::variant<int32_t, std::string, int64_t, double> DefaultVariantVal(RingtoneResultSetDataType type)
1113e5483f6Sopenharmony_ci    {
1123e5483f6Sopenharmony_ci        switch (type) {
1133e5483f6Sopenharmony_ci            case RingtoneResultSetDataType::DATA_TYPE_STRING:
1143e5483f6Sopenharmony_ci                return std::string();
1153e5483f6Sopenharmony_ci            case RingtoneResultSetDataType::DATA_TYPE_INT32:
1163e5483f6Sopenharmony_ci                return 0;
1173e5483f6Sopenharmony_ci            case RingtoneResultSetDataType::DATA_TYPE_INT64:
1183e5483f6Sopenharmony_ci                return static_cast<int64_t>(0);
1193e5483f6Sopenharmony_ci            case RingtoneResultSetDataType::DATA_TYPE_DOUBLE:
1203e5483f6Sopenharmony_ci                return static_cast<double>(0);
1213e5483f6Sopenharmony_ci            default:
1223e5483f6Sopenharmony_ci                RINGTONE_ERR_LOG("invalid data type %{public}d", type);
1233e5483f6Sopenharmony_ci        }
1243e5483f6Sopenharmony_ci
1253e5483f6Sopenharmony_ci        return 0;
1263e5483f6Sopenharmony_ci    }
1273e5483f6Sopenharmony_ci};
1283e5483f6Sopenharmony_ci
1293e5483f6Sopenharmony_citemplate<typename ResultSet>
1303e5483f6Sopenharmony_cistatic inline std::string GetStringVal(const std::string &field, const ResultSet &result)
1313e5483f6Sopenharmony_ci{
1323e5483f6Sopenharmony_ci    return std::get<std::string>(ResultSetUtils::GetValFromColumn(field, result, DATA_TYPE_STRING));
1333e5483f6Sopenharmony_ci}
1343e5483f6Sopenharmony_citemplate<typename ResultSet>
1353e5483f6Sopenharmony_cistatic inline int32_t GetInt32Val(const std::string &field, const ResultSet &result)
1363e5483f6Sopenharmony_ci{
1373e5483f6Sopenharmony_ci    return std::get<int32_t>(ResultSetUtils::GetValFromColumn(field, result, DATA_TYPE_INT32));
1383e5483f6Sopenharmony_ci}
1393e5483f6Sopenharmony_citemplate<typename ResultSet>
1403e5483f6Sopenharmony_cistatic inline int64_t GetInt64Val(const std::string &field, const ResultSet &result)
1413e5483f6Sopenharmony_ci{
1423e5483f6Sopenharmony_ci    return std::get<int64_t>(ResultSetUtils::GetValFromColumn(field, result, DATA_TYPE_INT64));
1433e5483f6Sopenharmony_ci}
1443e5483f6Sopenharmony_ci} // namespace Media
1453e5483f6Sopenharmony_ci} // namespace  OHOS
1463e5483f6Sopenharmony_ci#endif // RESULT_SET_UTILS_H
147