1 /*
2 * Copyright (c) 2025 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/wms_rdb_open_callback.h"
17 #include "window_manager_hilog.h"
18
19 namespace OHOS {
20 namespace Rosen {
21 namespace {
22 constexpr int32_t RDB_VERSION_1 = 1;
23 constexpr int32_t RDB_VERSION_2 = 2;
24 const std::string STARTWINDOW_TYPE_COLUMN_INFO = "STARTWINDOW_TYPE TEXT";
25 } // namespace
WmsRdbOpenCallback(const WmsRdbConfig & wmsRdbConfig)26 WmsRdbOpenCallback::WmsRdbOpenCallback(const WmsRdbConfig& wmsRdbConfig)\
27 : wmsRdbConfig_(wmsRdbConfig) {}
OnCreate(NativeRdb::RdbStore & rdbStore)28 int32_t WmsRdbOpenCallback::OnCreate(NativeRdb::RdbStore& rdbStore)
29 {
30 TLOGI(WmsLogTag::WMS_PATTERN, "version: %{public}d", wmsRdbConfig_.version);
31 int32_t sqlResult = rdbStore.ExecuteSql(wmsRdbConfig_.createTableSql);
32 if (sqlResult != NativeRdb::E_OK) {
33 TLOGE(WmsLogTag::WMS_PATTERN, "execute sql error: %{public}d", sqlResult);
34 }
35 return sqlResult;
36 }
37
OnUpgrade(NativeRdb::RdbStore & rdbStore,int currentVersion,int targetVersion)38 int32_t WmsRdbOpenCallback::OnUpgrade(NativeRdb::RdbStore& rdbStore, int currentVersion, int targetVersion)
39 {
40 TLOGI(WmsLogTag::WMS_PATTERN, "%{public}d -> %{public}d", currentVersion, targetVersion);
41 while (currentVersion < targetVersion) {
42 UpgradeDbToNextVersion(rdbStore, ++currentVersion);
43 }
44 return NativeRdb::E_OK;
45 }
46
UpgradeDbToNextVersion(NativeRdb::RdbStore & rdbStore,int newVersion)47 void WmsRdbOpenCallback::UpgradeDbToNextVersion(NativeRdb::RdbStore& rdbStore, int newVersion)
48 {
49 switch (newVersion) {
50 case RDB_VERSION_1:
51 // no need upgrade for first version
52 break;
53 case RDB_VERSION_2:
54 AddColumn(rdbStore, STARTWINDOW_TYPE_COLUMN_INFO);
55 break;
56 default:
57 TLOGW(WmsLogTag::WMS_PATTERN, "unknown version: %{public}d", newVersion);
58 break;
59 }
60 }
61
AddColumn(NativeRdb::RdbStore & rdbStore,const std::string columnInfo)62 void WmsRdbOpenCallback::AddColumn(NativeRdb::RdbStore& rdbStore, const std::string columnInfo)
63 {
64 const std::string addColumnSql = "ALTER TABLE " + wmsRdbConfig_.tableName +
65 " ADD COLUMN " + columnInfo;
66 int32_t sqlResult = rdbStore.ExecuteSql(addColumnSql);
67 TLOGI(WmsLogTag::WMS_PATTERN, "res: %{public}d", sqlResult);
68 }
69
OnDowngrade(NativeRdb::RdbStore & rdbStore,int currentVersion,int targetVersion)70 int32_t WmsRdbOpenCallback::OnDowngrade(NativeRdb::RdbStore& rdbStore, int currentVersion, int targetVersion)
71 {
72 TLOGI(WmsLogTag::WMS_PATTERN, "%{public}d -> %{public}d", currentVersion, targetVersion);
73 return NativeRdb::E_OK;
74 }
75
OnOpen(NativeRdb::RdbStore & rdbStore)76 int32_t WmsRdbOpenCallback::OnOpen(NativeRdb::RdbStore& rdbStore)
77 {
78 TLOGI(WmsLogTag::WMS_PATTERN, "version: %{public}d", wmsRdbConfig_.version);
79 return NativeRdb::E_OK;
80 }
81
onCorruption(std::string databaseFile)82 int32_t WmsRdbOpenCallback::onCorruption(std::string databaseFile)
83 {
84 TLOGI(WmsLogTag::WMS_PATTERN, "version: %{public}d", wmsRdbConfig_.version);
85 return NativeRdb::E_OK;
86 }
87 } // namespace Rosen
88 } // namespace OHOS