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_ABS_SHARED_RESULT_SET_H 17 #define NATIVE_RDB_ABS_SHARED_RESULT_SET_H 18 19 #include <memory> 20 #include <string> 21 #include <thread> 22 #include <vector> 23 24 #include "abs_result_set.h" 25 #include "shared_result_set.h" 26 27 namespace OHOS { 28 namespace NativeRdb { 29 /** 30 * The AbsResultSet class of RDB. 31 * Provides methods for accessing a database result set generated by querying the database. 32 */ 33 class API_EXPORT AbsSharedResultSet : public AbsResultSet, public SharedResultSet { 34 public: 35 /** 36 * @brief Constructor. 37 */ 38 API_EXPORT AbsSharedResultSet(); 39 40 /** 41 * @brief Constructor. 42 * 43 * A parameterized constructor used to create an AbsSharedResultSet instance. 44 * 45 * @param tableName Indicates the table name of the database. 46 */ 47 API_EXPORT explicit AbsSharedResultSet(std::string name); 48 49 /** 50 * @brief Destructor. 51 */ 52 API_EXPORT virtual ~AbsSharedResultSet(); 53 54 /** 55 * @brief Obtains the value of the specified column in the current row as a byte array. 56 * 57 * The implementation class determines whether to throw an exception if the value of the specified column 58 * in the current row is null or the specified column is not of the Blob type. 59 * 60 * @param columnIndex Indicates the specified column index, which starts from 0. 61 * 62 * @return Returns the value of the specified column as a byte array. 63 */ 64 API_EXPORT int GetBlob(int columnIndex, std::vector<uint8_t> &blob) override; 65 66 /** 67 * @brief Obtains the value of the specified column in the current row as string. 68 * 69 * The implementation class determines whether to throw an exception if the value of the specified column 70 * in the current row is null or the specified column is not of the string type. 71 * 72 * @param columnIndex Indicates the specified column index, which starts from 0. 73 * 74 * @return Returns the value of the specified column as a string. 75 */ 76 API_EXPORT int GetString(int columnIndex, std::string &value) override; 77 78 /** 79 * @brief Obtains the value of the specified column in the current row as int. 80 * 81 * The implementation class determines whether to throw an exception if the value of the specified column 82 * in the current row is null or the specified column is not of the integer type. 83 * 84 * @param columnIndex Indicates the specified column index, which starts from 0. 85 * 86 * @return Returns the value of the specified column as a int. 87 */ 88 API_EXPORT int GetInt(int columnIndex, int &value) override; 89 90 /** 91 * @brief Obtains the value of the specified column in the current row as long. 92 * 93 * The implementation class determines whether to throw an exception if the value of the specified column 94 * in the current row is null or the specified column is not of the long type. 95 * 96 * @param columnIndex Indicates the specified column index, which starts from 0. 97 * 98 * @return Returns the value of the specified column as a long. 99 */ 100 API_EXPORT int GetLong(int columnIndex, int64_t &value) override; 101 102 /** 103 * @brief Obtains the value of the specified column in the current row as double. 104 * 105 * The implementation class determines whether to throw an exception if the value of the specified column 106 * in the current row is null or the specified column is not of the double type. 107 * 108 * @param columnIndex Indicates the specified column index, which starts from 0. 109 * 110 * @return Returns the value of the specified column as a double. 111 */ 112 API_EXPORT int GetDouble(int columnIndex, double &value) override; 113 114 /** 115 * @brief Obtains the value of the specified column in the current row as asset. 116 * 117 * The implementation class determines whether to throw an exception if the value of the specified column 118 * in the current row is null or the specified column is not of the double type. 119 * 120 * @param columnIndex Indicates the specified column index, which starts from 0. 121 * 122 * @return Returns the value of the specified column as a double. 123 */ 124 API_EXPORT int GetAsset(int32_t col, ValueObject::Asset &value) override; 125 126 /** 127 * @brief Obtains the value of the specified column in the current row as assets. 128 * 129 * The implementation class determines whether to throw an exception if the value of the specified column 130 * in the current row is null or the specified column is not of the double type. 131 * 132 * @param columnIndex Indicates the specified column index, which starts from 0. 133 * 134 * @return Returns the value of the specified column as a double. 135 */ 136 API_EXPORT int GetAssets(int32_t col, ValueObject::Assets &value) override; 137 138 /** 139 * @brief Get the size of blob or text. 140 * 141 * @param columnIndex Indicates the zero-based index of the target column. 142 */ 143 API_EXPORT int GetSize(int columnIndex, size_t &size) override; 144 145 /** 146 * @brief Checks whether the value of the specified column in the current row is null. 147 * 148 * @param columnIndex Indicates the specified column index, which starts from 0. 149 * 150 * @return Returns true if the value of the specified column in the current row is null; 151 * returns false otherwise. 152 */ 153 API_EXPORT int IsColumnNull(int columnIndex, bool &isNull) override; 154 155 /** 156 * @brief Obtains data type of the given column's value. 157 * 158 * @param columnIndex Indicates the specified column index, which starts from 0. 159 * 160 * @return Returns column value type. 161 */ 162 API_EXPORT int GetColumnType(int columnIndex, ColumnType &columnType) override; 163 164 /** 165 * @brief Move the cursor to an absolute position. 166 * 167 * @param position Indicates the specified column index, which starts from 0. 168 * 169 * @return Returns whether the requested move succeeded. 170 */ 171 API_EXPORT int GoToRow(int position) override; 172 173 /** 174 * @brief Obtains the names of all columns in a result set. 175 */ 176 API_EXPORT int GetAllColumnNames(std::vector<std::string> &columnNames) override; 177 178 /** 179 * @brief Obtains the number of rows in the result set. 180 */ 181 API_EXPORT int GetRowCount(int &count) override; 182 183 /** 184 * @brief Obtains a block from the {@link SharedResultSet}. 185 */ 186 API_EXPORT AppDataFwk::SharedBlock *GetBlock() override; 187 188 /** 189 * @brief Called when the position of the result set changes. 190 */ 191 API_EXPORT bool OnGo(int oldRowIndex, int newRowIndex) override; 192 193 /** 194 * @brief Adds the data of a {@code SharedResultSet} to a {@link SharedBlock}. 195 */ 196 API_EXPORT void FillBlock(int startRowIndex, AppDataFwk::SharedBlock *block) override; 197 198 /** 199 * @brief Allocates a new shared block to an {@link AbsSharedResultSet} 200 */ 201 API_EXPORT virtual void SetBlock(AppDataFwk::SharedBlock *block); 202 203 /** 204 * @brief Closes the result set. 205 * 206 * Calling this method on the result set will release all of its resources and makes it ineffective. 207 */ 208 API_EXPORT int Close() override; 209 210 /** 211 * @brief Checks whether an {@code AbsSharedResultSet} object contains shared blocks. 212 */ 213 API_EXPORT bool HasBlock(); 214 215 protected: 216 int CheckState(int columnIndex); 217 void ClearBlock(); 218 void ClosedBlock(); 219 virtual void Finalize(); 220 221 private: 222 // The default position of the cursor 223 static const int INIT_POS = -1; 224 static const size_t DEFAULT_BLOCK_SIZE = 2 * 1024 * 1024; 225 friend class ISharedResultSetStub; 226 friend class ISharedResultSetProxy; 227 // The SharedBlock owned by this AbsSharedResultSet 228 AppDataFwk::SharedBlock *sharedBlock_ = nullptr; 229 std::string sharedBlockName_ = "defaultSharedBlockName"; 230 }; 231 } // namespace NativeRdb 232 } // namespace OHOS 233 234 #endif