• 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 #define LOG_TAG "RdbQuery"
16 #include "rdb_query.h"
17 #include "log_print.h"
18 #include "utils/anonymous.h"
19 #include "value_proxy.h"
20 namespace OHOS::DistributedRdb {
21 using namespace DistributedData;
22 
RdbQuery(bool isRemote)23 RdbQuery::RdbQuery(bool isRemote) : isRemote_(isRemote) {}
24 
IsEqual(uint64_t tid)25 bool RdbQuery::IsEqual(uint64_t tid)
26 {
27     return tid == TYPE_ID;
28 }
29 
GetTables()30 std::vector<std::string> RdbQuery::GetTables()
31 {
32     return tables_;
33 }
34 
SetDevices(const std::vector<std::string> & devices)35 void RdbQuery::SetDevices(const std::vector<std::string> &devices)
36 {
37     devices_ = devices;
38 }
39 
SetSql(const std::string & sql,DistributedData::Values && args)40 void RdbQuery::SetSql(const std::string &sql, DistributedData::Values &&args)
41 {
42     sql_ = sql;
43     args_ = std::move(args);
44 }
45 
GetQuery() const46 DistributedDB::Query RdbQuery::GetQuery() const
47 {
48     return query_;
49 }
50 
GetDevices() const51 std::vector<std::string> RdbQuery::GetDevices() const
52 {
53     return devices_;
54 }
55 
FromTable(const std::vector<std::string> & tables)56 void RdbQuery::FromTable(const std::vector<std::string> &tables)
57 {
58     ZLOGD("table count=%{public}zu", tables.size());
59     query_.FromTable(tables);
60 }
61 
MakeQuery(const PredicatesMemo & predicates)62 void RdbQuery::MakeQuery(const PredicatesMemo &predicates)
63 {
64     ZLOGD("table=%{public}zu", predicates.tables_.size());
65     query_ = predicates.tables_.size() == 1 ? DistributedDB::Query::Select(*predicates.tables_.begin())
66                                             : DistributedDB::Query::Select();
67     if (predicates.tables_.size() > 1) {
68         query_.FromTable(predicates.tables_);
69     }
70     for (const auto &operation : predicates.operations_) {
71         if (operation.operator_ >= 0 && operation.operator_ < OPERATOR_MAX) {
72             (this->*HANDLES[operation.operator_])(operation);
73         }
74     }
75     devices_ = predicates.devices_;
76     tables_ = predicates.tables_;
77 }
78 
IsRemoteQuery()79 bool RdbQuery::IsRemoteQuery()
80 {
81     return isRemote_;
82 }
83 
GetRemoteCondition() const84 DistributedDB::RemoteCondition RdbQuery::GetRemoteCondition() const
85 {
86     auto args = args_;
87     std::vector<std::string> bindArgs = ValueProxy::Convert(std::move(args));
88     return { sql_, bindArgs };
89 }
90 
EqualTo(const RdbPredicateOperation & operation)91 void RdbQuery::EqualTo(const RdbPredicateOperation &operation)
92 {
93     query_.EqualTo(operation.field_, operation.values_[0]);
94 }
95 
NotEqualTo(const RdbPredicateOperation & operation)96 void RdbQuery::NotEqualTo(const RdbPredicateOperation &operation)
97 {
98     query_.NotEqualTo(operation.field_, operation.values_[0]);
99 }
100 
And(const RdbPredicateOperation & operation)101 void RdbQuery::And(const RdbPredicateOperation &operation)
102 {
103     query_.And();
104 }
105 
Or(const RdbPredicateOperation & operation)106 void RdbQuery::Or(const RdbPredicateOperation &operation)
107 {
108     query_.Or();
109 }
110 
OrderBy(const RdbPredicateOperation & operation)111 void RdbQuery::OrderBy(const RdbPredicateOperation &operation)
112 {
113     bool isAsc = operation.values_[0] == "true";
114     query_.OrderBy(operation.field_, isAsc);
115 }
116 
Limit(const RdbPredicateOperation & operation)117 void RdbQuery::Limit(const RdbPredicateOperation &operation)
118 {
119     char *end = nullptr;
120     int limit = static_cast<int>(strtol(operation.field_.c_str(), &end, DECIMAL_BASE));
121     int offset = static_cast<int>(strtol(operation.values_[0].c_str(), &end, DECIMAL_BASE));
122     if (limit < 0) {
123         limit = 0;
124     }
125     if (offset < 0) {
126         offset = 0;
127     }
128     query_.Limit(limit, offset);
129 }
130 } // namespace OHOS::DistributedRdb
131