1 /*
2  * Copyright (C) 2021 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 
16 #include "rdb_sim_callback.h"
17 
18 #include "data_storage_log_wrapper.h"
19 #include "rdb_errno.h"
20 #include "sim_data.h"
21 
22 namespace OHOS {
23 namespace NativeRdb {
24 class RdbStore;
25 }
26 namespace Telephony {
OnUpgrade(NativeRdb::RdbStore & rdbStore,int oldVersion,int newVersion)27 int RdbSimCallback::OnUpgrade(NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion)
28 {
29     DATA_STORAGE_LOGI("upgrade start oldVersion = %{public}d, newVersion = %{public}d ", oldVersion, newVersion);
30     if (oldVersion < VERSION_2 && newVersion >= VERSION_2) {
31         rdbStore.ExecuteSql(
32             "ALTER TABLE " + std::string(TABLE_SIM_INFO) + " DROP COLUMN " + std::string(SimData::IS_ACTIVE) + ";");
33         rdbStore.ExecuteSql("ALTER TABLE " + std::string(TABLE_SIM_INFO) + " ADD COLUMN " +
34                             std::string(SimData::IS_ACTIVE) + " INTEGER DEFAULT " + "1;");
35         oldVersion = VERSION_2;
36     }
37     if (oldVersion < VERSION_3 && newVersion >= VERSION_3) {
38         rdbStore.ExecuteSql("ALTER TABLE " + std::string(TABLE_SIM_INFO) + " ADD COLUMN " +
39                             std::string(SimData::OPKEY) + " TEXT DEFAULT " + " '' ;");
40         rdbStore.ExecuteSql("ALTER TABLE " + std::string(TABLE_SIM_INFO) + " ADD COLUMN " +
41                             std::string(SimData::MCC) + " TEXT DEFAULT " + " '' ;");
42         rdbStore.ExecuteSql("ALTER TABLE " + std::string(TABLE_SIM_INFO)+ " ADD COLUMN " +
43                             std::string(SimData::MNC) + " TEXT DEFAULT " + " '' ;");
44         oldVersion = VERSION_3;
45     }
46     if (oldVersion != newVersion) {
47         DATA_STORAGE_LOGE("upgrade error oldVersion = %{public}d, newVersion = %{public}d ", oldVersion, newVersion);
48         return NativeRdb::E_ERROR;
49     }
50     return NativeRdb::E_OK;
51 }
52 
OnDowngrade(NativeRdb::RdbStore & rdbStore,int currentVersion,int targetVersion)53 int RdbSimCallback::OnDowngrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion)
54 {
55     DATA_STORAGE_LOGI(
56         "Data_Storage RdbSimCallback::OnDowngrade##currentVersion = "
57         "%d, targetVersion = %d\n",
58         currentVersion, targetVersion);
59     return NativeRdb::E_OK;
60 }
61 
OnOpen(NativeRdb::RdbStore & rdbStore)62 int RdbSimCallback::OnOpen(NativeRdb::RdbStore &rdbStore)
63 {
64     return NativeRdb::E_OK;
65 }
66 } // namespace Telephony
67 } // namespace OHOS
68