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 "data_share_store.h"
17
18 #include <string>
19 #include <utility>
20 #include <vector>
21
22 #include "hilog/log.h"
23 #include "rdb_errno.h"
24 #include "rdb_helper.h"
25
26 #include "data_share_store_callback.h"
27 #include "file_util.h"
28 #include "sql_util.h"
29
30 using namespace OHOS::HiviewDFX::SubscribeStore;
31
32 namespace OHOS {
33 namespace HiviewDFX {
34
35 namespace {
36 constexpr HiLogLabel LABEL = {LOG_CORE, 0xD002D10, "HiView-DataShareStore"};
37 } // namespace
38
OnCreate(NativeRdb::RdbStore & rdbStore)39 int DataShareStoreCallback::OnCreate(NativeRdb::RdbStore &rdbStore)
40 {
41 std::vector<std::pair<std::string, std::string>> fields = {{EventTable::FIELD_UID, SQL_INT_TYPE},
42 {EventTable::FIELD_BUNDLE_NAME, SQL_TEXT_TYPE},
43 {EventTable::FIELD_SUBSCRIBETIME, SQL_BIGINT_TYPE},
44 {EventTable::FIELD_EVENTLIST, SQL_TEXT_TYPE}};
45 std::string sql = SqlUtil::GenerateCreateSql(EventTable::TABLE, fields);
46 if (int ret = rdbStore.ExecuteSql(sql); ret != NativeRdb::E_OK) {
47 HiLog::Error(LABEL, "failed to create events table, ret=%{public}d", ret);
48 return ret;
49 }
50 return NativeRdb::E_OK;
51 }
52
OnUpgrade(NativeRdb::RdbStore & rdbStore,int oldVersion,int newVersion)53 int DataShareStoreCallback::OnUpgrade(NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion)
54 {
55 HiLog::Debug(LABEL, "OnUpgrade, oldVersion=%{public}d, newVersion=%{public}d", oldVersion, newVersion);
56 return NativeRdb::E_OK;
57 }
58
GetDbStore()59 std::shared_ptr<NativeRdb::RdbStore> DataShareStore::GetDbStore()
60 {
61 if (dbStore_ == nullptr) {
62 std::lock_guard<std::mutex> lockGuard(dbMutex_);
63 if (dbStore_ == nullptr) {
64 dbStore_ = CreateDbStore();
65 }
66 }
67 return dbStore_;
68 }
69
DropTable(const std::string & tableName)70 int DataShareStore::DropTable(const std::string &tableName)
71 {
72 auto dbStore = GetDbStore();
73 if (dbStore == nullptr) {
74 HiLog::Error(LABEL, "failed to drop table %{public}s, db is null", tableName.c_str());
75 return DB_FAILED;
76 }
77 std::string sql = SqlUtil::GenerateDropSql(tableName);
78 if (int ret = dbStore->ExecuteSql(sql); ret != NativeRdb::E_OK) {
79 HiLog::Error(LABEL, "failed to drop table %{public}s, ret=%{public}d", tableName.c_str(), ret);
80 return DB_FAILED;
81 }
82 return DB_SUCC;
83 }
84
CreateDbStore()85 std::shared_ptr<NativeRdb::RdbStore> DataShareStore::CreateDbStore()
86 {
87 if (dirPath_.empty()) {
88 HiLog::Error(LABEL, "failed to create db store, path is empty");
89 return nullptr;
90 }
91 if (!FileUtil::FileExists(dirPath_) && !FileUtil::ForceCreateDirectory(dirPath_)) {
92 HiLog::Error(LABEL, "failed to create database dir.");
93 return nullptr;
94 }
95 int ret = NativeRdb::E_OK;
96 NativeRdb::RdbStoreConfig config(dirPath_ + DATABASE_NAME);
97 DataShareStoreCallback callback;
98 auto dbStore = NativeRdb::RdbHelper::GetRdbStore(config, 1, callback, ret);
99 if (ret != NativeRdb::E_OK || dbStore == nullptr) {
100 HiLog::Error(LABEL, "failed to create db store, ret=%{public}d", ret);
101 return nullptr;
102 }
103 return dbStore;
104 }
105
DestroyDbStore()106 int DataShareStore::DestroyDbStore()
107 {
108 if (dbStore_ == nullptr) {
109 return DB_SUCC;
110 }
111 dbStore_ = nullptr;
112 if (int ret = NativeRdb::RdbHelper::DeleteRdbStore(dirPath_ + DATABASE_NAME); ret != NativeRdb::E_OK) {
113 HiLog::Error(LABEL, "failed to destroy db store, ret=%{public}d", ret);
114 return DB_FAILED;
115 }
116 return DB_SUCC;
117 }
118
119 } // namespace HiviewDFX
120 } // namespace OHOS