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 DATASHARESERVICE_RDB_DELEGATE_H 17 #define DATASHARESERVICE_RDB_DELEGATE_H 18 19 #include <mutex> 20 #include <string> 21 22 #include "concurrent_map.h" 23 #include "db_delegate.h" 24 #include "rdb_errno.h" 25 #include "rdb_helper.h" 26 #include "rdb_store.h" 27 #include "uri_utils.h" 28 #include "rdb_utils.h" 29 30 namespace OHOS::DataShare { 31 using namespace OHOS::NativeRdb; 32 class RdbDelegate final : public DBDelegate { 33 public: 34 explicit RdbDelegate(const DistributedData::StoreMetaData &meta, int version, 35 bool registerFunction, const std::string &extUri, const std::string &backup); 36 int64_t Insert(const std::string &tableName, const DataShareValuesBucket &valuesBucket) override; 37 int64_t Update(const std::string &tableName, const DataSharePredicates &predicate, 38 const DataShareValuesBucket &valuesBucket) override; 39 int64_t Delete(const std::string &tableName, const DataSharePredicates &predicate) override; 40 std::pair<int, std::shared_ptr<DataShareResultSet>> Query(const std::string &tableName, 41 const DataSharePredicates &predicates, const std::vector<std::string> &columns, 42 int32_t callingPid, uint32_t callingTokenId) override; 43 std::string Query(const std::string &sql, const std::vector<std::string> &selectionArgs) override; 44 std::shared_ptr<NativeRdb::ResultSet> QuerySql(const std::string &sql) override; 45 bool IsInvalid() override; 46 std::pair<int64_t, int64_t> InsertEx(const std::string &tableName, 47 const DataShareValuesBucket &valuesBucket) override; 48 std::pair<int64_t, int64_t> UpdateEx(const std::string &tableName, 49 const DataSharePredicates &predicate, const DataShareValuesBucket &valuesBucket) override; 50 std::pair<int64_t, int64_t> DeleteEx(const std::string &tableName, 51 const DataSharePredicates &predicate) override; 52 53 private: 54 void TryAndSend(int errCode); 55 std::pair<int, RdbStoreConfig> GetConfig(const DistributedData::StoreMetaData &meta, bool registerFunction); 56 bool IsLimit(int count, int32_t callingPid, uint32_t callingTokenId); 57 static std::atomic<int32_t> resultSetCount; 58 static ConcurrentMap<uint32_t, int32_t> resultSetCallingPids; 59 static constexpr std::chrono::milliseconds WAIT_TIME = std::chrono::milliseconds(50); 60 std::shared_ptr<RdbStore> store_; 61 int errCode_ = E_OK; 62 static constexpr int RETRY = 3; 63 static constexpr const char *DUAL_WRITE = "dualWrite"; 64 static constexpr const char *PERIODIC = "periodic"; 65 uint32_t tokenId_; 66 std::string bundleName_; 67 std::string storeName_; 68 int32_t haMode_; 69 std::string extUri_; 70 std::string backup_; 71 }; 72 class DefaultOpenCallback : public RdbOpenCallback { 73 public: OnCreate(RdbStore & rdbStore)74 int OnCreate(RdbStore &rdbStore) override 75 { 76 return E_OK; 77 } OnUpgrade(RdbStore & rdbStore,int oldVersion,int newVersion)78 int OnUpgrade(RdbStore &rdbStore, int oldVersion, int newVersion) override 79 { 80 return E_OK; 81 } 82 }; 83 } // namespace OHOS::DataShare 84 #endif // DATASHARESERVICE_RDB_DELEGATE_H 85