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