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 MakeRemoteQuery(const std::string &devices, const std::string &sql, DistributedData::Values &&args);
45     void MakeCloudQuery(const PredicatesMemo &predicates);
46 
47 private:
48     void EqualTo(const RdbPredicateOperation& operation);
49     void NotEqualTo(const RdbPredicateOperation& operation);
50     void And(const RdbPredicateOperation& operation);
51     void Or(const RdbPredicateOperation& operation);
52     void OrderBy(const RdbPredicateOperation& operation);
53     void Limit(const RdbPredicateOperation& operation);
54     void In(const RdbPredicateOperation& operation);
55     void NotIn(const RdbPredicateOperation& operation);
56     void Contain(const RdbPredicateOperation& operation);
57     void BeginWith(const RdbPredicateOperation& operation);
58     void EndWith(const RdbPredicateOperation& operation);
59     void IsNull(const RdbPredicateOperation& operation);
60     void IsNotNull(const RdbPredicateOperation& operation);
61     void Like(const RdbPredicateOperation& operation);
62     void Glob(const RdbPredicateOperation& operation);
63     void Between(const RdbPredicateOperation& operation);
64     void NotBetween(const RdbPredicateOperation& operation);
65     void GreaterThan(const RdbPredicateOperation& operation);
66     void GreaterThanOrEqual(const RdbPredicateOperation& operation);
67     void LessThan(const RdbPredicateOperation& operation);
68     void LessThanOrEqual(const RdbPredicateOperation& operation);
69     void Distinct(const RdbPredicateOperation& operation);
70     void IndexedBy(const RdbPredicateOperation& operation);
71     void BeginGroup(const RdbPredicateOperation& operation);
72     void EndGroup(const RdbPredicateOperation& operation);
73     using PredicateHandle = void (RdbQuery::*)(const RdbPredicateOperation &operation);
74     static constexpr inline PredicateHandle HANDLES[OPERATOR_MAX] = {
75         &RdbQuery::EqualTo,
76         &RdbQuery::NotEqualTo,
77         &RdbQuery::And,
78         &RdbQuery::Or,
79         &RdbQuery::OrderBy,
80         &RdbQuery::Limit,
81         &RdbQuery::BeginGroup,
82         &RdbQuery::EndGroup,
83         &RdbQuery::In,
84         &RdbQuery::NotIn,
85         &RdbQuery::Contain,
86         &RdbQuery::BeginWith,
87         &RdbQuery::EndWith,
88         &RdbQuery::IsNull,
89         &RdbQuery::IsNotNull,
90         &RdbQuery::Like,
91         &RdbQuery::Glob,
92         &RdbQuery::Between,
93         &RdbQuery::NotBetween,
94         &RdbQuery::GreaterThan,
95         &RdbQuery::GreaterThanOrEqual,
96         &RdbQuery::LessThan,
97         &RdbQuery::LessThanOrEqual,
98         &RdbQuery::Distinct,
99         &RdbQuery::IndexedBy
100     };
101     static constexpr inline uint32_t DECIMAL_BASE = 10;
102 
103     DistributedDB::Query query_;
104     std::shared_ptr<Predicates> predicates_;
105     std::vector<std::string> columns_;
106     bool isRemote_ = false;
107     bool isPriority_ = false;
108     std::string sql_;
109     DistributedData::Values args_;
110     std::vector<std::string> devices_;
111     std::vector<std::string> tables_;
112     DistributedData::QueryNodes queryNodes_;
113 };
114 } // namespace OHOS::DistributedRdb
115 #endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_QUERY_H
116