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_SQLITE_SHARED_RESULT_SET_H 17 #define NATIVE_RDB_SQLITE_SHARED_RESULT_SET_H 18 19 #include <chrono> 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 "connection.h" 28 #include "shared_block.h" 29 #include "statement.h" 30 #include "value_object.h" 31 32 namespace OHOS { 33 namespace NativeRdb { 34 class SqliteSharedResultSet : public AbsSharedResultSet { 35 public: 36 using Values = std::vector<ValueObject>; 37 using Conn = std::shared_ptr<Connection>; 38 using Time = std::chrono::steady_clock::time_point; 39 SqliteSharedResultSet(Time start, Conn conn, std::string sql, const Values &args, const std::string &path); 40 ~SqliteSharedResultSet() override; 41 int Close() override; 42 int32_t OnGo(int oldPosition, int newPosition) override; 43 void SetBlock(AppDataFwk::SharedBlock *block) override; 44 int PickFillBlockStartPosition(int resultSetPosition, int blockCapacity) const; 45 void SetFillBlockForwardOnly(bool isOnlyFillResultSetBlockInput); 46 47 protected: 48 void Finalize() override; 49 std::pair<int, std::vector<std::string>> GetColumnNames() override; 50 51 private: 52 int InitRowCount(); 53 std::pair<std::shared_ptr<Statement>, int> PrepareStep(); 54 int32_t FillBlock(int requiredPos); 55 int32_t ExecuteForSharedBlock(AppDataFwk::SharedBlock *block, int start, int required); 56 57 private: 58 // The specified value is -1 when there is no data 59 static constexpr int NO_COUNT = -1; 60 // The pick position of the shared block for search 61 static constexpr int PICK_POS = 3; 62 // Max times of retrying query 63 static const int MAX_RETRY_TIMES = 50; 64 // Interval of retrying query in millisecond 65 static const int RETRY_INTERVAL = 1000; 66 // Controls fetching of rows relative to requested position 67 bool isOnlyFillBlock_ = false; 68 uint32_t blockCapacity_ = 0; 69 // The number of rows in the cursor 70 int rowNum_ = NO_COUNT; 71 72 std::shared_ptr<Connection> conn_; 73 std::shared_ptr<Statement> statement_; 74 std::string qrySql_; 75 std::vector<ValueObject> bindArgs_; 76 std::mutex mutex_; 77 }; 78 } // namespace NativeRdb 79 } // namespace OHOS 80 81 #endif