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