1 /* 2 * Copyright (c) 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 16 #ifndef DATASHARESERVICE_RDB_ADAPTOR_H 17 #define DATASHARESERVICE_RDB_ADAPTOR_H 18 19 #include <mutex> 20 #include <string> 21 22 #include "concurrent_map.h" 23 #include "datashare_predicates.h" 24 #include "datashare_result_set.h" 25 #include "datashare_values_bucket.h" 26 #include "metadata/store_meta_data.h" 27 #include "rdb_errno.h" 28 #include "rdb_helper.h" 29 #include "rdb_store.h" 30 #include "timer.h" 31 #include "uri_utils.h" 32 33 namespace OHOS::DataShare { 34 using StoreMetaData = OHOS::DistributedData::StoreMetaData; 35 using namespace OHOS::NativeRdb; 36 class RdbDelegate { 37 public: 38 explicit RdbDelegate(const StoreMetaData &data); 39 virtual ~RdbDelegate(); 40 int64_t Insert(const std::string &tableName, const DataShareValuesBucket &valuesBucket); 41 int64_t Update( 42 const std::string &tableName, const DataSharePredicates &predicate, const DataShareValuesBucket &valuesBucket); 43 int64_t Delete(const std::string &tableName, const DataSharePredicates &predicate); 44 std::shared_ptr<DataShareResultSet> Query( 45 const std::string &tableName, const DataSharePredicates &predicates, const std::vector<std::string> &columns); 46 47 private: 48 std::shared_ptr<RdbStore> store_; 49 }; 50 class RdbAdaptor { 51 public: 52 static int32_t Insert(const UriInfo &uriInfo, const DataShareValuesBucket &valuesBucket, const int32_t userId); 53 static int32_t Update(const UriInfo &uriInfo, const DataSharePredicates &predicate, 54 const DataShareValuesBucket &valuesBucket, const int32_t userId); 55 static int32_t Delete(const UriInfo &uriInfo, const DataSharePredicates &predicate, const int32_t userId); 56 static std::shared_ptr<DataShareResultSet> Query(const UriInfo &uriInfo, const DataSharePredicates &predicates, 57 const std::vector<std::string> &columns, const int32_t userId); 58 59 private: 60 static constexpr int OPEN_TIME = 30000; // 30s 61 static std::shared_ptr<RdbDelegate> GetDelegate(const UriInfo &uriInfo, const int32_t userId); 62 static void AutoClose(); 63 static ConcurrentMap<UriInfo, std::shared_ptr<RdbDelegate>> delegates_; 64 static std::unique_ptr<Utils::Timer> timer_; 65 static uint32_t timerId_; 66 }; 67 class DefaultOpenCallback : public RdbOpenCallback { 68 public: OnCreate(RdbStore & rdbStore)69 int OnCreate(RdbStore &rdbStore) override 70 { 71 return E_OK; 72 } OnUpgrade(RdbStore & rdbStore,int oldVersion,int newVersion)73 int OnUpgrade(RdbStore &rdbStore, int oldVersion, int newVersion) override 74 { 75 return E_OK; 76 } 77 }; 78 } // namespace OHOS::DataShare 79 #endif // DATASHARESERVICE_RDB_ADAPTOR_H 80