1 /*
2 * Copyright (C) 2023 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 #include "command/list_command_v10.h"
16
17 #include <set>
18
19 #include "constant.h"
20 #include "datashare_result_set.h"
21 #include "media_column.h"
22 #include "media_log.h"
23 #include "medialibrary_errno.h"
24 #include "userfile_client_ex.h"
25 #include "utils/database_utils.h"
26
27 namespace OHOS {
28 namespace Media {
29 namespace MediaTool {
ListAsset(const ExecEnv & env,const std::string & tableName,const std::string & uri)30 static int32_t ListAsset(const ExecEnv &env, const std::string &tableName, const std::string &uri)
31 {
32 if (tableName.empty()) {
33 printf("%s table name issue, uri:%s\n", STR_FAIL.c_str(), uri.c_str());
34 return Media::E_ERR;
35 }
36 printf("Table Name: %s\n", tableName.c_str());
37 std::shared_ptr<DataShare::DataShareResultSet> resultSet;
38 auto res = UserFileClientEx::Query(tableName, uri, resultSet, true);
39 std::shared_ptr<FetchResult<FileAsset>> fetchResult = std::make_shared<FetchResult<FileAsset>>(resultSet);
40 MEDIA_DEBUG_LOG("fetchResult count:%{public}d", fetchResult->GetCount());
41 fetchResult->SetResultNapiType(ResultNapiType::TYPE_USERFILE_MGR);
42 if (res != Media::E_OK) {
43 printf("%s query issue failed. tableName:%s, uri:%s\n", STR_FAIL.c_str(), tableName.c_str(),
44 uri.c_str());
45 return Media::E_ERR;
46 }
47 if (fetchResult == nullptr) {
48 return Media::E_OK;
49 }
50 DumpOpt dumpOpt;
51 dumpOpt.count = fetchResult->GetCount();
52 dumpOpt.columns = {
53 MEDIA_DATA_DB_URI,
54 MediaColumn::MEDIA_NAME,
55 MediaColumn::MEDIA_FILE_PATH
56 };
57 DatabaseUtils::Dump(dumpOpt, fetchResult);
58 fetchResult->Close();
59 return Media::E_OK;
60 }
61
ListAssets(const ExecEnv & env,const std::string & tableName)62 static int32_t ListAssets(const ExecEnv &env, const std::string &tableName)
63 {
64 if (tableName.empty()) {
65 printf("%s table name is empty.\n", STR_FAIL.c_str());
66 return Media::E_ERR;
67 }
68 printf("Table Name: %s\n", tableName.c_str());
69 std::shared_ptr<DataShare::DataShareResultSet> resultSet;
70 auto res = UserFileClientEx::Query(tableName, "", resultSet, true);
71 std::shared_ptr<FetchResult<FileAsset>> fetchResult = std::make_shared<FetchResult<FileAsset>>(resultSet);
72 MEDIA_DEBUG_LOG("fetchResult count:%{public}d", fetchResult->GetCount());
73 fetchResult->SetResultNapiType(ResultNapiType::TYPE_USERFILE_MGR);
74 if (res != Media::E_OK) {
75 printf("%s query issue failed. tableName:%s\n", STR_FAIL.c_str(), tableName.c_str());
76 return Media::E_ERR;
77 }
78 if (fetchResult == nullptr) {
79 return Media::E_OK;
80 }
81 DumpOpt dumpOpt;
82 dumpOpt.count = fetchResult->GetCount();
83 MEDIA_DEBUG_LOG("count:%{public}d", dumpOpt.count);
84 dumpOpt.columns = {
85 MEDIA_DATA_DB_URI,
86 MediaColumn::MEDIA_NAME,
87 MediaColumn::MEDIA_FILE_PATH
88 };
89 DatabaseUtils::Dump(dumpOpt, fetchResult);
90 fetchResult->Close();
91 return Media::E_OK;
92 }
93
Start(const ExecEnv & env)94 int32_t ListCommandV10::Start(const ExecEnv &env)
95 {
96 if (env.listParam.isListAll) {
97 bool hasError = false;
98 auto tables = UserFileClientEx::GetSupportTables();
99 for (auto tableName : tables) {
100 if (ListAssets(env, tableName) != Media::E_OK) {
101 hasError = true;
102 }
103 printf("\n");
104 }
105 return hasError ? Media::E_ERR : Media::E_OK;
106 } else {
107 string tableName = UserFileClientEx::GetTableNameByUri(env.listParam.listUri);
108 return ListAsset(env, tableName, env.listParam.listUri);
109 }
110 }
111 } // namespace MediaTool
112 } // namespace Media
113 } // namespace OHOS
114