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     std::pair<int, std::shared_ptr<DataShareResultSet>> Query(const std::string &tableName,
37         const DataSharePredicates &predicates, const std::vector<std::string> &columns,
38         const int32_t callingPid) override;
39     std::string Query(const std::string &sql, const std::vector<std::string> &selectionArgs) override;
40     std::shared_ptr<NativeRdb::ResultSet> QuerySql(const std::string &sql) override;
41     bool IsInvalid() override;
42     std::pair<int64_t, int64_t> InsertEx(const std::string &tableName,
43         const DataShareValuesBucket &valuesBucket) override;
44     std::pair<int64_t, int64_t> UpdateEx(const std::string &tableName,
45         const DataSharePredicates &predicate, const DataShareValuesBucket &valuesBucket) override;
46     std::pair<int64_t, int64_t> DeleteEx(const std::string &tableName,
47         const DataSharePredicates &predicate) override;
48 private:
49     void TryAndSend(int errCode);
50     std::pair<int, RdbStoreConfig> GetConfig(const DistributedData::StoreMetaData &meta, bool registerFunction);
51     bool IsLimit(int count, const int32_t callingPid);
52     static std::atomic<int32_t> resultSetCount;
53     static ConcurrentMap<uint32_t, int32_t> resultSetCallingPids;
54     static constexpr std::chrono::milliseconds WAIT_TIME = std::chrono::milliseconds(50);
55     std::shared_ptr<RdbStore> store_;
56     int errCode_ = E_OK;
57     static constexpr int RETRY = 3;
58     static constexpr const char *DUAL_WRITE = "dualWrite";
59     static constexpr const char *PERIODIC = "periodic";
60     uint32_t tokenId_;
61     std::string bundleName_;
62     std::string storeName_;
63     int32_t haMode_;
64     std::string extUri_;
65     std::string backup_;
66 };
67 class DefaultOpenCallback : public RdbOpenCallback {
68 public:
69     int OnCreate(RdbStore &rdbStore) override
70     {
71         return E_OK;
72     }
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_DELEGATE_H
80