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
27namespace OHOS {
28namespace Telephony {
29RdbSimHelper::RdbSimHelper() {}
30
31int 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
45void RdbSimHelper::UpdateDbPath(const std::string &path)
46{
47    dbPath_ = path + DB_NAME;
48}
49
50void 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 1, ");
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(SimData::OPKEY).append(" TEXT DEFAULT '', ");
70    createTableStr.append(SimData::MCC).append(" TEXT DEFAULT '', ");
71    createTableStr.append(SimData::MNC).append(" TEXT DEFAULT '', ");
72    createTableStr.append("UNIQUE (");
73    createTableStr.append(SimData::ICC_ID).append(", ");
74    createTableStr.append(SimData::CARD_ID).append("))");
75}
76
77int32_t RdbSimHelper::SetDefaultCardByType(int32_t simId, int32_t type)
78{
79    int result = BeginTransaction();
80    if (result != NativeRdb::E_OK) {
81        DATA_STORAGE_LOGE("RdbSimHelper::SetDefaultCardByType BeginTransaction is error!");
82        return result;
83    }
84    int updateState = 0;
85    int whereState = 1;
86    result = UpdateCardStateByType(type, updateState, whereState);
87    if (result != NativeRdb::E_OK) {
88        DATA_STORAGE_LOGE("RdbSimHelper::SetDefaultCardByType UpdateCardStateByType is error!");
89        return result;
90    }
91    int changedRows = 0;
92    NativeRdb::ValuesBucket values;
93    switch (type) {
94        case static_cast<int32_t>(SimCardType::MAIN): {
95            values.PutInt(SimData::IS_MAIN_CARD, 1);
96            break;
97        }
98        case static_cast<int32_t>(SimCardType::VOICE): {
99            values.PutInt(SimData::IS_VOICE_CARD, 1);
100            break;
101        }
102        case static_cast<int32_t>(SimCardType::MESSAGE): {
103            values.PutInt(SimData::IS_MESSAGE_CARD, 1);
104            break;
105        }
106        case static_cast<int32_t>(SimCardType::CELLULAR_DATA): {
107            values.PutInt(SimData::IS_CELLULAR_DATA_CARD, 1);
108            break;
109        }
110        default:
111            DATA_STORAGE_LOGE("RdbSimHelper::SetDefaultDataByType is error!");
112            return DATA_STORAGE_ERROR;
113    }
114    std::string whereClause;
115    whereClause.append(SimData::SIM_ID).append("=").append(std::to_string(simId));
116    result = Update(changedRows, TABLE_SIM_INFO, values, whereClause);
117    if (result != NativeRdb::E_OK) {
118        DATA_STORAGE_LOGE("RdbSimHelper::SetDefaultCardByType Update is error!");
119    }
120    result = CommitTransactionAction();
121    return result;
122}
123
124int32_t RdbSimHelper::UpdateCardStateByType(int32_t type, int32_t updateState, int32_t whereSate)
125{
126    int32_t result = DATA_STORAGE_ERROR;
127    bool isExist = false;
128    NativeRdb::ValuesBucket values;
129    std::string whereClause;
130    switch (type) {
131        case static_cast<int32_t>(SimCardType::MAIN): {
132            isExist = true;
133            values.PutInt(SimData::IS_MAIN_CARD, updateState);
134            whereClause.append(SimData::IS_MAIN_CARD).append("=").append(std::to_string(whereSate));
135            break;
136        }
137        case static_cast<int32_t>(SimCardType::VOICE): {
138            isExist = true;
139            values.PutInt(SimData::IS_VOICE_CARD, updateState);
140            whereClause.append(SimData::IS_VOICE_CARD).append("=").append(std::to_string(whereSate));
141            break;
142        }
143        case static_cast<int32_t>(SimCardType::MESSAGE): {
144            isExist = true;
145            values.PutInt(SimData::IS_MESSAGE_CARD, updateState);
146            whereClause.append(SimData::IS_MESSAGE_CARD).append("=").append(std::to_string(whereSate));
147            break;
148        }
149        case static_cast<int32_t>(SimCardType::CELLULAR_DATA): {
150            isExist = true;
151            values.PutInt(SimData::IS_CELLULAR_DATA_CARD, updateState);
152            whereClause.append(SimData::IS_CELLULAR_DATA_CARD).append("=").append(std::to_string(whereSate));
153            break;
154        }
155        default:
156            break;
157    }
158    if (isExist) {
159        int changedRows = 0;
160        return Update(changedRows, TABLE_SIM_INFO, values, whereClause);
161    }
162    return result;
163}
164
165int RdbSimHelper::CommitTransactionAction()
166{
167    int result = Commit();
168    if (result != NativeRdb::E_OK) {
169        RollBack();
170    }
171    return result;
172}
173
174int32_t RdbSimHelper::ClearData()
175{
176    std::string sql;
177    sql.append("delete from ").append(TABLE_SIM_INFO);
178    return ExecuteSql(sql);
179}
180} // namespace Telephony
181} // namespace OHOS