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 #include "ringtone_restore_db_utils.h"
17 
18 #include "ringtone_db_const.h"
19 #include "ringtone_log.h"
20 #include "ringtone_errno.h"
21 #include "result_set_utils.h"
22 #include "ringtone_rdb_callbacks.h"
23 #include "os_account_manager.h"
24 
25 namespace OHOS {
26 namespace Media {
27 const static int32_t CONNECT_SIZE = 10;
28 const std::string CUSTOM_COUNT = "count";
29 const std::string PRAGMA_TABLE_NAME = "name";
30 const std::string PRAGMA_TABLE_TYPE = "type";
InitDb(std::shared_ptr<NativeRdb::RdbStore> & rdbStore,const std::string & dbName,const std::string & dbPath,const std::string & bundleName,bool isMediaLibrary)31 int32_t RingtoneRestoreDbUtils::InitDb(std::shared_ptr<NativeRdb::RdbStore> &rdbStore, const std::string &dbName,
32     const std::string &dbPath, const std::string &bundleName, bool isMediaLibrary)
33 {
34     NativeRdb::RdbStoreConfig config(dbName);
35     config.SetPath(dbPath);
36     config.SetBundleName(bundleName);
37     config.SetReadConSize(CONNECT_SIZE);
38     config.SetSecurityLevel(NativeRdb::SecurityLevel::S3);
39 
40     int32_t err;
41     RingtoneDataCallBack cb;
42     rdbStore = NativeRdb::RdbHelper::GetRdbStore(config, RINGTONE_RDB_VERSION, cb, err);
43     return err;
44 }
45 
QueryInt(std::shared_ptr<NativeRdb::RdbStore> rdbStore,const std::string & sql,const std::string & column)46 int32_t RingtoneRestoreDbUtils::QueryInt(std::shared_ptr<NativeRdb::RdbStore> rdbStore, const std::string &sql,
47     const std::string &column)
48 {
49     if (rdbStore == nullptr) {
50         RINGTONE_ERR_LOG("rdb_ is nullptr, Maybe init failed.");
51         return 0;
52     }
53     auto resultSet = rdbStore->QuerySql(sql);
54     if (resultSet == nullptr || resultSet->GoToFirstRow() != NativeRdb::E_OK) {
55         return 0;
56     }
57     int32_t result = GetInt32Val(column, resultSet);
58     return result;
59 }
60 
Update(std::shared_ptr<NativeRdb::RdbStore> & rdbStore,int32_t & changeRows,NativeRdb::ValuesBucket & valuesBucket,std::unique_ptr<NativeRdb::AbsRdbPredicates> & predicates)61 int32_t RingtoneRestoreDbUtils::Update(std::shared_ptr<NativeRdb::RdbStore> &rdbStore, int32_t &changeRows,
62     NativeRdb::ValuesBucket &valuesBucket, std::unique_ptr<NativeRdb::AbsRdbPredicates> &predicates)
63 {
64     if (rdbStore == nullptr) {
65         RINGTONE_ERR_LOG("rdb_ is nullptr, Maybe init failed.");
66         return E_FAIL;
67     }
68     return rdbStore->Update(changeRows, valuesBucket, *predicates);
69 }
70 
QueryRingtoneCount(std::shared_ptr<NativeRdb::RdbStore> rdbStore)71 int32_t RingtoneRestoreDbUtils::QueryRingtoneCount(std::shared_ptr<NativeRdb::RdbStore> rdbStore)
72 {
73     static std::string QUERY_TONEFILES_ALL_COUNT = "SELECT count(1) AS count FROM ToneFiles";
74     return QueryInt(rdbStore, QUERY_TONEFILES_ALL_COUNT, CUSTOM_COUNT);
75 }
76 
GetQueryResultSet(const std::shared_ptr<NativeRdb::RdbStore> & rdbStore,const std::string & querySql)77 std::shared_ptr<NativeRdb::ResultSet> RingtoneRestoreDbUtils::GetQueryResultSet(
78     const std::shared_ptr<NativeRdb::RdbStore> &rdbStore, const std::string &querySql)
79 {
80     if (rdbStore == nullptr) {
81         RINGTONE_ERR_LOG("rdbStore is nullptr");
82         return nullptr;
83     }
84     return rdbStore->QuerySql(querySql);
85 }
86 
GetColumnInfoMap(const std::shared_ptr<NativeRdb::RdbStore> & rdbStore,const std::string & tableName)87 std::unordered_map<std::string, std::string> RingtoneRestoreDbUtils::GetColumnInfoMap(
88     const std::shared_ptr<NativeRdb::RdbStore> &rdbStore, const std::string &tableName)
89 {
90     std::unordered_map<std::string, std::string> columnInfoMap;
91     std::string querySql = "SELECT name, type FROM pragma_table_info('" + tableName + "')";
92     auto resultSet = GetQueryResultSet(rdbStore, querySql);
93     if (resultSet == nullptr) {
94         RINGTONE_ERR_LOG("resultSet is nullptr");
95         return columnInfoMap;
96     }
97     while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
98         std::string columnName = GetStringVal(PRAGMA_TABLE_NAME, resultSet);
99         std::string columnType = GetStringVal(PRAGMA_TABLE_TYPE, resultSet);
100         if (columnName.empty() || columnType.empty()) {
101             RINGTONE_ERR_LOG("Empty column name or type: %{public}s, %{public}s", columnName.c_str(),
102                 columnType.c_str());
103             continue;
104         }
105         columnInfoMap[columnName] = columnType;
106     }
107     return columnInfoMap;
108 }
109 
GetUserID(int & userId)110 bool RingtoneRestoreDbUtils::GetUserID(int &userId)
111 {
112     std::vector<int> activeIds;
113     int ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeIds);
114     if (ret != 0) {
115         RINGTONE_ERR_LOG("QueryActiveOsAccountIds failed ret:%{public}d", ret);
116         return false;
117     }
118     if (activeIds.empty()) {
119         RINGTONE_ERR_LOG("QueryActiveOsAccountIds activeIds empty");
120         return false;
121     }
122     userId = activeIds[0];
123     return true;
124 }
125 } // namespace Media
126 } // namespace OHOS
127