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