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_RESULT_SET_H
17 #define DATASHARE_RESULT_SET_H
18 
19 #include <memory>
20 #include <shared_mutex>
21 #include <string>
22 #include <thread>
23 #include <vector>
24 
25 #include "datashare_abs_result_set.h"
26 #include "datashare_errno.h"
27 #include "datashare_shared_result_set.h"
28 #include "message_parcel.h"
29 #include "result_set_bridge.h"
30 
31 namespace OHOS {
32 namespace DataShare {
33 class DataShareBlockWriterImpl;
34 
35 /**
36  * This module provides data resultsets.
37  */
38 class DataShareResultSet : public DataShareAbsResultSet, public DataShareSharedResultSet {
39 public:
40     DataShareResultSet();
41     explicit DataShareResultSet(std::shared_ptr<ResultSetBridge> &bridge);
42     virtual ~DataShareResultSet();
43 
44     /**
45      * @brief Get the data whose value type is blob from the database according to the columnIndex.
46      *
47      * @param columnIndex the zero-based index of the target column.
48      * @param glob Indicates the value of glob type data to put.
49      *
50      * @return Return the value of the requested column as a byte array.
51      */
52     int GetBlob(int columnIndex, std::vector<uint8_t> &blob) override;
53 
54     /**
55      * @brief Get the data whose value type is string from the database according to the columnIndex.
56      *
57      * @param columnIndex the zero-based index of the target column.
58      * @param value Indicates the value of columnIndex data to put or update.
59      *
60      * @return Return the value of the requested column as a String.
61      */
62     int GetString(int columnIndex, std::string &value) override;
63 
64     /**
65      * @brief Get the data whose value type is int from the database according to the columnIndex.
66      *
67      * @param columnIndex the zero-based index of the target column.
68      * @param value Indicates the value of columnIndex data to put or update.
69      *
70      * @return Return the value of the requested column as a int.
71      */
72     int GetInt(int columnIndex, int &value) override;
73 
74     /**
75      * @brief Get the data whose value type is long from the database according to the columnIndex.
76      *
77      * @param columnIndex the zero-based index of the target column.
78      * @param value Indicates the value of columnIndex data to put or update.
79      *
80      * @return Return the value of the requested column as a long.
81      */
82     int GetLong(int columnIndex, int64_t &value) override;
83 
84     /**
85      * @brief Get the data whose value type is double from the database according to the columnIndex.
86      *
87      * @param columnIndex the zero-based index of the target column.
88      * @param value Indicates the value of columnIndex data to put or update.
89      *
90      * @return Return the value of the requested column as a double.
91      */
92     int GetDouble(int columnIndex, double &value) override;
93 
94     /**
95      * @brief Get the data whose value type is isNull from the database according to the columnIndex.
96      *
97      * @param columnIndex the zero-based index of the target column.
98      * @param isNull Indicates the value of glob type data to put.
99      *
100      * @return Return whether the column value is null.
101      */
102     int IsColumnNull(int columnIndex, bool &isNull) override;
103 
104     /**
105      * @brief data type of the given column's value.
106      *
107      * @param columnIndex the zero-based index of the target column.
108      * @param Place the obtained data type.
109      *
110      * @return Return column value type.
111      */
112     int GetDataType(int columnIndex, DataType &dataType) override;
113 
114     /**
115      * @brief Go to row based on position.
116      *
117      * @param position the zero-based position to move to.
118      *
119      * @return Return whether the requested move succeeded.
120      */
121     int GoToRow(int position) override;
122 
123     /**
124      * @brief Returns a string array holding the names of all of the columns in the result set.
125      *
126      * @return Return the names of the columns contains in this query result.
127      */
128     int GetAllColumnNames(std::vector<std::string> &columnNames) override;
129 
130     /**
131      * @return Return the numbers of rows in the result set.
132      */
133     int GetRowCount(int &count) override;
134 
135     /**
136      * Obtains a block from the SharedResultSet.
137      */
138     std::shared_ptr<AppDataFwk::SharedBlock> GetBlock() override;
139 
140     /**
141      * Called when the position of the result set changes.
142      */
143     bool OnGo(int startRowIndex, int targetRowIndex, int *cachedIndex = nullptr) override;
144 
145     /**
146      * Adds the data of a SharedResultSet to a SharedBlock.
147      */
148     void FillBlock(int startRowIndex, AppDataFwk::SharedBlock *block) override;
149 
150     /**
151      * SetBlock.
152      */
153     virtual void SetBlock(AppDataFwk::SharedBlock *block);
154 
155     /**
156      * Closes the result set, releasing all of its resources and making it completely invalid.
157      */
158     int Close() override;
159 
160     /**
161      * Checks whether an DataShareResultSet object contains shared blocks.
162      */
163     bool HasBlock();
164 
165     std::shared_ptr<ResultSetBridge> GetBridge();
166 protected:
167     int CheckState(int columnIndex);
168     void ClosedBlockAndBridge();
169     virtual void Finalize();
170 
171     friend class ISharedResultSetStub;
172     friend class ISharedResultSetProxy;
173     bool Unmarshalling(MessageParcel &parcel);
174     bool Marshalling(MessageParcel &parcel);
175 
176 private:
177     static int blockId_;
178     // The actual position of the first row of data in the shareblock
179     int startRowPos_ = -1;
180     // The actual position of the last row of data in the shareblock
181     int endRowPos_ = -1;
182     // The SharedBlock owned by this DataShareResultSet
183     std::shared_mutex mutex_;
184     std::shared_ptr<AppDataFwk::SharedBlock> sharedBlock_  = nullptr;
185     std::shared_ptr<DataShareBlockWriterImpl> blockWriter_ = nullptr;
186     std::shared_ptr<ResultSetBridge> bridge_ = nullptr;
187 };
188 } // namespace DataShare
189 } // namespace OHOS
190 
191 #endif // DATASHARE_RESULT_SET_H