• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (status == AppDataFwk::SharedBlock::SHARED_BLOCK_NO_MEMORY) {
47             isFull = true;
48         }
49         sharedBlock_->FreeLastRow();
50         return SQLITE_FULL;
51     }
52 
GetTotalRows()53     int GetTotalRows() const
54     {
55         return atotalRows;
56     }
57 
GetAddedRows()58     int GetAddedRows() const
59     {
60         return raddedRows;
61     }
62 
GetStartPos()63     int GetStartPos() const
64     {
65         return astartPos;
66     }
67 
IsFull()68     bool IsFull() const
69     {
70         return isFull;
71     }
72 
73 private:
74     AppDataFwk::SharedBlock *sharedBlock_;
75     sqlite3_stmt *statement_ = nullptr;
76     int anumColumns;
77     int atotalRows;
78     int astartPos;
79     int raddedRows;
80     bool isFull = false;
81 };
82 
83 struct Sqlite3SharedBlockMethods {
84     int iVersion;
85     void *pContext;
86     int countAllRows;
87     int startPos;
88     int requiredPos;
89     int (*xAddRow)(void *pCtx, int addedRows);
90     int (*xReset)(void *pCtx, int startPos);
91     int (*xFinish)(void *pCtx, int addedRows, int totalRows);
92     int (*xPutString)(void *pCtx, int addedRows, int column, const char *text, int len);
93     int (*xPutLong)(void *pCtx, int addedRows, int column, sqlite3_int64 value);
94     int (*xPutDouble)(void *pCtx, int addedRows, int column, double value);
95     int (*xPutBlob)(void *pCtx, int addedRows, int column, const void *blob, int len);
96     int (*xPutNull)(void *pCtx, int addedRows, int column);
97     int (*xPutOther)(void *pCtx, int addedRows, int column);
98     // Additional methods may be added in future releases
99 };
100 
101 static const int SQLITE_DBCONFIG_SET_SHAREDBLOCK = 2004;
102 
103 static const int SQLITE_DBCONFIG_USE_SHAREDBLOCK = 2005;
104 #ifdef __cplusplus
105 }
106 #endif
107 } // namespace NativeRdb
108 } // namespace OHOS
109 
110 #endif
111