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