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 "hiview_logger.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 DEFINE_LOG_TAG("HiView-DataShareStore");
OnCreate(NativeRdb::RdbStore & rdbStore)35 int DataShareStoreCallback::OnCreate(NativeRdb::RdbStore &rdbStore)
36 {
37     std::vector<std::pair<std::string, std::string>> fields = {{EventTable::FIELD_UID, SQL_INT_TYPE},
38         {EventTable::FIELD_BUNDLE_NAME, SQL_TEXT_TYPE},
39         {EventTable::FIELD_SUBSCRIBETIME, SQL_BIGINT_TYPE},
40         {EventTable::FIELD_EVENTLIST, SQL_TEXT_TYPE}};
41     std::string sql = SqlUtil::GenerateCreateSql(EventTable::TABLE, fields);
42     if (int ret = rdbStore.ExecuteSql(sql); ret != NativeRdb::E_OK) {
43         HIVIEW_LOGE("failed to create events table, ret=%{public}d", ret);
44         return ret;
45     }
46     return NativeRdb::E_OK;
47 }
48 
OnUpgrade(NativeRdb::RdbStore & rdbStore,int oldVersion,int newVersion)49 int DataShareStoreCallback::OnUpgrade(NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion)
50 {
51     HIVIEW_LOGD("OnUpgrade, oldVersion=%{public}d, newVersion=%{public}d", oldVersion, newVersion);
52     return NativeRdb::E_OK;
53 }
54 
GetDbStore()55 std::shared_ptr<NativeRdb::RdbStore> DataShareStore::GetDbStore()
56 {
57     if (dbStore_ == nullptr) {
58         std::lock_guard<std::mutex> lockGuard(dbMutex_);
59         if (dbStore_ == nullptr) {
60             dbStore_ = CreateDbStore();
61         }
62     }
63     return dbStore_;
64 }
65 
DropTable(const std::string & tableName)66 int DataShareStore::DropTable(const std::string &tableName)
67 {
68     auto dbStore = GetDbStore();
69     if (dbStore == nullptr) {
70         HIVIEW_LOGE("failed to drop table %{public}s, db is null", tableName.c_str());
71         return DB_FAILED;
72     }
73     std::string sql = SqlUtil::GenerateDropSql(tableName);
74     if (int ret = dbStore->ExecuteSql(sql); ret != NativeRdb::E_OK) {
75         HIVIEW_LOGE("failed to drop table %{public}s, ret=%{public}d", tableName.c_str(), ret);
76         return DB_FAILED;
77     }
78     return DB_SUCC;
79 }
80 
CreateDbStore()81 std::shared_ptr<NativeRdb::RdbStore> DataShareStore::CreateDbStore()
82 {
83     if (dirPath_.empty()) {
84         HIVIEW_LOGE("failed to create db store, path is empty");
85         return nullptr;
86     }
87     if (!FileUtil::FileExists(dirPath_) && !FileUtil::ForceCreateDirectory(dirPath_)) {
88         HIVIEW_LOGE("failed to create database dir.");
89         return nullptr;
90     }
91     int ret = NativeRdb::E_OK;
92     NativeRdb::RdbStoreConfig config(dirPath_ + DATABASE_NAME);
93     DataShareStoreCallback callback;
94     auto dbStore = NativeRdb::RdbHelper::GetRdbStore(config, 1, callback, ret);
95     if (ret != NativeRdb::E_OK || dbStore == nullptr) {
96         HIVIEW_LOGE("failed to create db store, ret=%{public}d", ret);
97         return nullptr;
98     }
99     return dbStore;
100 }
101 
DestroyDbStore()102 int DataShareStore::DestroyDbStore()
103 {
104     if (dbStore_ == nullptr) {
105         return DB_SUCC;
106     }
107     dbStore_ = nullptr;
108     if (int ret = NativeRdb::RdbHelper::DeleteRdbStore(dirPath_ + DATABASE_NAME); ret != NativeRdb::E_OK) {
109         HIVIEW_LOGE("failed to destroy db store, ret=%{public}d", ret);
110         return DB_FAILED;
111     }
112     return DB_SUCC;
113 }
114 
115 }  // namespace HiviewDFX
116 }  // namespace OHOS