125c1cde8Sopenharmony_ci/*
225c1cde8Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
325c1cde8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
425c1cde8Sopenharmony_ci * you may not use this file except in compliance with the License.
525c1cde8Sopenharmony_ci * You may obtain a copy of the License at
625c1cde8Sopenharmony_ci *
725c1cde8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
825c1cde8Sopenharmony_ci *
925c1cde8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1025c1cde8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1125c1cde8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1225c1cde8Sopenharmony_ci * See the License for the specific language governing permissions and
1325c1cde8Sopenharmony_ci * limitations under the License.
1425c1cde8Sopenharmony_ci */
1525c1cde8Sopenharmony_ci
1625c1cde8Sopenharmony_ci#include "rdb_sim_callback.h"
1725c1cde8Sopenharmony_ci
1825c1cde8Sopenharmony_ci#include "data_storage_log_wrapper.h"
1925c1cde8Sopenharmony_ci#include "rdb_errno.h"
2025c1cde8Sopenharmony_ci#include "sim_data.h"
2125c1cde8Sopenharmony_ci
2225c1cde8Sopenharmony_cinamespace OHOS {
2325c1cde8Sopenharmony_cinamespace NativeRdb {
2425c1cde8Sopenharmony_ciclass RdbStore;
2525c1cde8Sopenharmony_ci}
2625c1cde8Sopenharmony_cinamespace Telephony {
2725c1cde8Sopenharmony_ciint RdbSimCallback::OnUpgrade(NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion)
2825c1cde8Sopenharmony_ci{
2925c1cde8Sopenharmony_ci    DATA_STORAGE_LOGI("upgrade start oldVersion = %{public}d, newVersion = %{public}d ", oldVersion, newVersion);
3025c1cde8Sopenharmony_ci    if (oldVersion < VERSION_2 && newVersion >= VERSION_2) {
3125c1cde8Sopenharmony_ci        rdbStore.ExecuteSql(
3225c1cde8Sopenharmony_ci            "ALTER TABLE " + std::string(TABLE_SIM_INFO) + " DROP COLUMN " + std::string(SimData::IS_ACTIVE) + ";");
3325c1cde8Sopenharmony_ci        rdbStore.ExecuteSql("ALTER TABLE " + std::string(TABLE_SIM_INFO) + " ADD COLUMN " +
3425c1cde8Sopenharmony_ci                            std::string(SimData::IS_ACTIVE) + " INTEGER DEFAULT " + "1;");
3525c1cde8Sopenharmony_ci        oldVersion = VERSION_2;
3625c1cde8Sopenharmony_ci    }
3725c1cde8Sopenharmony_ci    if (oldVersion < VERSION_3 && newVersion >= VERSION_3) {
3825c1cde8Sopenharmony_ci        rdbStore.ExecuteSql("ALTER TABLE " + std::string(TABLE_SIM_INFO) + " ADD COLUMN " +
3925c1cde8Sopenharmony_ci                            std::string(SimData::OPKEY) + " TEXT DEFAULT " + " '' ;");
4025c1cde8Sopenharmony_ci        rdbStore.ExecuteSql("ALTER TABLE " + std::string(TABLE_SIM_INFO) + " ADD COLUMN " +
4125c1cde8Sopenharmony_ci                            std::string(SimData::MCC) + " TEXT DEFAULT " + " '' ;");
4225c1cde8Sopenharmony_ci        rdbStore.ExecuteSql("ALTER TABLE " + std::string(TABLE_SIM_INFO)+ " ADD COLUMN " +
4325c1cde8Sopenharmony_ci                            std::string(SimData::MNC) + " TEXT DEFAULT " + " '' ;");
4425c1cde8Sopenharmony_ci        oldVersion = VERSION_3;
4525c1cde8Sopenharmony_ci    }
4625c1cde8Sopenharmony_ci    if (oldVersion != newVersion) {
4725c1cde8Sopenharmony_ci        DATA_STORAGE_LOGE("upgrade error oldVersion = %{public}d, newVersion = %{public}d ", oldVersion, newVersion);
4825c1cde8Sopenharmony_ci        return NativeRdb::E_ERROR;
4925c1cde8Sopenharmony_ci    }
5025c1cde8Sopenharmony_ci    return NativeRdb::E_OK;
5125c1cde8Sopenharmony_ci}
5225c1cde8Sopenharmony_ci
5325c1cde8Sopenharmony_ciint RdbSimCallback::OnDowngrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion)
5425c1cde8Sopenharmony_ci{
5525c1cde8Sopenharmony_ci    DATA_STORAGE_LOGI(
5625c1cde8Sopenharmony_ci        "Data_Storage RdbSimCallback::OnDowngrade##currentVersion = "
5725c1cde8Sopenharmony_ci        "%d, targetVersion = %d\n",
5825c1cde8Sopenharmony_ci        currentVersion, targetVersion);
5925c1cde8Sopenharmony_ci    return NativeRdb::E_OK;
6025c1cde8Sopenharmony_ci}
6125c1cde8Sopenharmony_ci
6225c1cde8Sopenharmony_ciint RdbSimCallback::OnOpen(NativeRdb::RdbStore &rdbStore)
6325c1cde8Sopenharmony_ci{
6425c1cde8Sopenharmony_ci    return NativeRdb::E_OK;
6525c1cde8Sopenharmony_ci}
6625c1cde8Sopenharmony_ci} // namespace Telephony
6725c1cde8Sopenharmony_ci} // namespace OHOS
68