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