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 #ifndef SHARED_BLOCK_SERIALIZER_INFO_H
17 #define SHARED_BLOCK_SERIALIZER_INFO_H
18 
19 #include <sqlite3sym.h>
20 
21 #include "shared_block.h"
22 
23 namespace OHOS {
24 namespace NativeRdb {
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 class SharedBlockSerializerInfo {
29 public:
30     SharedBlockSerializerInfo(AppDataFwk::SharedBlock *sharedBlock, sqlite3_stmt *stat, int numColumns, int startPos);
31     ~SharedBlockSerializerInfo();
32     int AddRow(int addedRows);
33     int Reset(int startPos);
34     int Finish(int addedRows, int totalRows);
35     int PutLong(int row, int column, sqlite3_int64 value);
36     int PutDouble(int row, int column, double value);
37     int PutBlob(int row, int column, const void *blob, int len);
38     int PutNull(int row, int column);
39     int PutOther(int row, int column);
PutString(int row,int column,const char * text,int sizeIncludingNull)40     int PutString(int row, int column, const char *text, int sizeIncludingNull)
41     {
42         int status = sharedBlock_->PutString(row, column, text, sizeIncludingNull);
43         if (status == AppDataFwk::SharedBlock::SHARED_BLOCK_OK) {
44             return SQLITE_OK;
45         }
46         sharedBlock_->FreeLastRow();
47         return SQLITE_FULL;
48     }
49 
GetTotalRows()50     int GetTotalRows() const
51     {
52         return atotalRows;
53     }
54 
GetAddedRows()55     int GetAddedRows() const
56     {
57         return raddedRows;
58     }
59 
GetStartPos()60     int GetStartPos() const
61     {
62         return astartPos;
63     }
64 private:
65     AppDataFwk::SharedBlock *sharedBlock_;
66     sqlite3_stmt *statement_ = nullptr;
67     int anumColumns;
68     int atotalRows;
69     int astartPos;
70     int raddedRows;
71 };
72 
73 struct Sqlite3SharedBlockMethods {
74     int iVersion;
75     void *pContext;
76     int countAllRows;
77     int startPos;
78     int requiredPos;
79     int (*xAddRow)(void *pCtx, int addedRows);
80     int (*xReset)(void *pCtx, int startPos);
81     int (*xFinish)(void *pCtx, int addedRows, int totalRows);
82     int (*xPutString)(void *pCtx, int addedRows, int column, const char *text, int len);
83     int (*xPutLong)(void *pCtx, int addedRows, int column, sqlite3_int64 value);
84     int (*xPutDouble)(void *pCtx, int addedRows, int column, double value);
85     int (*xPutBlob)(void *pCtx, int addedRows, int column, const void *blob, int len);
86     int (*xPutNull)(void *pCtx, int addedRows, int column);
87     int (*xPutOther)(void *pCtx, int addedRows, int column);
88     // Additional methods may be added in future releases
89 };
90 
91 static const int SQLITE_DBCONFIG_SET_SHAREDBLOCK = 2004;
92 
93 static const int SQLITE_DBCONFIG_USE_SHAREDBLOCK = 2005;
94 #ifdef __cplusplus
95 }
96 #endif
97 } // namespace NativeRdb
98 } // namespace OHOS
99 
100 #endif
101