1 /*
2  * Copyright (c) 2023 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_global_params_helper.h"
17 
18 #include <vector>
19 
20 #include "data_storage_errors.h"
21 #include "data_storage_log_wrapper.h"
22 #include "parser_util.h"
23 #include "global_params_data.h"
24 #include "rdb_errno.h"
25 #include "rdb_global_params_callback.h"
26 #include "rdb_store_config.h"
27 #include "values_bucket.h"
28 
29 namespace OHOS {
30 namespace Telephony {
RdbGlobalParamsHelper()31 RdbGlobalParamsHelper::RdbGlobalParamsHelper() {}
32 
Init()33 int RdbGlobalParamsHelper::Init()
34 {
35     int errCode = NativeRdb::E_OK;
36     NativeRdb::RdbStoreConfig config(dbPath_);
37     config.SetJournalMode(NativeRdb::JournalMode::MODE_TRUNCATE);
38     std::string numMatchTableStr;
39     CreateGlobalParamsTableStr(numMatchTableStr, TABLE_NUMBER_MATCH);
40     std::string numMatchIndexStr;
41     CreateNumMatchIndexStr(numMatchIndexStr);
42     std::string eccDataTableStr;
43     CreateGlobalParamsTableStr(eccDataTableStr, TABLE_ECC_DATA);
44     std::vector<std::string> createTableVec;
45     createTableVec.push_back(numMatchTableStr);
46     createTableVec.push_back(numMatchIndexStr);
47     createTableVec.push_back(eccDataTableStr);
48     RdbGlobalParamsCallback callback(createTableVec);
49     CreateRdbStore(config, VERSION, callback, errCode);
50     return errCode;
51 }
52 
UpdateDbPath(const std::string &path)53 void RdbGlobalParamsHelper::UpdateDbPath(const std::string &path)
54 {
55     dbPath_ = path + DB_NAME;
56 }
57 
CreateNumMatchTableStr(std::string &createTableStr)58 void RdbGlobalParamsHelper::CreateNumMatchTableStr(std::string &createTableStr)
59 {
60     DATA_STORAGE_LOGD("RdbGlobalParamsHelper::CreateNumMatchTableStr start");
61     createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_NUMBER_MATCH).append("(");
62     createTableStr.append(NumMatchData::ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
63     createTableStr.append(NumMatchData::NAME).append(" TEXT DEFAULT '', ");
64     createTableStr.append(NumMatchData::MCC).append(" TEXT DEFAULT '', ");
65     createTableStr.append(NumMatchData::MNC).append(" TEXT DEFAULT '', ");
66     createTableStr.append(NumMatchData::MCCMNC).append(" TEXT NOT NULL , ");
67     createTableStr.append(NumMatchData::NUM_MATCH).append(" INTEGER DEFAULT 0, ");
68     createTableStr.append(NumMatchData::NUM_MATCH_SHORT).append(" INTEGER DEFAULT 0, ");
69     createTableStr.append("UNIQUE (").append(NumMatchData::MCCMNC).append(", ");
70     createTableStr.append(NumMatchData::NAME).append("))");
71     DATA_STORAGE_LOGD("RdbGlobalParamsHelper::CreateNumMatchTableStr end: %s", createTableStr.c_str());
72 }
73 
CreateNumMatchIndexStr(std::string &createIndexStr)74 void RdbGlobalParamsHelper::CreateNumMatchIndexStr(std::string &createIndexStr)
75 {
76     DATA_STORAGE_LOGD("RdbGlobalParamsHelper::CreateNumMatchIndexStr start");
77     createIndexStr.append("CREATE INDEX IF NOT EXISTS [").append(NUMERIC_INDEX).append("]");
78     createIndexStr.append("ON [").append(TABLE_NUMBER_MATCH).append("]");
79     createIndexStr.append("([").append(NumMatchData::MCCMNC).append("])");
80     DATA_STORAGE_LOGD("RdbGlobalParamsHelper::CreateNumMatchIndexStr end: %s", createIndexStr.c_str());
81 }
82 
CreateEccDataTableStr(std::string &createTableStr)83 void RdbGlobalParamsHelper::CreateEccDataTableStr(std::string &createTableStr)
84 {
85     createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_ECC_DATA).append("(");
86     createTableStr.append(EccData::ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
87     createTableStr.append(EccData::NAME).append(" TEXT DEFAULT '', ");
88     createTableStr.append(EccData::MCC).append(" TEXT DEFAULT '', ");
89     createTableStr.append(EccData::MNC).append(" TEXT DEFAULT '', ");
90     createTableStr.append(EccData::NUMERIC).append(" TEXT DEFAULT '', ");
91     createTableStr.append(EccData::ECC_WITH_CARD).append(" TEXT DEFAULT '', ");
92     createTableStr.append(EccData::ECC_NO_CARD).append(" TEXT DEFAULT '', ");
93     createTableStr.append(EccData::ECC_FAKE).append(" TEXT DEFAULT '', ");
94     createTableStr.append("UNIQUE (").append(EccData::NUMERIC).append("))");
95 }
CreateGlobalParamsTableStr(std::string &createTableStr, const std::string &tableName)96 void RdbGlobalParamsHelper::CreateGlobalParamsTableStr(std::string &createTableStr, const std::string &tableName)
97 {
98     if (tableName == TABLE_ECC_DATA) {
99         return CreateEccDataTableStr(createTableStr);
100     } else if (tableName == TABLE_NUMBER_MATCH) {
101         return CreateNumMatchTableStr(createTableStr);
102     } else {
103         DATA_STORAGE_LOGI("TableName is not TABLE_ECC_DATA or TABLE_NUMBER_MATCH");
104     }
105     return;
106 }
CommitTransactionAction()107 int RdbGlobalParamsHelper::CommitTransactionAction()
108 {
109     int result = Commit();
110     if (result != NativeRdb::E_OK) {
111         RollBack();
112     }
113     return result;
114 }
115 } // namespace Telephony
116 } // namespace OHOS
117