1 /*
2 * Copyright (c) 2022 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 #ifdef RELATIONAL_STORE
16 #include "relational_row_data_impl.h"
17
18 namespace DistributedDB {
GetColSize()19 std::size_t RelationalRowDataImpl::GetColSize()
20 {
21 return data_.size();
22 }
23
CalcLength() const24 int RelationalRowDataImpl::CalcLength() const
25 {
26 size_t length = Parcel::GetUInt32Len(); // For save the count.
27 for (const auto &dataValue : data_) {
28 length += Parcel::GetUInt32Len(); // For save the dataValue's type.
29 length += DataTransformer::CalDataValueLength(dataValue);
30 if (length > static_cast<size_t>(INT_MAX)) {
31 return 0;
32 }
33 }
34 return static_cast<int>(Parcel::GetEightByteAlign(length));
35 }
36
Serialize(Parcel & parcel) const37 int RelationalRowDataImpl::Serialize(Parcel &parcel) const
38 {
39 parcel.WriteUInt32(data_.size());
40 for (const auto &dataValue : data_) {
41 if (DataTransformer::SerializeDataValue(dataValue, parcel) != E_OK) {
42 return -E_PARSE_FAIL;
43 }
44 }
45 parcel.EightByteAlign();
46 return E_OK;
47 }
48
DeSerialize(Parcel & parcel)49 int RelationalRowDataImpl::DeSerialize(Parcel &parcel)
50 {
51 uint32_t size = 0;
52 (void)parcel.ReadUInt32(size);
53 if (parcel.IsError() ||
54 size > DBConstant::MAX_REMOTEDATA_SIZE / DataTransformer::CalDataValueLength(DataValue {})) {
55 return -E_PARSE_FAIL;
56 }
57 while (size-- > 0u) {
58 DataValue value;
59 if (DataTransformer::DeserializeDataValue(value, parcel) != E_OK) {
60 return -E_PARSE_FAIL;
61 }
62 data_.emplace_back(std::move(value));
63 }
64 parcel.EightByteAlign();
65 return E_OK;
66 }
67
GetType(int index,StorageType & type) const68 int RelationalRowDataImpl::GetType(int index, StorageType &type) const
69 {
70 if (index < 0 || index >= static_cast<int>(data_.size())) {
71 return -E_NONEXISTENT;
72 }
73 type = data_.at(index).GetType();
74 return E_OK;
75 }
76
Get(int index,int64_t & value) const77 int RelationalRowDataImpl::Get(int index, int64_t &value) const
78 {
79 if (index < 0 || index >= static_cast<int>(data_.size())) {
80 return -E_NONEXISTENT;
81 }
82 if (data_.at(index).GetInt64(value) != E_OK) {
83 return -E_TYPE_MISMATCH;
84 }
85 return E_OK;
86 }
87
Get(int index,double & value) const88 int RelationalRowDataImpl::Get(int index, double &value) const
89 {
90 if (index < 0 || index >= static_cast<int>(data_.size())) {
91 return -E_NONEXISTENT;
92 }
93 if (data_.at(index).GetDouble(value) != E_OK) {
94 return -E_TYPE_MISMATCH;
95 }
96 return E_OK;
97 }
98
Get(int index,std::string & value) const99 int RelationalRowDataImpl::Get(int index, std::string &value) const
100 {
101 if (index < 0 || index >= static_cast<int>(data_.size())) {
102 return -E_NONEXISTENT;
103 }
104 if (data_.at(index).GetText(value) != E_OK) {
105 return -E_TYPE_MISMATCH;
106 }
107 return E_OK;
108 }
109
Get(int index,std::vector<uint8_t> & value) const110 int RelationalRowDataImpl::Get(int index, std::vector<uint8_t> &value) const
111 {
112 if (index < 0 || index >= static_cast<int>(data_.size())) {
113 return -E_NONEXISTENT;
114 }
115 Blob blob;
116 int errCode = data_.at(index).GetBlob(blob);
117 if (errCode != E_OK) {
118 return errCode == -E_NOT_SUPPORT ? -E_TYPE_MISMATCH : errCode;
119 }
120 value = blob.ToVector();
121 return errCode;
122 }
123 } // namespace DistributedDB
124 #endif