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 string. 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 string 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 string. 63 */ 64 API_EXPORT int GetString(int columnIndex, std::string &value) override; 65 66 /** 67 * @brief Obtains the value of the specified column in the current row as assets. 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 double 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 double. 75 */ 76 API_EXPORT int Get(int32_t col, ValueObject &value) override; 77 78 /** 79 * @brief Get the size of blob or text. 80 * 81 * @param columnIndex Indicates the zero-based index of the target column. 82 */ 83 API_EXPORT int GetSize(int columnIndex, size_t &size) override; 84 85 /** 86 * @brief Obtains data type of the given column's value. 87 * 88 * @param columnIndex Indicates the specified column index, which starts from 0. 89 * 90 * @return Returns column value type. 91 */ 92 API_EXPORT int GetColumnType(int columnIndex, ColumnType &columnType) override; 93 94 /** 95 * @brief Move the cursor to an absolute position. 96 * 97 * @param position Indicates the specified column index, which starts from 0. 98 * 99 * @return Returns whether the requested move succeeded. 100 */ 101 API_EXPORT int GoToRow(int position) override; 102 103 /** 104 * @brief Obtains the number of rows in the result set. 105 */ 106 API_EXPORT int GetRowCount(int &count) override; 107 108 /** 109 * @brief Obtains a block from the {@link SharedResultSet}. 110 */ 111 API_EXPORT std::shared_ptr<AppDataFwk::SharedBlock> GetBlock() override; 112 113 /** 114 * @brief Called when the position of the result set changes. 115 */ 116 API_EXPORT int32_t OnGo(int oldRowIndex, int newRowIndex) override; 117 118 /** 119 * @brief Allocates a new shared block to an {@link AbsSharedResultSet} 120 */ 121 API_EXPORT virtual void SetBlock(AppDataFwk::SharedBlock *block); 122 123 /** 124 * @brief Closes the result set. 125 * 126 * Calling this method on the result set will release all of its resources and makes it ineffective. 127 */ 128 API_EXPORT int Close() override; 129 130 /** 131 * @brief Checks whether an {@code AbsSharedResultSet} object contains shared blocks. 132 */ 133 API_EXPORT bool HasBlock(); 134 135 protected: 136 int CheckState(int columnIndex); 137 void ClearBlock(); 138 void ClosedBlock(); 139 virtual void Finalize(); 140 141 // The default position of the cursor 142 static const int INIT_POS = -1; 143 private: 144 int GetCustomerValue(int index, ValueObject &value, AppDataFwk::SharedBlock *block); 145 int UpdateBlockPos(int position, int rowCnt); 146 147 static const size_t DEFAULT_BLOCK_SIZE = 2 * 1024 * 1024; 148 friend class ISharedResultSetStub; 149 friend class ISharedResultSetProxy; 150 // The SharedBlock owned by this AbsSharedResultSet 151 std::shared_ptr<AppDataFwk::SharedBlock> sharedBlock_ = nullptr; 152 std::string sharedBlockName_ = "defaultSharedBlockName"; 153 // If the shared memory application fails for the first time, GetBlock() returns nullptr forever 154 bool lowMem_ = false; 155 }; 156 } // namespace NativeRdb 157 } // namespace OHOS 158 159 #endif