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