• 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_DB_DELEGATE_H
17 #define DATASHARESERVICE_DB_DELEGATE_H
18 
19 #include <string>
20 
21 #include "abs_shared_result_set.h"
22 #include "concurrent_map.h"
23 #include "datashare_predicates.h"
24 #include "datashare_result_set.h"
25 #include "datashare_values_bucket.h"
26 #include "executor_pool.h"
27 #include "result_set.h"
28 #include "serializable/serializable.h"
29 
30 namespace OHOS::DataShare {
31 class DBDelegate {
32 public:
33     static std::shared_ptr<DBDelegate> Create(const std::string &dir, int version, bool registerFunction = true,
34         bool isEncrypt = false, const std::string &secretMetaKey = "");
35     virtual int64_t Insert(const std::string &tableName, const DataShareValuesBucket &valuesBucket) = 0;
36     virtual int64_t Update(const std::string &tableName, const DataSharePredicates &predicate,
37         const DataShareValuesBucket &valuesBucket) = 0;
38     virtual int64_t Delete(const std::string &tableName, const DataSharePredicates &predicate) = 0;
39     virtual std::shared_ptr<DataShareResultSet> Query(const std::string &tableName,
40         const DataSharePredicates &predicates, const std::vector<std::string> &columns, int &errCode) = 0;
41     virtual std::string Query(
42         const std::string &sql, const std::vector<std::string> &selectionArgs = std::vector<std::string>()) = 0;
43     virtual std::shared_ptr<NativeRdb::ResultSet> QuerySql(const std::string &sql) = 0;
44 };
45 
46 class Id : public DistributedData::Serializable {
47 public:
48     static constexpr int INVALID_USER = -1;
49     Id(const std::string &id, const int32_t userId);
50     ~Id() = default;
51     bool Marshal(json &node) const override;
52     bool Unmarshal(const json &node) override;
string()53     operator std::string()
54     {
55         return DistributedData::Serializable::Marshall(*this);
56     }
57 
58 private:
59     std::string _id;
60     int32_t userId;
61 };
62 
63 class VersionData : public DistributedData::Serializable {
64 public:
65     explicit VersionData(int version);
66     bool Marshal(json &node) const override;
67     bool Unmarshal(const json &node) override;
SetVersion(int ver)68     virtual void SetVersion(int ver)
69     {
70         version = ver;
71     };
GetVersion()72     virtual int GetVersion() const
73     {
74         return version;
75     };
76 
77 private:
78     int version;
79 };
80 
81 class KvData {
82 public:
83     explicit KvData(const Id &id);
84     const std::string &GetId() const;
85     virtual bool HasVersion() const = 0;
86     virtual int GetVersion() const = 0;
87     virtual std::string GetValue() const = 0;
88 
89 private:
90     std::string id;
91 };
92 
93 class KvDBDelegate {
94 public:
95     static constexpr const char *TEMPLATE_TABLE = "template_";
96     static constexpr const char *DATA_TABLE = "data_";
97     static std::shared_ptr<KvDBDelegate> GetInstance(
98         bool reInit = false, const std::string &dir = "", const std::shared_ptr<ExecutorPool> &executors = nullptr);
99     virtual ~KvDBDelegate() = default;
100     virtual int32_t Upsert(const std::string &collectionName, const KvData &value) = 0;
101     virtual int32_t Delete(const std::string &collectionName, const std::string &filter) = 0;
102     virtual int32_t Get(const std::string &collectionName, const Id &id, std::string &value) = 0;
103     virtual int32_t Get(const std::string &collectionName, const std::string &filter, const std::string &projection,
104         std::string &result) = 0;
105     virtual int32_t GetBatch(const std::string &collectionName, const std::string &filter,
106         const std::string &projection, std::vector<std::string> &result) = 0;
107 };
108 } // namespace OHOS::DataShare
109 #endif // DATASHARESERVICE_DB_DELEGATE_H
110