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 != newVersion) {
46 DATA_STORAGE_LOGE("upgrade error oldVersion = %{public}d, newVersion = %{public}d ", oldVersion, newVersion);
47 return NativeRdb::E_ERROR;
48 }
49 return NativeRdb::E_OK;
50 }
51
OnDowngrade(NativeRdb::RdbStore & rdbStore,int currentVersion,int targetVersion)52 int RdbPdpProfileCallback::OnDowngrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion)
53 {
54 DATA_STORAGE_LOGI(
55 "RdbPdpProfileCallback::OnDowngrade##currentVersion = %d, "
56 "targetVersion = %d\n",
57 currentVersion, targetVersion);
58 return NativeRdb::E_OK;
59 }
60
OnCreate(NativeRdb::RdbStore & rdbStore)61 int RdbPdpProfileCallback::OnCreate(NativeRdb::RdbStore &rdbStore)
62 {
63 DATA_STORAGE_LOGI("RdbPdpProfileCallback::OnCreate");
64 RdbBaseCallBack::OnCreate(rdbStore);
65 InitData(rdbStore, TABLE_PDP_PROFILE);
66 return NativeRdb::E_OK;
67 }
68
OnOpen(NativeRdb::RdbStore & rdbStore)69 int RdbPdpProfileCallback::OnOpen(NativeRdb::RdbStore &rdbStore)
70 {
71 DATA_STORAGE_LOGI("RdbPdpProfileCallback::OnOpen");
72 return NativeRdb::E_OK;
73 }
74
InitData(NativeRdb::RdbStore & rdbStore,const std::string & tableName)75 void RdbPdpProfileCallback::InitData(NativeRdb::RdbStore &rdbStore, const std::string &tableName)
76 {
77 DATA_STORAGE_LOGD("InitData start");
78 ParserUtil util;
79 std::vector<PdpProfile> vec;
80 int resultCode = util.ParserPdpProfileJson(vec);
81 if (resultCode != DATA_STORAGE_SUCCESS) {
82 DATA_STORAGE_LOGE("ParserPdpProfileJson fail");
83 return;
84 }
85 int32_t result = rdbStore.BeginTransaction();
86 if (result != NativeRdb::E_OK) {
87 DATA_STORAGE_LOGE("BeginTransaction error!");
88 return;
89 }
90 DATA_STORAGE_LOGD("InitData size = %{public}zu", vec.size());
91 for (size_t i = 0; i < vec.size(); i++) {
92 NativeRdb::ValuesBucket value;
93 util.ParserPdpProfileToValuesBucket(value, vec[i]);
94 int64_t id;
95 rdbStore.Insert(id, tableName, value);
96 }
97 rdbStore.Commit();
98 DATA_STORAGE_LOGD("InitData end");
99 }
100 } // namespace Telephony
101 } // namespace OHOS
102