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_pdp_profile_callback.h"
17
18 #include "data_storage_errors.h"
19 #include "data_storage_log_wrapper.h"
20 #include "parser_util.h"
21 #include "pdp_profile_data.h"
22 #include "rdb_errno.h"
23 #include "rdb_store.h"
24 #include "values_bucket.h"
25
26 namespace OHOS {
27 namespace Telephony {
OnUpgrade(NativeRdb::RdbStore & rdbStore,int oldVersion,int newVersion)28 int RdbPdpProfileCallback::OnUpgrade(NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion)
29 {
30 DATA_STORAGE_LOGI("upgrade start oldVersion = %{public}d, newVersion = %{public}d ", oldVersion, newVersion);
31 if (oldVersion < VERSION_2 && newVersion >= VERSION_2) {
32 rdbStore.ExecuteSql("ALTER TABLE " + std::string(TABLE_PDP_PROFILE) + " ADD COLUMN " +
33 std::string(PdpProfileData::MVNO_TYPE) + " TEXT;");
34 rdbStore.ExecuteSql("ALTER TABLE " + std::string(TABLE_PDP_PROFILE) + " ADD COLUMN " +
35 std::string(PdpProfileData::MVNO_MATCH_DATA) + " TEXT;");
36 oldVersion = VERSION_2;
37 }
38 if (oldVersion < VERSION_3 && newVersion >= VERSION_3) {
39 rdbStore.ExecuteSql("ALTER TABLE " + std::string(TABLE_PDP_PROFILE) + " ADD COLUMN " +
40 std::string(PdpProfileData::EDITED_STATUS) + " INTEGER;");
41 rdbStore.ExecuteSql("ALTER TABLE " + std::string(TABLE_PDP_PROFILE) + " ADD COLUMN " +
42 std::string(PdpProfileData::SERVER) + " TEXT;");
43 oldVersion = VERSION_3;
44 }
45 if (oldVersion < VERSION_4 && newVersion >= VERSION_4) {
46 #ifdef OHOS_BUILD_ENABLE_TELEPHONY_EXT
47 rdbStore.ExecuteSql("DELETE FROM " + std::string(TABLE_PDP_PROFILE));
48 #endif
49 rdbStore.ExecuteSql("ALTER TABLE " + std::string(TABLE_PDP_PROFILE) + " ADD COLUMN " +
50 std::string(PdpProfileData::OPKEY) + " TEXT;");
51 oldVersion = VERSION_4;
52 }
53 if (oldVersion != newVersion) {
54 DATA_STORAGE_LOGE("upgrade error oldVersion = %{public}d, newVersion = %{public}d ", oldVersion, newVersion);
55 return NativeRdb::E_ERROR;
56 }
57 return NativeRdb::E_OK;
58 }
59
OnDowngrade(NativeRdb::RdbStore & rdbStore,int currentVersion,int targetVersion)60 int RdbPdpProfileCallback::OnDowngrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion)
61 {
62 DATA_STORAGE_LOGI(
63 "RdbPdpProfileCallback::OnDowngrade##currentVersion = %d, "
64 "targetVersion = %d\n",
65 currentVersion, targetVersion);
66 return NativeRdb::E_OK;
67 }
68
OnCreate(NativeRdb::RdbStore & rdbStore)69 int RdbPdpProfileCallback::OnCreate(NativeRdb::RdbStore &rdbStore)
70 {
71 DATA_STORAGE_LOGI("RdbPdpProfileCallback::OnCreate");
72 RdbBaseCallBack::OnCreate(rdbStore);
73 InitData(rdbStore, TABLE_PDP_PROFILE);
74 return NativeRdb::E_OK;
75 }
76
OnOpen(NativeRdb::RdbStore & rdbStore)77 int RdbPdpProfileCallback::OnOpen(NativeRdb::RdbStore &rdbStore)
78 {
79 DATA_STORAGE_LOGD("RdbPdpProfileCallback::OnOpen");
80 return NativeRdb::E_OK;
81 }
82
InitData(NativeRdb::RdbStore & rdbStore,const std::string & tableName)83 void RdbPdpProfileCallback::InitData(NativeRdb::RdbStore &rdbStore, const std::string &tableName)
84 {
85 DATA_STORAGE_LOGD("InitData start");
86 ParserUtil util;
87 std::vector<PdpProfile> vec;
88 int resultCode = util.ParserPdpProfileJson(vec);
89 if (resultCode != DATA_STORAGE_SUCCESS) {
90 DATA_STORAGE_LOGE("ParserPdpProfileJson fail");
91 return;
92 }
93 int32_t result = rdbStore.BeginTransaction();
94 if (result != NativeRdb::E_OK) {
95 DATA_STORAGE_LOGE("BeginTransaction error!");
96 return;
97 }
98 DATA_STORAGE_LOGD("InitData size = %{public}zu", vec.size());
99 for (size_t i = 0; i < vec.size(); i++) {
100 NativeRdb::ValuesBucket value;
101 util.ParserPdpProfileToValuesBucket(value, vec[i]);
102 int64_t id;
103 rdbStore.Insert(id, tableName, value);
104 }
105 rdbStore.Commit();
106 DATA_STORAGE_LOGD("InitData end");
107 }
108 } // namespace Telephony
109 } // namespace OHOS
110