• 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 OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_QUERY_H
17 #define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_QUERY_H
18 #include "query.h"
19 #include "rdb_predicates.h"
20 #include "store/general_value.h"
21 #include "store_types.h"
22 namespace OHOS::DistributedRdb {
23 class RdbQuery : public DistributedData::GenQuery {
24 public:
25     using Predicates = NativeRdb::RdbPredicates;
26     static constexpr uint64_t TYPE_ID = 0x20000001;
27     RdbQuery() = default;
28     ~RdbQuery() override = default;
29     bool IsEqual(uint64_t tid) override;
30     std::vector<std::string> GetTables() override;
31     void SetQueryNodes(const std::string& tableName, DistributedData::QueryNodes&& nodes) override;
32     DistributedData::QueryNodes GetQueryNodes(const std::string& tableName) override;
33     std::vector<std::string> GetDevices() const;
34     std::string GetStatement() const;
35     DistributedData::Values GetBindArgs() const;
36     void SetColumns(std::vector<std::string> &&columns);
37     void SetColumns(const std::vector<std::string> &columns);
38     std::vector<std::string> GetColumns() const;
39     DistributedDB::Query GetQuery() const;
40     DistributedDB::RemoteCondition GetRemoteCondition() const;
41     bool IsRemoteQuery();
42     bool IsPriority();
43     void MakeQuery(const PredicatesMemo &predicates);
44     void MakeQuery(const std::string &table);
45     void MakeRemoteQuery(const std::string &devices, const std::string &sql, DistributedData::Values &&args);
46     void MakeCloudQuery(const PredicatesMemo &predicates);
47 
48 private:
49     void EqualTo(const RdbPredicateOperation& operation);
50     void NotEqualTo(const RdbPredicateOperation& operation);
51     void And(const RdbPredicateOperation& operation);
52     void Or(const RdbPredicateOperation& operation);
53     void OrderBy(const RdbPredicateOperation& operation);
54     void Limit(const RdbPredicateOperation& operation);
55     void In(const RdbPredicateOperation& operation);
56     void NotIn(const RdbPredicateOperation& operation);
57     void Contain(const RdbPredicateOperation& operation);
58     void BeginWith(const RdbPredicateOperation& operation);
59     void EndWith(const RdbPredicateOperation& operation);
60     void IsNull(const RdbPredicateOperation& operation);
61     void IsNotNull(const RdbPredicateOperation& operation);
62     void Like(const RdbPredicateOperation& operation);
63     void Glob(const RdbPredicateOperation& operation);
64     void Between(const RdbPredicateOperation& operation);
65     void NotBetween(const RdbPredicateOperation& operation);
66     void GreaterThan(const RdbPredicateOperation& operation);
67     void GreaterThanOrEqual(const RdbPredicateOperation& operation);
68     void LessThan(const RdbPredicateOperation& operation);
69     void LessThanOrEqual(const RdbPredicateOperation& operation);
70     void Distinct(const RdbPredicateOperation& operation);
71     void IndexedBy(const RdbPredicateOperation& operation);
72     void BeginGroup(const RdbPredicateOperation& operation);
73     void EndGroup(const RdbPredicateOperation& operation);
74     void NotContains(const RdbPredicateOperation& operation);
75     void NotLike(const RdbPredicateOperation& operation);
76     void AssetsOnly(const RdbPredicateOperation& operation);
77     using PredicateHandle = void (RdbQuery::*)(const RdbPredicateOperation &operation);
78     static constexpr inline PredicateHandle HANDLES[OPERATOR_MAX] = {
79         &RdbQuery::EqualTo,
80         &RdbQuery::NotEqualTo,
81         &RdbQuery::And,
82         &RdbQuery::Or,
83         &RdbQuery::OrderBy,
84         &RdbQuery::Limit,
85         &RdbQuery::BeginGroup,
86         &RdbQuery::EndGroup,
87         &RdbQuery::In,
88         &RdbQuery::NotIn,
89         &RdbQuery::Contain,
90         &RdbQuery::BeginWith,
91         &RdbQuery::EndWith,
92         &RdbQuery::IsNull,
93         &RdbQuery::IsNotNull,
94         &RdbQuery::Like,
95         &RdbQuery::Glob,
96         &RdbQuery::Between,
97         &RdbQuery::NotBetween,
98         &RdbQuery::GreaterThan,
99         &RdbQuery::GreaterThanOrEqual,
100         &RdbQuery::LessThan,
101         &RdbQuery::LessThanOrEqual,
102         &RdbQuery::Distinct,
103         &RdbQuery::NotContains,
104         &RdbQuery::NotLike,
105         &RdbQuery::AssetsOnly,
106         &RdbQuery::IndexedBy
107     };
108     static constexpr inline uint32_t DECIMAL_BASE = 10;
109 
110     DistributedDB::Query query_;
111     std::shared_ptr<Predicates> predicates_;
112     std::vector<std::string> columns_;
113     bool isRemote_ = false;
114     bool isPriority_ = false;
115     std::string sql_;
116     DistributedData::Values args_;
117     std::vector<std::string> devices_;
118     std::vector<std::string> tables_;
119     DistributedData::QueryNodes queryNodes_;
120 };
121 } // namespace OHOS::DistributedRdb
122 #endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_QUERY_H