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
OnCreate(NativeRdb::RdbStore & rdbStore)47 int RdbOpKeyCallback::OnCreate(NativeRdb::RdbStore &rdbStore)
48 {
49 DATA_STORAGE_LOGI("RdbOpKeyCallback::OnCreate");
50 RdbBaseCallBack::OnCreate(rdbStore);
51 InitData(rdbStore, TABLE_OPKEY_INFO);
52 return NativeRdb::E_OK;
53 }
54
OnOpen(NativeRdb::RdbStore & rdbStore)55 int RdbOpKeyCallback::OnOpen(NativeRdb::RdbStore &rdbStore)
56 {
57 DATA_STORAGE_LOGI("RdbOpKeyCallback::OnOpen");
58 return NativeRdb::E_OK;
59 }
60
InitData(NativeRdb::RdbStore & rdbStore,const std::string & tableName)61 void RdbOpKeyCallback::InitData(NativeRdb::RdbStore &rdbStore, const std::string &tableName)
62 {
63 ParserUtil util;
64 std::vector<OpKey> vec;
65 if (!util.ParseFromCustomSystem(vec)) {
66 return;
67 }
68 ClearData(rdbStore);
69 for (size_t i = 0; i < vec.size(); i++) {
70 NativeRdb::ValuesBucket value;
71 util.ParserOpKeyToValuesBucket(value, vec[i]);
72 int64_t id;
73 rdbStore.Insert(id, tableName, value);
74 }
75 }
76
ClearData(NativeRdb::RdbStore & rdbStore)77 int RdbOpKeyCallback::ClearData(NativeRdb::RdbStore &rdbStore)
78 {
79 std::string sql;
80 sql.append("delete from ").append(TABLE_OPKEY_INFO);
81 return rdbStore.ExecuteSql(sql);
82 }
83 } // namespace Telephony
84 } // namespace OHOS
85