• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 DISTRIBUTED_RDB_RDB_RESULT_SET_IMPL_H
17 #define DISTRIBUTED_RDB_RDB_RESULT_SET_IMPL_H
18 
19 #include <shared_mutex>
20 #include "rdb_result_set_stub.h"
21 #include "rdb_errno.h"
22 #include "store/cursor.h"
23 #include "value_proxy.h"
24 #include "store/general_value.h"
25 
26 namespace OHOS::DistributedRdb {
27 class RdbResultSetImpl final : public RdbResultSetStub {
28 public:
29     explicit RdbResultSetImpl(std::shared_ptr<DistributedData::Cursor> resultSet);
~RdbResultSetImpl()30     ~RdbResultSetImpl() override {};
31     int GetAllColumnNames(std::vector<std::string> &columnNames) override;
32     int GetColumnCount(int &count) override;
33     int GetColumnType(int columnIndex, ColumnType &columnType) override;
34     int GetColumnIndex(const std::string &columnName, int &columnIndex) override;
35     int GetColumnName(int columnIndex, std::string &columnName) override;
36     int GetRowCount(int &count) override;
37     int GetRowIndex(int &position) const override;
38     int GoTo(int offset) override;
39     int GoToRow(int position) override;
40     int GoToFirstRow() override;
41     int GoToLastRow() override;
42     int GoToNextRow() override;
43     int GoToPreviousRow() override;
44     int IsEnded(bool &result) override;
45     int IsStarted(bool &result) const override;
46     int IsAtFirstRow(bool &result) const override;
47     int IsAtLastRow(bool &result) override;
48     int GetBlob(int columnIndex, std::vector<uint8_t> &value) override;
49     int GetString(int columnIndex, std::string &value) override;
50     int GetInt(int columnIndex, int &value) override;
51     int GetLong(int columnIndex, int64_t &value) override;
52     int GetDouble(int columnIndex, double &value) override;
53     int IsColumnNull(int columnIndex, bool &isNull) override;
54     bool IsClosed() const override;
55     int Close() override;
56 
57 private:
58     template<typename T>
59     std::enable_if_t < ValueProxy::CVT_INDEX<T, ValueProxy::Proxy><ValueProxy::MAX, int>
60     Get(int columnIndex, T &value) const
61     {
62         auto [ret, val] = GetValue(columnIndex);
63         value = val.operator T();
64         return ret;
65     };
66 
GetValue(int columnIndex)67     std::pair<int32_t, ValueProxy::Value> GetValue(int columnIndex) const
68     {
69         DistributedData::Value var;
70         auto status = resultSet_->Get(columnIndex, var);
71         if (status != DistributedData::GeneralError::E_OK) {
72             return { NativeRdb::E_ERROR, ValueProxy::Value() };
73         }
74         return {NativeRdb::E_OK, ValueProxy::Convert(std::move(var))};
75     };
76 
77     mutable std::shared_mutex mutex_ {};
78     static constexpr ColumnType COLUMNTYPES[DistributedData::TYPE_MAX] = {
79         [DistributedData::TYPE_INDEX<std::monostate>] = ColumnType::TYPE_NULL,
80         [DistributedData::TYPE_INDEX<int64_t>] = ColumnType::TYPE_INTEGER,
81         [DistributedData::TYPE_INDEX<double>] = ColumnType::TYPE_FLOAT,
82         [DistributedData::TYPE_INDEX<std::string>] = ColumnType::TYPE_STRING,
83         [DistributedData::TYPE_INDEX<bool>] = ColumnType::TYPE_INTEGER,
84         [DistributedData::TYPE_INDEX<DistributedData::Bytes>] = ColumnType::TYPE_BLOB,
85         [DistributedData::TYPE_INDEX<DistributedData::Asset>] = ColumnType::TYPE_BLOB,
86         [DistributedData::TYPE_INDEX<DistributedData::Assets>] = ColumnType::TYPE_BLOB,
87     };
88     std::shared_ptr<DistributedData::Cursor> resultSet_;
89     std::atomic<int32_t> current_ = -1;
90     int32_t count_ = 0;
91     std::vector<std::string> colNames_;
92     ColumnType ConvertColumnType(int32_t columnType) const;
93 };
94 } // namespace OHOS::DistributedRdb
95 #endif // DISTRIBUTED_RDB_RDB_RESULT_SET_IMPL_H
96