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