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