1 /*
2  * Copyright (C) 2024 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 #define MLOG_TAG "RdbStore"
16 
17 #include "ringtone_rdbstore.h"
18 
19 #include <sys/stat.h>
20 
21 #include "rdb_sql_utils.h"
22 #include "ringtone_errno.h"
23 #include "ringtone_log.h"
24 #include "ringtone_tracer.h"
25 #include "result_set_utils.h"
26 #include "ringtone_rdb_callbacks.h"
27 
28 namespace OHOS::Media {
29 using namespace std;
30 using namespace OHOS;
31 
32 shared_ptr<NativeRdb::RdbStore> RingtoneRdbStore::rdbStore_;
33 
GetInstance(const std::shared_ptr<OHOS::AbilityRuntime::Context> & context)34 shared_ptr<RingtoneUnistore> RingtoneRdbStore::GetInstance(
35     const std::shared_ptr<OHOS::AbilityRuntime::Context> &context)
36 {
37     static shared_ptr<RingtoneRdbStore> instance = nullptr;
38     if (instance == nullptr && context == nullptr) {
39         RINGTONE_ERR_LOG("RingtoneRdbStore is not initialized");
40         return nullptr;
41     }
42     if (instance == nullptr) {
43         instance = make_shared<RingtoneRdbStore>(context);
44         if (instance->Init() != 0) {
45             RINGTONE_ERR_LOG("init RingtoneRdbStore failed");
46             instance = nullptr;
47             return instance;
48         }
49     }
50     return instance;
51 }
52 
RingtoneRdbStore(const shared_ptr<OHOS::AbilityRuntime::Context> & context)53 RingtoneRdbStore::RingtoneRdbStore(const shared_ptr<OHOS::AbilityRuntime::Context> &context)
54 {
55     if (context == nullptr) {
56         RINGTONE_ERR_LOG("Failed to get context");
57         return;
58     }
59     string databaseDir = context->GetDatabaseDir();
60     string name = RINGTONE_LIBRARY_DB_NAME;
61     int32_t errCode = 0;
62     string realPath = NativeRdb::RdbSqlUtils::GetDefaultDatabasePath(databaseDir, name, errCode);
63     config_.SetName(move(name));
64     config_.SetPath(move(realPath));
65     config_.SetBundleName(context->GetBundleName());
66     config_.SetArea(context->GetArea());
67     config_.SetSecurityLevel(NativeRdb::SecurityLevel::S3);
68 }
69 
Init()70 int32_t RingtoneRdbStore::Init()
71 {
72     RINGTONE_INFO_LOG("Init rdb store");
73     if (rdbStore_ != nullptr) {
74         return E_OK;
75     }
76 
77     int32_t errCode = 0;
78     RingtoneDataCallBack rdbDataCallBack;
79     rdbStore_ = NativeRdb::RdbHelper::GetRdbStore(config_, RINGTONE_RDB_VERSION, rdbDataCallBack, errCode);
80     if (rdbStore_ == nullptr) {
81         RINGTONE_ERR_LOG("GetRdbStore is failed , errCode=%{public}d", errCode);
82         return E_ERR;
83     }
84     RINGTONE_INFO_LOG("SUCCESS");
85     return E_OK;
86 }
87 
88 RingtoneRdbStore::~RingtoneRdbStore() = default;
89 
Stop()90 void RingtoneRdbStore::Stop()
91 {
92     if (rdbStore_ == nullptr) {
93         return;
94     }
95 
96     rdbStore_ = nullptr;
97 }
98 
Insert(RingtoneDataCommand & cmd,int64_t & rowId)99 int32_t RingtoneRdbStore::Insert(RingtoneDataCommand &cmd, int64_t &rowId)
100 {
101     RingtoneTracer tracer;
102     tracer.Start("RingtoneRdbStore::Insert");
103     if (rdbStore_ == nullptr) {
104         RINGTONE_ERR_LOG("Pointer rdbStore_ is nullptr. Maybe it didn't init successfully.");
105         return E_HAS_DB_ERROR;
106     }
107 
108     int32_t ret = rdbStore_->Insert(rowId, cmd.GetTableName(), cmd.GetValueBucket());
109     if (ret != NativeRdb::E_OK) {
110         RINGTONE_ERR_LOG("rdbStore_->Insert failed, ret = %{public}d", ret);
111         return E_HAS_DB_ERROR;
112     }
113 
114     RINGTONE_DEBUG_LOG("rdbStore_->Insert end, rowId = %d, ret = %{public}d", (int)rowId, ret);
115     return ret;
116 }
117 
Delete(RingtoneDataCommand & cmd,int32_t & deletedRows)118 int32_t RingtoneRdbStore::Delete(RingtoneDataCommand &cmd, int32_t &deletedRows)
119 {
120     if (rdbStore_ == nullptr) {
121         RINGTONE_ERR_LOG("Pointer rdbStore_ is nullptr. Maybe it didn't init successfully.");
122         return E_HAS_DB_ERROR;
123     }
124     RingtoneTracer tracer;
125     tracer.Start("RdbStore->DeleteByCmd");
126 
127     auto predicates = cmd.GetAbsRdbPredicates();
128     int32_t ret = rdbStore_->Delete(deletedRows, cmd.GetTableName(), predicates->GetWhereClause(),
129         predicates->GetWhereArgs());
130     if (ret != NativeRdb::E_OK) {
131         RINGTONE_ERR_LOG("rdbStore_->Delete failed, ret = %{public}d", ret);
132         return E_HAS_DB_ERROR;
133     }
134     return ret;
135 }
136 
Update(RingtoneDataCommand & cmd,int32_t & changedRows)137 int32_t RingtoneRdbStore::Update(RingtoneDataCommand &cmd, int32_t &changedRows)
138 {
139     if (rdbStore_ == nullptr) {
140         RINGTONE_ERR_LOG("rdbStore_ is nullptr");
141         return E_HAS_DB_ERROR;
142     }
143 
144     RingtoneTracer tracer;
145     tracer.Start("RdbStore->UpdateByCmd");
146     int32_t ret = rdbStore_->Update(changedRows, cmd.GetTableName(), cmd.GetValueBucket(),
147         cmd.GetAbsRdbPredicates()->GetWhereClause(), cmd.GetAbsRdbPredicates()->GetWhereArgs());
148     if (ret != NativeRdb::E_OK) {
149         RINGTONE_ERR_LOG("rdbStore_->Update failed, ret = %{public}d", ret);
150         return E_HAS_DB_ERROR;
151     }
152     return ret;
153 }
154 
Query(RingtoneDataCommand & cmd,const vector<string> & columns)155 shared_ptr<NativeRdb::ResultSet> RingtoneRdbStore::Query(RingtoneDataCommand &cmd,
156     const vector<string> &columns)
157 {
158     if (rdbStore_ == nullptr) {
159         RINGTONE_ERR_LOG("rdbStore_ is nullptr");
160         return nullptr;
161     }
162 
163     RingtoneTracer tracer;
164     tracer.Start("RdbStore->QueryByCmd");
165 
166     auto resultSet = rdbStore_->Query(*cmd.GetAbsRdbPredicates(), columns);
167     return resultSet;
168 }
169 
ExecuteSql(const string & sql)170 int32_t RingtoneRdbStore::ExecuteSql(const string &sql)
171 {
172     if (rdbStore_ == nullptr) {
173         RINGTONE_ERR_LOG("Pointer rdbStore_ is nullptr. Maybe it didn't init successfully.");
174         return E_HAS_DB_ERROR;
175     }
176     RingtoneTracer tracer;
177     tracer.Start("RdbStore->ExecuteSql");
178     int32_t ret = rdbStore_->ExecuteSql(sql);
179     if (ret != NativeRdb::E_OK) {
180         RINGTONE_ERR_LOG("rdbStore_->ExecuteSql failed, ret = %{public}d", ret);
181         return E_HAS_DB_ERROR;
182     }
183     return ret;
184 }
185 
QuerySql(const string & sql,const vector<string> & selectionArgs)186 shared_ptr<NativeRdb::ResultSet> RingtoneRdbStore::QuerySql(const string &sql, const vector<string> &selectionArgs)
187 {
188     if (rdbStore_ == nullptr) {
189         RINGTONE_ERR_LOG("Pointer rdbStore_ is nullptr. Maybe it didn't init successfully.");
190         return nullptr;
191     }
192 
193     RingtoneTracer tracer;
194     tracer.Start("RdbStore->QuerySql");
195     auto resultSet = rdbStore_->QuerySql(sql, selectionArgs);
196     return resultSet;
197 }
198 
GetRaw()199 shared_ptr<NativeRdb::RdbStore> RingtoneRdbStore::GetRaw()
200 {
201     return rdbStore_;
202 }
203 } // namespace OHOS::Media
204