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