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