• 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 "firmware_database_callback.h"
17 
18 #include <vector>
19 
20 #include "rdb_errno.h"
21 
22 #include "itable.h"
23 #include "firmware_component_table.h"
24 #include "firmware_task_table.h"
25 
26 namespace OHOS {
27 namespace UpdateService {
28 
29 enum {
30     VERSION_INITIAL_VERSION = 1,
31     VERSION_ADD_STREAM_CAPACITY,
32 };
33 
OnCreate(NativeRdb::RdbStore & rdbStore)34 int FirmwareDatabaseCallback::OnCreate(NativeRdb::RdbStore &rdbStore)
35 {
36     std::vector<std::string> dbCreateSqlVector = GenerateDbCreateSqlVector();
37     for (auto &sql : dbCreateSqlVector) {
38         int ret = rdbStore.ExecuteSql(sql);
39         if (ret != NativeRdb::E_OK) {
40             ENGINE_LOGE("FirmwareDatabaseCallback OnCreate create table %{public}s, ret=%{public}d", sql.c_str(), ret);
41             return ret;
42         }
43     }
44     ENGINE_LOGI("FirmwareDatabaseCallback OnCreate success");
45     return NativeRdb::E_OK;
46 }
47 
OnUpgrade(NativeRdb::RdbStore & rdbStore,int oldVersion,int newVersion)48 int FirmwareDatabaseCallback::OnUpgrade(NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion)
49 {
50     ENGINE_LOGI("FirmwareDatabaseCallback OnUpgrade oldVersion=%{public}d newVersion=%{public}d", oldVersion,
51         newVersion);
52     if (oldVersion < VERSION_ADD_STREAM_CAPACITY && newVersion >= VERSION_ADD_STREAM_CAPACITY) {
53         rdbStore.ExecuteSql("ALTER TABLE " + FIRMWARE_TABLE_COMPONENT + " ADD COLUMN " +
54             COLUMN_COMPONENT_RECORD_POINT + " bigint;");
55         rdbStore.ExecuteSql("ALTER TABLE " + FIRMWARE_TABLE_TASK + " ADD COLUMN " +
56             COLUMN_TASK_IS_STREAM_UPGRADE + " integer;");
57     }
58     return NativeRdb::E_OK;
59 }
60 
OnOpen(NativeRdb::RdbStore & rdbStore)61 int FirmwareDatabaseCallback::OnOpen(NativeRdb::RdbStore &rdbStore)
62 {
63     ENGINE_LOGI("FirmwareDatabaseCallback OnOpen");
64     return NativeRdb::E_OK;
65 }
66 
GenerateDbCreateSqlVector()67 std::vector<std::string> FirmwareDatabaseCallback::GenerateDbCreateSqlVector()
68 {
69     std::vector<std::string> dbCreateSqlVector;
70     dbCreateSqlVector.push_back(FirmwareComponentTable().GetTableCreateSql());
71     dbCreateSqlVector.push_back(FirmwareTaskTable().GetTableCreateSql());
72     return dbCreateSqlVector;
73 }
74 } // namespace UpdateService
75 } // namespace OHOS