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