1 /*
2 * Copyright (c) 2021 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 #include "data_ability_predicates.h"
17
18 #include <algorithm>
19
20 #include "predicates_utils.h"
21
22 namespace OHOS {
23 namespace NativeRdb {
24 int g_invalidObjectFlag = 0;
25 int g_validObjectFlag = 1;
26 int g_defaultSelectArgNumber = 8;
27 bool DataAbilityPredicates::result = false;
DataAbilityPredicates()28 DataAbilityPredicates::DataAbilityPredicates() : isRawSelection(false), judgeSource(false)
29 {
30 }
31
DataAbilityPredicates(const std::string & rawSelection)32 DataAbilityPredicates::DataAbilityPredicates(const std::string &rawSelection) : isRawSelection(true), judgeSource(false)
33 {
34 AbsPredicates::SetWhereClause(rawSelection);
35 }
36
DataAbilityPredicates(OHOS::Parcel * source)37 DataAbilityPredicates::DataAbilityPredicates(OHOS::Parcel *source) : isRawSelection(false), judgeSource(true)
38 {
39 if (source == nullptr) {
40 this->judgeSource = false;
41 } else {
42 this->isRawSelection = source->ReadBool();
43 std::string whereClause = (source->ReadInt32() != g_invalidObjectFlag) ? source->ReadString() : "";
44 std::vector<std::string> whereArgs;
45 if (source->ReadInt32() != g_invalidObjectFlag) {
46 source->ReadStringVector(&whereArgs);
47 }
48 bool isDistinct = source->ReadBool();
49 std::string index = (source->ReadInt32() != g_invalidObjectFlag) ? source->ReadString() : "";
50 std::string group = (source->ReadInt32() != g_invalidObjectFlag) ? source->ReadString() : "";
51 std::string order = (source->ReadInt32() != g_invalidObjectFlag) ? source->ReadString() : "";
52 int limit = (source->ReadInt32() != g_invalidObjectFlag) ? source->ReadInt32()
53 : AbsPredicates::INIT_LIMIT_VALUE;
54 int offset = (source->ReadInt32() != g_invalidObjectFlag) ? source->ReadInt32()
55 : AbsPredicates::INIT_OFFSET_VALUE;
56 std::vector<ValueObject> bindArgs;
57 std::for_each(
58 whereArgs.begin(), whereArgs.end(), [&bindArgs](const auto &it) { bindArgs.push_back(ValueObject(it)); });
59 PredicatesUtils::SetWhereClauseAndArgs(this, whereClause, bindArgs);
60 PredicatesUtils::SetAttributes(this, isDistinct, index, group, order, limit, offset);
61 }
62 }
63 /**
64 * Obtain value of variable isRawSelection.
65 */
IsRawSelection() const66 bool DataAbilityPredicates::IsRawSelection() const
67 {
68 return isRawSelection;
69 }
70
GetJudgeSource() const71 bool DataAbilityPredicates::GetJudgeSource() const
72 {
73 return judgeSource;
74 }
75
76 /**
77 * Write DataAbilityPredicates object to Parcel.
78 */
Marshalling(OHOS::Parcel & parcel) const79 bool DataAbilityPredicates::Marshalling(OHOS::Parcel &parcel) const
80 {
81 parcel.WriteBool(this->isRawSelection);
82 MarshallingString(GetWhereClause(), parcel);
83 MarshallingStringList(GetWhereArgs(), parcel);
84 parcel.WriteBool(IsDistinct());
85 MarshallingString(GetIndex(), parcel);
86 MarshallingString(GetGroup(), parcel);
87 MarshallingString(GetOrder(), parcel);
88
89 int limit = GetLimit();
90 int offset = GetOffset();
91 if (limit != INIT_LIMIT_VALUE) {
92 parcel.WriteInt32(g_validObjectFlag);
93 parcel.WriteInt32(limit);
94 } else {
95 parcel.WriteInt32(g_invalidObjectFlag);
96 }
97 if (offset != INIT_OFFSET_VALUE) {
98 parcel.WriteInt32(g_validObjectFlag);
99 parcel.WriteInt32(offset);
100 } else {
101 parcel.WriteInt32(g_invalidObjectFlag);
102 }
103
104 return true;
105 }
106 /**
107 * Read from Parcel object.
108 */
Unmarshalling(OHOS::Parcel & parcel)109 DataAbilityPredicates* DataAbilityPredicates::Unmarshalling(OHOS::Parcel &parcel)
110 {
111 result = true;
112 return new DataAbilityPredicates(&parcel);
113 }
114
MarshallingString(std::string value,OHOS::Parcel & parcel) const115 void DataAbilityPredicates::MarshallingString(std::string value, OHOS::Parcel &parcel) const
116 {
117 if (value.length() != 0) {
118 parcel.WriteInt32(g_validObjectFlag);
119 parcel.WriteString(value);
120 } else {
121 parcel.WriteInt32(g_invalidObjectFlag);
122 }
123 }
124
MarshallingStringList(std::vector<std::string> list,OHOS::Parcel & parcel) const125 void DataAbilityPredicates::MarshallingStringList(std::vector<std::string> list, OHOS::Parcel &parcel) const
126 {
127 if (list.size() != 0) {
128 parcel.WriteInt32(g_validObjectFlag);
129 parcel.WriteStringVector(list);
130 } else {
131 parcel.WriteInt32(g_invalidObjectFlag);
132 }
133 }
134
~DataAbilityPredicates()135 DataAbilityPredicates::~DataAbilityPredicates() {}
136 } // namespace NativeRdb
137 } // namespace OHOS