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