• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef DATASHARE_BLOCK_WRITER_IMPL_H
17 #define DATASHARE_BLOCK_WRITER_IMPL_H
18 
19 #include "shared_block.h"
20 #include "result_set_bridge.h"
21 #include "datashare_errno.h"
22 
23 namespace OHOS {
24 namespace DataShare {
25 /**
26  * This class stores a set of rows from a database in a buffer,
27  * which is used as the set of query result.
28  */
29 class DataShareBlockWriterImpl : public virtual ResultSetBridge::Writer {
30 public:
31     /**
32      * SharedBlock constructor.
33      */
34     DataShareBlockWriterImpl();
35 
36     /**
37      * SharedBlock constructor.
38      */
39     DataShareBlockWriterImpl(const std::string &name, size_t size);
40 
41     /**
42      * SharedBlock Deconstruction.
43      */
44     virtual ~DataShareBlockWriterImpl();
45 
46     /**
47      * Allocate a row unit and its directory.
48      */
49     int AllocRow() override;
50 
51     /**
52      * Write Null data to the shared block.
53      */
54     virtual int Write(uint32_t column) override;
55 
56     /**
57      * Write long data to the shared block.
58      */
59     virtual int Write(uint32_t column, int64_t value) override;
60 
61     /**
62      * Write Double data to the shared block.
63      */
64     virtual int Write(uint32_t column, double value) override;
65 
66     /**
67      * Write blob data to the shared block.
68      */
69     virtual int Write(uint32_t column, const uint8_t *value, size_t Size) override;
70 
71     /**
72      * Write string data to the shared block.
73      */
74     virtual int Write(uint32_t column, const char *value, size_t sizeIncludingNull) override;
75 
76     /**
77      * Get Block
78      */
79     std::shared_ptr<AppDataFwk::SharedBlock> GetBlock();
80 
81 private:
82     /**
83      * The fd of shared memory
84      */
85     bool GetCurrentRowIndex(uint32_t &rowIndex);
86 
87     /**
88      * Convert ShareBlock error code to DataShare format
89      */
ConvertErrorCode(int shareBlockErr)90     int ConvertErrorCode(int shareBlockErr)
91     {
92         return shareBlockErr == AppDataFwk::SharedBlock::SHARED_BLOCK_OK ? E_OK : E_ERROR;
93     }
94 
95 private:
96     std::shared_ptr<AppDataFwk::SharedBlock> shareBlock_;
97 };
98 } // namespace DataShare
99 } // namespace OHOS
100 #endif
101