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