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 "shared_block_serializer_info.h"
17 #include "logger.h"
18
19 namespace OHOS {
20 namespace NativeRdb {
SharedBlockSerializerInfo(AppDataFwk::SharedBlock * sharedBlock,int numColumns,int startPos)21 SharedBlockSerializerInfo::SharedBlockSerializerInfo(AppDataFwk::SharedBlock *sharedBlock, int numColumns, int startPos)
22 :sharedBlock_(sharedBlock), anumColumns(numColumns), atotalRows(0), astartPos(startPos), raddedRows(0),
23 risFull(false)
24 {
25 }
26
~SharedBlockSerializerInfo()27 SharedBlockSerializerInfo::~SharedBlockSerializerInfo() {}
28
AddRow(int addedRows)29 int SharedBlockSerializerInfo::AddRow(int addedRows)
30 {
31 // Allocate a new field directory for the row.
32 int status = sharedBlock_->AllocRow();
33 if (status != AppDataFwk::SharedBlock::SHARED_BLOCK_OK) {
34 risFull = true;
35 return SQLITE_FULL;
36 }
37
38 raddedRows = addedRows + 1;
39 return SQLITE_OK;
40 }
41
Reset(int startPos)42 int SharedBlockSerializerInfo::Reset(int startPos)
43 {
44 int status = sharedBlock_->Clear();
45 if (status != AppDataFwk::SharedBlock::SHARED_BLOCK_OK) {
46 LOG_ERROR("reset : Failed in clearing, error=%{public}d", status);
47 return SQLITE_ERROR;
48 }
49 status = sharedBlock_->SetColumnNum(anumColumns);
50 if (status != AppDataFwk::SharedBlock::SHARED_BLOCK_OK) {
51 return SQLITE_ERROR;
52 }
53 astartPos = startPos;
54 raddedRows = 0;
55 risFull = false;
56 return SQLITE_OK;
57 }
58
Finish(int addedRows,int totalRows)59 int SharedBlockSerializerInfo::Finish(int addedRows, int totalRows)
60 {
61 raddedRows = addedRows;
62 atotalRows = totalRows;
63 return SQLITE_OK;
64 }
65
PutString(int row,int column,const char * text,int sizeIncludingNull)66 int SharedBlockSerializerInfo::PutString(int row, int column, const char *text, int sizeIncludingNull)
67 {
68 int status = sharedBlock_->PutString(row, column, text, sizeIncludingNull);
69 if (status != AppDataFwk::SharedBlock::SHARED_BLOCK_OK) {
70 sharedBlock_->FreeLastRow();
71 risFull = true;
72 return SQLITE_FULL;
73 }
74
75 return SQLITE_OK;
76 }
77
PutLong(int row,int column,sqlite3_int64 value)78 int SharedBlockSerializerInfo::PutLong(int row, int column, sqlite3_int64 value)
79 {
80 int status = sharedBlock_->PutLong(row, column, value);
81 if (status != AppDataFwk::SharedBlock::SHARED_BLOCK_OK) {
82 sharedBlock_->FreeLastRow();
83 risFull = true;
84 return SQLITE_FULL;
85 }
86
87 return SQLITE_OK;
88 }
89
PutDouble(int row,int column,double value)90 int SharedBlockSerializerInfo::PutDouble(int row, int column, double value)
91 {
92 int status = sharedBlock_->PutDouble(row, column, value);
93 if (status != AppDataFwk::SharedBlock::SHARED_BLOCK_OK) {
94 sharedBlock_->FreeLastRow();
95 risFull = true;
96 return SQLITE_FULL;
97 }
98
99 return SQLITE_OK;
100 }
101
PutBlob(int row,int column,const void * blob,int len)102 int SharedBlockSerializerInfo::PutBlob(int row, int column, const void *blob, int len)
103 {
104 int status = sharedBlock_->PutBlob(row, column, blob, len);
105 if (status != AppDataFwk::SharedBlock::SHARED_BLOCK_OK) {
106 sharedBlock_->FreeLastRow();
107 risFull = true;
108 return SQLITE_FULL;
109 }
110
111 return SQLITE_OK;
112 }
113
PutNull(int row,int column)114 int SharedBlockSerializerInfo::PutNull(int row, int column)
115 {
116 int status = sharedBlock_->PutNull(row, column);
117 if (status != AppDataFwk::SharedBlock::SHARED_BLOCK_OK) {
118 sharedBlock_->FreeLastRow();
119 risFull = true;
120 LOG_ERROR("Failed allocating space for a null in column %{public}d, error=%{public}d", column, status);
121 return SQLITE_FULL;
122 }
123
124 return SQLITE_OK;
125 }
126
PutOther(int row,int column)127 int SharedBlockSerializerInfo::PutOther(int row, int column)
128 {
129 sharedBlock_->FreeLastRow();
130 return SQLITE_ERROR;
131 }
132
GetTotalRows() const133 int SharedBlockSerializerInfo::GetTotalRows() const
134 {
135 return atotalRows;
136 }
137
GetAddedRows() const138 int SharedBlockSerializerInfo::GetAddedRows() const
139 {
140 return raddedRows;
141 }
142
GetStartPos() const143 int SharedBlockSerializerInfo::GetStartPos() const
144 {
145 return astartPos;
146 }
147 } // namespace NativeRdb
148 } // namespace OHOS