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_sim_helper.h"
17
18 #include "data_storage_errors.h"
19 #include "data_storage_log_wrapper.h"
20 #include "rdb_errno.h"
21 #include "rdb_sim_callback.h"
22 #include "rdb_store_config.h"
23 #include "sim_data.h"
24 #include "values_bucket.h"
25 #include "vector"
26
27 namespace OHOS {
28 namespace Telephony {
RdbSimHelper()29 RdbSimHelper::RdbSimHelper() {}
30
Init()31 int RdbSimHelper::Init()
32 {
33 int errCode = NativeRdb::E_OK;
34 NativeRdb::RdbStoreConfig config(dbPath_);
35 config.SetJournalMode(NativeRdb::JournalMode::MODE_TRUNCATE);
36 std::string simInfoStr;
37 CreateSimInfoTableStr(simInfoStr);
38 std::vector<std::string> createTableVec;
39 createTableVec.push_back(simInfoStr);
40 RdbSimCallback callback(createTableVec);
41 CreateRdbStore(config, VERSION, callback, errCode);
42 return errCode;
43 }
44
UpdateDbPath(const std::string & path)45 void RdbSimHelper::UpdateDbPath(const std::string &path)
46 {
47 dbPath_ = path + DB_NAME;
48 }
49
CreateSimInfoTableStr(std::string & createTableStr)50 void RdbSimHelper::CreateSimInfoTableStr(std::string &createTableStr)
51 {
52 createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_SIM_INFO);
53 createTableStr.append("(").append(SimData::SIM_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
54 createTableStr.append(SimData::ICC_ID).append(" TEXT NOT NULL, ");
55 createTableStr.append(SimData::CARD_ID).append(" TEXT NOT NULL, ");
56 createTableStr.append(SimData::SLOT_INDEX).append(" INTEGER DEFAULT -1, ");
57 createTableStr.append(SimData::SHOW_NAME).append(" TEXT DEFAULT '', ");
58 createTableStr.append(SimData::PHONE_NUMBER).append(" TEXT DEFAULT '', ");
59 createTableStr.append(SimData::COUNTRY_CODE).append(" TEXT DEFAULT '', ");
60 createTableStr.append(SimData::IMSI).append(" TEXT DEFAULT '', ");
61 createTableStr.append(SimData::IS_MAIN_CARD).append(" INTEGER DEFAULT 0, ");
62 createTableStr.append(SimData::IS_VOICE_CARD).append(" INTEGER DEFAULT 0, ");
63 createTableStr.append(SimData::IS_MESSAGE_CARD).append(" INTEGER DEFAULT 0, ");
64 createTableStr.append(SimData::IS_CELLULAR_DATA_CARD).append(" INTEGER DEFAULT 0, ");
65 createTableStr.append(SimData::IS_ACTIVE).append(" INTEGER DEFAULT 0, ");
66 createTableStr.append(SimData::CARD_TYPE).append(" INTEGER , ");
67 createTableStr.append(SimData::IMS_SWITCH).append(" INTEGER DEFAULT -1, ");
68 createTableStr.append(SimData::LANGUAGE).append(" TEXT DEFAULT '', ");
69 createTableStr.append("UNIQUE (");
70 createTableStr.append(SimData::ICC_ID).append(", ");
71 createTableStr.append(SimData::CARD_ID).append("))");
72 }
73
SetDefaultCardByType(int32_t simId,int32_t type)74 int32_t RdbSimHelper::SetDefaultCardByType(int32_t simId, int32_t type)
75 {
76 int result = BeginTransaction();
77 if (result != NativeRdb::E_OK) {
78 DATA_STORAGE_LOGE("RdbSimHelper::SetDefaultCardByType BeginTransaction is error!");
79 return result;
80 }
81 int updateState = 0;
82 int whereState = 1;
83 result = UpdateCardStateByType(type, updateState, whereState);
84 if (result != NativeRdb::E_OK) {
85 DATA_STORAGE_LOGE("RdbSimHelper::SetDefaultCardByType UpdateCardStateByType is error!");
86 return result;
87 }
88 int changedRows = 0;
89 NativeRdb::ValuesBucket values;
90 switch (type) {
91 case static_cast<int32_t>(SimCardType::MAIN): {
92 values.PutInt(SimData::IS_MAIN_CARD, 1);
93 break;
94 }
95 case static_cast<int32_t>(SimCardType::VOICE): {
96 values.PutInt(SimData::IS_VOICE_CARD, 1);
97 break;
98 }
99 case static_cast<int32_t>(SimCardType::MESSAGE): {
100 values.PutInt(SimData::IS_MESSAGE_CARD, 1);
101 break;
102 }
103 case static_cast<int32_t>(SimCardType::CELLULAR_DATA): {
104 values.PutInt(SimData::IS_CELLULAR_DATA_CARD, 1);
105 break;
106 }
107 default:
108 DATA_STORAGE_LOGE("RdbSimHelper::SetDefaultDataByType is error!");
109 return DATA_STORAGE_ERROR;
110 }
111 std::string whereClause;
112 whereClause.append(SimData::SIM_ID).append("=").append(std::to_string(simId));
113 result = Update(changedRows, TABLE_SIM_INFO, values, whereClause);
114 if (result != NativeRdb::E_OK) {
115 DATA_STORAGE_LOGE("RdbSimHelper::SetDefaultCardByType Update is error!");
116 }
117 result = CommitTransactionAction();
118 return result;
119 }
120
UpdateCardStateByType(int32_t type,int32_t updateState,int32_t whereSate)121 int32_t RdbSimHelper::UpdateCardStateByType(int32_t type, int32_t updateState, int32_t whereSate)
122 {
123 int32_t result = DATA_STORAGE_ERROR;
124 bool isExist = false;
125 NativeRdb::ValuesBucket values;
126 std::string whereClause;
127 switch (type) {
128 case static_cast<int32_t>(SimCardType::MAIN): {
129 isExist = true;
130 values.PutInt(SimData::IS_MAIN_CARD, updateState);
131 whereClause.append(SimData::IS_MAIN_CARD).append("=").append(std::to_string(whereSate));
132 break;
133 }
134 case static_cast<int32_t>(SimCardType::VOICE): {
135 isExist = true;
136 values.PutInt(SimData::IS_VOICE_CARD, updateState);
137 whereClause.append(SimData::IS_VOICE_CARD).append("=").append(std::to_string(whereSate));
138 break;
139 }
140 case static_cast<int32_t>(SimCardType::MESSAGE): {
141 isExist = true;
142 values.PutInt(SimData::IS_MESSAGE_CARD, updateState);
143 whereClause.append(SimData::IS_MESSAGE_CARD).append("=").append(std::to_string(whereSate));
144 break;
145 }
146 case static_cast<int32_t>(SimCardType::CELLULAR_DATA): {
147 isExist = true;
148 values.PutInt(SimData::IS_CELLULAR_DATA_CARD, updateState);
149 whereClause.append(SimData::IS_CELLULAR_DATA_CARD).append("=").append(std::to_string(whereSate));
150 break;
151 }
152 default:
153 break;
154 }
155 if (isExist) {
156 int changedRows = 0;
157 return Update(changedRows, TABLE_SIM_INFO, values, whereClause);
158 }
159 return result;
160 }
161
CommitTransactionAction()162 int RdbSimHelper::CommitTransactionAction()
163 {
164 int result = Commit();
165 if (result != NativeRdb::E_OK) {
166 RollBack();
167 }
168 return result;
169 }
170
ClearData()171 int32_t RdbSimHelper::ClearData()
172 {
173 std::string sql;
174 sql.append("delete from ").append(TABLE_SIM_INFO);
175 return ExecuteSql(sql);
176 }
177 } // namespace Telephony
178 } // namespace OHOS