1 /*
2  * Copyright (c) 2023 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_REMOTE_RESULT_SET_H
17 #define NATIVE_RDB_REMOTE_RESULT_SET_H
18 
19 #include <string>
20 #include <vector>
21 #include "rdb_visibility.h"
22 
23 namespace OHOS {
24 namespace NativeRdb {
25 
26 /**
27  * @brief Indicates the column type.
28  *
29  * Value returned by getColumnType(int)
30  */
31 enum class ColumnType {
32     /** Indicates the column type is NULL.*/
33     TYPE_NULL = 0,
34     /** Indicates the column type is INTEGER.*/
35     TYPE_INTEGER,
36     /** Indicates the column type is FLOAT.*/
37     TYPE_FLOAT,
38     /** Indicates the column type is STRING.*/
39     TYPE_STRING,
40     /** Indicates the column type is BLOB.*/
41     TYPE_BLOB,
42     /** Indicates the column type is ASSET.*/
43     TYPE_ASSET,
44     /** Indicates the column type is ASSETS.*/
45     TYPE_ASSETS,
46     /** Indicates the column type is Float32.*/
47     TYPE_FLOAT32_ARRAY,
48     /** Indicates the column type is BigInt.*/
49     TYPE_BIGINT
50 };
51 
52 /**
53  * The RemoteResultSet class of RDB.
54  * Provides methods for accessing a database result set generated by remote query the database.
55  */
56 class API_EXPORT RemoteResultSet {
57 public:
58     /**
59      * @brief The error CMD in the correct case.
60      */
61     enum Code {
62         /** Indicates the current CMD is CMD_GET_ALL_COLUMN_NAMES.*/
63         CMD_GET_ALL_COLUMN_NAMES,
64         /** Indicates the current CMD is CMD_GET_COLUMN_COUNT.*/
65         CMD_GET_COLUMN_COUNT,
66         /** Indicates the current CMD is CMD_GET_COLUMN_TYPE.*/
67         CMD_GET_COLUMN_TYPE,
68         /** Indicates the current CMD is CMD_GET_ROW_COUNT.*/
69         CMD_GET_ROW_COUNT,
70         /** Indicates the current CMD is CMD_GET_ROW_INDEX.*/
71         CMD_GET_ROW_INDEX,
72         /** Indicates the current CMD is CMD_GO_TO.*/
73         CMD_GO_TO,
74         /** Indicates the current CMD is CMD_GO_TO_ROW.*/
75         CMD_GO_TO_ROW,
76         /** Indicates the current CMD is CMD_GO_TO_FIRST_ROW.*/
77         CMD_GO_TO_FIRST_ROW,
78         /** Indicates the current CMD is CMD_GO_TO_LAST_ROW.*/
79         CMD_GO_TO_LAST_ROW,
80         /** Indicates the current CMD is CMD_GO_TO_NEXT_ROW.*/
81         CMD_GO_TO_NEXT_ROW,
82         /** Indicates the current CMD is CMD_GO_TO_PREV_ROW.*/
83         CMD_GO_TO_PREV_ROW,
84         /** Indicates the current CMD is CMD_IS_ENDED_ROW.*/
85         CMD_IS_ENDED_ROW,
86         /** Indicates the current CMD is CMD_IS_STARTED_ROW.*/
87         CMD_IS_STARTED_ROW,
88         /** Indicates the current CMD is CMD_IS_AT_FIRST_ROW.*/
89         CMD_IS_AT_FIRST_ROW,
90         /** Indicates the current CMD is CMD_IS_AT_LAST_ROW.*/
91         CMD_IS_AT_LAST_ROW,
92         /** Indicates the current CMD is CMD_GET.*/
93         CMD_GET,
94         /** Indicates the current CMD is CMD_GET.*/
95         CMD_GET_SIZE,
96         /** Indicates the current CMD is CMD_CLOSE.*/
97         CMD_CLOSE,
98         /** Indicates the current CMD is CMD_MAX.*/
99         CMD_MAX
100     };
101 
102     /**
103      * @brief Destructor.
104      */
~RemoteResultSet()105     virtual ~RemoteResultSet() {}
106 
107     /**
108      * @brief Obtains a string array holding the names of all of the columns in the result set.
109      *
110      * @return Returns the names of the columns contains in this query result.
111      */
112     virtual int GetAllColumnNames(std::vector<std::string> &columnNames) = 0;
113 
114     /**
115      * @brief Obtains the total number of columns.
116      *
117      * @return Returns the number of columns
118      */
119     virtual int GetColumnCount(int &count) = 0;
120 
121     /**
122      * @brief Obtains data type of the given column's value.
123      *
124      * @param columnIndex Indicates the zero-based index of the target column.
125      * @return Returns column value type.
126      */
127     virtual int GetColumnType(int columnIndex, ColumnType &columnType) = 0;
128 
129     /**
130      * @brief Obtains the zero-based index for the given column name.
131      *
132      * @param columnName Indicates the name of the column.
133      * @return Returns the column index for the given column, or -1 if the column does not exist.
134      */
135     virtual int GetColumnIndex(const std::string &columnName, int &columnIndex) = 0;
136 
137     /**
138      * @brief Obtains the column name at the given column index.
139      *
140      * @param columnIndex Indicates the zero-based index.
141      * @return Returns the column name for the given index.
142      */
143     virtual int GetColumnName(int columnIndex, std::string &columnName) = 0;
144 
145     /**
146      * @brief Obtains the numbers of rows in the result set.
147      */
148     virtual int GetRowCount(int &count) = 0;
149 
150     /**
151      * @brief Obtains the current position of the cursor in the result set.
152      *
153      * The value is zero-based. When the result set is first returned the cursor
154      * will be at position -1, which is before the first row.
155      * After the last row is returned another call to next() will leave the cursor past
156      * the last entry, at a position of count().
157      *
158      * @return Returns the current cursor position.
159      */
160     virtual int GetRowIndex(int &position) const = 0;
161 
162     /**
163      * @brief Move the cursor a relative amount from current position. Positive offset move forward,
164      * negative offset move backward.
165      *
166      * @param offset Indicates the offset to be applied from the current position.
167      * @return Returns whether the requested move succeeded.
168      */
169     virtual int GoTo(int offset) = 0;
170 
171     /**
172      * @brief Move the cursor to an absolute position.
173      *
174      * @param position Indicates the zero-based position to move to.
175      * @return Returns whether the requested move succeeded.
176      */
177     virtual int GoToRow(int position) = 0;
178 
179     /**
180      * @brief Move the cursor to the first row.
181      *
182      * @return Returns whether the requested move succeeded.
183      */
184     virtual int GoToFirstRow() = 0;
185 
186     /**
187      * @brief Move the cursor to the last row.
188      *
189      * @return Returns whether the requested move succeeded.
190      */
191     virtual int GoToLastRow() = 0;
192 
193     /**
194      * @brief Move the cursor to the next row.
195      *
196      * @return Returns whether the requested move succeeded.
197      */
198     virtual int GoToNextRow() = 0;
199 
200     /**
201      * @brief Move the cursor to the previous row.
202      *
203      * @return Returns whether the requested move succeeded.
204      */
205     virtual int GoToPreviousRow() = 0;
206 
207     /**
208      * @brief Obtains whether the cursor is pointing to the position after the last row.
209      *
210      * @return Returns whether the cursor is after the last row.
211      */
212     virtual int IsEnded(bool &result) = 0;
213 
214     /**
215      * @brief Obtains whether the cursor is pointing to the position before the first row.
216      *
217      * @return Returns whether the cursor is before the first row.
218      */
219     virtual int IsStarted(bool &result) const = 0;
220 
221     /**
222      * @brief Obtains whether the cursor is pointing to the first row.
223      *
224      * @return Returns whether the cursor is pointing at the first entry.
225      */
226     virtual int IsAtFirstRow(bool &result) const = 0;
227 
228     /**
229      * @brief Obtains whether the cursor is pointing to the last row.
230      *
231      * @return Returns whether the cursor is pointing at the last entry.
232      */
233     virtual int IsAtLastRow(bool &result) = 0;
234 
235     /**
236      * @brief Obtains the value of the requested column as a byte array.
237      *
238      * @param columnIndex Indicates the zero-based index of the target column.
239      * @return Returns the value of the requested column as a byte array.
240      */
241     virtual int GetBlob(int columnIndex, std::vector<uint8_t> &blob) = 0;
242 
243     /**
244      * @brief Obtains the value of the requested column as a String.
245      *
246      * @param columnIndex Indicates the zero-based index of the target column.
247      * @return Returns the value of the requested column as a String.
248      */
249     virtual int GetString(int columnIndex, std::string &value) = 0;
250 
251     /**
252      * @brief Obtains the value of the requested column as a int.
253      *
254      * @param columnIndex Indicates the zero-based index of the target column.
255      * @return Returns the value of the requested column as a int.
256      */
257     virtual int GetInt(int columnIndex, int &value) = 0;
258 
259     /**
260      * @brief Obtains the value of the requested column as a long.
261      *
262      * @param columnIndex Indicates the zero-based index of the target column.
263      * @return Returns the value of the requested column as a long.
264      */
265     virtual int GetLong(int columnIndex, int64_t &value) = 0;
266 
267     /**
268      * @brief Obtains the value of the requested column as a double.
269      *
270      * @param columnIndex Indicates the zero-based index of the target column.
271      * @return Returns the value of the requested column as a double.
272      */
273     virtual int GetDouble(int columnIndex, double &value) = 0;
274 
275     /**
276      * @brief Obtains Whether the value of the requested column is null.
277      *
278      * @param columnIndex Indicates the zero-based index of the target column.
279      * @return Returns whether the column value is null.
280      */
281     virtual int IsColumnNull(int columnIndex, bool &isNull) = 0;
282 
283     /**
284      * @brief Obtains Return true if the result set is closed.
285      *
286      * @return Returns true if the result set is closed.
287      */
288     virtual bool IsClosed() const = 0;
289 
290     /**
291      * @brief Closes the result set, releasing all of its resources and making it completely invalid.
292      */
293     virtual int Close() = 0;
294 };
295 
296 } // namespace NativeRdb
297 } // namespace OHOS
298 #endif
299