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 KV_STORE_RESULT_SET_IMPL_H
17 #define KV_STORE_RESULT_SET_IMPL_H
18 
19 #include <mutex>
20 
21 #include "kv_store_result_set.h"
22 #include "ikvdb_result_set.h"
23 
24 namespace DistributedDB {
25 class KvStoreResultSetImpl final : public KvStoreResultSet {
26 public:
27     explicit KvStoreResultSetImpl(IKvDBResultSet *resultSet);
~KvStoreResultSetImpl()28     ~KvStoreResultSetImpl() override {};
29 
30     DISABLE_COPY_ASSIGN_MOVE(KvStoreResultSetImpl);
31 
32     // Returns the numbers of rows in the result set.
33     int GetCount() const override;
34 
35     // Returns the current position of the result set in the row set.
36     int GetPosition() const override;
37 
38     // Move the result set to the first row, return false if the result set is empty.
39     bool MoveToFirst() override;
40 
41     // Move the result set to the last row, return false if the result set is empty.
42     bool MoveToLast() override;
43 
44     // Move the result set to the next row, return false if the result set is already past
45     // the last entry in the result set.
46     bool MoveToNext() override;
47 
48     // Move the result set to the previous row, return false if the result set is already before
49     // the first entry in the result set
50     bool MoveToPrevious() override;
51 
52     // Move the result set by a relative amount, forward or backward, from the current position.
53     bool Move(int offset) override;
54 
55     // Move the result set to an absolute position, the valid range of value is [-1, count]
56     bool MoveToPosition(int position) override;
57 
58     // Returns whether the result set is pointing to the first row.
59     bool IsFirst() const override;
60 
61     // Returns whether the result set is pointing to the last row.
62     bool IsLast() const override;
63 
64     // Returns whether the result set is pointing to the position before the first row.
65     bool IsBeforeFirst() const override;
66 
67     // Returns whether the result set is pointing to the position after the last row
68     bool IsAfterLast() const override;
69 
70     // Get a key-value entry.
71     DBStatus GetEntry(Entry &entry) const override;
72 
73     // Get the result set obj
74     void GetResultSet(IKvDBResultSet *&resultSet) const;
75 
76     // Returns whether the result set is empty.
77     bool IsClosed() const override;
78 
79     // Clear the result set. Set the position -1.
80     void Close() override;
81 
82     // Get column names.
83     void GetColumnNames(std::vector<std::string> &columnNames) const override;
84 
85     // Get the column name by column index. Returns OK, NOT_FOUND or NONEXISTENT.
86     DBStatus GetColumnType(int columnIndex, ColumnType &columnType) const override;
87 
88     // Get the column index by column name. Returns OK, NOT_FOUND or NONEXISTENT.
89     DBStatus GetColumnIndex(const std::string &columnName, int &columnIndex) const override;
90 
91     // Get the column name by column index. Returns OK, NOT_FOUND or NONEXISTENT.
92     DBStatus GetColumnName(int columnIndex, std::string &columnName) const override;
93 
94     // Get blob. Returns OK,, NOT_FOUND NONEXISTENT or TYPE_MISMATCH.
95     DBStatus Get(int columnIndex, std::vector<uint8_t> &value) const override;
96 
97     // Get string. Returns OK, NOT_FOUND, NONEXISTENT or TYPE_MISMATCH.
98     DBStatus Get(int columnIndex, std::string &value) const override;
99 
100     // Get int64. Returns OK, NOT_FOUND, NONEXISTENT or TYPE_MISMATCH.
101     DBStatus Get(int columnIndex, int64_t &value) const override;
102 
103     // Get double. Returns OK, NOT_FOUND, NONEXISTENT or TYPE_MISMATCH.
104     DBStatus Get(int columnIndex, double &value) const override;
105 
106     // Get whether the column value is null. Returns OK, NOT_FOUND or NONEXISTENT.
107     DBStatus IsColumnNull(int columnIndex, bool &isNull) const override;
108 
109     // Get the row record. Returns OK, NOT_FOUND or NOT_SUPPORT.
110     DBStatus GetRow(std::map<std::string, VariantData> &data) const override;
111 
112 private:
113     static const int INIT_POSITION;
114     IKvDBResultSet * const resultSet_;
115 };
116 } // namespace DistributedDB
117 
118 #endif // KV_STORE_RESULT_SET_IMPL_H