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