• 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 NATIVE_RDB_CACHE_RESULT_SET_H
17 #define NATIVE_RDB_CACHE_RESULT_SET_H
18 
19 #include <map>
20 #include <memory>
21 #include <shared_mutex>
22 #include <string>
23 #include <vector>
24 
25 #include "result_set.h"
26 #include "value_object.h"
27 #include "values_bucket.h"
28 
29 #define RDB_UTILS_PUSH_WARNING _Pragma("GCC diagnostic push")
30 #define RDB_UTILS_POP_WARNING _Pragma("GCC diagnostic pop")
31 #define RDB_UTILS_DISABLE_WARNING_INTERNAL2(warningName) #warningName
32 #define RDB_UTILS_DISABLE_WARNING(warningName) \
33     _Pragma(                                     \
34         RDB_UTILS_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored warningName))
35 
36 namespace OHOS {
37 namespace NativeRdb {
38 /**
39 * The CacheResultSet class of RDB.
40 * Provides methods for accessing a database result set generated by querying the database.
41 */
42 class API_EXPORT CacheResultSet : public ResultSet {
43 public:
44     /**
45     * @brief Constructor.
46     */
47     API_EXPORT CacheResultSet(std::vector<NativeRdb::ValuesBucket> &&valueBuckets);
48     /**
49     * @brief Destructor.
50     */
51     API_EXPORT virtual ~CacheResultSet();
52 
53     /**
54     * @brief Obtains the number of rows in the result set.
55     */
56     API_EXPORT int GetRowCount(int &count) override;
57 
58     /**
59     * @brief Obtains the names of all columns in a result set.
60     */
61     API_EXPORT int GetAllColumnNames(std::vector<std::string> &columnNames) override;
62 
63     /**
64     * @brief Obtains the value of the specified column in the current row as a byte array.
65     *
66     * The implementation class determines whether to throw an exception if the value of the specified column
67     * in the current row is null or the specified column is not of the Blob type.
68     *
69     * @param columnIndex Indicates the specified column index, which starts from 0.
70     *
71     * @return Returns the value of the specified column as a byte array.
72     */
73     API_EXPORT int GetBlob(int columnIndex, std::vector<uint8_t> &blob) override;
74 
75     /**
76     * @brief Obtains the value of the specified column in the current row as string.
77     *
78     * The implementation class determines whether to throw an exception if the value of the specified column
79     * in the current row is null or the specified column is not of the string type.
80     *
81     * @param columnIndex Indicates the specified column index, which starts from 0.
82     *
83     * @return Returns the value of the specified column as a string.
84     */
85     API_EXPORT int GetString(int columnIndex, std::string &value) override;
86 
87     /**
88     * @brief Obtains the value of the specified column in the current row as int.
89     *
90     * The implementation class determines whether to throw an exception if the value of the specified column
91     * in the current row is null or the specified column is not of the integer type.
92     *
93     * @param columnIndex Indicates the specified column index, which starts from 0.
94     *
95     * @return Returns the value of the specified column as a int.
96     */
97     API_EXPORT int GetInt(int columnIndex, int &value) override;
98 
99     /**
100     * @brief Obtains the value of the specified column in the current row as long.
101     *
102     * The implementation class determines whether to throw an exception if the value of the specified column
103     * in the current row is null or the specified column is not of the long type.
104     *
105     * @param columnIndex Indicates the specified column index, which starts from 0.
106     *
107     * @return Returns the value of the specified column as a long.
108     */
109     API_EXPORT int GetLong(int columnIndex, int64_t &value) override;
110 
111     /**
112     * @brief Obtains the value of the specified column in the current row as double.
113     *
114     * The implementation class determines whether to throw an exception if the value of the specified column
115     * in the current row is null or the specified column is not of the double type.
116     *
117     * @param columnIndex Indicates the specified column index, which starts from 0.
118     *
119     * @return Returns the value of the specified column as a double.
120     */
121     API_EXPORT int GetDouble(int columnIndex, double &value) override;
122 
123     /**
124     * @brief Obtains the value of the specified column in the current row as asset.
125     *
126     * The implementation class determines whether to throw an exception if the value of the specified column
127     * in the current row is null or the specified column is not of the Asset type.
128     *
129     * @param columnIndex Indicates the specified column index, which starts from 0.
130     *
131     * @return Returns the value of the specified column as a double.
132     */
133     API_EXPORT int GetAsset(int32_t col, ValueObject::Asset &value) override;
134 
135     /**
136     * @brief Obtains the value of the specified column in the current row as assets.
137     *
138     * The implementation class determines whether to throw an exception if the value of the specified column
139     * in the current row is null or the specified column is not of the Assets type.
140     *
141     * @param columnIndex Indicates the specified column index, which starts from 0.
142     *
143     * @return Returns the value of the specified column as a double.
144     */
145     API_EXPORT int GetAssets(int32_t col, ValueObject::Assets &value) override;
146 
147     /**
148      * @brief Obtains the value of the specified column in the current row as vector.
149      *
150      * The implementation class determines whether to throw an exception if the value of the specified column
151      * in the current row is null or the specified column is not of the vector type.
152      *
153      * @param columnIndex Indicates the specified column index, which starts from 0.
154      *
155      * @return Returns the value of the specified column as a double.
156      */
157     API_EXPORT int GetFloat32Array(int32_t index, ValueObject::FloatVector &vecs) override;
158 
159     /**
160     * @brief Obtains the value of the specified column in the current row as ValueObject.
161     *
162     * The implementation class determines whether to throw an exception if the value of the specified column
163     * in the current row is null or the specified column is not of the double type.
164     *
165     * @param columnIndex Indicates the specified column index, which starts from 0.
166     *
167     * @return Returns the value of the specified column as a double.
168     */
169     API_EXPORT int Get(int32_t col, ValueObject &value) override;
170 
171     /**
172     * @brief Checks whether the value of the specified column in the current row is null.
173     *
174     * @param columnIndex Indicates the specified column index, which starts from 0.
175     *
176     * @return Returns true if the value of the specified column in the current row is null;
177     * returns false otherwise.
178     */
179     API_EXPORT int IsColumnNull(int columnIndex, bool &isNull) override;
180 
181     /**
182     * @brief Gets the entire row of data for the current row from the result set.
183     */
184     API_EXPORT int GetRow(RowEntity &rowEntity) override;
185 
186     /**
187     * @brief Move the cursor to an absolute position.
188     *
189     * @param position Indicates the specified column index, which starts from 0.
190     *
191     * @return Returns whether the requested move succeeded.
192     */
193     API_EXPORT int GoToRow(int position) override;
194 
195     /**
196     * @brief Obtains data type of the given column's value.
197     *
198     * @param columnIndex Indicates the specified column index, which starts from 0.
199     *
200     * @return Returns column value type.
201     */
202     API_EXPORT int GetColumnType(int columnIndex, ColumnType &columnType) override;
203 
204     /**
205     * @brief Returns the current position of the cursor in the result set.
206     *
207     * The value is zero-based. When the result set is first returned the cursor
208     * will be at position -1, which is before the first row.
209     * After the last row is returned another call to next() will leave the cursor past
210     * the last entry, at a position of count().
211     *
212     * @return Returns the current cursor position.
213     */
214     API_EXPORT int GetRowIndex(int &position) const override;
215 
216     /**
217     * @brief Go to the specified row of the result set forwards or backwards by an offset
218     * relative to its current position.
219     *
220     * A positive offset indicates moving backwards, and a negative offset indicates moving forwards.
221     *
222     * @param offset Indicates the offset relative to the current position.
223     *
224     * @return Returns whether true if the result set is moved successfully and does not go beyond the range;
225     * returns false otherwise.
226     */
227     API_EXPORT int GoTo(int offset) override;
228 
229     /**
230     * @brief Go to the first row of the result set.
231     *
232     * @return Returns if the result set is moved successfully;
233     * returns false otherwise, for example, if the result set is empty.
234     */
235     API_EXPORT int GoToFirstRow() override;
236 
237     /**
238     * @brief Go to the last row of the result set.
239     *
240     * @return Returns if the result set is moved successfully;
241     * returns false otherwise, for example, if the result set is empty.
242     */
243     API_EXPORT int GoToLastRow() override;
244 
245     /**
246     * @brief Go to the next row of the result set.
247     *
248     * @return Returns if the result set is moved successfully;
249     * returns false otherwise, for example, if the result set is already in the last row.
250     */
251     API_EXPORT int GoToNextRow() override;
252 
253     /**
254     * @brief Go to the previous row of the result set.
255     *
256     * @return Returns if the result set is moved successfully;
257     * returns false otherwise, for example, if the result set is already in the first row.
258     */
259     API_EXPORT int GoToPreviousRow() override;
260 
261     /**
262     * @brief Checks whether the result set is positioned at the first row.
263     */
264     API_EXPORT int IsAtFirstRow(bool &result) const override;
265 
266     /**
267     * @brief Checks whether the result set is positioned at the last row.
268     */
269     API_EXPORT int IsAtLastRow(bool &result) override;
270 
271     /**
272     * @brief Returns whether the cursor is pointing to the position before the first row.
273     */
274     API_EXPORT int IsStarted(bool &result) const override;
275 
276     /**
277     * @brief Checks whether the result set is positioned after the last row.
278     */
279     API_EXPORT int IsEnded(bool &result) override;
280 
281     /**
282     * @brief Obtains the number of columns in the result set.
283     */
284     API_EXPORT int GetColumnCount(int &count) override;
285 
286     /**
287     * @brief Returns the zero-based index for the given column name.
288     *
289     * @param columnName Indicates the specified name of the column.
290     *
291     * @return Returns the column index for the given column, or -1 if the column does not exist.
292     */
293     API_EXPORT int GetColumnIndex(const std::string &columnName, int &columnIndex) override;
294 
295     /**
296     * @brief Returns the column name at the given column index.
297     *
298     * @param columnIndex Indicates the specified column index, which starts from 0.
299     *
300     * @return Returns the column name for the given index.
301     */
302     API_EXPORT int GetColumnName(int columnIndex, std::string &columnName) override;
303 
304     /**
305     * @brief Checks whether the current result set is closed.
306     *
307     * @return Returns the true if the result set is closed by calling the close method.
308     */
309     API_EXPORT bool IsClosed() const override;
310 
311     /**
312     * @brief Closes the result set.
313     *
314     * Calling this method on the result set will release all of its resources and makes it ineffective.
315     */
316     API_EXPORT int Close() override;
317     int GetSize(int columnIndex, size_t &size) override;
318 
319 private:
320 RDB_UTILS_PUSH_WARNING
321 RDB_UTILS_DISABLE_WARNING("-Wc99-designator")
322     static constexpr ColumnType COLUMNTYPES[ValueObject::TYPE_MAX] = {
323         [ValueObject::TYPE_NULL] = ColumnType::TYPE_NULL,
324         [ValueObject::TYPE_INT] = ColumnType::TYPE_INTEGER,
325         [ValueObject::TYPE_DOUBLE] = ColumnType::TYPE_FLOAT,
326         [ValueObject::TYPE_STRING] = ColumnType::TYPE_STRING,
327         [ValueObject::TYPE_BOOL] = ColumnType::TYPE_INTEGER,
328         [ValueObject::TYPE_BLOB] = ColumnType::TYPE_BLOB,
329         [ValueObject::TYPE_ASSET] = ColumnType::TYPE_BLOB,
330         [ValueObject::TYPE_ASSETS] = ColumnType::TYPE_BLOB,
331     };
332 RDB_UTILS_POP_WARNING
333     int32_t row_;
334     mutable std::shared_mutex rwMutex_;
335     int32_t maxRow_;
336     int32_t maxCol_;
337     std::vector<std::string> colNames_;
338     std::vector<int32_t> colTypes_;
339     std::vector<NativeRdb::ValuesBucket> valueBuckets_;
340 };
341 } // namespace NativeRdb
342 } // namespace OHOS
343 
344 #endif
345