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 #ifndef CALENDAR_DATA_SHARE_HELPER_MANAGER_H 17 #define CALENDAR_DATA_SHARE_HELPER_MANAGER_H 18 19 #include <memory> 20 #include "singleton.h" 21 #include "datashare_helper.h" 22 23 namespace OHOS::CalendarApi { 24 class DataShareHelperManager : public OHOS::Singleton<DataShareHelperManager> { 25 public: 26 void SetDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> helper); 27 /** 28 * @brief Get the dataShareHelper instance. 29 * 30 * @return Returns DataShareHelper instance or nullptr when failed. 31 */ 32 std::shared_ptr<DataShare::DataShareHelper> GetDataShareHelper(); 33 /** 34 * @brief Inserts a single data record into the database. 35 * 36 * @param uri Indicates the path of the data to operate. 37 * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted. 38 * 39 * @return Returns the index of the inserted data record. 40 */ 41 int Insert(const Uri &uri, const DataShare::DataShareValuesBucket &value); 42 43 /** 44 * @brief batch insert data records into the database. 45 * 46 * @param uri Indicates the path of the data to operate. 47 * @param values Indicates the data records to insert. If this parameter is null, a blank row will be inserted. 48 * 49 * @return Returns the index of the inserted data count. 50 */ 51 int BatchInsert(const Uri &uri, const std::vector<DataShare::DataShareValuesBucket> &values); 52 53 /** 54 * @brief Updates data records in the database. 55 * 56 * @param uri Indicates the path of data to update. 57 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 58 * @param value Indicates the data to update. This parameter can be null. 59 * 60 * @return Returns the number of data records updated. 61 */ 62 int Update(const Uri &uri, const DataShare::DataSharePredicates &predicates, 63 const DataShare::DataShareValuesBucket &value); 64 65 /** 66 * @brief Deletes one or more data records from the database. 67 * 68 * @param uri Indicates the path of the data to operate. 69 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 70 * 71 * @return Returns the number of data records deleted. 72 */ 73 int Delete(const Uri &uri, const DataShare::DataSharePredicates &predicates); 74 75 /** 76 * @brief Query records from the database. 77 * 78 * @param uri Indicates the path of data to query. 79 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 80 * @param columns Indicates the columns to query. If this parameter is null, all columns are queried. 81 * @param businessError Indicates the error by query. 82 * 83 * @return Returns the query result. 84 */ 85 std::shared_ptr<DataShare::DataShareResultSet> Query(const Uri &uri, 86 const DataShare::DataSharePredicates &predicates, std::vector<std::string> &columns, 87 DataShare::DatashareBusinessError *businessError = nullptr); 88 private: 89 /** 90 * @brief Create a dataShareHelper instance. 91 * 92 * @return Returns DataShareHelper instance or nullptr when failed. 93 */ 94 std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(); 95 /** 96 * @brief Destroy the dataShareHelper instance when there is no dataShare operations for a period of time, 97 * otherwise no destruction is performed. 98 * 99 * @return Returns a boolean whether the dataShare helper is destroyed. 100 */ 101 bool DestroyDataShareHelper(); 102 /** 103 * @brief set the timer when DataShareHelper should be destoried when there is no operations 104 * during a period of time, default to 3000 milliseconds 105 * 106 * @param milliseconds set the timer in milliseconds to destory dataShareHeloer when there is no 107 * operation performed, default to 3000 millis. 108 * 109 * @return void. 110 */ 111 void SetDataShareHelperTimer(int milliseconds = 3000); 112 std::shared_ptr<DataShare::DataShareHelper> m_dataShareHelper; 113 std::atomic<long long> expire = 0; 114 std::atomic<uint32_t> useCount = 0; 115 std::recursive_mutex dataShareLock; 116 }; 117 } // namespace OHOS::CalendarApi 118 119 #endif // CALENDAR_DATA_SHARE_HELPER_MANAGER_H