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_STEP_RESULT_SET_H
17 #define NATIVE_RDB_STEP_RESULT_SET_H
18 
19 #include <memory>
20 #include <shared_mutex>
21 #include <thread>
22 #include <vector>
23 
24 #include "abs_result_set.h"
25 #include "connection.h"
26 #include "connection_pool.h"
27 #include "statement.h"
28 
29 namespace OHOS {
30 namespace NativeRdb {
31 class StepResultSet : public AbsResultSet {
32 public:
33     using Values = std::vector<ValueObject>;
34     using Conn = std::shared_ptr<Connection>;
35     using Time = std::chrono::steady_clock::time_point;
36     StepResultSet(Time start, Conn conn, const std::string &sql, const Values &args, bool preCount, bool safe = false);
37     ~StepResultSet() override;
38     int GetColumnType(int columnIndex, ColumnType &columnType) override;
39     int GoToRow(int position) override;
40     int GoToNextRow() override;
41     int GetSize(int columnIndex, size_t &size) override;
42     int Get(int32_t col, ValueObject &value) override;
43     int Close() override;
44     int GetRowCount(int &count) override;
45 
46 protected:
47     std::pair<int, std::vector<std::string>> GetColumnNames() override;
48 
49 private:
50     template<typename T>
51     int GetValue(int32_t col, T &value);
52     std::pair<int, ValueObject> GetValueObject(int32_t col, size_t index);
53     std::shared_ptr<Statement> GetStatement();
54     int Reset();
55     int InitRowCount();
56     int PrepareStep();
57 
58     // Max times of retrying step query
59     static const int STEP_QUERY_RETRY_MAX_TIMES = 50;
60     // Interval of retrying step query in millisecond
61     static const int STEP_QUERY_RETRY_INTERVAL = 1000;
62     static const int EMPTY_ROW_COUNT = 0;
63 
64     bool isSupportCountRow_ = true;
65     std::shared_ptr<Statement> statement_;
66     std::shared_ptr<Connection> conn_;
67 
68     std::string sql_;
69     std::vector<ValueObject> args_;
70 };
71 } // namespace NativeRdb
72 } // namespace OHOS
73 #endif
74