• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 "AlbumOperation"
16 
17 #include "medialibrary_album_operations.h"
18 
19 #include "media_log.h"
20 #include "medialibrary_data_manager_utils.h"
21 #include "medialibrary_errno.h"
22 #include "medialibrary_object_utils.h"
23 #include "medialibrary_unistore_manager.h"
24 #include "values_bucket.h"
25 
26 using namespace std;
27 using namespace OHOS::NativeRdb;
28 
29 namespace OHOS {
30 namespace Media {
CreateAlbumOperation(MediaLibraryCommand & cmd)31 int32_t MediaLibraryAlbumOperations::CreateAlbumOperation(MediaLibraryCommand &cmd)
32 {
33     int64_t outRow = -1;
34     int32_t errCode = MediaLibraryObjectUtils::CreateDirObj(cmd, outRow);
35     if (errCode == E_SUCCESS) {
36         return outRow;
37     }
38     return errCode;
39 }
40 
DeleteAlbumOperation(MediaLibraryCommand & cmd)41 int32_t MediaLibraryAlbumOperations::DeleteAlbumOperation(MediaLibraryCommand &cmd)
42 {
43     string strId = cmd.GetOprnFileId();
44     string dirPath = MediaLibraryObjectUtils::GetPathByIdFromDb(strId);
45     if (dirPath.empty()) {
46         MEDIA_ERR_LOG("Get path of id %{private}s from database file!", strId.c_str());
47         return E_INVALID_PATH;
48     }
49     return MediaLibraryObjectUtils::DeleteDirObj(cmd, dirPath);
50 }
51 
52 // only support modify in the same parent folder, like: a/b/c --> a/b/d
ModifyAlbumOperation(MediaLibraryCommand & cmd)53 int32_t MediaLibraryAlbumOperations::ModifyAlbumOperation(MediaLibraryCommand &cmd)
54 {
55     string strId = cmd.GetOprnFileId();
56     string srcDirPath = MediaLibraryObjectUtils::GetPathByIdFromDb(strId);
57     if (srcDirPath.empty()) {
58         MEDIA_ERR_LOG("Get path of id %{private}s from database file!", strId.c_str());
59         return E_INVALID_PATH;
60     }
61 
62     auto values = cmd.GetValueBucket();
63     string dstDirName;
64     ValueObject valueObject;
65     if (values.GetObject(MEDIA_DATA_DB_ALBUM_NAME, valueObject)) {
66         valueObject.GetString(dstDirName);
67     }
68     string dstDirPath = MediaLibraryDataManagerUtils::GetParentPath(srcDirPath) + "/" + dstDirName;
69 
70     return MediaLibraryObjectUtils::RenameDirObj(cmd, srcDirPath, dstDirPath);
71 }
72 
GetDistributedAlbumSql(const string & strQueryCondition,const string & tableName)73 string MediaLibraryAlbumOperations::GetDistributedAlbumSql(const string &strQueryCondition, const string &tableName)
74 {
75     string distributedAlbumSql = "SELECT * FROM ( " + DISTRIBUTED_ABLUM_COLUMNS + " FROM " + tableName + " " +
76         FILE_TABLE + ", " + tableName + " " + ABLUM_TABLE +
77         DISTRIBUTED_ABLUM_WHERE_AND_GROUPBY + " )";
78     if (!strQueryCondition.empty()) {
79         distributedAlbumSql += " WHERE " + strQueryCondition;
80     }
81     MEDIA_DEBUG_LOG("GetDistributedAlbumSql distributedAlbumSql = %{private}s", distributedAlbumSql.c_str());
82     return distributedAlbumSql;
83 }
84 
QueryAlbumOperation(MediaLibraryCommand & cmd,vector<string> columns)85 shared_ptr<AbsSharedResultSet> MediaLibraryAlbumOperations::QueryAlbumOperation(
86     MediaLibraryCommand &cmd, vector<string> columns)
87 {
88     auto uniStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
89     if (uniStore == nullptr) {
90         MEDIA_ERR_LOG("uniStore is nullptr!");
91         return nullptr;
92     }
93 
94     if (cmd.GetOprnObject() == OperationObject::MEDIA_VOLUME) {
95         MEDIA_DEBUG_LOG("QUERY_MEDIA_VOLUME = %{public}s", QUERY_MEDIA_VOLUME.c_str());
96         return uniStore->QuerySql(QUERY_MEDIA_VOLUME);
97     }
98 
99     string strQueryCondition = cmd.GetAbsRdbPredicates()->GetWhereClause();
100     strQueryCondition += " GROUP BY " + MEDIA_DATA_DB_BUCKET_ID;
101     cmd.GetAbsRdbPredicates()->SetWhereClause(strQueryCondition);
102     string networkId = cmd.GetOprnDevice();
103     if (!networkId.empty()) {
104         string tableName = cmd.GetTableName();
105         MEDIA_INFO_LOG("tableName is %{private}s", tableName.c_str());
106         if (!strQueryCondition.empty()) {
107             strQueryCondition = MediaLibraryDataManagerUtils::ObtionCondition(strQueryCondition,
108                 cmd.GetAbsRdbPredicates()->GetWhereArgs());
109         }
110         string distributedAlbumSql = GetDistributedAlbumSql(strQueryCondition, tableName);
111         return uniStore->QuerySql(distributedAlbumSql);
112     }
113 
114     if (!strQueryCondition.empty()) {
115         return uniStore->Query(cmd, columns);
116     }
117     string querySql = "SELECT * FROM " + cmd.GetTableName();
118     return uniStore->QuerySql(querySql);
119 }
120 } // namespace Media
121 } // namespace OHOS
122