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