1 /*
2 * Copyright (C) 2021-2022 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_helper.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_pdp_profile_callback.h"
24 #include "rdb_store_config.h"
25 #include "values_bucket.h"
26 #include "vector"
27
28 namespace OHOS {
29 namespace Telephony {
RdbPdpProfileHelper()30 RdbPdpProfileHelper::RdbPdpProfileHelper() {}
31
Init()32 int RdbPdpProfileHelper::Init()
33 {
34 int errCode = NativeRdb::E_OK;
35 NativeRdb::RdbStoreConfig config(dbPath_);
36 config.SetJournalMode(NativeRdb::JournalMode::MODE_TRUNCATE);
37 std::string pdpProfileStr;
38 CreatePdpProfileTableStr(pdpProfileStr, TABLE_PDP_PROFILE);
39 std::vector<std::string> createTableVec;
40 createTableVec.push_back(pdpProfileStr);
41 RdbPdpProfileCallback callback(createTableVec);
42 CreateRdbStore(config, VERSION, callback, errCode);
43 return errCode;
44 }
45
UpdateDbPath(const std::string & path)46 void RdbPdpProfileHelper::UpdateDbPath(const std::string &path)
47 {
48 dbPath_ = path + DB_NAME;
49 }
50
CreatePdpProfileTableStr(std::string & createTableStr,const std::string & tableName)51 void RdbPdpProfileHelper::CreatePdpProfileTableStr(std::string &createTableStr, const std::string &tableName)
52 {
53 createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(tableName).append("(");
54 createTableStr.append(PdpProfileData::PROFILE_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
55 createTableStr.append(PdpProfileData::PROFILE_NAME).append(" TEXT DEFAULT '', ");
56 createTableStr.append(PdpProfileData::MCC).append(" TEXT DEFAULT '', ");
57 createTableStr.append(PdpProfileData::MNC).append(" TEXT DEFAULT '', ");
58 createTableStr.append(PdpProfileData::MCCMNC).append(" TEXT DEFAULT '', ");
59 createTableStr.append(PdpProfileData::APN).append(" TEXT DEFAULT '', ");
60 createTableStr.append(PdpProfileData::AUTH_TYPE).append(" INTEGER, ");
61 createTableStr.append(PdpProfileData::AUTH_USER).append(" TEXT DEFAULT '', ");
62 createTableStr.append(PdpProfileData::AUTH_PWD).append(" TEXT DEFAULT '', ");
63 createTableStr.append(PdpProfileData::APN_TYPES).append(" TEXT DEFAULT '', ");
64 createTableStr.append(PdpProfileData::IS_ROAMING_APN).append(" INTEGER, ");
65 createTableStr.append(PdpProfileData::APN_PROTOCOL).append(" TEXT DEFAULT '', ");
66 createTableStr.append(PdpProfileData::APN_ROAM_PROTOCOL).append(" TEXT DEFAULT '', ");
67 createTableStr.append(PdpProfileData::HOME_URL).append(" TEXT DEFAULT '', ");
68 createTableStr.append(PdpProfileData::MMS_IP_ADDRESS).append(" TEXT DEFAULT '', ");
69 createTableStr.append(PdpProfileData::PROXY_IP_ADDRESS).append(" TEXT DEFAULT '', ");
70 createTableStr.append(PdpProfileData::BEARING_SYSTEM_TYPE).append(" INTEGER, ");
71 createTableStr.append("UNIQUE (").append(PdpProfileData::MCC).append(", ");
72 createTableStr.append(PdpProfileData::MNC).append(", ");
73 createTableStr.append(PdpProfileData::APN).append(", ");
74 createTableStr.append(PdpProfileData::APN_TYPES).append(", ");
75 createTableStr.append(PdpProfileData::IS_ROAMING_APN).append(", ");
76 createTableStr.append(PdpProfileData::APN_PROTOCOL).append(", ");
77 createTableStr.append(PdpProfileData::APN_ROAM_PROTOCOL).append(", ");
78 createTableStr.append(PdpProfileData::HOME_URL).append(", ");
79 createTableStr.append(PdpProfileData::MMS_IP_ADDRESS).append(", ");
80 createTableStr.append(PdpProfileData::PROXY_IP_ADDRESS).append("))");
81 }
82
ResetApn()83 int RdbPdpProfileHelper::ResetApn()
84 {
85 int ret = BeginTransaction();
86 if (ret != NativeRdb::E_OK) {
87 DATA_STORAGE_LOGE("RdbPdpProfileHelper::ResetApn BeginTransaction is error!");
88 return ret;
89 }
90 std::string pdpProfileStr;
91 CreatePdpProfileTableStr(pdpProfileStr, TEMP_TABLE_PDP_PROFILE);
92 ret = ExecuteSql(pdpProfileStr);
93 if (ret != NativeRdb::E_OK) {
94 DATA_STORAGE_LOGE("RdbPdpProfileHelper::ResetApn create table temp_pdp_profile ret = %{public}d", ret);
95 return ret;
96 }
97 DATA_STORAGE_LOGI("RdbPdpProfileHelper::ResetApn create table success");
98 ParserUtil util;
99 std::vector<PdpProfile> vec;
100 ret = util.ParserPdpProfileJson(vec);
101 if (ret != DATA_STORAGE_SUCCESS) {
102 RollBack();
103 return ret;
104 }
105 for (size_t i = 0; i < vec.size(); i++) {
106 NativeRdb::ValuesBucket value;
107 util.ParserPdpProfileToValuesBucket(value, vec[i]);
108 int64_t id;
109 Insert(id, value, TEMP_TABLE_PDP_PROFILE);
110 }
111 ret = ExecuteSql("drop table " + std::string(TABLE_PDP_PROFILE));
112 if (ret != NativeRdb::E_OK) {
113 DATA_STORAGE_LOGE("RdbPdpProfileHelper::ResetApn drop table ret = %{public}d", ret);
114 RollBack();
115 return ret;
116 }
117 DATA_STORAGE_LOGI("RdbPdpProfileHelper::ResetApn success");
118 std::string sql;
119 sql.append("alter table ").append(TEMP_TABLE_PDP_PROFILE).append(" rename to ").append(TABLE_PDP_PROFILE);
120 ret = ExecuteSql(sql);
121 if (ret != NativeRdb::E_OK) {
122 DATA_STORAGE_LOGE("RdbPdpProfileHelper::ResetApn alter table ret = %{public}d", ret);
123 RollBack();
124 return ret;
125 }
126 DATA_STORAGE_LOGI("RdbPdpProfileHelper::ResetApn alter table success");
127 ret = CommitTransactionAction();
128 return ret;
129 }
130
CommitTransactionAction()131 int RdbPdpProfileHelper::CommitTransactionAction()
132 {
133 int result = Commit();
134 if (result != NativeRdb::E_OK) {
135 RollBack();
136 }
137 return result;
138 }
139 } // namespace Telephony
140 } // namespace OHOS
141