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
16 #include "ext_special_controller.h"
17 #include "datashare_log.h"
18
19 namespace OHOS {
20 namespace DataShare {
21 constexpr int INVALID_VALUE = -1;
OpenFile(const Uri & uri,const std::string & mode)22 int ExtSpecialController::OpenFile(const Uri &uri, const std::string &mode)
23 {
24 auto connection = connection_;
25 if (connection == nullptr) {
26 LOG_ERROR("connection is nullptr");
27 return INVALID_VALUE;
28 }
29 auto proxy = connection->GetDataShareProxy(uri_, token_);
30 if (proxy == nullptr) {
31 LOG_ERROR("proxy is nullptr");
32 return INVALID_VALUE;
33 }
34 return proxy->OpenFile(uri, mode);
35 }
36
OpenRawFile(const Uri & uri,const std::string & mode)37 int ExtSpecialController::OpenRawFile(const Uri &uri, const std::string &mode)
38 {
39 auto connection = connection_;
40 if (connection == nullptr) {
41 LOG_ERROR("connection is nullptr");
42 return INVALID_VALUE;
43 }
44 auto proxy = connection->GetDataShareProxy(uri_, token_);
45 if (proxy == nullptr) {
46 LOG_ERROR("proxy is nullptr");
47 return INVALID_VALUE;
48 }
49 return proxy->OpenRawFile(uri, mode);
50 }
51
GetType(const Uri & uri)52 std::string ExtSpecialController::GetType(const Uri &uri)
53 {
54 auto connection = connection_;
55 if (connection == nullptr) {
56 LOG_ERROR("connection is nullptr");
57 return "";
58 }
59 auto proxy = connection->GetDataShareProxy(uri_, token_);
60 if (proxy == nullptr) {
61 LOG_ERROR("proxy is nullptr");
62 return "";
63 }
64 return proxy->GetType(uri);
65 }
66
BatchInsert(const Uri & uri,const std::vector<DataShareValuesBucket> & values)67 int ExtSpecialController::BatchInsert(const Uri &uri, const std::vector<DataShareValuesBucket> &values)
68 {
69 auto connection = connection_;
70 if (connection == nullptr) {
71 LOG_ERROR("connection is nullptr");
72 return INVALID_VALUE;
73 }
74 auto proxy = connection->GetDataShareProxy(uri_, token_);
75 if (proxy == nullptr) {
76 LOG_ERROR("proxy is nullptr");
77 return INVALID_VALUE;
78 }
79 return proxy->BatchInsert(uri, values);
80 }
81
BatchUpdate(const UpdateOperations & operations,std::vector<BatchUpdateResult> & results)82 int ExtSpecialController::BatchUpdate(const UpdateOperations &operations,
83 std::vector<BatchUpdateResult> &results)
84 {
85 auto connection = connection_;
86 if (connection == nullptr) {
87 LOG_ERROR("connection is nullptr");
88 return INVALID_VALUE;
89 }
90 auto proxy = connection->GetDataShareProxy(uri_, token_);
91 if (proxy == nullptr) {
92 LOG_ERROR("proxy is nullptr");
93 return INVALID_VALUE;
94 }
95 return proxy->BatchUpdate(operations, results);
96 }
97
InsertExt(Uri & uri,const DataShareValuesBucket & value,std::string & result)98 int ExtSpecialController::InsertExt(Uri &uri, const DataShareValuesBucket &value, std::string &result)
99 {
100 auto connection = connection_;
101 if (connection == nullptr) {
102 LOG_ERROR("connection is nullptr");
103 return INVALID_VALUE;
104 }
105 auto proxy = connection->GetDataShareProxy(uri_, token_);
106 if (proxy == nullptr) {
107 LOG_ERROR("proxy is nullptr");
108 return INVALID_VALUE;
109 }
110 return proxy->InsertExt(uri, value, result);
111 }
112
ExecuteBatch(const std::vector<OperationStatement> & statements,ExecResultSet & result)113 int ExtSpecialController::ExecuteBatch(const std::vector<OperationStatement> &statements, ExecResultSet &result)
114 {
115 auto connection = connection_;
116 if (connection == nullptr) {
117 LOG_ERROR("connection is nullptr");
118 return INVALID_VALUE;
119 }
120 auto proxy = connection->GetDataShareProxy(uri_, token_);
121 if (proxy == nullptr) {
122 LOG_ERROR("proxy is nullptr");
123 return INVALID_VALUE;
124 }
125 return proxy->ExecuteBatch(statements, result);
126 }
127
NormalizeUri(const Uri & uri)128 Uri ExtSpecialController::NormalizeUri(const Uri &uri)
129 {
130 auto connection = connection_;
131 if (connection == nullptr) {
132 LOG_ERROR("connection is nullptr");
133 return Uri("");
134 }
135 auto proxy = connection->GetDataShareProxy(uri_, token_);
136 if (proxy == nullptr) {
137 LOG_ERROR("proxy is nullptr");
138 return Uri("");
139 }
140 return proxy->NormalizeUri(uri);
141 }
142
DenormalizeUri(const Uri & uri)143 Uri ExtSpecialController::DenormalizeUri(const Uri &uri)
144 {
145 auto connection = connection_;
146 if (connection == nullptr) {
147 LOG_ERROR("connection is nullptr");
148 return Uri("");
149 }
150 auto proxy = connection->GetDataShareProxy(uri_, token_);
151 if (proxy == nullptr) {
152 LOG_ERROR("proxy is nullptr");
153 return Uri("");
154 }
155 return proxy->DenormalizeUri(uri);
156 }
157
GetFileTypes(const Uri & uri,const std::string & mimeTypeFilter)158 std::vector<std::string> ExtSpecialController::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter)
159 {
160 auto connection = connection_;
161 if (connection == nullptr) {
162 LOG_ERROR("connection is nullptr");
163 return std::vector<std::string>();
164 }
165 auto proxy = connection->GetDataShareProxy(uri_, token_);
166 if (proxy == nullptr) {
167 LOG_ERROR("proxy is nullptr");
168 return std::vector<std::string>();
169 }
170 return proxy->GetFileTypes(uri, mimeTypeFilter);
171 }
172
ExtSpecialController(std::shared_ptr<DataShareConnection> connection,const Uri & uri,const sptr<IRemoteObject> & token)173 ExtSpecialController::ExtSpecialController(std::shared_ptr<DataShareConnection> connection, const Uri &uri,
174 const sptr<IRemoteObject> &token)
175 : connection_(connection), token_(token), uri_(uri)
176 {
177 }
178 } // namespace DataShare
179 } // namespace OHOS
180