• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     std::pair<std::shared_ptr<Statement>, int> PrepareStep();
53     int32_t FillBlock(int requiredPos);
54     int32_t ExecuteForSharedBlock(AppDataFwk::SharedBlock *block, int start, int required);
55 
56 private:
57     // The specified value is -1 when there is no data
58     static constexpr int NO_COUNT = -1;
59     // The pick position of the shared block for search
60     static constexpr int PICK_POS = 3;
61     // Max times of retrying query
62     static const int MAX_RETRY_TIMES = 50;
63     // Interval of retrying query in millisecond
64     static const int RETRY_INTERVAL = 1000;
65     // Controls fetching of rows relative to requested position
66     bool isOnlyFillBlock_ = false;
67     uint32_t blockCapacity_ = 0;
68     // The number of rows in the cursor
69     int rowNum_ = NO_COUNT;
70 
71     std::shared_ptr<Connection> conn_;
72     std::shared_ptr<Statement> statement_;
73     std::string qrySql_;
74     std::vector<ValueObject> bindArgs_;
75     std::mutex mutex_;
76 };
77 } // namespace NativeRdb
78 } // namespace OHOS
79 
80 #endif