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_callback.h"
17
18 #include "data_storage_errors.h"
19 #include "data_storage_log_wrapper.h"
20 #include "opkey_data.h"
21 #include "parser_util.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 {
OnUpgrade(NativeRdb::RdbStore & rdbStore,int oldVersion,int newVersion)29 int RdbOpKeyCallback::OnUpgrade(NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion)
30 {
31 DATA_STORAGE_LOGI(
32 "Data_Storage RdbOpKeyCallback::OnUpgrade##oldVersion = %d, "
33 "newVersion = %d\n",
34 oldVersion, newVersion);
35 return NativeRdb::E_OK;
36 }
37
OnDowngrade(NativeRdb::RdbStore & rdbStore,int currentVersion,int targetVersion)38 int RdbOpKeyCallback::OnDowngrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion)
39 {
40 DATA_STORAGE_LOGI(
41 "Data_Storage RdbOpKeyCallback::OnDowngrade##currentVersion = "
42 "%d, targetVersion = %d\n",
43 currentVersion, targetVersion);
44 return NativeRdb::E_OK;
45 }
46
OnOpen(NativeRdb::RdbStore & rdbStore)47 int RdbOpKeyCallback::OnOpen(NativeRdb::RdbStore &rdbStore)
48 {
49 DATA_STORAGE_LOGI("RdbOpKeyCallback::OnOpen\n");
50 InitData(rdbStore, TABLE_OPKEY_INFO);
51 return NativeRdb::E_OK;
52 }
53
InitData(NativeRdb::RdbStore & rdbStore,const std::string & tableName)54 void RdbOpKeyCallback::InitData(NativeRdb::RdbStore &rdbStore, const std::string &tableName)
55 {
56 ParserUtil util;
57 std::vector<OpKey> vec;
58 if (!util.ParseFromCustomSystem(vec)) {
59 return;
60 }
61 ClearData(rdbStore);
62 for (size_t i = 0; i < vec.size(); i++) {
63 NativeRdb::ValuesBucket value;
64 util.ParserOpKeyToValuesBucket(value, vec[i]);
65 int64_t id;
66 rdbStore.Insert(id, tableName, value);
67 }
68 }
69
ClearData(NativeRdb::RdbStore & rdbStore)70 int RdbOpKeyCallback::ClearData(NativeRdb::RdbStore &rdbStore)
71 {
72 std::string sql;
73 sql.append("delete from ").append(TABLE_OPKEY_INFO);
74 return rdbStore.ExecuteSql(sql);
75 }
76 } // namespace Telephony
77 } // namespace OHOS
78