• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 "Media_Cloud_Utils"
17 
18 #include "cloud_media_dao_utils.h"
19 
20 #include <sstream>
21 
22 #include "medialibrary_data_manager_utils.h"
23 #include "media_log.h"
24 #include "result_set_utils.h"
25 #include "medialibrary_unistore_manager.h"
26 
27 namespace OHOS::Media::CloudSync {
ToStringWithCommaAndQuote(const std::vector<std::string> & values)28 std::string CloudMediaDaoUtils::ToStringWithCommaAndQuote(const std::vector<std::string> &values)
29 {
30     std::stringstream os;
31     for (size_t i = 0; i < values.size(); ++i) {
32         os << "'" << values[i] << "'";
33         if (i != values.size() - 1) {
34             os << ",";
35         }
36     }
37     return os.str();
38 }
39 
ToStringWithComma(const std::vector<std::string> & fileIds)40 std::string CloudMediaDaoUtils::ToStringWithComma(const std::vector<std::string> &fileIds)
41 {
42     std::stringstream os;
43     for (size_t i = 0; i < fileIds.size(); ++i) {
44         os << fileIds[i];
45         if (i != fileIds.size() - 1) {
46             os << ",";
47         }
48     }
49     return os.str();
50 }
51 
FillParams(const std::string & sql,const std::vector<std::string> & bindArgs)52 std::string CloudMediaDaoUtils::FillParams(const std::string &sql, const std::vector<std::string> &bindArgs)
53 {
54     std::stringstream os;
55     std::string flag;
56     const std::string leftBrace = "{";
57     const std::string rightBrace = "}";
58     std::string val;
59     std::string result = sql;
60     for (size_t i = 0; i < bindArgs.size(); i++) {
61         flag = leftBrace + std::to_string(i) + rightBrace;
62         val = bindArgs[i];
63         size_t pos = result.find(flag);
64         while (pos != std::string::npos) {
65             os.str("");
66             os << result.substr(0, pos) << bindArgs[i];
67             os << (pos + flag.length() <= result.length() ? result.substr(pos + flag.length()) : "");
68             result = os.str();
69             os.str("");
70             pos = result.find(flag);
71         }
72     }
73     return result;
74 }
75 
GetNumbers(const std::vector<std::string> & albumIds)76 std::vector<std::string> CloudMediaDaoUtils::GetNumbers(const std::vector<std::string> &albumIds)
77 {
78     std::vector<std::string> numbers;
79     bool isNumber = false;
80     for (const auto &albumId : albumIds) {
81         isNumber = MediaLibraryDataManagerUtils::IsNumber(albumId);
82         if (!isNumber) {
83             continue;
84         }
85         numbers.emplace_back(albumId);
86     }
87     return numbers;
88 }
89 
ToInt32(const std::string & str)90 int32_t CloudMediaDaoUtils::ToInt32(const std::string &str)
91 {
92     char *end;
93     long number = std::strtol(str.c_str(), &end, 10);
94     if (*end != '\0') {
95         MEDIA_ERR_LOG("ToNumber failed, has invalid char. str: %{public}s", str.c_str());
96         return 0;
97     } else if (number < INT_MIN || number > INT_MAX) {
98         MEDIA_ERR_LOG("ToNumber failed, number overflow. str: %{public}s", str.c_str());
99         return 0;
100     }
101     return static_cast<int32_t>(number);
102 }
103 
GetStringVector(const std::vector<int32_t> & intVals)104 std::vector<std::string> CloudMediaDaoUtils::GetStringVector(const std::vector<int32_t> &intVals)
105 {
106     std::vector<std::string> strVals;
107     for (auto &val : intVals) {
108         strVals.emplace_back(std::to_string(val));
109     }
110     return strVals;
111 }
112 
VectorToString(const std::vector<uint64_t> & vec,const std::string & sep)113 std::string CloudMediaDaoUtils::VectorToString(const std::vector<uint64_t> &vec, const std::string &sep)
114 {
115     std::stringstream ss;
116     ss << "[";
117     for (size_t i = 0; i < vec.size(); ++i) {
118         ss << vec[i];
119         if (i != vec.size() - 1)
120             ss << sep;
121     }
122     ss << "]";
123     return ss.str();
124 }
125 
QueryCount(const std::string & sql,const std::string & columnName,int32_t & count)126 int32_t CloudMediaDaoUtils::QueryCount(const std::string &sql, const std::string &columnName, int32_t &count)
127 {
128     std::shared_ptr<MediaLibraryRdbStore> rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
129     CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_RDB, "rdbStore is nullptr");
130     auto resultSet = rdbStore->QueryByStep(sql);
131     CHECK_AND_RETURN_RET_LOG(
132         resultSet != nullptr, E_RDB, "Query failed, failed when executing sql: %{public}s", sql.c_str());
133     CHECK_AND_RETURN_RET_LOG(
134         resultSet->GoToFirstRow() == E_OK, E_RDB, "Go to first row failed, sql: %{public}s", sql.c_str());
135     count = GetInt32Val(columnName, resultSet);
136     return E_OK;
137 }
138 
ExecuteSql(const std::string & sql)139 int32_t CloudMediaDaoUtils::ExecuteSql(const std::string &sql)
140 {
141     std::shared_ptr<MediaLibraryRdbStore> rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
142     CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_RDB, "rdbStore is nullptr");
143     return rdbStore->ExecuteSql(sql);
144 }
145 }  // namespace OHOS::Media::CloudSync