1 /*
2  * Copyright (C) 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_opkey_helper.h"
17 
18 #include "opkey_data.h"
19 #include "rdb_errno.h"
20 #include "rdb_opkey_callback.h"
21 #include "rdb_store_config.h"
22 #include "vector"
23 
24 namespace OHOS {
25 namespace Telephony {
26 const int VERSION = 1;
RdbOpKeyHelper()27 RdbOpKeyHelper::RdbOpKeyHelper()
28 {
29 }
30 
Init()31 int RdbOpKeyHelper::Init()
32 {
33     int errCode = NativeRdb::E_OK;
34     NativeRdb::RdbStoreConfig config(dbPath_);
35     config.SetJournalMode(NativeRdb::JournalMode::MODE_TRUNCATE);
36     std::string opKeyTableStr;
37     CreateOpKeyInfoTableStr(opKeyTableStr);
38     std::string opKeyIndexStr;
39     CreateOpKeyInfoIndexStr(opKeyIndexStr);
40     std::vector<std::string> createTableVec;
41     createTableVec.push_back(opKeyTableStr);
42     createTableVec.push_back(opKeyIndexStr);
43     RdbOpKeyCallback callback(createTableVec);
44     CreateRdbStore(config, VERSION, callback, errCode);
45     return errCode;
46 }
47 
UpdateDbPath(const std::string & path)48 void RdbOpKeyHelper::UpdateDbPath(const std::string &path)
49 {
50     dbPath_ = path + DB_NAME;
51 }
52 
CreateOpKeyInfoTableStr(std::string & createTableStr)53 void RdbOpKeyHelper::CreateOpKeyInfoTableStr(std::string &createTableStr)
54 {
55     createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_OPKEY_INFO).append("(");
56     createTableStr.append(OpKeyData::ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
57     createTableStr.append(OpKeyData::MCCMNC).append(" TEXT NOT NULL , ");
58     createTableStr.append(OpKeyData::GID1).append(" TEXT , ");
59     createTableStr.append(OpKeyData::GID2).append(" TEXT , ");
60     createTableStr.append(OpKeyData::IMSI).append(" TEXT , ");
61     createTableStr.append(OpKeyData::SPN).append(" TEXT , ");
62     createTableStr.append(OpKeyData::ICCID).append(" TEXT , ");
63     createTableStr.append(OpKeyData::OPERATOR_NAME).append(" TEXT , ");
64     createTableStr.append(OpKeyData::OPERATOR_KEY).append(" TEXT DEFAULT '-1', ");
65     createTableStr.append(OpKeyData::OPERATOR_KEY_EXT).append(" TEXT DEFAULT '', ");
66     createTableStr.append(OpKeyData::RULE_ID).append(" INTEGER DEFAULT 0, ");
67     createTableStr.append("UNIQUE (").append(OpKeyData::MCCMNC).append(", ");
68     createTableStr.append(OpKeyData::GID1).append(", ");
69     createTableStr.append(OpKeyData::GID2).append(", ");
70     createTableStr.append(OpKeyData::IMSI).append(", ");
71     createTableStr.append(OpKeyData::SPN).append(", ");
72     createTableStr.append(OpKeyData::ICCID).append("))");
73 }
74 
CreateOpKeyInfoIndexStr(std::string & createIndexStr)75 void RdbOpKeyHelper::CreateOpKeyInfoIndexStr(std::string &createIndexStr)
76 {
77     createIndexStr.append("CREATE INDEX IF NOT EXISTS [").append(MCCMNC_INDEX).append("]");
78     createIndexStr.append("ON [").append(TABLE_OPKEY_INFO).append("]");
79     createIndexStr.append("([").append(OpKeyData::MCCMNC).append("])");
80 }
81 
CommitTransactionAction()82 int RdbOpKeyHelper::CommitTransactionAction()
83 {
84     int result = Commit();
85     if (result != NativeRdb::E_OK) {
86         RollBack();
87     }
88     return result;
89 }
90 
InitOpKeyDatabase()91 int64_t RdbOpKeyHelper::InitOpKeyDatabase()
92 {
93     return RdbOpKeyCallback::InitData(*store_, TABLE_OPKEY_INFO);
94 }
95 } // namespace Telephony
96 } // namespace OHOS
97