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 17 #ifndef NATIVE_RDB_SQLITE_SHARED_RESULT_SET_H 18 #define NATIVE_RDB_SQLITE_SHARED_RESULT_SET_H 19 20 #include <memory> 21 #include <mutex> 22 #include <string> 23 #include <thread> 24 #include <vector> 25 26 #include "abs_shared_result_set.h" 27 #include "rdb_store_impl.h" 28 #include "shared_block.h" 29 #include "sqlite_connection_pool.h" 30 #include "sqlite_statement.h" 31 #include "value_object.h" 32 33 namespace OHOS { 34 namespace NativeRdb { 35 class SqliteSharedResultSet : public AbsSharedResultSet { 36 public: 37 SqliteSharedResultSet(std::shared_ptr<RdbStoreImpl> store, SqliteConnectionPool *connectionPool, std::string path, 38 std::string sql, const std::vector<ValueObject> &bindArgs); 39 ~SqliteSharedResultSet() override; 40 int GetAllColumnNames(std::vector<std::string> &columnNames) override; 41 int Close() override; 42 int GetRowCount(int &count) override; 43 bool OnGo(int oldPosition, int newPosition) override; 44 void SetBlock(AppDataFwk::SharedBlock *block) override; 45 int PickFillBlockStartPosition(int resultSetPosition, int blockCapacity) const; 46 void SetFillBlockForwardOnly(bool isOnlyFillResultSetBlockInput); 47 48 protected: 49 void Finalize() override; 50 51 private: 52 std::shared_ptr<SqliteStatement> PrepareStep(SqliteConnection* connection, int &errCode); 53 void FillSharedBlock(int requiredPos); 54 private: 55 // The specified value is -1 when there is no data 56 static const int NO_COUNT = -1; 57 // The pick position of the shared block for search 58 static const int PICK_POS = 3; 59 std::shared_ptr<RdbStoreImpl> store_; 60 SqliteConnectionPool *connectionPool_; 61 // The number of rows that can fit in the shared block, 0 if unknown 62 int resultSetBlockCapacity; 63 // Controls fetching of rows relative to requested position 64 bool isOnlyFillResultSetBlock; 65 std::string qrySql; 66 std::vector<ValueObject> bindArgs_; 67 // The number of rows in the cursor 68 int rowNum; 69 std::vector<std::string> columnNames_; 70 std::mutex columnNamesLock_; 71 }; 72 } // namespace NativeRdb 73 } // namespace OHOS 74 75 #endif