• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_callback.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_store.h"
24 #include "string"
25 #include "values_bucket.h"
26 
27 namespace OHOS {
28 namespace Telephony {
RdbGlobalParamsCallback(const std::vector<std::string> & createTableVec)29 RdbGlobalParamsCallback::RdbGlobalParamsCallback(
30     const std::vector<std::string> &createTableVec) : RdbBaseCallBack(createTableVec) {}
31 
OnUpgrade(NativeRdb::RdbStore & rdbStore,int oldVersion,int newVersion)32 int RdbGlobalParamsCallback::OnUpgrade(NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion)
33 {
34     DATA_STORAGE_LOGI(
35         "RdbGlobalParamsCallback::OnUpgrade##oldVersion = %d, newVersion = %d\n", oldVersion, newVersion);
36     return NativeRdb::E_OK;
37 }
38 
OnDowngrade(NativeRdb::RdbStore & rdbStore,int currentVersion,int targetVersion)39 int RdbGlobalParamsCallback::OnDowngrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion)
40 {
41     DATA_STORAGE_LOGI(
42         "RdbGlobalParamsCallback::OnDowngrade##currentVersion = %d, "
43         "targetVersion = %d\n",
44         currentVersion, targetVersion);
45     return NativeRdb::E_OK;
46 }
47 
OnCreate(NativeRdb::RdbStore & rdbStore)48 int RdbGlobalParamsCallback::OnCreate(NativeRdb::RdbStore &rdbStore)
49 {
50     DATA_STORAGE_LOGI("RdbGlobalParamsCallback::OnCreate");
51     RdbBaseCallBack::OnCreate(rdbStore);
52     InitData(rdbStore, TABLE_NUMBER_MATCH);
53     InitData(rdbStore, TABLE_ECC_DATA);
54     return NativeRdb::E_OK;
55 }
56 
OnOpen(NativeRdb::RdbStore & rdbStore)57 int RdbGlobalParamsCallback::OnOpen(NativeRdb::RdbStore &rdbStore)
58 {
59     DATA_STORAGE_LOGI("RdbGlobalParamsCallback::OnOpen");
60     return NativeRdb::E_OK;
61 }
62 
InitEccData(NativeRdb::RdbStore & rdbStore,const std::string & tableName)63 void RdbGlobalParamsCallback::InitEccData(NativeRdb::RdbStore &rdbStore, const std::string &tableName)
64 {
65     DATA_STORAGE_LOGD("InitData start");
66     ParserUtil util;
67     std::vector<EccNum> vec;
68     int ret = util.ParserEccDataJson(vec);
69     if (ret != DATA_STORAGE_SUCCESS) {
70         DATA_STORAGE_LOGE("ParserEccDataJson fail!");
71         return;
72     }
73     ret = rdbStore.BeginTransaction();
74     if (ret != NativeRdb::E_OK) {
75         DATA_STORAGE_LOGE("BeginTransaction error!");
76         return;
77     }
78     DATA_STORAGE_LOGD("InitData size = %{public}zu", vec.size());
79     for (size_t i = 0; i < vec.size(); i++) {
80         NativeRdb::ValuesBucket value;
81         util.ParserEccDataToValuesBucket(value, vec[i]);
82         int64_t id;
83         rdbStore.Insert(id, tableName, value);
84     }
85     rdbStore.Commit();
86     DATA_STORAGE_LOGD("InitData end");
87 }
88 
InitNumMatchData(NativeRdb::RdbStore & rdbStore,const std::string & tableName)89 void RdbGlobalParamsCallback::InitNumMatchData(NativeRdb::RdbStore &rdbStore, const std::string &tableName)
90 {
91     DATA_STORAGE_LOGD("InitData start");
92     ParserUtil util;
93     std::vector<NumMatch> vec;
94     int resultCode = util.ParserNumMatchJson(vec);
95     if (resultCode != DATA_STORAGE_SUCCESS) {
96         DATA_STORAGE_LOGE("ParserNumMatchJson fail!");
97         return;
98     }
99     resultCode = rdbStore.BeginTransaction();
100     if (resultCode != NativeRdb::E_OK) {
101         DATA_STORAGE_LOGE("BeginTransaction error!");
102         return;
103     }
104     DATA_STORAGE_LOGD("InitData size = %{public}zu", vec.size());
105     for (size_t i = 0; i < vec.size(); i++) {
106         NativeRdb::ValuesBucket value;
107         util.ParserNumMatchToValuesBucket(value, vec[i]);
108         int64_t id;
109         rdbStore.Insert(id, tableName, value);
110     }
111     rdbStore.Commit();
112     DATA_STORAGE_LOGD("InitData end");
113 }
114 
InitData(NativeRdb::RdbStore & rdbStore,const std::string & tableName)115 void RdbGlobalParamsCallback::InitData(NativeRdb::RdbStore &rdbStore, const std::string &tableName)
116 {
117     DATA_STORAGE_LOGD("InitData start");
118     ClearData(rdbStore, tableName);
119     if (tableName.compare(TABLE_NUMBER_MATCH) == 0) {
120         InitNumMatchData(rdbStore, tableName);
121     } else if (tableName.compare(TABLE_ECC_DATA) == 0) {
122         InitEccData(rdbStore, tableName);
123     } else {
124         DATA_STORAGE_LOGE("RdbGlobalParamsCallback::InitData failed: tableName %s invalid\n", tableName.c_str());
125     }
126 }
127 
ClearData(NativeRdb::RdbStore & rdbStore,const std::string & tableName)128 int RdbGlobalParamsCallback::ClearData(NativeRdb::RdbStore &rdbStore, const std::string &tableName)
129 {
130     std::string sql;
131     sql.append("delete from").append(tableName);
132     return rdbStore.ExecuteSql(sql);
133 }
134 } // namespace Telephony
135 } // namespace OHOS
136